我想在(Open)MPI的上下文中提出一个关于rand()的问题.我们在并行编程课程中获得了一个实现任务 - 创建一个MPI应用程序,其中所有参与者进程选择一个领导者(随机 - 他们必须"投票").我的程序看起来像这样:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <mpi.h>
int main (int argc, char *argv[]) {
int rank, size, vote, result;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
vote = rand(); // Each process' vote.
printf("%d: %d\n",rank+1, vote); // Only for debugging purposes here.
MPI_Allreduce(&vote, &result, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
result = (result & INT_MAX) % size + 1; // Select the leader.
printf("Process %*d/%d: %d is the leader.\n", (int)(ceil(log10(size+1))), rank+1, size, result);
MPI_Finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我使用OpenMPI1.6编译并运行它时,无论程序启动了多少进程,每个进程的投票都是1804289383.在程序的每次新运行中,该数字始终相同.因此,如果我运行mpirun -np 7 ./a.out,领导者总是5号,如果我用-np 8运行它,第一个进程始终是领导者,依此类推......
可以请任何人解释我我做错了什么以及如何解决这个问题?
非常感谢你.
| 归档时间: |
|
| 查看次数: |
5639 次 |
| 最近记录: |