MPI data type for 2 d array

I need to pass an array of integer arrays (basically a 2 d array) for all root processors. I use MPI in C programs. How to declare an MPI data type for a 2nd array. How to send a message (if I use broadcast or scatter)

+3
source share
4 answers

You will need to use Broadcast because you want to send a copy of the same message to each process. Scatter breaks the message and distributes the pieces between the processes.

How to send data: HIndexed for you.

Suppose your 2d array is defined as follows:

int N;            // number of arrays (first dimension)
int sizes[N];     // number of elements in each array (second dimensions)
int* arrays[N];   // pointers to the start of each array

, , :

MPI_Aint base;
MPI_Address(arrays[0], &base);
MPI_Aint* displacements = new int[N];
for (int i=0; i<N; ++i)
{
    MPI_Address(arrays[i], &displacements[i]);
    displacements[i] -= base;
}

:

MPI_Datatype newType;
MPI_Type_hindexed(N, sizes, displacements, MPI_INTEGER, &newType);
MPI_Type_commit(&newType);

, , . , :

MPI_Bcast(arrays, 1, newType, root, comm);   // 'root' and 'comm' is whatever you need

. : , ( int). N, sizes arrays , , , , - , ( , ), :

MPI_Bcast(arrays, 1, newType, root, comm);    // 'root' and 'comm' must have the same value as in the sender code

! .

, , 2- 2d- M. int[N*M]: ++ , , , :

MPI_Bcast(arrays, N*M, MPI_INTEGER, root, comm);

: Indexed HIndexed. , Indexed displacements , HIndexed - (H ). Indexed, , displacements, sizeof(int). , , , , "" ++, HIndexed () .

+12

( , C , Fortran ) MPI, , , . - , , ; . , - , ( C):

MPI_Bcast(&buf, numRows*numCols, MPI_INT, root, MPI_COMM_WORLD)

&buf -

numRows*numCols - , , 2D-

MPI_INT () ,

root - ,

MPI_COMM_WORLD - ,

, - , .

, , , MPI.

+3
MPI_Send(tempmat,16,MPI_INT,0,0,MPI_COMM_WORLD);

MPI_Recv(resultmaster,16,MPI_INT,MPI_ANY_SOURCE , 0, MPI_COMM_WORLD, &stat);

API.

0

, ; . "2d-" .

2) . Broadcast MPI . Scatter, OTOH, MPI. , .

-1
source

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


All Articles