Transfer an integer array and a double-precision array from 0th processes to 1st processes, which are implemented by packaging and unpacking.
Program pack <br/> include 'mpif. h' <br/> integer maxbuf, Len <br/> parameter (maxbuf = 200, Len = 10) <br/> integer myid, P, mycomm, ierr, status (mpi_status_size, 2), <br/> & Ia (LEN), count1, count2, I, POS <br/> real * 8 A (LEN) <br/> character Buf (maxbuf) </P> <p> call mpi_init (ierr) <br/> call mpi_comm_dup (mpi_comm_world, mycomm, ierr) <br/> call mpi_comm_rank (mycomm, myid, ierr) <br/> call mpi_comm_size (mycomm, P, ierr) <br/> Print *, 'process', myid, 'of', P, 'is running' </P> <p> If (myid. eq.0) Then <br/> do 10 I = 1, Len <br/> Ia (I) = I <br/> A (I) = dble (I) <br/> 10 continue <br/> endif </P> <p> call mpi_pack_size (Len, mpi_integer, mycomm, count1, ierr) <br/> call mpi_pack_size (Len, mpi_double_precision, mycomm, <br/> & count2, ierr) <br/> Print *, 'the pack size of 10 integer and real8 are: ', <br/> & count1, count2 <br/> Pos = 1 <br/> If (myid. eq.0.and. p. gt.1) Then <br/> call mpi_pack (IA, Len, mpi_integer, Buf, maxbuf, POs, <br/> & mycomm, ierr) <br/> call mpi_pack (, len, mpi_double_precision, Buf, maxbuf, <br/> & Pos, mycomm, ierr) <br/> call mpi_send (BUF, pos-1, mpi_packed, 1, 1, mycomm, ierr) <br/> elseif (myid. eq.1) Then <br/> call mpi_recv (BUF, maxbuf, mpi_packed, 0, 1, mycomm, <br/> & status, ierr) <br/> call mpi_unpack (BUF, maxbuf, POs, IA, Len, mpi_integer, <br/> & mycomm, ierr) <br/> call mpi_unpack (BUF, maxbuf, POs, A, Len, mpi_double_precision, <br/> & mycomm, ierr) <br/> Print *, 'the specified ED values are: ', A (1), a (2), a (3) <br/> endif <br/> call mpi_comm_free (mycomm, ierr) <br/> call mpi_finalize (ierr) <br/> stop <br/> end </P> <p>
Result:
[Root @ c0109 zlt] # mpiexec-N 2. /A <br/> process 0 of 2 is running <br/> the pack size of 10 integer and real8 are: 40 80 <br/> process 1 of 2 is running <br/> the pack size of 10 integer and real8 are: 40 80 <br/> the specified ED values are: 1.00000000000000 2.00000000000000 3.00000000000000