Question in parallel programming MPI_Gather

I am a newbie who inserts into parallel programming. As far as I understand, I'm trying to compile the code myself. Then I discovered that I do not understand in MPI_Gather. Let's look at 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 goal is to collect the value in a helper array that is generated in each process and store it in array a. Then let process 0 print it.

First of all, I deduced all the value in the array to 100

Then I calculate the starting index of the index and the end for each process (in this case, the number of processes is a multiple of 12)

Next, I assign a value to an array equal to its rank

Next, I collect it

Finally, I print it on the screen

Here is an excerpt for 3 process size = 3

rank 2 sets a [8] to 2

2 a [9] 2

2 a [10] 2

2 a [11] 2

1 a [4], 1

1 a [5], 1

1 a [6], 1

1 a [7] 1

rank 0 a [0], 0

rank 0 a [1] 0

rank 0 a [2], 0

rank 0 a [3], 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

, The Collect , .

.

+3
2

, .

MPI_Gather

MPI_Gather(&a[start],12/size,MPI_INT,a,12/size,MPI_INT,0,MPI_COMM_WORLD);

MPI_Gather

#include "mpi.h"
int MPI_Gather ( void *sendbuf, int sendcnt, MPI_Datatype sendtype, 
                void *recvbuf, int recvcount, MPI_Datatype recvtype, 
                int root, MPI_Comm comm )

, sendbuf, sendtype, recvbuf, recvtype, root comm correct; . 12/size*rank, start, integers; 12/size end-start , .

MPI_Gather , ( , MPI_Gatherv()). , , 0, 4, 8; , , , , . (, , , , 0, 0 int, 0 intgs ). , 0 ; 100 , , 0.

, :

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
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 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
0    0
1    0
2    0
3    0
4    1
5    1
6    1
7    1
8    2
9    2
10    2
11    2
+7

Plz, !

#include "mpi.h"
#include <stdio.h>
int main (int argc, char *argv[]) {
int size;
int rank;
int a[12] , b[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,MPI_INT,b,12/size,MPI_INT,0,MPI_COMM_WORLD);
if(rank==0)
{
    for(i=0;i<12;i++)
    {
        printf("%d    %d\n",i,b[i]);
    }
}
MPI_Finalize();
return 0;
}
0

Source: https://habr.com/ru/post/1781110/


All Articles