我使用编程语言c在openMPI
中新增了,我使用函数MPI_SPAWN()
来生成从主服务器到工作服务器的其他进程,我也使用了函数{{1 }& MPI_Gather()
分割数组并将其发送给工人并从中收集结果。
结果似乎是:
MPI_Scatter()
这是我的代码:
主人:
the rank : 2
the rank : 3
0 0 0 0
the rank : 0
0 1073741824 0 1073741824
0 -1879048192 0 -1879048192
the rank : 1
0 -1073741824 0 -1073741824
工人:
int main(int argc, char *argv[]) {
int rank,n_spawns = 4;
int *msg = malloc(sizeof(int)* n_spawns*4);
int *msg1 = malloc(sizeof(int)* n_spawns*4);
int *recvbuf = malloc(sizeof(int) * n_spawns);
srand(0);
for(int i=0;i<4*n_spawns;i++) {
msg[i] = rand() % 20;
printf("%d ", msg[i]);
}
MPI_Init(&argc, &argv);
MPI_Comm intercomm;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_spawn("/Users/a/Desktop/Study/AAPCours/FirstOMPI/worker",
MPI_ARGV_NULL, n_spawns, MPI_INFO_NULL, 0, MPI_COMM_SELF,
&intercomm,MPI_ERRCODES_IGNORE);
MPI_Scatter(msg, n_spawns, MPI_INT, &recvbuf, n_spawns, MPI_INT,
MPI_ROOT, intercomm);
MPI_Gather(&recvbuf, n_spawns, MPI_INT, msg, n_spawns, MPI_INT, 0,
intercomm);
for (int i = 0; i<4*n_spawns; i++) {
printf("%d ", msg[i]);
}
MPI_Finalize();
return 0;
}
是的,有人能帮帮我吗?