Openmpi mpmd 获取通信大小
Openmpi mpmd get communication size
我有两个 openmpi 程序,我像这样开始
mpirun -n 4 ./prog1 : -n 2 ./prog2
现在如何使用MPI_Comm_size(MPI_COMM_WORLD, &size)
,以便我得到的大小值为
prog1 size=4
prog2 size=2.
截至目前,我在两个程序中都得到"6"。
我知道
这个问题已经过时了,但我想添加到Hristo Lliev的答案中,使其不仅适用于OpenMPI:
您可以使用 MPI 参数的值MPI_APPNUM每个可执行文件的不同值作为"颜色",并将MPI_COMM_WORLD拆分为单独的通信器,然后打印这些子通信器的大小。使用 MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_APPNUM, &val, &flag );以获取 MPI_APPNUM 的值。
这是可行的,尽管有点麻烦。其原理是根据包含可执行文件名称的argv[0]
的值将MPI_COMM_WORLD
拆分为通信器。
这可能是这样的:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>
int main( int argc, char *argv[] ) {
MPI_Init( &argc, &argv );
int wRank, wSize;
MPI_Comm_rank( MPI_COMM_WORLD, &wRank );
MPI_Comm_size( MPI_COMM_WORLD, &wSize );
int myLen = strlen( argv[0] ) + 1;
int maxLen;
// Gathering the maximum length of the executable' name
MPI_Allreduce( &myLen, &maxLen, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD );
// Allocating memory for all of them
char *names = malloc( wSize * maxLen );
// and copying my name at its place in the array
strcpy( names + ( wRank * maxLen ), argv[0] );
// Now collecting all executable' names
MPI_Allgather( MPI_IN_PLACE, 0, MPI_DATATYPE_NULL,
names, maxLen, MPI_CHAR, MPI_COMM_WORLD );
// With that, I can sort-out who is executing the same binary as me
int binIdx = 0;
while( strcmp( argv[0], names + binIdx * maxLen ) != 0 ) {
binIdx++;
}
free( names );
// Now, all processes with the same binIdx value are running the same binary
// I can split MPI_COMM_WORLD accordingly
MPI_Comm binComm;
MPI_Comm_split( MPI_COMM_WORLD, binIdx, wRank, &binComm );
int bRank, bSize;
MPI_Comm_rank( binComm, &bRank );
MPI_Comm_size( binComm, &bSize );
printf( "Hello from process WORLD %d/%d running %d/%d %s binaryn",
wRank, wSize, bRank, bSize, argv[0] );
MPI_Comm_free( &binComm );
MPI_Finalize();
return 0;
}
在我的机器上,我编译并运行了它,如下所示:
~> mpicc mpmd.c
~> cp a.out b.out
~> mpirun -n 3 ./a.out : -n 2 ./b.out
Hello from process WORLD 0/5 running 0/3 ./a.out binary
Hello from process WORLD 1/5 running 1/3 ./a.out binary
Hello from process WORLD 4/5 running 1/2 ./b.out binary
Hello from process WORLD 2/5 running 2/3 ./a.out binary
Hello from process WORLD 3/5 running 0/2 ./b.out binary
理想情况下,如果存在用于按二进制文件排序的相应类型,则可以使用 MPI_Comm_split_type()
大大简化此操作。不幸的是,在 3.1 MPI 标准中没有预定义的此类MPI_COMM_TYPE_
。唯一预定义的MPI_COMM_TYPE_SHARED
是在同一共享内存计算节点上运行的进程之间进行排序...太糟糕了!也许下一个版本的标准需要考虑一些事情?
由于您使用的是 Open MPI,因此有一个非常简单的特定于 OMPI 的解决方案:
#include <stdlib.h>
MPI_Comm appcomm;
int app_id = atoi(getenv("OMPI_MCA_orte_app_num"));
MPI_Comm_split(MPI_COMM_WORLD, app_id, 0, &appcomm);
现在,有多少应用程序上下文,就会有尽可能多的不同appcomm
通信器。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 数组长度,为什么从命令行获取时不能使用它?
- 如何在进程间通信中获取其他终结点的进程 ID
- Openmpi mpmd 获取通信大小
- 由于PROTOBUF服务器中的字符串数据类型变量和客户端通过cpp中recv端的套接字进行通信,因此正在获取segfau
- 获取用于与C++应用程序通信的网页