您的位置:首页 > 其它

MPI广播

2017-04-02 18:04 106 查看
/*------------------------MPI广播---------------------------*/
//  Broadcasts a message from the process with rank "root"
//  to all other processes of the communicator,include itself.
//  以某个进程作为根进程将buffer广播给同通信域中的所有进程.

#include "mpi.h"
#include <stdio.h>
#define N 3

int main(int argc, char *argv[])
{
int i, myrank, nprocs;
int buffer
;      //广播数据缓存

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

//为每一个进程的buffer缓存赋初始值
printf("-------------------------------------------------\n");
printf("myrank = %d\nbefore bcasting my buffer's data is:\n", myrank);
for (i = 0; i < N; i++)
{
buffer[i] = myrank+i;
printf("buffer[%d] = %d\n",i, buffer[i]);
}
printf("\n");

/*---------------------------MPI_Bcast详解--------------------------*/
//功能:
//  broadcasts a message from the process with rank root to all
//  processes of the group, itself included.
//  以某个进程作为根进程将buffer广播给同通信域中的所有进程.
//  On return, the contents of root's communication buffer has
//  been copied to all processes.
//  函数返回后根进程数据缓冲区的数据将被拷贝到该通信域所有进程各自
//  的数据缓冲区中去
//函数参数:
//  MPI_Bcast(数据缓存地址,数据个数,数据类型,数据发送进程[root]的标识,通信域)
MPI_Bcast(buffer, N, MPI_INT, 0, MPI_COMM_WORLD);

/*结果输出*/
printf("after bcasting my buffer's data is:\n");
for (i = 0; i < N; i++)
printf("buffer[%d] = %d\n", i, buffer[i]);
printf("\n");

MPI_Finalize();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息