I'm a newbie that inserting in parallel programing. For my understanding I try to code by myself. Then i found that, I'm not understand in MPI_Gather. Let see the code first then I will explain later.
#include "mpi.h"
#include <stdio.h>
int main (int argc, char *argv[]) {
int size;
int rank;
int a[12];
int i;
int start,end;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(rank==0)
{
for(i=0;i<12;i++)
{
a[i] = 100;
}
}
start = 12/size*rank;
end = 12/size*(rank+1);
for(i=start;i<end;i++)
{
a[i] = rank;
printf("rank %d set a[%d] equal to %d\n",rank,i,rank);
}
MPI_Gather(&a[start],12/size*rank,MPI_INT,a,12/size*rank,MPI_INT,0,MPI_COMM_WORLD);
if(rank==0)
{
for(i=0;i<12;i++)
{
printf("%d %d\n",i,a[i]);
}
}
MPI_Finalize();
return 0;
}
For this code my aim is to gather the value in the sub array that generate in each process and keep it in array a. Then let process 0 to print it.
First of all, I initial all value in array to 100
Then, I calculate the starting index and end index for each process(in this case number of process much be a multiple of 12)
Next, I assign the value to array that equal to it rank
Next, I gather it
Finally, I print it on the screen
Here is a out put for 3 process of size = 3
rank 2 set a[8] equal to 2
rank 2 set a[9] equal to 2
rank 2 set a[10] equal to 2
rank 2 set a[11] equal to 2
rank 1 set a[4] equal to 1
rank 1 set a[5] equal to 1
rank 1 set a[6] equal to 1
rank 1 set a[7] equal to 1
rank 0 set a[0] equal to 0
rank 0 set a[1] equal to 0
rank 0 set a[2] equal to 0
rank 0 set a[3] equal to 0
0 0
1 0
2 0
3 0
4 100
5 100
6 100
7 100
8 100
9 100
10 100
11 100
As you see, The gather collect only the first length of data, how can i fix it.
Thank you in advance.