【问题标题】:Dealing with pointer arithmetic of a void data type处理 void 数据类型的指针算法
【发布时间】:2017-05-04 14:38:23
【问题描述】:

我知道在 C 中不允许对 void 指针进行算术运算。但是我必须找到解决这个问题的方法。

我有一个函数将数组的前半部分发送到排名 0,将后半部分发送到排名 1:

void send_expand_multiple(void * data, MPI_Datatype datatype, int size) {
    int my_rank, comm_size, intercomm_size, localSize, factor, dst, iniPart, iniPartBytes, i;
    int datatype_size;
    MPI_Type_size(datatype, &datatype_size);

    int local_size = size / 2;

    //Sending the first half
    MPI_Send(data, local_size, datatype, dst, 0, comm);

    //Sending the second half
    int iniPart = local_size;
    int iniPartBytes = iniPart * datatype_size;
    MPI_Send(((char *) data) + iniPartBytes, local_size, datatype, dst, 1, comm);
}

我的解决方案是基于“序列化”的原则。前半部分没有问题,但后半部分我解析了缓冲区并通过添加该值来移动指针 iniPartBytes

最后,计数和数据类型配置 MPI_Send 以便发送 datatype 类型的 local_size 元素。

我的方法正确吗?

【问题讨论】:

  • int local_size = size / 2; 可以在奇数的情况下丢失 1 个字节
  • 这是对代码的简化,所以我们假设它不可能是奇数。谢谢。
  • @EugeneSh。你是对的。我的错,对不起。现在已编辑。
  • 你确实陈述了一个问题,所以它是 OT 上的。你可以在codereview.stackexchange.com/获得更多好运
  • @LPs 这就是我的想法,但是那里的用户重定向了我...

标签: c mpi


【解决方案1】:

我认为麻烦的两个方面:

指针计算iniPart * datatype_size 可能溢出。建议使用 C99 指针数学。

size 可能很奇怪 - 尽管 OP 不理会这一点,但修复起来很容易。

void send_expand_multiple(const void * data, MPI_Datatype datatype, int element_count) {
    int dst = tbd();
    MPI_Comm comm = tbd();

    int datatype_size;
    MPI_Type_size(datatype, &datatype_size);
    int first_half_count = element_count / 2;
    int second_half_count = element_count  - first_half_count;

    //Sending the first half
    MPI_Send(data, first_half_count, datatype, dst, 0, comm);

    //Sending the second half
    const char (*first_half)[first_half_count][datatype_size] = data;
    MPI_Send(first_half + 1, second_half_count, datatype, dst, 1, comm);

    // or
    const char (*first_half)[datatype_size] = data;
    MPI_Send(first_half + first_half_count, second_half_count, datatype, dst, 1, comm);
}

【讨论】:

  • “OP”是什么意思?
  • @siserte OP 是原始发帖人,siserte(你),在这种情况下。
猜你喜欢
  • 2016-11-23
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多