C++ OpenMPMD获取通信大小
我有两个openmpi程序,我是这样开始的C++ OpenMPMD获取通信大小,c++,c,parallel-processing,mpi,openmpi,C++,C,Parallel Processing,Mpi,Openmpi,我有两个openmpi程序,我是这样开始的 mpirun -n 4 ./prog1 : -n 2 ./prog2 现在我如何使用MPI\u Comm\u size(MPI\u Comm\u WORLD,&size)以获得如下大小值: prog1 size=4 prog2 size=2. 到目前为止,我在两个程序中都得到了“6”。这是可行的,尽管得到它有点麻烦。其原理是根据包含可执行文件名称的argv[0]的值,将MPI\u COMM\u WORLD拆分为通信程序 可能是这样的: #inclu
mpirun -n 4 ./prog1 : -n 2 ./prog2
现在我如何使用MPI\u Comm\u size(MPI\u Comm\u WORLD,&size)
以获得如下大小值:
prog1 size=4
prog2 size=2.
到目前为止,我在两个程序中都得到了“6”。这是可行的,尽管得到它有点麻烦。其原理是根据包含可执行文件名称的
argv[0]
的值,将MPI\u COMM\u 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 binary\n",
wRank, wSize, bRank, bSize, argv[0] );
MPI_Comm_free( &binComm );
MPI_Finalize();
return 0;
}
理想情况下,如果存在用于按二进制文件排序的相应类型,则使用
MPI\u Comm\u split\u type()
可以大大简化这一过程。不幸的是,3.1 MPI标准中没有预先定义的MPI\u COMM\u TYPE
。唯一预定义的是MPI\u COMM\u TYPE\u SHARED
,用于在相同共享内存计算节点上运行的进程之间进行排序。。。太糟糕了!对于下一个版本的标准,可能需要考虑什么?,因为您使用开放式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);
#包括
MPI_通信应用通信;
int app_id=atoi(getenv(“OMPI_MCA_orte_app_num”);
MPI通信拆分(MPI通信世界、应用程序id、0和应用程序通信);
现在将有许多不同的
appcomm
通讯器,就像有应用程序上下文一样。我知道这个问题已经过时了,但我想补充一下Hristo Lliev的答案,使它不仅适用于OpenMPI:
您可以使用MPI参数MPI_APPNUM的值,该值对于每个可执行文件都是不同的“颜色”,并将MPI_COMM_WORLD拆分为单独的通讯器,然后打印这些子通讯器的大小。使用MPI_Comm_get_attr(MPI_Comm_WORLD,MPI_APPNUM,&val,&flag);获取MPI_APPNUM的值
#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);