MPI通信,的确存在通信的启动时间。
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
int N = 1024;
int *buf = (int *)malloc(sizeof(int) * (N + 1));
if (buf == NULL) printf("Not enouth memory!/n");
for (i = 0; i < N; i++){
buf[i] = myid + i;
}
if(myid == 0) other = 1;
if(myid == 1) other = 0;
start = MPI_Wtime();
if(myid == 0)
MPI_Send(buf, N, MPI_INT, other, 99, MPI_COMM_WORLD);
else
MPI_Recv(buf, N, MPI_INT, other, 99, MPI_COMM_WORLD, &status);
end = MPI_Wtime();
if(myid == 1) printf("case 1: %f/n", end - start);
start = MPI_Wtime();
i = N;
while (i--) {
if(myid == 0)
MPI_Send(&buf[i], 1, MPI_INT, other, 99, MPI_COMM_WORLD);
else
MPI_Recv(&buf[i], 1, MPI_INT, other, 99, MPI_COMM_WORLD, &status);
}
end = MPI_Wtime();
if(myid == 1) printf("case 2: %f/n", end - start);
free(buf);
buf = NULL;
//printf("process %d freeing memory.../n",myid);
MPI_Finalize();
}
result: