program pack ! ! This simple pack program packs everything in a ! double precision vector. One has to be a bit careful with ! integers. The program converts them to double precision ! (thereby wasting storage). To avoid possible rounding ! errors we add 0.1 before the conversion and when we ! unpack we truncate. The MPI_Pack/MPI_Unpack stores data ! on a byte-level, but the principle is roughly the same. ! implicit none include "mpif.h" integer my_rank, err, k, n, status(MPI_STATUS_SIZE) integer buf_len parameter ( buf_len = 100 ) double precision buffer(buf_len), vec(5) call MPI_Init(err) call MPI_Comm_rank(MPI_COMM_WORLD, my_rank, err) if ( my_rank .eq. 0 ) then n = 123 do k = 1, 5 vec(k) = k + 1.0d0 / k end do ! ! Pack ! buffer(1) = n + 0.1 do k = 1, 5 buffer(k + 1) = vec(k) end do call MPI_Send(buffer, 6, MPI_DOUBLE_PRECISION, 1, 1, & MPI_COMM_WORLD, err) else ! ! Receive message from master ! call MPI_Recv(buffer, 6, MPI_DOUBLE_PRECISION, 0, 1, & MPI_COMM_WORLD, status, err) ! ! Unpack ! n = buffer(1) ! truncation of decimals do k = 1, 5 vec(k) = buffer(k + 1) end do print*, 'In slave: n = ', n print*, 'In slave: vec = ', vec end if call MPI_Finalize(err) end program pack