gpt4 book ai didi

c - MPI_Scatter() 后跟 malloc 会导致段错误

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

我正在使用 MPI 和 C 进行编程,并且使用根等级从文件中读取数据,然后将其分发到其余等级。我的 MPI_Scatter 工作正常,我打印出这些值以确保它们是正确的(而且确实如此)。我的问题是,分配结构后,当尝试从根级别以外的其他级别访问它们时,我会出现段错误。

    pr_graph * graph = malloc(sizeof(*graph));
....

MPI_Scatter(verticesCountArray, 1, MPI_INT, &(graph->nvtxs), 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
MPI_Scatter(edgesCountArray, 1, MPI_INT, &(graph->nedges), 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);

for(int rank = 0; rank<numProcesses; rank++){
if (rank == myrank){
fprintf(stderr, "%d %d \n",graph->nvtxs, graph->nedges );
graph->xadj = malloc((graph->nvtxs + 1) * sizeof(*graph->xadj));
graph->nbrs = malloc(graph->nedges * sizeof(*graph->nbrs));
// graph->xadj[graph->nvtxs] = graph->nedges;

}
MPI_Barrier(MPI_COMM_WORLD);
}

我的输出是:

    2 4 
2 4
2 4

这是正确的。但是当我取消注释行时,我得到:

    2 4 
2 4
[phi01:07170] *** Process received signal ***
[phi01:07170] Signal: Segmentation fault (11)
[phi01:07170] Signal code: (128)
[phi01:07170] Failing at address: (nil)
[phi01:07170] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x11390)[0x7f5740503390]
[phi01:07170] [ 1] ./pagerank[0x401188]
[phi01:07170] [ 2] ./pagerank[0x400c73]
[phi01:07170] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f5740149830]
[phi01:07170] [ 4] ./pagerank[0x400ce9]
[phi01:07170] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 7170 on node phi01 exited on signal 11 (Segmentation fault).

这意味着只有等级 0 才能访问它分配的结构。谁能指出我为什么?谢谢!

编辑:

插入两个recvbuffer的任何值都不会出现段错误并且会打印出正确的值。看来错误根源在于使用 MPI_Scatter()。

    graph->nvtxs = 2;
graph->nedges = 4;
for(int rank = 0; rank<numProcesses; rank++){
if (rank == myrank){
fprintf(stderr, "%d %d \n",graph->nvtxs, graph->nedges );
graph->xadj = malloc((graph->nvtxs + 1) * sizeof(*graph->xadj));
graph->nbrs = malloc(graph->nedges * sizeof(*graph->nbrs));
graph->xadj[graph->nvtxs] = graph->nedges;

}
MPI_Barrier(MPI_COMM_WORLD);
}

最佳答案

我找到了问题的解决方案。我会先发布它,然后尝试理解它为什么有效。

    pr_int * nvtxs = malloc(sizeof(pr_int));
pr_int * nedges = malloc(sizeof(pr_int));

MPI_Scatter(verticesCountArray, 1, MPI_INT, &(nvtxs), 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
MPI_Scatter(edgesCountArray, 1, MPI_INT, &(nedges), 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);

graph->nvtxs = nvtxs;
graph->nedges = nedges;
for(int rank = 0; rank<numProcesses; rank++){
if (rank == myrank){
fprintf(stderr, "%d %d \n",graph->nvtxs, graph->nedges );
graph->xadj = malloc((graph->nvtxs + 1) * sizeof(*graph->xadj));
graph->nbrs = malloc(graph->nedges * sizeof(*graph->nbrs));
graph->xadj[graph->nvtxs] = graph->nedges;

}
MPI_Barrier(MPI_COMM_WORLD);
}

我认为我没有使用实际的缓冲区(指针)来接收,只是使用常规变量。它们可能在调用 malloc 期间被转换为指针(地址值),这就是结构大小可能非常疯狂的原因。然而,我仍然不确定为什么我能够打印这些值,甚至不确定 0 级是如何毫无问题地工作的。任何想法将不胜感激!谢谢!

关于c - MPI_Scatter() 后跟 malloc 会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43214210/

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