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!
The meaning of the line
is the following: “
ais a point in memory where I have 1 integer. Send it.`In the code you posted above, this is absolutely true:
adoes 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 lineis “receive 1 integer, and store it at
&b.bis a regularint, so it’s all good. In the first receive,you’re trying to receive an integer into anthere is no allocated memory thatint*variablebis pointing to, soRecvhas 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.