MPI_Op_create: 候选函数不可行。自定义结构指针不能解释为 void 指针

MPI_Op_create: candidate function not viable. Custom structure pointer cannot be interpreted as void pointer

我正在尝试使用 MPI_Op_create() 创建我自己的缩减,以便我可以为函数 MPI_Allreduce() 传递自定义结构类型。参见 link 例如: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.

的定义