我是 MPI 初学者,如果我用 C++ 编写这样的代码,系统将如何决定程序将有多少等级? 这不像我们在面向对象语言中遵循的逻辑,当你定义一个数组时,你就知道它的大小。 他们在 MPI 中使用什么机制让系统决定多少等级和整个大小? 它灵活吗?由机器功率决定?还是调用时自动生成?
int main(int argc, char *argv[]){
MPI_Init(NULL, NULL);
int size, rank;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 4)
master();
else
slave(rank);
MPI_Finalize();
return 0;}
最佳答案
这是一个很好的引用:
- http://mpitutorial.com/tutorials/mpi-hello-world/
- http://mpitutorial.com/tutorials/introduction-to-groups-and-communicators/
这是一个教程:
这是一个相关的问题
回答你的问题:
Rank 是(逻辑)进程号(如线程 ID)
Size 是进程总数(已分配,并行度)
当您使用 MPI_Init 时,请参阅:https://www.sharcnet.ca/help/index.php/Getting_Started_with_MPI观察如何设置进程数。
mpirun -n 4 ./parhello
您可以使用“-n N”构造您的 argv[] 数组并指定世界大小以调用 MPI_init。
您还可以使用环境变量设置世界大小,
借用另一个问题/答案:
MPI_Comm_size returns the size of a communicator. In our example, MPI_COMM_WORLD (which is constructed for us by MPI) encloses all of the processes in the job, so this call should return the amount of processes that were requested for the job.
MPI_Comm_rank returns the rank of a process in a communicator. Each process inside of a communicator is assigned an incremental rank starting from zero. The ranks of the processes are primarily used for identification purposes when sending and receiving messages.
等级用于区分进程。您可能有一个“主”进程(等级 = 0)向等级 1-15 的“从”应用程序发送消息。或者您可以使用其他拓扑来划分工作负载。
关于c++ - MPI 如何决定其排名大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144510/