2014-12-07 162 views
0

我有一个任务,我需要使用OpenMPI,在这里我需要创建一个新的数据类型来发送消息,因此我搜索了这个并找到了一些东西。MPI派生的数据类型

以下的教程中,我这样做后:

typedef struct { 

    int a; 
    int b; 

}SpecialData; 

SpecialData p,q,vector[size]; 
MPI_Datatype tipSpecial , oldType[2]; 
int noBlocks[2]; 
MPI_Aint offsets[2],extent; 

MPI_Init(&argc, &argv); 

MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
MPI_Comm_size(MPI_COMM_WORLD, &size); 

// setup the 7 double fields 
offsets[0] = 0 ; 
oldType[0] = MPI_DOUBLE; 
noBlocks[0] = 7; 

//setup the 4 int fields of the struct 
MPI_Type_extent(MPI_DOUBLE, &extent); 
offsets[1] = 8 * extent; 
oldType[1] = MPI_INT; 
noBlocks[1] = 4; 
MPI_Type_struct(2, noBlocks, offsets, oldType, &tipSpecial); 
MPI_Type_commit(&tipSpecial); 
///... Some code here where I do things based on the rank. 
p.a = 9; 
p.b = 9; 


MPI_Send(p, 1, tipSpecial, 0, tag, MPI_COMM_WORLD); 
MPI_Recv(q,1, tipSpecial, 0, tag, MPI_COMM_WORLD, &status); 

我在送得到一个错误,并在第一个参数专门接收。

Error: 
Main.c: In function ‘main’: 
Main.c:125:3: error: incompatible type for argument 1 of ‘MPI_Send’ 
    MPI_Send(p, 1, tipSpecial, 0, tag, MPI_COMM_WORLD); 

任何想法为什么会发生这种情况?

+0

你还应该包括什么错误信息你得到 – 2014-12-07 15:39:54

+0

你应该提供'MPI_Send'和'MPI_RECV '带有指向数据的指针,即'&p'和'&q'。 – 2014-12-07 16:00:01

+0

我不能相信这是造成这种情绪的原因。非常感谢你。如果你可以请发表你的回复@HristoIliev所以其他人都可以看到它 – tudoricc 2014-12-07 16:04:28

回答

2

MPI_SendMPI_Recv都希望指向数据缓冲区的指针作为它们的第一个参数。不像阵列,以获得一个标量变量的地址,就必须使用引用操作符&

MPI_Send(&p, 1, tipSpecial, 0, tag, MPI_COMM_WORLD); 
//  ^
MPI_Recv(&q, 1, tipSpecial, 0, tag, MPI_COMM_WORLD, &status); 
//  ^
+0

有一件事。现在,当我尝试收到我得到的东西错误代码7(总线错误),是因为特殊类型的对象没有被初始化或者为什么? – tudoricc 2014-12-07 19:21:53

+0

随着您引入的更改,派生的MPI数据类型不再与结构相匹配。 – 2014-12-07 21:52:11

+0

是的,你是对的,因为那是偏移矢量。 – tudoricc 2014-12-07 21:53:15