2
1 ! Hw#1: Spring 2011: Due: February 10 ! Let A, B and C be 2-dimensional, double precision ! arrays of size n by n where n is an integer parameter. ! Fortran has an intrinsic function for matrix multiplication ! written as C = C + matmul(A,B), i.e. ! do i = 1, m ! do j = 1, n ! do k = 1, kk ! C(i,j) = C(i,j) + A(i,k)*B(k,j) ! enddo ! enddo ! enddo ! Write a program that will time C = C + matmul(A,B) executing ! on each MPI processes but with different data. ! Use mpi_wtime() to measure the wall-clock time on each ! MPI process. Before starting the time on each MPI ! process, call mpi_barrier(comm, ierror) to that all ! processes will start at (about) the same time. ! Use mpi_ssend to send times measured on each process ! to process 0 and store in array time_array(0:p-1) that ! has been allocated after p has been set. ! From process 0, print all the times stored in time_array ! and say which time should be used to measure time ! of the parallel execution. ! Compute and print the maximum and minimum of the times using ! mpi_reduce with the op = mpi_max and mpi_min, respectively. ! Run with 4, 8 and 16 MPI processes using a script. Append the ! output obtained as comments at the end of your program. use mpi ! brings everything needed to use MPI. implicit none ! requires all variables to be declared to ! help with debugging. integer, parameter :: dp=mpi_double_precision, comm = mpi_comm_world integer :: i, ierror, p, rank, status(mpi_status_size) integer, parameter :: n = 64 double precision :: A(n,n), B(n,n), C(n,n) double precision :: t1, t2, time, max_time, min_time, tmp double precision, allocatable :: time_array(:) call mpi_init(ierror) call mpi_comm_size(comm, p, ierror) call mpi_comm_rank(comm, rank, ierror) ! Allocate time_array and initialize A, B and C: allocate(time_array(0:p-1)) time_array(0:p-1) = -1.d0 ! useful for debugging call random_number(A) A = A + float(rank) call random_number(B) B = B - float (rank) C = 0.d0 call mpi_barrier(comm, ierror) t1 = mpi_wtime() C = C + matmul(A,B) t2 = mpi_wtime() time = t2 - t1

hw1.s11

Embed Size (px)

DESCRIPTION

Comps 525 HW 1

Citation preview

Page 1: hw1.s11

1

! Hw#1: Spring 2011: Due: February 10 ! Let A, B and C be 2-dimensional, double precision ! arrays of size n by n where n is an integer parameter. ! Fortran has an intrinsic function for matrix multiplication ! written as C = C + matmul(A,B), i.e. ! do i = 1, m ! do j = 1, n ! do k = 1, kk ! C(i,j) = C(i,j) + A(i,k)*B(k,j) ! enddo ! enddo ! enddo ! Write a program that will time C = C + matmul(A,B) executing ! on each MPI processes but with different data. ! Use mpi_wtime() to measure the wall-clock time on each ! MPI process. Before starting the time on each MPI ! process, call mpi_barrier(comm, ierror) to that all ! processes will start at (about) the same time. ! Use mpi_ssend to send times measured on each process ! to process 0 and store in array time_array(0:p-1) that ! has been allocated after p has been set. ! From process 0, print all the times stored in time_array ! and say which time should be used to measure time ! of the parallel execution. ! Compute and print the maximum and minimum of the times using ! mpi_reduce with the op = mpi_max and mpi_min, respectively. ! Run with 4, 8 and 16 MPI processes using a script. Append the ! output obtained as comments at the end of your program. use mpi ! brings everything needed to use MPI. implicit none ! requires all variables to be declared to ! help with debugging. integer, parameter :: dp=mpi_double_precision, comm = mpi_comm_world integer :: i, ierror, p, rank, status(mpi_status_size) integer, parameter :: n = 64 double precision :: A(n,n), B(n,n), C(n,n) double precision :: t1, t2, time, max_time, min_time, tmp double precision, allocatable :: time_array(:) call mpi_init(ierror) call mpi_comm_size(comm, p, ierror) call mpi_comm_rank(comm, rank, ierror) ! Allocate time_array and initialize A, B and C: allocate(time_array(0:p-1)) time_array(0:p-1) = -1.d0 ! useful for debugging call random_number(A) A = A + float(rank) call random_number(B) B = B - float (rank) C = 0.d0 call mpi_barrier(comm, ierror) t1 = mpi_wtime() C = C + matmul(A,B) t2 = mpi_wtime() time = t2 - t1

Page 2: hw1.s11

2

! Find the maximum and minimum of all the times and put on process 0 call mpi_reduce(time, max_time, 1, dp, mpi_max, 0, comm, ierror) call mpi_reduce(time, min_time, 1, dp, mpi_min, 0, comm, ierror) ! Send time to process 0 and receive into time_array . . . ! Print results do i = 0, p-1 print*,'For rank = ',i,' time = ',time_array(i) enddo print*,' ' print*,' maximum time = ', max_time,' seconds' print*,' minimum time = ', min_time,' seconds' print*,' ' print*,'The value of n used was n = ', n endif call mpi_finalize(ierror) end ! Your output may vary some from the numbers below and that is okay. ! hpc-class-% mpirun -np 4 a.out ! For rank = 0 time = 6.709098815917969E-004 ! For rank = 1 time = 6.530284881591797E-004 ! For rank = 2 time = 6.568431854248047E-004 ! For rank = 3 time = 6.709098815917969E-004 ! maximum time = 6.709098815917969E-004 seconds ! minimum time = 6.530284881591797E-004 seconds ! The value of n used was n = 64 ! . . .