C This program uses a subroutine, matvec(), to multiply a matrix by C a vector. The matrix has 100 rows and 10,000 columns for a total C of 1,000,000 elements. For the sake of timing, the multiplication C is repeated 100 times. (The elements of the matrix and vector are C set somewhat arbitrarily.) C C There are two versions of the matvec subroutine given here (one C version is commented out). One version carries out the C multiplication a row at a time (corresponding to how people C usually think about matrix-vector multiplication). The other C approach builds up the result by multiplying each column of the C matrix by the corresponding element of the matrix, i.e., working C in a column-centric way. The program was timed using each of the C subroutines. C C Using a row-centric approach (i.e., the "natural" way): C C real 0m14.412s C user 0m14.400s C sys 0m0.010s C C Using a column-centric approach: C C real 0m7.973s C user 0m7.930s C sys 0m0.040s C C Note that the column-centric approach is about 45 percent faster C than the row-centric approach (based on "user" time). C C John Schneider C CptS 203, HW 10, problem 1. C-------------------------------------------------------------------------- program multim implicit none integer i, j, nrow, ncol parameter (nrow=100,ncol=10000) real a(nrow*ncol), b(ncol), c(nrow) C.....Fill the matrix and vector with "reasonable" values. do j=1,ncol b(j) = sqrt(real(j))/3000. do i=1,nrow a(i+(j-1)*nrow) = real(i*j)*.00312 - j/150. + i enddo enddo C.....Multiply the matrix and vector 100 times. do i=1,100 call matvec(a,b,c,nrow,ncol) enddo print *,c stop end C..... C This subroutine multiplies an nrow by ncol matrix by a vector and C returns the result in another vector. The multiplication is done C along the columns of the matrix. C..... subroutine matvec(a,b,c,nrow,ncol) implicit none integer nrow, ncol, i,j real a(nrow,ncol), b(ncol), c(nrow) do 10 i=1,nrow c(i) = 0.0 10 continue do 30 j=1,ncol do 20 i=1,nrow c(i) = c(i) + a(i,j)*b(j) 20 continue 30 continue return end C..... C This subroutine multiplies an nrow by ncol matrix by a vector and C returns the result in another vector. The multiplication is done C along the rows (i.e., in the "natural" order). C..... c$$$ subroutine matvec(a,b,c,nrow,ncol) c$$$ implicit none c$$$ integer nrow, ncol, i,j c$$$ c$$$ real a(nrow,ncol), b(ncol), c(nrow) c$$$ c$$$ do 20 i=1,nrow c$$$ c(i) = 0.0 c$$$ do 10 j=1,ncol c$$$ c(i) = c(i) + a(i,j)*b(j) c$$$ 10 continue c$$$ 20 continue c$$$ c$$$ return c$$$ end