【发布时间】:2018-05-31 05:34:24
【问题描述】:
我正在使用 MPI 和 CGM 现实并行模型实现 Chan 和 Dehne 排序算法。到目前为止,每个进程都从原始向量接收 N/p 个数字,然后每个进程使用快速排序对它们的数字进行顺序排序,然后每个进程从其本地向量创建一个样本(样本大小为 p),然后每个进程将它们的样本发送过来到 P0; P0 应该在一个更大的 p*p 向量中接收所有样本,以便它可以容纳来自所有处理器的数据。这就是我卡住的地方,它似乎正在工作,但由于某种原因,在 P0 接收到它退出的所有数据后,它带有 Signal: Segmentation fault (11)。谢谢。
以下是代码的相关部分:
// Step 2. Each process calculates it's local sample with size comm_sz
local_sample = create_local_sample(sub_vec, n_over_p, comm_sz);
// Step 3. Each process sends it's local sample to P0
if (my_rank == 0) {
global_sample_receiver = (int*)malloc(pow(comm_sz,2)*sizeof(int));
global_sample_receiver = local_sample;
for (i = 1; i < comm_sz; i++) {
MPI_Recv(global_sample_receiver+(i*comm_sz), comm_sz, MPI_INT,
i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
} else {
MPI_Send(local_sample, comm_sz, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
printf("P%d got here\n", my_rank);
MPI_Finalize();
有趣的是,每个进程都到达命令printf("P%d got here\n", my_rank); 并因此打印到终端。同样global_sample_receiver 确实包含它应该在末尾包含的数据,但程序仍然以分段错误完成。
这是输出:
P2 got here
P0 got here
P3 got here
P1 got here
[Krabbe-Ubuntu:05969] *** Process received signal ***
[Krabbe-Ubuntu:05969] Signal: Segmentation fault (11)
[Krabbe-Ubuntu:05969] Signal code: Address not mapped (1)
[Krabbe-Ubuntu:05969] Failing at address: 0x18000003e7
--------------------------------------------------------------------------
mpiexec noticed that process rank 0 with PID 5969 on node Krabbe-Ubuntu
exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
编辑:我发现了问题,原来 local_sample 也需要一个 malloc。
【问题讨论】:
-
你能发一个MCV吗?
-
我回家试试看
-
你为什么要
malloc`global_sample_receiver`然后覆盖它后面的那一行? -
这里的目的不是覆盖它,而是将global_sample_receiver的前p个位置关联到local_sample的p个元素。您认为这是造成问题的原因吗?
标签: c segmentation-fault mpi