【发布时间】:2011-10-24 02:51:00
【问题描述】:
我目前正在通过一些基本的 MPI 示例来刷新我的记忆(使用 Pacheco 的书作为指南),但我遇到了一个我不太理解的问题。为了演示属性缓存,我编写了以下程序:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
int rank, size;
int key;
int *value;
void* extra_arg; /* unused */
/* Broadcast value for sync */
int x;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Keyval_create(MPI_DUP_FN, MPI_NULL_DELETE_FN,
&key, extra_arg);
if (rank==0) {
*value = 42;
MPI_Attr_put(MPI_COMM_WORLD, key, value);
x=17;
MPI_Bcast(&x,1,MPI_INT,0,MPI_COMM_WORLD);
} else {
MPI_Bcast(&x,1,MPI_INT,0,MPI_COMM_WORLD);
int* newval;
int flag;
MPI_Attr_get(MPI_COMM_WORLD,key,&newval,&flag);
printf("Value = %d \n", *newval);
}
MPI_Finalize();
return 0;
}
(广播只是为了防止 MPI_Attr_get 在 put 之前发生。)
如果我做得正确,这应该会导致所有进程,但排名 0 打印“Value = 42\n”。但是,如果我执行“mpirun -np 2 ./a.out”,我得到的是
[exp:27936] *** Process received signal ***
[exp:27936] Signal: Segmentation fault (11)
[exp:27936] Signal code: Invalid permissions (2)
[exp:27936] Failing at address: 0xb763aff4
[exp:27936] [ 0] [0xf57fe40c]
[exp:27936] [ 1] ./a.out(main+0x74) [0x80488e8]
[exp:27936] [ 2] /lib/libc.so.6(__libc_start_main+0xdc) [0xb74fbe9c]
[exp:27936] [ 3] ./a.out [0x80487c1]
[exp:27936] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 27936 on node exp exited on signal 11 (Segmentation fault).--------------------------------------------------------------------------
我不明白为什么这是段错误!无论我在 main 的顶部还是在 else 内部声明“int newval”都会发生相同的错误,并且仅在运行 MPI_Attr_get 时发生:将其注释掉并使用 newval 执行其他操作就可以了。
想法?
【问题讨论】:
-
因为我对阻塞的记忆很粗略,我只是尝试用 Send/Recv 替换 Bcast 以进行同步,并得到了相同的结果。