gpt4 book ai didi

c++ - MPI_Op_create : candidate function not viable. 自定义结构指针不能解释为空指针

转载 作者:太空宇宙 更新时间:2023-11-04 07:47:51 26 4
gpt4 key购买 nike

我正在尝试使用 MPI_Op_create() 创建我自己的缩减,以便我可以为函数 MPI_Allreduce() 传递自定义结构类型。例如,请参见链接:http://www.netlib.org/utk/papers/mpi-book/node118.html

我定义的归约有签名:

void reduction_op(data_t *in, data_t *inout, int *len, MPI_Datatype * datatype)

data_t 是我自定义结构的名称。如果我将 reduction_op 传递给 MPI_OP_create() 我会收到以下编译器错误:

kmeans_short.cpp:60:5: error: no matching function for call to 'MPI_Op_create'
MPI_Op_create(reduction_op, 1, &reduc_op);
^~~~~~~~~~~~~
/usr/local/include/mpi.h:1051:5: note: candidate function not viable: no known conversion from
'void (data_t *, data_t *, int *, MPI_Datatype *)' (aka 'void (data *, data *, int *, int
*)') to 'MPI_User_function *' (aka 'void (*)(void *, void *, int *, int *)') for 1st
argument
int MPI_Op_create(MPI_User_function *user_fn, int commute, MPI_Op *op) MPICH_API_PUBLIC;
^
1 error generated.
make: *** [kmeans] Error 1

请参阅下面的玩具示例。我用 mpicxx 编译(我也尝试了 mpicc 和 mpic++ 并得到了同样的错误)。非常感谢任何解决上述编译错误的帮助!

#include <iostream>
#include <stdlib.h>
#include <mpi.h>
#include <unistd.h>
#include <float.h>
#define N_DATA 1493

using namespace std;


#define FEATURES 8
typedef struct data{//Custom data structure for reduce operation
float feat[FEATURES];
long cluster;
} data_t;


void reduction_op(data_t *in, data_t *inout, int *len, MPI_Datatype * datatype){
data_t temp;
for(int i=0; i< *len; i++){
temp.cluster = in->cluster + inout->cluster;
for(int j=0; j<FEATURES; j++)
temp.feat[j] = in->feat[j] + inout->feat[j];
*inout = temp;
in++;
inout++;
}
}


int main(int argc, char * argv[]){

MPI_Init(&argc, &argv);
int n_data = 1493;

int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int p_data = n_data/world_size; /*length of data per process*/
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

data_t data; data.feat[0] = 0.0; data.cluster = 0;

//mpi type for data_t
MPI_Datatype MPI_data_t;
int structlen = 2;
int blocklength[structlen];
MPI_Datatype type[structlen];
MPI_Aint displacement[structlen];
blocklength[0] = FEATURES; type[0] = MPI_FLOAT;
displacement[0] = (size_t)&(data.feat)-(size_t)&data;
blocklength[1] = 1; type[1] = MPI_LONG;
displacement[1] = (size_t)&(data.cluster) - (size_t)&data;
MPI_Type_create_struct(structlen, blocklength, displacement, type, &MPI_data_t);
MPI_Type_commit(&MPI_data_t);

//CUSTOM REDUCE FUNCTION FOR ALLREDUCE WITH MPI_data_t
MPI_Op reduc_op;
MPI_Op_create(reduction_op, 1, &reduc_op); //ERROR OCCURS HERE


MPI_Type_free(&MPI_data_t);
MPI_Finalize();


return 0;
}

最佳答案

您应该将 reduce_op 的函数声明更改为

void reduction_op(void *in, void *inout, int *len, int* datatype)

然后将void* invoid* inout重铸为data_t。基本上,您对 reduction_op 的定义必须明确遵循 MPI_User_function 的定义。

关于c++ - MPI_Op_create : candidate function not viable. 自定义结构指针不能解释为空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55785837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com