Getting with MPI_Type_vector in C

I have a statically allocated NxN 2D array and I want to send the column i th (with i = 0 ... N-1 ).

Writing:

 int main(int argc, char **argv) { int myrank, nprocs; int i,j; int matrix[N][N]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; int col[N]; ... // Define type "column" MPI_Datatype column; MPI_Type_vector(N,1,N,MPI_INT,&column); MPI_Type_commit(&column); if(myrank==0){ j=0; MPI_Send(&matrix[0][j],1,column,1,99,MPI_COMM_WORLD); } if(myrank==1){ // **** FIRST MODE: Don't use "column" type ***** MPI_Recv(col,N,MPI_INT,0,99,MPI_COMM_WORLD,&info); // **** SECOND MODE: Use "column" type ***** // MPI_Recv(col,1,column_INT,0,99,MPI_COMM_WORLD,&info); printf("\nColumn: "); for(j=0;j<N;j++) printf("\n %d",col[j]); } MPI_Type_free(&column); MPI_Finalize(); return 0; } 

Why the first mode returns correctly:

 1 5 9 13 

And the second comes back wrong?

 1 -2 1980804601 1980804675 
+4
source share
1 answer

The MPI data type describes both data composition and volume. So, for example, the number 1 of your column types describes N ints, but so will the number N MPI_INT s. The difference is that your column type (correctly) describes a jump in an NxN sized array to retrieve the column. Sending using this type retrieves N int and associates them with the message.

So, when you send, you send one of the column types to send specific N ints that form the column in your array. But when it's time to receive, while you still want to receive N ints, but you get it in a continuous array of 1d integers (your array is col[] ), so you just want to get N MPI_INT s. If you get the column data type, you will still get N ints, but they will be put into memory at intervals of N ints (indeed, if N is large enough, you will get segfault, because you write well outside the border of your col array). Therefore, when you got with the column type, only the first number was correct; the rest is garbage, because the rest of the array remains uninitialized. Your second piece of data, 5 , will be written just behind the end of your col array.

+5
source

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


All Articles