1

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.

4

1 回答 1

2

指针本质上是内存中的地址。每个 MPI 进程都有自己的地址空间,这是使它们成为进程的一部分。指向一个进程空间中地址的指针在另一个进程空间中是无用的。

不能保证两个不同的 MPI 进程在其内存空间中具有相同的地址集。

即使它们确实具有相同的地址空间,也没有隐含的保证,也没有任何方式保证两个对象,其中一个包含指向另一个的指针,将从一个进程发送到另一个进程,使得指针在原始地址空间中指向接收地址空间中的另一个对象。

如果您需要将带有指针的结构从一个进程发送到另一个进程,则必须发送足够的信息以使接收进程能够在自己的地址空间中建立自己的指针。

另请参阅MPI 在 Fortran 90 中使用指针发送派生数据类型

于 2013-11-06T22:31:09.057 回答