c++ - MPI 如何决定其排名大小

标签 c++ mpi computer-science distributed-system

我是 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;}

最佳答案

这是一个很好的引用:

这是一个教程:

这是一个相关的问题

回答你的问题:

  • 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/

相关文章:

c++ - 检查一个 bitset 是否包含另一个 bitset 的所有值

c++ - 将二进制数据 blob 解码为结构

c++ - 如何在可变参数模板函数中访问参数

c++ - MPI 警告 : Program exiting with outstanding receive requests

math - 了解 lambda 演算有多大帮助?

c++ - Qt 读取访问冲突位于 :0x0, flags=0x0(第一次机会)

c - MPI 中的不同进程之间发送和接收

c - Mpi函数定义

math - 32 位计算机如何处理大位数?前任。 512位整数

algorithm - 沿树的边缘分布重量