gpt4 book ai didi

c - MPI_UNPACK 截断,缓冲区大小无效

转载 作者:行者123 更新时间:2023-11-30 16:57:47 24 4
gpt4 key购买 nike

我正在使用 MPI 广播包含 4 个整数和 1 个 float 的打包消息。

我计算缓冲区大小的方法是否不正确?我在此函数的 MPI_BCast 上收到消息截断错误。这应该意味着我的缓冲区大小不足以存储接收到的数据。

我已经以完全相同的方式计算了发送/打包数据的缓冲区大小。

   void bcast_sharedData(SharedData *d) {
fprintf(stderr, "Broadcasting Shared Data\n");

int position = 0;
int buff_size = sizeof(int) * 4 + sizeof(float);
char * buffer = malloc(buff_size);

int result = MPI_SUCCESS;
while(result == MPI_SUCCESS) {
result = MPI_Pack(&d->max_tree_depth, 1, MPI_INT, buffer,
buff_size, &position, MPI_COMM_WORLD);
result = MPI_Pack(&d->bit_depth, 1, MPI_INT, buffer, buff_size,
&position, MPI_COMM_WORLD);
result = MPI_Pack(&d->dimensions, 1, MPI_INT, buffer, buff_size,
&position, MPI_COMM_WORLD);
result = MPI_Pack(&d->origin, 1, MPI_INT, buffer, buff_size,
&position, MPI_COMM_WORLD);
result = MPI_Pack(&d->colour_max, 1, MPI_FLOAT, buffer,
buff_size, &position, MPI_COMM_WORLD);
break;
}

if (result != MPI_SUCCESS) {
fprintf(stderr,
"ID 0: Could not Pack Shared Data for Broadcasting");
MPI_Abort(MPI_COMM_WORLD, -1);
}

if (MPI_Bcast(
buffer, 5, MPI_PACKED, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {

fprintf(stderr, "ID 0: Could not Broadcast Shared Data\n");
MPI_Abort(MPI_COMM_WORLD, -1);
};

MPI_Barrier(MPI_COMM_WORLD);

fprintf(stderr, "Broadcasting Kernel\n");
if (MPI_Bcast(
&d->kernel, d->dimensions, MPI_FLOAT, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {

fprintf(stderr, "ID 0: Could not Broadcast Shared Data -> Kernel\n");
MPI_Abort(MPI_COMM_WORLD, -1);
}

MPI_Barrier(MPI_COMM_WORLD);
fprintf(stderr, "DONE Broadcasting Kernel\n");
}

void recv_sharedData(int me, SharedData *d) {
fprintf(stderr, "%d: RECEIVING SHARED DATA\n", me);
int buff_size = sizeof(int) * 4 + sizeof(float);
char * buffer = malloc(buff_size);
int position = 0;

if (MPI_Bcast(
buffer, 5, MPI_PACKED, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {
fprintf(stderr, "ID %d: Could not receive SharedData", me);
MPI_Abort(MPI_COMM_WORLD, -1);
}

fprintf(stderr, "%d: UNPACKING SHARED DATA\n", me);
MPI_Unpack(buffer, 1, &position, &d->max_tree_depth, buff_size,
MPI_INT, MPI_COMM_WORLD);
MPI_Unpack(buffer, 1, &position, &d->bit_depth, buff_size,
MPI_INT, MPI_COMM_WORLD);
MPI_Unpack(buffer, 1, &position, &d->dimensions, buff_size,
MPI_INT, MPI_COMM_WORLD);
MPI_Unpack(buffer, 1, &position, &d->origin, buff_size,
MPI_INT, MPI_COMM_WORLD);
MPI_Unpack(buffer, 1, &position, &d->colour_max, buff_size,
MPI_FLOAT, MPI_COMM_WORLD);

MPI_Barrier(MPI_COMM_WORLD);
fprintf(stderr, "%d: Done Recieving SharedData\n", me);

d->kernel = (float **) malloc (d->dimensions * sizeof (float *));
int i;
for (i = 0; i < d->dimensions; i++)
{
d->kernel[i] = (float *) malloc (d->dimensions * sizeof (float));
}

if (MPI_Bcast(
d->kernel, d->dimensions, MPI_FLOAT, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {
fprintf(stderr, "ID %d: Could not receive SharedData->kernel", me);
}

MPI_Barrier(MPI_COMM_WORLD);
fprintf(stderr, "%d: Done Recieving SharedData->Kernel\n", me);
}

最佳答案

我的“buffsize”(arg5)和“要解包的项目”(arg2)的顺序不正确 - 它们应该被交换。

关于c - MPI_UNPACK 截断,缓冲区大小无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39338882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com