Reputation: 127
im trying to send the following structs on MPI:
union atributo{
int valor1;
char *valor2;
float valor3;
};
struct estructura{
int *tipo;
union atributo *list;
};
I know they are dinamic pointers but i need to use them. I have the sames structs with statics pointers working but with this ones i cant make them work.
The MPI structs are:
MPI_Datatype atributo_MPI;
MPI_Datatype type[1] = { MPI_BYTE };
int blocklen[1] = { (tamChar +1) };
MPI_Aint disp[1];
disp[0]= offsetof(union atributo, valor2);
MPI_Type_create_struct(1, blocklen, disp, type, &atributo_MPI);
MPI_Type_commit(&atributo_MPI);
MPI_Datatype estructura_MPI;
MPI_Datatype type2[2] = { MPI_INT, atributo_MPI };
int blocklen2[2] = { tamEstruct, tamEstruct};
MPI_Aint disp2[2];
disp2[0]= offsetof(struct estructura, tipo);
disp2[1]= offsetof(struct estructura, list);
MPI_Type_create_struct(2, blocklen2, disp2, type2, &estructura_MPI);
MPI_Type_commit(&estructura_MPI);
Here is an example of how i send the 3 types of union.
union atributo *atributo2;
atributo2=malloc(sizeof(char) * (tamChar+1));
atributo2[0].valor1= 99999999;
MPI_Send(&atributo2[0], 1, atributo_MPI, 1, 123, MPI_COMM_WORLD);
union atributo *atributo3;
atributo3=malloc(sizeof(char) * (tamChar+1));
for(k=0;k<tamChar;k++){
atributo3[0].valor2[k]= 'A' + ( rand() % ( 'Z' - 'A'));
}
atributo3[0].valor2[k] = '\0';
MPI_Send(&atributo3[0], 1, atributo_MPI, 2, 123, MPI_COMM_WORLD);
union atributo *atributo4;
atributo4=malloc(sizeof(char) * (tamChar+1));
float valor1,valor2;
srand(rdtsc());
valor1=(((float)rand())+1.0)*500000.0;
valor2=(((float)rand())+1.0)*25.0;
atributo4[0].valor3=valor2/valor1;
MPI_Send(&atributo4[0], 1, atributo_MPI, 3, 123, MPI_COMM_WORLD);
Here the Receive:
union atributo *atributo3;
atributo3=malloc(sizeof(char) * (tamChar+1));
MPI_Recv(&atributo3[0],1,atributo_MPI,0,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
printf("Valor atributo CHAR %s\n",atributo3[0].valor2); // HERE IT CRASH
If i try to send UNIONs, it works fine with float and int but dosnt work with the array of char.
Here is the error when i try to printf the value2:
*** Process received signal ***
Signal: Segmentation fault (11)
Signal code: Address not mapped (1)
Failing at address: 0x7fa02e917678
Of course, the struct dosnt work yet. I will appreciate any answer. Thank you.
Upvotes: 1
Views: 1659
Reputation: 78364
A pointer is, essentially, an address in memory. MPI processes each have their own address space, that's part of what makes them processes. A pointer to an address in one process's space is useless in another process's space.
There is no guarantee that two different MPI processes have the same set of addresses in their memory space.
Even if they do have the same address space there is no implicit guarantee, nor any way of guaranteeing, that two objects, one of which contains a pointer to the other, will be sent from one process to another in such a way that the pointer in the original address space points to the other object in the receiving address space.
If you need to send structs with pointers from one process to another you will have to send enough information to enable the receiving process to establish its own pointers in its own address space.
See also MPI send derived data type with pointer in Fortran 90
Upvotes: 2