/*
Reduces
values on all processes to a single value
将所有进程的值归并为一个值
*/
int main(
int argc, char * argv[])
{
int myid, nprocs;
int i;
int send[
3];
int recv[
3];
MPI_Init(&argc, &argv);
/*init MPI*/
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
/* get current process id */
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
/* get number of processes */
printf(
"--------------------------------------\n");
printf(
"myrank=%d\n", myid);
/*给每个进程的send缓存区填充数据*/
for (i =
0; i <
3; i++) {
send[i] = myid + i;
printf(
"send[i]=%d ",
send[i]);
}
printf(
"\n");
/*------------------------------MPI_Reduce详解-----------------------------*/
//功能:
// 将每个进程的
send缓存区数据进行MPI_Op[运算]归并,结果储存在根进程的
recv数
// 据缓存中
// 示例:如
send缓存为
int send[N],
recv缓存为
int recv[N],对
send缓存
// 数据进行求和归并。
// 则根进程
recv[i]=proc
0(
send[i])+proc1(
send[i])+...+procN(
send[i])
// N位该通信域进程的总数,proc(
0-N)为进程
0到进程N
//函数参数:
// MPI_Reduce(发送数据缓存地址,接受数据缓存地址,数据个数,数据类型,数据操作,
// 根进程,通信域)
MPI_Reduce(
send,
recv,
3, MPI_INT, MPI_SUM,
0, MPI_COMM_WORLD);
/*结果输出*/
if (myid ==
0)
{
for(i =
0;i<
3;i++)
printf(
"recv buf %d\n",
recv[i]);
}
MPI_Finalize();
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-38920.html