处理void数据类型

时间:2017-05-04 14:38:24

标签: c mpi

我知道在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

最后,count和数据类型配置MPI_Send以发送 dat_sype 数据类型类型的元素。

我的方法是否正确?

1 个答案:

答案 0 :(得分: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);
}
相关问题