我是mpi中的新手,从这段代码我想将一行2D阵列分配给所有处理器,例如
p1 = 1,2,3,4
p2 = 5,6,7,8
p3 = 9,10,11,12
p4 = 13,14,15,16
使用(mpirun -np 4 ./a)
运行程序时,
MPI_Scatter
工作正常但是
MPI_Reduce
导致终端停止我不知道如何通过找到localmax或(mymax)来处理MPI_Reduce
。任何人都可以帮忙吗?
#include "mpi.h"
#include <stdio.h>
#define size 4
int main ()
{
int np, rank, sendcount, recvcount, source,i;
int recvbuf[size];
int mymax;
int max=0;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &np);
int sendbuf[size][size] ={
{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11, 12},
{13, 14, 15, 16}};
source = 1;
sendcount = size;
recvcount = size;
MPI_Scatter(sendbuf,sendcount,MPI_INT,recvbuf,recvcount,MPI_INT,source,MPI_COMM_WORLD);
printf("rank= %d Results: %d %d %d %d \n",rank,recvbuf[0],recvbuf[1],recvbuf[2],recvbuf[3]);
//Each processor has a row, now find local max
mymax = recvbuf[0];
for(i=0;i<recvcount;i++) {
if(mymax < recvbuf[i]) {
mymax = recvbuf[i];
}
}
MPI_Reduce(&mymax,&max ,1,MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
if (rank==0) {
printf(" Processor %d has max data after reduce : max= %d ", rank,max);
}
else
printf("----.\n");
MPI_Finalize();
}