Pergunta

I mpi program like this:

........
MPI_Status status1;

.......
MPI_Recv(rb,buf_size,MPI_INT,prcid,1,MPI_COMM_WORLD,&status1);

.....

this program can run well. but when i change it like this:

........
MPI_Status* status2;

.......
MPI_Recv(rb,buf_size,MPI_INT,prcid,1,MPI_COMM_WORLD,status2);

.....

it can't run well and gets an error:

erro: Segmentation fault (signal 11)

Doesn't &status1 equal status2?

what is the difference between them ? thank you !

Foi útil?

Solução

No, they are not equivalent, although you are certainly passing the same type of argument each time, which is why the compiler does not complain.

In one case you are passing the address of a structure already allocated on the stack.

In the other case you are passing a pointer which is pointing at some arbitrary (probably non-writeable) location in memory, because it is not initialized.

You can fix the second call with malloc to allocate memory and point the pointer at it.

MPI_Status* status2 = malloc(sizeof(MPI_Status));

Of course, after you are done with it, you should be responsible and free() that memory:

free(status2);

Outras dicas

In the first case, status1 is an object, so &status1 is a pointer to a valid object which MPI_Recv can write to.

In the second case, status2 is an uninitialized pointer. Its value is probably some random stack garbage, so when MPI_Recv tries to write to it, it writes to some random memory location. A lot of memory doesn't like being written to, so you get a segfault. You could also get some subtle memory corruption that will cause strange problems later on.

If you try this, it should work:

MPI_Status status1;
MPI_Status* status2 = &status1;

.......
MPI_Recv(rb,buf_size,MPI_INT,prcid,1,MPI_COMM_WORLD,status2);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top