繁体   English   中英

MPI在C函数上创建结构吗?

[英]MPI creating structs over functions in C?

我正在创建一个结构以通过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的声明。

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

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

 int controlFunction(MPI_Dataype mpi_new_type, ...) {

或将其设为全局变量(尽管不要忘记不鼓励使用全局变量的所有原因。)

看来我无法发表评论。

问阿德里安的答复,为什么不鼓励全球人士?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM