Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, from this code I want to distribute one row of 2D array to all processor for example
p1 =1,2,3,4
p2=5,6,7,8
p3=9,10,11,12
p4=13,14,15,16
when run the program (mpirun -np 4 ./a)
MPI_Scatter 
is work fine but
MPI_Reduce
cause stopping in terminal what is the wrong?

What I have tried:

<pre>#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]);
   
if (rank==0) {
//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);
 printf(" Processor %d has max data after reduce : max= %d ", rank,max);

}

else 
    printf("----.\n");

MPI_Finalize();
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900