I’m currently walking myself through some basic MPI examples to refresh my memory (using Pacheco’s book as a guide), but I’m running into a problem I don’t quite understand. To demonstrate attribute caching, I’ve written the following program:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
int rank, size;
int key;
int *value;
void* extra_arg; /* unused */
/* Broadcast value for sync */
int x;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Keyval_create(MPI_DUP_FN, MPI_NULL_DELETE_FN,
&key, extra_arg);
if (rank==0) {
*value = 42;
MPI_Attr_put(MPI_COMM_WORLD, key, value);
x=17;
MPI_Bcast(&x,1,MPI_INT,0,MPI_COMM_WORLD);
} else {
MPI_Bcast(&x,1,MPI_INT,0,MPI_COMM_WORLD);
int* newval;
int flag;
MPI_Attr_get(MPI_COMM_WORLD,key,&newval,&flag);
printf("Value = %d \n", *newval);
}
MPI_Finalize();
return 0;
}
(The broadcast is just there to prevent MPI_Attr_get from occuring before the put.)
This, if I’m doing it properly, should result in all processes but rank 0 printing “Value = 42\n”. What I get, however, if I do “mpirun -np 2 ./a.out”, is
[exp:27936] *** Process received signal ***
[exp:27936] Signal: Segmentation fault (11)
[exp:27936] Signal code: Invalid permissions (2)
[exp:27936] Failing at address: 0xb763aff4
[exp:27936] [ 0] [0xf57fe40c]
[exp:27936] [ 1] ./a.out(main+0x74) [0x80488e8]
[exp:27936] [ 2] /lib/libc.so.6(__libc_start_main+0xdc) [0xb74fbe9c]
[exp:27936] [ 3] ./a.out [0x80487c1]
[exp:27936] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 27936 on node exp exited on signal 11 (Segmentation fault).--------------------------------------------------------------------------
What I don’t understand is why this is segfaulting! The same error occurs whether I declare “int newval” at the top of main or inside the else, and only occurs when MPI_Attr_get is run: commenting this out and doing something else with newval is fine.
Thoughts?
The segfault is because of a C thing, not an MPI thing – you need newval to be an integer, not a pointer to one:
I think this still doesn’t behave as you want; I’m not sure the attribute is broadcast to all processes associated with the communicator. (Also note that MPI_Attr_get/put have been deprecated as of MPI2 and replaced with MPI_Comm_get/set_attr()).