【问题标题】:MPI creating structs over functions in C?MPI在C中的函数上创建结构?
【发布时间】:2013-04-27 10:50:07
【问题描述】:

我正在创建一个通过 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 上的结构。问题在于controlFunction 和someFunction 在使用mpicc file.c -o file 编译我的程序时,我得到错误:mpi_new_type undeclared 在这两个函数中。

有什么方法可以在其他函数中访问此数据类型?

谢谢。

编辑 - 添加了更多代码以根据要求显示 mpi_new_type 的声明。

【问题讨论】:

  • 你能告诉我们mpi_new_type的声明吗?

标签: c function struct mpi


【解决方案1】:

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

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

 int controlFunction(MPI_Dataype mpi_new_type, ...) {

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

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 2016-05-08
    • 2014-11-24
    • 2016-09-16
    • 2011-02-14
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多