MPI虚拟拓扑设计 [英] MPI virtual topology design

查看:140
本文介绍了MPI虚拟拓扑设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用MPI_Comm_split创建星形拓扑,但是当我尝试与所有进程建立链接时似乎却遇到了问题.预期这些进程将链接到MPI_COMM_WORLD的p0.问题是我在行中崩溃

I have been trying to create a star topology using MPI_Comm_split but I seem to have and issue when I try to establish the links withing all processes. The processes are allo expected to link to p0 of MPI_COMM_WORLD . Problem is I get a crash in the line

error=MPI_Intercomm_create(  MPI_COMM_WORLD, 0, NEW_COMM, 0 ,create_tag, &INTERCOMM );

错误是:MPI_ERR_COMM: invalid communicator.

尽管不知道如何解决,但我已经知道了原因.似乎这是由于进程0的调用而导致的,该调用不属于新的communicator(NEW_COMM).我试图放置一个if语句以在process = 0时停止执行此行,但是由于它是一个集体调用,因此这又失败了.

I have and idea of the cause though I don't know how to fix it . It seems this is due to a call by process zero which doesn't belong to the new communicator(NEW_COMM) . I have tried to put an if statement to stop execution of this line if process = 0, but this again fails since its a collective call.

任何建议将不胜感激.

#include <iostream>
#include "mpi.h"

using namespace std;


int main(){

 MPI_Comm NEW_COMM , INTERCOMM;
 MPI_Init(NULL,NULL);
 int world_rank , world_size,new_size, error; 

 error = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
 error = MPI_Comm_size(MPI_COMM_WORLD,&world_size);

 int color = MPI_UNDEFINED;
 if ( world_rank > 0 )
     color = world_rank ;

 error = MPI_Comm_split(MPI_COMM_WORLD, color , world_rank, &NEW_COMM);

 int new_rank;
 if ( world_rank > 0 ) {
      error = MPI_Comm_rank( NEW_COMM , &new_rank);
      error = MPI_Comm_size(NEW_COMM, &new_size);
  }
  int create_tag = 99;

  error=MPI_Intercomm_create(  MPI_COMM_WORLD, 0, NEW_COMM, 0 ,create_tag, &INTERCOMM );

 if ( world_rank > 0 )
    cout<<" My Rank in WORLD  = "<< world_rank <<"  New rank = "<<new_rank << " size of NEWCOMM = "<<new_size  <<endl;
 else
    cout<<" Am centre "<<endl;


   MPI_Finalize();

   return 0;


}

推荐答案

使用MPI拓扑又如何呢?像这样:

What about using a MPI topology rather? Something like this:

#include <mpi.h>
#include <iostream>

int main( int argc, char *argv[] ) {
    MPI_Init( &argc, &argv );
    int rank, size;

    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Comm_size( MPI_COMM_WORLD, &size );

    int indegree, outdegree, *sources, *sourceweights, *destinations, *destweights;

    if ( rank == 0 ) { //centre of the star
        indegree = outdegree = size - 1;
        sources = new int[size - 1];
        sourceweights = new int[size - 1];
        destinations = new int[size - 1];
        destweights = new int[size - 1];
        for ( int i = 0; i < size - 1; i++ ) {
            sources[i] = destinations[i] = i + 1;
            sourceweights[i] = destweights[i] = 1;
        }
    }
    else { // tips of the star
        indegree = outdegree =  1;
        sources = new int[1];
        sourceweights = new int[1];
        destinations = new int[1];
        destweights = new int[1];
        sources[0] = destinations[0] = 0;
        sourceweights[0] = destweights[0] = 1;
    }

    MPI_Comm star;
    MPI_Dist_graph_create_adjacent( MPI_COMM_WORLD, indegree, sources, sourceweights,
                                    outdegree, destinations, destweights, MPI_INFO_NULL,
                                    true, &star );
    delete[] sources;
    delete[] sourceweights;
    delete[] destinations;
    delete[] destweights;

    int starrank;

    MPI_Comm_rank( star, &starrank );

    std::cout << "Process #" << rank << " of MPI_COMM_WORLD is process #" << starrank << " of the star\n";

    MPI_Comm_free( &star);

    MPI_Finalize();

    return 0;
}

那是你所追求的吗?如果不是,您的沟通者是干什么的?

Is that the sort of thing you were after? If not, what is your communicator for?

编辑:有关MPI拓扑的说明

EDIT: Explanation about MPI topologies

我想澄清的是,即使这种图形通信器是这样显示的,它在大多数方面也与MPI_COMM_WORLD没什么不同.值得注意的是,它包括最初存在于MPI_COMM_WORLD中的整个MPI进程集.确实,尽管已经定义了它的星形,并且例如,我们没有代表流程1和流程2之间的任何联系,但是没有什么可以阻止您在这两个流程之间进行点对点交流.简单地,通过定义此图拓扑,您可以指示代码将公开的通信模式.然后,您要求库尝试重新排序物理节点上的等级,以使计算机/网络的物理布局与您表达的需求之间更好地匹配.这可以通过例如使用模拟退火方法使成本函数最小化的算法在内部完成,但这是昂贵的.而且,这假设网络的实际布局可在某个地方供图书馆使用(多数情况下并非如此).因此,在一天的大部分时间结束时,只会忽略此放置优化阶段,而最终得到的索引与您输入的索引相同...我只知道基于网络的一些网状/环形形状机器实际执行MPI_Car_create()的放置阶段,但是也许我已经过时了.

I wanted to clarify that, even if this graph communicator is presented as such, it is no different to MPI_COMM_WORLD in most aspects. Notably, it comprises the whole set of MPI processes initially present in MPI_COMM_WORLD. Indeed, although its star shape has been defined and we didn't represent any link between process #1 and process #2 for example, nothing prevents you from making a point to point communication between these two processes. Simply, by defining this graph topology, you give an indication of the sort of communication pattern your code will expose. Then you ask the library to try to reorder the ranks on the physical nodes, for coming up with a possibly better match between the physical layout of your machine / network, and the needs you express. This can be done internally by an algorithm minimising a cost function using a simulated annealing method for example, but this is costly. Moreover, this supposes that the actual layout of the network is available somewhere for the library to use (which isn't the case most of the time). So at the end of the day, most of the time, this placement optimisation phase is just ignored and you end-up with the same indexes as the ones you entered... Only do I know about some meshed / torus shaped network-based machines to actually perform the placement phase for MPI_Car_create(), but maybe I'm outdated on that.

无论如何,最重要的是,我了解到您想与传播者一起学习,但是不要期望他们太多.这里最好的学习方法是如何以最少,最简单的方式获得所需的电话,这是我希望的建议.

Anyway, the bottom line is that I understand you want to play with communicators for learning, but don't expect too much out of them. The best thing to learn here is how to get the ones you want in the least and simplest possible calls, which is I hope what I proposed.

这篇关于MPI虚拟拓扑设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆