Back to index

salome-med  6.5.0
Public Member Functions | Private Attributes | Static Private Attributes
MPI2Connector Class Reference

#include <MPI2Connector.hxx>

Collaboration diagram for MPI2Connector:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MPI2Connector ()
 ~MPI2Connector ()
MPI_Comm remoteMPI2Connect (const std::string &service)
void remoteMPI2Disconnect (const std::string &service)

Private Attributes

int _num_proc
int _nb_proc
MPI_Comm _gcom
bool _srv
std::string _port_name

Static Private Attributes

static const int TIMEOUT = 5

Detailed Description

Definition at line 27 of file MPI2Connector.hxx.


Constructor & Destructor Documentation

Definition at line 25 of file MPI2Connector.cxx.

Here is the call graph for this function:

Definition at line 31 of file MPI2Connector.cxx.

{
}

Member Function Documentation

MPI_Comm MPI2Connector::remoteMPI2Connect ( const std::string &  service)

Definition at line 35 of file MPI2Connector.cxx.

{
  int i;
  char port_name[MPI_MAX_PORT_NAME];
  char port_name_clt[MPI_MAX_PORT_NAME];
  std::ostringstream msg;
  MPI_Comm icom;

  if( service.size() == 0 )
    {
      msg << "[" << _num_proc << "] You have to give a service name !";
      std::cerr << msg.str().c_str() << std::endl;
      throw std::exception();
    }

  _srv = false;

  MPI_Barrier(MPI_COMM_WORLD);

  MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
  if( _num_proc == 0 )
    { 
      /* rank 0 try to be a server. If service is already published, try to be a cient */
      MPI_Open_port(MPI_INFO_NULL, port_name); 
      if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS )
        {
          std::cerr << "[" << _num_proc << "] I get the connection with " << service << " at " << port_name_clt << std::endl;
          MPI_Close_port( port_name );
        }
      else if ( MPI_Publish_name((char*)service.c_str(), MPI_INFO_NULL, port_name) == MPI_SUCCESS )
        {
          _srv = true;
          _port_name = port_name;
          std::cerr << "[" << _num_proc << "] service " << service << " available at " << port_name << std::endl;
        }      
      else if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS )
        {
          std::cerr << "[" << _num_proc << "] I get the connection with " << service << " at " << port_name_clt << std::endl;
          MPI_Close_port( port_name );
        }
      else
        {
          msg << "[" << _num_proc << "] Error on connection with " << service << " at " << port_name_clt;
          std::cerr << msg.str().c_str() << std::endl;
          throw std::exception();
        }
    }
  else
    {
      i=0;
      /* Waiting rank 0 publish name and try to be a client */
      while ( i != TIMEOUT  ) 
        {
          sleep(1);
          if ( MPI_Lookup_name((char*)service.c_str(), MPI_INFO_NULL, port_name_clt) == MPI_SUCCESS )
            {
              std::cerr << "[" << _num_proc << "] I get the connection with " << service << " at " << port_name_clt << std::endl;
              break;
            }
          i++;
        }
      if(i==TIMEOUT)
        {
          msg << "[" << _num_proc << "] Error on connection with " << service << " at " << port_name_clt;
          std::cerr << msg.str().c_str() << std::endl;
          throw std::exception();
        }
    }
  MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
  
  /* If rank 0 is server, all processes call MPI_Comm_accept */
  /* If rank 0 is not server, all processes call MPI_Comm_connect */
  int srv = (int)_srv;
  MPI_Bcast(&srv,1,MPI_INT,0,MPI_COMM_WORLD);
  _srv = (bool)srv;
  if ( _srv )
    MPI_Comm_accept( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &icom );
  else
    MPI_Comm_connect(port_name_clt, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &icom );

  /* create global communicator: servers have low index in global communicator*/
  MPI_Intercomm_merge(icom,!_srv,&_gcom);

  /* only rank 0 can be server for unpublish name */
  if(_num_proc != 0) _srv = false;

  return _gcom;

}

Here is the call graph for this function:

Here is the caller graph for this function:

void MPI2Connector::remoteMPI2Disconnect ( const std::string &  service)

Definition at line 125 of file MPI2Connector.cxx.

{
  std::ostringstream msg;

  if( service.size() == 0 )
    {
      msg << "[" << _num_proc << "] You have to give a service name !";
      std::cerr << msg.str().c_str() << std::endl;
      throw std::exception();
    }

  MPI_Comm_disconnect( &_gcom ); 
  if ( _srv )
    {

      char port_name[MPI_MAX_PORT_NAME];
      strcpy(port_name,_port_name.c_str());

      MPI_Unpublish_name((char*)service.c_str(), MPI_INFO_NULL, port_name); 
      std::cerr << "[" << _num_proc << "] " << service << ": close port " << _port_name << std::endl;
      MPI_Close_port( port_name ); 
    }
  
}

Here is the caller graph for this function:


Member Data Documentation

MPI_Comm MPI2Connector::_gcom [private]

Definition at line 41 of file MPI2Connector.hxx.

int MPI2Connector::_nb_proc [private]

Definition at line 40 of file MPI2Connector.hxx.

int MPI2Connector::_num_proc [private]

Definition at line 38 of file MPI2Connector.hxx.

std::string MPI2Connector::_port_name [private]

Definition at line 43 of file MPI2Connector.hxx.

bool MPI2Connector::_srv [private]

Definition at line 42 of file MPI2Connector.hxx.

const int MPI2Connector::TIMEOUT = 5 [static, private]

Definition at line 45 of file MPI2Connector.hxx.


The documentation for this class was generated from the following files: