c - 处理 void 数据类型的指针算法

标签 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

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

我的做法是否正确?

最佳答案

我认为有问题的两个方面:

指针计算 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);
}

关于c - 处理 void 数据类型的指针算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43785904/

相关文章:

linux - 在集群上以 sudo 权限运行 MPI 程序

c - 并行效率下降不一致

c - MPI 和 C 中的矩阵向​​量乘法

c - 确定处理器的字长

mysql - 如何从 Ubuntu 终端读取一组字符串并将该值插入 MySQL

c - scanf 二维数组大小 C

c - 如何使用 blas 以最佳方式转置矩阵?

c++ - 如何连续运行 MPI 程序?

c++ - 是否有不搜索短文件名的 Windows FindFirstFile/FindNextFile API 的替代方法?

c - C 中的 MPI_Testsome 和 MPI_Iprobe