MPI在C中的函数创建结构?

时间:2013-05-03 05:15:17

标签: c function struct mpi

我正在创建一个通过MPI发送的结构,但是在其他函数中使用该结构时遇到了一些麻烦。

typedef struct Coordinates
{
    int x;
    int y;
} XY;

int main (int argc, char *argv[])
{
    MPI_Init(&argc, &argv);
    .
    .
    .
    const int num_items = 2;
    int blocklengths[2] = {1, 1};
    MPI_Aint offsets [2];
    offsets[0] = offsetof(XY, x);
    offsets[1] = offsetof(XY, y);
    MPI_Datatype types[2] = {MPI_INT, MPI_INT};
    MPI_Datatype mpi_new_type;

    MPI_Type_struct(...., &mpi_new_type);
    MPI_Type_commit(&mpi_new_type);

    // Call some function here depending on rank
    if (rank == 0)
        controlFunction(..);
    else
        someFunction(..);

    return 0;
}

int controlFunction(..)
{
    MPI_Recv(.., mpi_new_type,...);
    .
    .
}

int someFunction(..)
{
    MPI_Send(.., mpi_new_type,...);
    .
    .
}

所以基本的想法是我创建一个包含一些数据的结构,并创建一个新的MPI_Datatype来处理MPI上的结构。问题出在controlFunctionsomeFunction,在使用mpicc file.c -o file编译程序时,我在两个函数中都收到错误:mpi_new_type undeclared

我有什么办法可以在其他函数中访问这个数据类型吗?

感谢。

编辑 - 添加了更多代码以显示所请求的mpi_new_type声明。

1 个答案:

答案 0 :(得分:3)

变量mpi_new_type仅在main函数体的范围内可见。该名称在someFunctioncontrolFunction的正文范围内未声明。您可以将变量作为参数传递给那些

 int main() {   
 ...
 if (...)
    controlFunction(mpi_new_type, ...);
 else
    ...
 ...
 }

 int controlFunction(MPI_Dataype mpi_new_type, ...) {

或使其成为一个全局变量(尽管不要忘记为什么不鼓励全局变量的所有原因。)