把一个整数数组和一个双精度数组从第0个进程传送到第1个进程,使用打包和拆包的方式实现。 program pack include 'mpif.h' integer maxbuf, len parameter (maxbuf = 200, len = 10) integer myid, p, mycomm, ierr, status(mpi_status_size, 2), & ia(len), count1, count2, i, pos real*8 a(len) character buf(maxbuf) call mpi_init(ierr) call mpi_comm_dup(mpi_comm_world, mycomm, ierr) call mpi_comm_rank(mycomm, myid, ierr) call mpi_comm_size(mycomm, p, ierr) print *, 'Process ', myid, 'of', p, 'is running' if(myid.eq.0) then do 10 i = 1, len ia(i) = i a(i) = dble(i) 10 continue endif call mpi_pack_size(len, mpi_integer, mycomm, count1, ierr) call mpi_pack_size(len, mpi_double_precision, mycomm, & count2, ierr) print *, 'The pack size of 10 integer and real8 are:', & count1, count2 pos = 1 if (myid.eq.0.and.p.gt.1) then call mpi_pack(ia, len, mpi_integer, buf, maxbuf, pos, & mycomm, ierr) call mpi_pack(a, len, mpi_double_precision, buf, maxbuf, & pos, mycomm, ierr) call mpi_send(buf, pos-1, mpi_packed, 1, 1, mycomm, ierr) elseif (myid.eq.1) then call mpi_recv(buf, maxbuf, mpi_packed, 0, 1, mycomm, & status, ierr) call mpi_unpack(buf, maxbuf, pos, ia, len, mpi_integer, & mycomm, ierr) call mpi_unpack(buf, maxbuf, pos, a, len, mpi_double_precision, & mycomm, ierr) print *, 'The received values are:', a(1), a(2), a(3) endif call mpi_comm_free(mycomm, ierr) call mpi_finalize(ierr) stop end result: [root@c0109 zlt]# mpiexec -n 2 ./a Process 0 of 2 is running The pack size of 10 integer and real8 are: 40 80 Process 1 of 2 is running The pack size of 10 integer and real8 are: 40 80 The received values are: 1.00000000000000 2.00000000000000 3.00000000000000