Discussion:
mpi bcast of a pointer and dereferencing.
(too old to reply)
John Smith
2009-05-04 13:42:46 UTC
Permalink
Hello.

I'm new in MPI and I'm trying to build an MPI application.
I have a problem with the following snippet. I work on a Shared
Memory machine.

Basically I want to allocate some pointers to an abstract class and
then send
the pointers to the processes. In the snippet I have a problem with
the allocation.

It crashes on the line "here b". For some reason the processes cannot
dereference the
pointer

Thanks a lot for any help.

I use the commands :
mpicxx crash.cpp -o crash.exe
mpirun -n 4 ./crash.exe


the file crash.cpp is the following:

#include <mpi.h>
int main( int argc, char *argv[] ) {
const size_t ROOT = 0;
int size, rank;
double *data = 0;
//
MPI_Init( &argc, &argv );
//
MPI_Comm_size( MPI_COMM_WORLD, &size );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
//
data = new double;
*data = 1.1;
//
MPI_Bcast( &data, 1, MPI_LONG, ROOT, MPI_COMM_WORLD );
//
MPI_Barrier( MPI_COMM_WORLD );
//
printf(" --->>>> here a rank %d, data %p\n", rank, data);
printf(" --->>>> here b rank %d, data %p, value %f\n", rank, data,
*data);
//
MPI_Finalize();
//
return 0;
}
Michael Hofmann
2009-05-05 06:59:17 UTC
Permalink
Post by John Smith
Hello.
I'm new in MPI and I'm trying to build an MPI application.
I have a problem with the following snippet. I work on a Shared
Memory machine.
Basically I want to allocate some pointers to an abstract class and
then send
the pointers to the processes. In the snippet I have a problem with
the allocation.
It crashes on the line "here b". For some reason the processes cannot
dereference the
pointer
MPI can be used to move data from the address space of one process to that
of another process. Even though you are working on a shared memory
machine, the different MPI processes (usually) have separated address
spaces.
Post by John Smith
data = new double;
*data = 1.1;
//
MPI_Bcast( &data, 1, MPI_LONG, ROOT, MPI_COMM_WORLD );
You are brodadcasting the pointer value ("data") from the root process to
all other processes. However, this value points to a valid location in
memory only on the root process.

If you want to broadcast the data that "data" is pointing to, you should
use something like this:

MPI_Bcast( data, 1, MPI_DOUBLE, ROOT, MPI_COMM_WORLD );


Michael

Loading...