MPI 标签已禁用

发布于 2025-01-05 17:24:01 字数 1611 浏览 2 评论 0原文

我正在使用 MPI 编写背包问题解决器。打包的当前最佳值作为消息上的标签发送。但是,程序因“MPI_ERR_TAG:无效标记”而崩溃。每当执行时。在阅读了一些 MPI 文档后,似乎标签必须是非负的并且小于常量 MPI_TAG_UB。在检查 MPI_TAG_UB 时,我发现它被设置为零,从而使所有标签无效。为什么会这样呢?我必须以某种方式自己设置这个值吗?

附件是一小段代码,它打印“MPI_TAG_UB = 0”并在我的系统上中止。

#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>

struct P {
    char choices[64];
    int next_choice;
};

typedef struct P Packing;

int main(int argc,char** argv) {
    int rank;
    MPI_Datatype PACKING_TYPE;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    /*Defines a new MPI datatype tag for Packings.*/
    int field_count = 2;
    int field_lengths[2] = {64,1};
    MPI_Aint field_offsets[2];
    field_offsets[0] = 0;
    field_offsets[1] = 64;
    MPI_Datatype field_types[2] = {MPI_CHAR,MPI_INT};
    MPI_Type_struct(field_count,field_lengths,
        field_offsets,field_types,&PACKING_TYPE);
    MPI_Type_commit(&PACKING_TYPE);

    int tag = 1;
    if (rank == 0) {
        Packing pack;
        printf("MPI_TAG_UB = %d\n",MPI_TAG_UB);
        if(MPI_TAG_UB == 0) {
            fprintf(stderr,"Tags disabled!\n");
            abort();
        }
        MPI_Send(&pack,1,PACKING_TYPE,1,tag,MPI_COMM_WORLD);
        puts("Process 0 sent message.");
    }

    if (rank == 1) {
        Packing pack;
        MPI_Status status;
        MPI_Recv(&pack,1,PACKING_TYPE,MPI_ANY_SOURCE,
                MPI_ANY_TAG,MPI_COMM_WORLD,&status);
        puts("Process 1 recieved message.");
    }

    MPI_Type_free(&PACKING_TYPE);
    MPI_Finalize();
    return 0;
}

I'm writing a knapsack problem solver using MPI. The currect optimal value for a packing is sent as a tag on a message. However, the program crached with "MPI_ERR_TAG: Invalid tag." whenever executed. After some reading of the MPI documentation, it seemed that tags had to be non-negative and less than the constant MPI_TAG_UB. On inspection of MPI_TAG_UB, I found it was set to zero, thus making all tags invalid. Why could this be? Do I have to set this value myself somehow?

Attached is a short piece of code that prints "MPI_TAG_UB = 0" and aborts on my system.

#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>

struct P {
    char choices[64];
    int next_choice;
};

typedef struct P Packing;

int main(int argc,char** argv) {
    int rank;
    MPI_Datatype PACKING_TYPE;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    /*Defines a new MPI datatype tag for Packings.*/
    int field_count = 2;
    int field_lengths[2] = {64,1};
    MPI_Aint field_offsets[2];
    field_offsets[0] = 0;
    field_offsets[1] = 64;
    MPI_Datatype field_types[2] = {MPI_CHAR,MPI_INT};
    MPI_Type_struct(field_count,field_lengths,
        field_offsets,field_types,&PACKING_TYPE);
    MPI_Type_commit(&PACKING_TYPE);

    int tag = 1;
    if (rank == 0) {
        Packing pack;
        printf("MPI_TAG_UB = %d\n",MPI_TAG_UB);
        if(MPI_TAG_UB == 0) {
            fprintf(stderr,"Tags disabled!\n");
            abort();
        }
        MPI_Send(&pack,1,PACKING_TYPE,1,tag,MPI_COMM_WORLD);
        puts("Process 0 sent message.");
    }

    if (rank == 1) {
        Packing pack;
        MPI_Status status;
        MPI_Recv(&pack,1,PACKING_TYPE,MPI_ANY_SOURCE,
                MPI_ANY_TAG,MPI_COMM_WORLD,&status);
        puts("Process 1 recieved message.");
    }

    MPI_Type_free(&PACKING_TYPE);
    MPI_Finalize();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2025-01-12 17:24:01

MPI_TAG_UB本身并不是一个实际定义的值,而是一个环境查询键。不幸的是你需要另一个间接的。要检查它,您可以使用:

MPI_Aint* tag_ub_ptr;
MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_TAG_UB, &tag_ub_ptr, &flag);
printf("TAG_UB is %d\n", *tag_ub_ptr);

此外,它保证至少为 32767 并且包含在内。

我不建议使用标签作为实际数据值。这不是它的目的,以这种方式滥用它可能会让您付出一些原本可以实现的优化的代价。您可以使用 PACK/UNPACK 或派生数据类型来发送打包和最佳值。

MPI_TAG_UB is not an actual defined value itself, but an environmental inquiry key. Unfortunately you need another indirection. To check it, you can use:

MPI_Aint* tag_ub_ptr;
MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_TAG_UB, &tag_ub_ptr, &flag);
printf("TAG_UB is %d\n", *tag_ub_ptr);

Furthermore it is guaranteed to be at least 32767 and also inclusive.

I would not recommend to use the tag as an actual data value. This is not what it is intended for and abusing it this way might cost you some optimizations the implementation could otherwise do. You could use PACK/UNPACK or derived data types to send the packing and optimal value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文