#include "..."
#include <stdio.h>
int main(int argc, char *argv[])
{
int rank, size, s1, s2, bufsize;
char *buf;
...;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size); //初始化
/*MPI规定必需用PACK_SIZE来为每个消息计算其使用buffer的大小*/
MPI_Pack_size(2, MPI_DOUBLE, MPI_COMM_WORLD, &s1);
//其中表示MPI_DOUBLE类型的数据个数为2, s1为返回值,以字节为单位
MPI_Pack_size(18, MPI_CHAR, MPI_COMM_WORLD, &s2);
bufsize = 2*MPI_BSEND_OVERHEAD + s1 + s2;
//其中MPI_BSEND_OVERHEAD为定义缓冲方式所需額外开销
buf = (char *)malloc(bufsize);
MPI_Buffer_attach(buf, bufsize); //装配一个用于通信的缓冲区,buf为缓冲区地址, bufsize为缓冲区大小
...........
if(rank == 0)
{
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bsend(......);
MPI_Barrier(MPI_COMM_WORLD);
/*MPI_Barrier为同步路障函数,阻塞通信域中所有调用了本函数的进程,
直到所有的调用者都调用了它,进程中的调用才可以返回,该函数用于对各进程实施同步*/
............
}
if(........)
{
//for(int i = 0; i < 90000; i++)这句会出现问题,mpi要求i的定义必须在for外,改成下面的
int i;
for(i = 0; i < 90000; i++)
MPI_Wtime(); //时间函数
.................
int beforRecv = MPI_Wtime();
MPI_Recv(..........);
int afterRecv = MPI_Wtime();
int lastTime_recv = afterRecv - beforeRecv; //latTime_recv为接收持续时间
...........
}
MPI_Buffer_detach(&buf, &bufsize); //卸载用于通信的缓冲区
free(buf);
MPI_Finalize();
return 0;
}
#include "..."
#include <stdio.h>
int main