!
! 2014/4/11
! Satoshi.T
! matrix_time.f90
!
! 引数として, [行列サイズ, 全体アクセスの反復回数, 1行or1列or対角要素アクセスの反復回数] が必要
!

    program matrix_time
        implicit none
        integer i, j, loop
        integer n, loopSize, loopSize2
        character(32) argv
        integer input, output
        double precision time
        integer t1, t2, t_rate, t_max, diff
        integer, allocatable :: mat(:,:)

        input = 1

        if (iargc() < 3) then
          print *, "[Usage] Size Loop1 Loop2"
          stop
        else
          call getarg(1,argv)
          read (argv,*) n
          call getarg(2,argv)
          read (argv,*) loopSize
          call getarg(3,argv)
          read (argv,*) loopSize2
        end if

        allocate(mat(n, n))

        call getarg(0,argv)
        print *, "program: ", argv
        print *, "matrix_size = ", n, " * ", n, ", loop1 = ", loopSize, ", loop2 = ", loopSize2

!************ row all ****************************************************************
        output = 0
        call system_clock(t1)

        do loop = 1,loopSize
            do i = 1,n
                do j = 1,n
                    mat(i,j) = input
                end do
            end do

            do i = 1,n
                do j = 1,n
                    output = output + mat(i,j)
                end do
            end do
        end do

        call system_clock(t2, t_rate, t_max)
        if(t2 < t1) then
            diff = t_max - t1 + t2
        else
            diff = t2 - t1
        endif
        time = diff/dble(t_rate)
        print *, "row all :", time, "(sec), ave=", time/loopSize, "(sec), sum=", output

!************ col all ****************************************************************

        output = 0
        call system_clock(t1)

        do loop = 1,loopSize
            do j = 1,n
                do i = 1,n
                    mat(i,j) = input
                end do
            end do

            do j = 1,n
                do i = 1,n
                    output = output + mat(i,j)
                end do
            end do
        end do

        call system_clock(t2, t_rate, t_max)
        if(t2 < t1) then
            diff = t_max - t1 + t2
        else
            diff = t2 - t1
        endif
        time = diff/dble(t_rate)
        print *, "col all :", time, "(sec), ave=", time/loopSize, "(sec), sum=", output

!************ diag     ****************************************************************
        output = 0
        call system_clock(t1)

        do loop = 1,loopSize2
            do i = 1,n
                !do j = 1,n
                    mat(i,i) = input
                !end do
            end do

            do i = 1,n
                !do j = 1,n
                    output = output + mat(i,i)
                !end do
            end do
        end do

        call system_clock(t2, t_rate, t_max)
        if(t2 < t1) then
            diff = t_max - t1 + t2
        else
            diff = t2 - t1
        endif
        time = diff/dble(t_rate)
        print *, "diag    :", time, "(sec), ave=", time/loopSize2, "(sec), sum=", output

!************ row 1   ****************************************************************
        output = 0
        i = 1
        call system_clock(t1)

        do loop = 1,loopSize2
            !do i = 1,n
                do j = 1,n
                    mat(i,j) = input
                end do
            !end do

            !do i = 1,n
                do j = 1,n
                    output = output + mat(i,j)
                end do
            !end do
        end do

        call system_clock(t2, t_rate, t_max)
        if(t2 < t1) then
            diff = t_max - t1 + t2
        else
            diff = t2 - t1
        endif
        time = diff/dble(t_rate)
        print *, "row 1   :", time, "(sec), ave=", time/loopSize2, "(sec), sum=", output

!************ col 1   ****************************************************************
        output = 0
        j = 1
        call system_clock(t1)

        do loop = 1,loopSize2
            do i = 1,n
                !do j = 1,n
                    mat(i,j) = input
                !end do
            end do

            do i = 1,n
                !do j = 1,n
                    output = output + mat(i,j)
                !end do
            end do
        end do

        call system_clock(t2, t_rate, t_max)
        if(t2 < t1) then
            diff = t_max - t1 + t2
        else
            diff = t2 - t1
        endif
        time = diff/dble(t_rate)
        print *, "col 1   :", time, "(sec), ave=", time/loopSize2, "(sec), sum=", output

!**********************************************************************************

        deallocate(mat)

    end program matrix_time
