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