|
| 1 | +program chain |
| 2 | + use mpi |
| 3 | + use iso_fortran_env, only : REAL64 |
| 4 | + |
| 5 | + implicit none |
| 6 | + integer, parameter :: size = 10000000 |
| 7 | + integer :: rc, myid, ntasks |
| 8 | + integer :: message(size) |
| 9 | + integer :: receiveBuffer(size) |
| 10 | + integer :: status(MPI_STATUS_SIZE,2) |
| 11 | + |
| 12 | + real(REAL64) :: t0, t1 |
| 13 | + |
| 14 | + integer :: source, destination |
| 15 | + integer :: count |
| 16 | + integer :: requests(2) |
| 17 | + |
| 18 | + call mpi_init(rc) |
| 19 | + call mpi_comm_rank(MPI_COMM_WORLD, myid, rc) |
| 20 | + call mpi_comm_size(MPI_COMM_WORLD, ntasks, rc) |
| 21 | + |
| 22 | + message = myid |
| 23 | + |
| 24 | + ! Set source and destination ranks |
| 25 | + if (myid < ntasks-1) then |
| 26 | + destination = myid + 1 |
| 27 | + else |
| 28 | + destination = MPI_PROC_NULL |
| 29 | + end if |
| 30 | + if (myid > 0) then |
| 31 | + source = myid - 1 |
| 32 | + else |
| 33 | + source = MPI_PROC_NULL |
| 34 | + end if |
| 35 | + |
| 36 | + ! Start measuring the time spent in communication |
| 37 | + call mpi_barrier(mpi_comm_world, rc) |
| 38 | + t0 = mpi_wtime() |
| 39 | + |
| 40 | + ! Receive messages in the back ground |
| 41 | + call mpi_irecv(receiveBuffer, size, MPI_INTEGER, source, & |
| 42 | + MPI_ANY_TAG, MPI_COMM_WORLD, requests(1), rc) |
| 43 | + ! Send messages in the back ground |
| 44 | + call mpi_isend(message, size, MPI_INTEGER, destination, & |
| 45 | + myid + 1, MPI_COMM_WORLD, requests(2), rc) |
| 46 | + |
| 47 | + ! Blocking wait for messages |
| 48 | + call mpi_waitall(2, requests, status, rc) |
| 49 | + |
| 50 | + ! Use status parameter to find out the no. of elements received |
| 51 | + call mpi_get_count(status(:,1), MPI_INTEGER, count, rc) |
| 52 | + write(*,'(A10,I3,A20,I8,A,I3,A,I3)') 'Sender: ', myid, & |
| 53 | + ' Sent elements: ', size, & |
| 54 | + '. Tag: ', myid + 1, & |
| 55 | + '. Receiver: ', destination |
| 56 | + write(*,'(A10,I3,A20,I8,A,I3,A,I3)') 'Receiver: ', myid, & |
| 57 | + 'received elements: ', count, & |
| 58 | + '. Tag: ', status(MPI_TAG, 1), & |
| 59 | + '. Sender: ', status(MPI_SOURCE, 1) |
| 60 | + |
| 61 | + ! Finalize measuring the time and print it out |
| 62 | + t1 = mpi_wtime() |
| 63 | + call mpi_barrier(mpi_comm_world, rc) |
| 64 | + call flush(6) |
| 65 | + |
| 66 | + call print_ordered(t1 - t0) |
| 67 | + |
| 68 | + call mpi_finalize(rc) |
| 69 | + |
| 70 | +contains |
| 71 | + |
| 72 | + subroutine print_ordered(t) |
| 73 | + implicit none |
| 74 | + real(REAL64) :: t |
| 75 | + |
| 76 | + integer i |
| 77 | + |
| 78 | + if (myid == 0) then |
| 79 | + write(*, '(A20, I3, A, F6.3)') 'Time elapsed in rank', myid, ':', t |
| 80 | + do i=1, ntasks-1 |
| 81 | + call mpi_recv(t, 1, MPI_DOUBLE_PRECISION, i, 11, & |
| 82 | + MPI_COMM_WORLD, MPI_STATUS_IGNORE, rc) |
| 83 | + write(*, '(A20, I3, A, F6.3)') 'Time elapsed in rank', i, ':', t |
| 84 | + end do |
| 85 | + else |
| 86 | + call mpi_send(t, 1, MPI_DOUBLE_PRECISION, 0, 11, & |
| 87 | + MPI_COMM_WORLD, rc) |
| 88 | + end if |
| 89 | + end subroutine print_ordered |
| 90 | + |
| 91 | +end program chain |
0 commit comments