There is indeed a startup time for MPI communication.
# Include "MPI. H "<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <time. h> <br/> # include <string. h> <br/> int main (INT argc, char ** argv) <br/>{< br/> int I, myid, other, numprocs; <br/> double start, end; <br/> mpi_status status; </P> <p> mpi_init (& argc, & argv); <br/> mpi_comm_rank (mpi_comm_world, & myid); <br/> mpi_comm_size (mpi_comm_world, & numprocs); <br/> int n = 1024; <br/> int * Buf = (int *) Malloc (sizeof (INT) * (n + 1); <br/> If (BUF = NULL) printf ("not enouth memory! /N "); <br/> for (I = 0; I <n; I ++) {<br/> Buf [I] = myid + I; <br/>}< br/> If (myid = 0) Other = 1; <br/> If (myid = 1) Other = 0; </P> <p> Start = mpi_wtime (); <br/> If (myid = 0) <br/> mpi_send (BUF, N, mpi_int, other, 99, mpi_comm_world); <br/> else <br/> mpi_recv (BUF, N, mpi_int, other, 99, mpi_comm_world, & status); <br/> end = mpi_wtime (); <br/> If (myid = 1) printf ("Case 1: % F/N", end-Start ); </P> <p> Start = mpi_wtime (); <br/> I = N; <br/> while (I --) {<br/> If (myid = 0) <br/> mpi_send (& Buf [I], 1, mpi_int, other, 99, mpi_comm_world ); <br/> else <br/> mpi_recv (& Buf [I], 1, mpi_int, other, 99, mpi_comm_world, & status ); <br/>}< br/> end = mpi_wtime (); <br/> If (myid = 1) printf ("Case 2: % F/N ", end-Start); <br/> free (BUF); <br/> Buf = NULL; <br/> // printf ("process % d freeing memory... /n ", myid); </P> <p> mpi_finalize (); <br/>}< br/>
Result:
[Root @ c0108 zlt] # mpicc comm. c-o comm <br/> [root @ c0108 zlt] # mpiexec-N 2. /Comm <br/> case 1: 0.002562 <br/> case 2: 0.004875