Lily
Lily

Reputation: 846

Save processors results to one Array in MPI

I have a function that is storing some numbers in an array depending on the rank, after finishing I would like to have a bigger array with the result of all processors. Lets say I have 4 processors and I'm running my program as the following

     mpirun -np 4 storesArrays.out

This is my code

int main(int argc, char *argv[])
{
 MPI_Init(&argc, &argv);

int currRank;
MPI_Comm_rank(MPI_COMM_WORLD, &currRank);

int numRanks;
MPI_Comm_size(MPI_COMM_WORLD, &numRanks);

int *currArray;
currArray = generateValues(currRank,numRanks);
MPI_Finalize();
}

Now I want to create an array that stores currArray of each processor. The problem is If I initialize the array in the main, it initialize it every time with each processor. Is there a way to do that?

Upvotes: 0

Views: 296

Answers (1)

jonas25007
jonas25007

Reputation: 251

There are two ways to do it:

Use MPI_Gather, which will gather up each processor's data to one "master" processor.

Or, if each processor needs all the processors' arrays, then use MPI_Allgather.

Upvotes: 1

Related Questions