program matrix implicit none integer, parameter :: rows1 = 500, cols1 = 500 integer, parameter :: rows2 = 500, cols2 = 500 integer, parameter :: maxnum = 10 integer m(rows1, cols1) integer n(rows2, cols2) integer prod(rows1, cols2) integer prod2(rows1, cols2) real, dimension(5) :: randomarray ! used for timing integer :: same character(20) :: filename1, filename2, filename3 integer time_array_0(8), time_array_1(8) real start_time, finish_time call random_seed() filename1='matrix500.txt' filename2='matrix500B.txt' write(*,*) 'Reading first matrix from file...' call matrixFromFile(m, rows1, cols1, filename1, 10) write(*,*) 'Reading second matrix from file...' call matrixFromFile(n, rows2, cols2, filename2, 20) write(*,*) 'Starting matrix multiplication...' call date_and_time(values=time_array_0) start_time = time_array_0 (5) * 3600 + time_array_0 (6) * 60 & + time_array_0 (7) + 0.001 * time_array_0 (8) write (6, '(8x, 1a, 1f16.6)') 'begin (date_and_time): ', & start_time call matMult(m,n,prod, rows1, cols1, rows2, cols2) call date_and_time(values=time_array_1) finish_time = time_array_1 (5) * 3600 + time_array_1 (6) * 60 & + time_array_1 (7) + 0.001 * time_array_1 (8) write(*,*) 'End matrix multiplication' write (6, '(8x, 1a, 1f16.6)') 'end (date_and_time): ', & finish_time write(*,*) 'Total time=', finish_time - start_time filename3 = 'prod500.txt' write(*,*) 'Reading product matrix solution from file...' call matrixFromFile(prod2, rows1,cols1, filename3, 30) call checkMatrices(prod,prod2, rows1, cols1, same) write(*,*) 'Same =', same if (same .eq. 1) then write(*,*) 'Your answer agrees with mine' else write(*,*) 'Your answer is not the same as mine' end if contains subroutine matMult(m, n, prod, rows1, cols1, rows2, cols2) implicit none integer, intent(in) :: rows1, cols1, rows2, cols2 integer, dimension(rows1,cols1), intent(in) :: m integer, dimension(rows2,cols2), intent(in) :: n integer, dimension(rows1,cols2), intent(out) :: prod integer i, j, k ! Insert your code for matrix multiplication here end subroutine subroutine checkMatrices(m, n, rows, cols, same) implicit none integer, intent(in) :: rows, cols integer, dimension(rows,cols), intent(in) :: m,n integer, intent(out) :: same integer i, j same = 1 do i=1, rows do j = 1, cols if (m(i,j) /= n(i,j) ) then same=0 write(*,*) 'Not the same at ', i, j end if end do end do end subroutine subroutine matrixToFile(rows, cols, filename, filenum) implicit none integer, intent(in) :: rows, cols integer, dimension(rows,cols) :: m character(20), intent(in) :: filename integer, intent(in) :: filenum integer i, j open(filenum,file=filename) do i=1,rows do j = 1, cols call random_number(randomarray) m(i,j) = int(randomarray(1)*maxnum) write(filenum,*) m(i,j) end do end do close(filenum) end subroutine subroutine matrixFromFile(m, rows, cols, filename, filenum) implicit none integer, intent(in) :: rows, cols integer, dimension(rows,cols),intent(out) :: m character(20), intent(in) :: filename integer, intent(in) :: filenum integer i, j open(filenum,file=filename) do i=1,rows do j = 1, cols read(filenum,*) m(i,j) end do end do close(filenum) end subroutine subroutine matrixWriteToFile(m, rows, cols, filename, filenum) implicit none integer, intent(in) :: rows, cols integer, dimension(rows,cols),intent(in) :: m character(20), intent(in) :: filename integer, intent(in) :: filenum integer i, j open(filenum,file=filename) do i=1,rows do j = 1, cols write(filenum,*) m(i,j) end do end do close(filenum) end subroutine subroutine printMatrix(m, rows, cols) implicit none integer, intent(in) :: rows, cols integer, dimension(rows,cols), intent(in) :: m integer i, j do i=1,rows write(*,*) (m(i,j), j = 1,cols) end do end subroutine END PROGRAM