I'm having trouble sending and receiving columns of a 2-d array.
I have 2 processes. The first process has a 2-d array and I want to send parts of it to the second process. So say each rank has a 9x9 array, I'd like rank 0 to send to rank 1 just certain columns:
Example:
-1--2--3-
-2--3--4-
-5--6--7-
...
I want to send "1,2,5,..." and "3,4,7,...".
I've written code to just send the first column, and I've read through this answer and I believe I've correctly defined an MPI_Type_vector for the column:
MPI_Type_vector(dime,1,dime-1,MPI_INT,&LEFT_SIDE);
Where dime
here, 9, is the size of the array; I'm sending 9 blocks of 1 MPI_INT, each separated by a stride of 8 - but even just sending this one column is giving me invalid results.
My code follows:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#define dime 9
int main (int argc, char *argv[])
{
int size,rank;
const int ltag=2;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &size); // Get the number of processes
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get the rank of the process
int table[dime][dime];
for (int i=0; i<dime; i++)
for (int j=0; j<dime; j++)
table[i][j] = rank;
int message[dime];
MPI_Datatype LEFT_SIDE;
MPI_Type_vector(dime,1,dime-1,MPI_INT,&LEFT_SIDE);
MPI_Type_commit(&LEFT_SIDE);
if(rank==0) {
MPI_Send(table, 1, LEFT_SIDE, 1, ltag, MPI_COMM_WORLD);
} else if(rank==1){
MPI_Status status;
MPI_Recv(message, 1, LEFT_SIDE, 0, ltag, MPI_COMM_WORLD, &status);
}
if(rank == 1 ){
printf("Rank 1's received data: ");
for(int i=0;i<dime;i++)
printf("%6d ",*(message+i));
printf("\n");
}
MPI_Finalize();
return 0;
}
But when I run it and look at the data I'm receiving, I get either all zeros or gibberish:
$ mpicc -o datatype datatype.c -Wall -g -O3 -std=c99
$ mpirun -np 2 datatype
Rank 1's received data: 0 32710 64550200 0 1828366128 32765 11780096 0 0
Where the numbers change each time. What am I doing wrong?