I'm new to MPI and i would like to send an int
array via MPI_Send
to another process.
// Code example
int main(int argc, char ** argv)
{
int * array;
int tag=1;
int size;
int rank;
MPI_Status status;
MPI_Init (&argc,&argv);
MPI_Comm_size (MPI_COMM_WORLD,&size);
MPI_Comm_rank (MPI_COMM_WORLD,&rank);
if (rank == 0)
{
array = malloc (10 * sizeof(int)); // Array of 10 elements
if (!array) // error checking
{
MPI_Abort (MPI_COMM_WORLD,1);
}
MPI_Send(&array,10,MPI_INT,1,tag,MPI_COMM_WORLD);
}
if (rank == 1)
{
MPI_Recv (&array,10,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
// more code here
}
MPI_Finalize();
// More code here
I would like to ask three things.
- Is this a SAFE way to send an array to another process-rank?
- Is this a syntax wise correct usage of
MPI_Send()
andMPI_Recv()
? - Is there a better way to send and receive an array without too much trouble?
Any help is appreciated.
MPI_Recv()
does not allocate the array for you. bothMPI_Send()
andMPI_Recv()
needs a pointer to the data, and not the address of a pointer to the data. – Devotional