Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPI in C and pointer transmissions

Tags:

mpi

I am having a new little problem;

I have a little pointer called:

int *a;

Now ..somewhere inside my main method I allocate some space for it using the following lines and assign a value:

a = (int *) malloc(sizeof(int));
*a=5;

..and then I attempt to transmit it (say to process 1):

MPI_Bsend(a, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

On the other end, if I try to receive that pointer

int *b;
MPI_Recv(b, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

printf("This is what I received: %d \n", *b);

I get an error about the buffer!

However if instead of declaring 'b' a pointer I do the following:

int b;
MPI_Recv(&b, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

printf("This is what I received: %d \n", b);

...all seems to be good! Could someone help me figure out what's happening and how to only use pointer?

Thanks in advance!

like image 260
stratis Avatar asked May 17 '26 05:05

stratis


1 Answers

The meaning of the line

MPI_Bsend(a, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

is the following: "a is a point in memory where I have 1 integer. Send it.`

In the code you posted above, this is absolutely true: a does point to an integer, and so it is sent. This is why you can receive it using your second method, since the meaning of the line

MPI_Recv(&b, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

is "receive 1 integer, and store it at &b. b is a regular int, so it's all good. In the first receive, you're trying to receive an integer into an int* variable there is no allocated memory that b is pointing to, so Recv has nowhere to write to. However, I should point out:

NEVER pass a pointer's contents to another process in MPI

MPI processes cannot read each others' memory, and virtual addressing makes one process' pointer completely meaningless to another.

like image 56
suszterpatt Avatar answered May 19 '26 00:05

suszterpatt