YARP
Yet Another Robot Platform
MpiComm.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2010 Daniel Krieg <krieg@fias.uni-frankfurt.de>
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_MPICOMM_H
11 #define YARP_MPICOMM_H
12 
13 #include <string>
14 #include <yarp/os/Semaphore.h>
15 #include <yarp/os/SystemClock.h>
16 #include <yarp/os/Thread.h>
17 
18 #include <string>
19 #include <iostream>
20 
21 #include <mpi.h>
22 
23 #include "MpiLogComponent.h"
24 
26  public yarp::os::Thread
27 {
28  bool terminate;
29 public:
30  MpiControlThread() : terminate(false) {}
31  void finalize() {
32  terminate = true;}
33  bool threadInit() override;
34  void run() override {
35  while (!terminate) {yarp::os::SystemClock::delaySystem(1);}
36  }
37  void threadRelease() override;
38 };
39 
41 
42 
43 
52 class MpiComm
53 {
54  std::string name;
55 
56 public:
57  char port_name[MPI_MAX_PORT_NAME];
58  char unique_id[10+MPI_MAX_PROCESSOR_NAME];
59  MPI_Comm comm;
61 
62 
63  MpiComm(std::string name);
65  yCTrace(MPI_CARRIER, "[MpiComm @ %s] Destructor", name.c_str() );
66  MPI_Comm_disconnect(&comm);
67  }
68  bool connect(std::string port);
69  bool accept();
70  void disconnect(bool disconn);
71  bool notLocal(std::string other);
72 
73  void openPort() {
74  MPI_Open_port(MPI_INFO_NULL, port_name);
75  }
76  void closePort() {
77  MPI_Close_port(port_name);
78  }
79  int rank() {
80  int rank;
81  MPI_Comm_rank(comm, &rank);
82  return rank;
83  }
84 };
85 
86 
87 #endif // YARP_MPICOMM_H
MpiControlThread * MpiControl
Definition: MpiComm.cpp:25
const yarp::os::LogComponent & MPI_CARRIER()
Wrapper for MPI_Comm communicator.
Definition: MpiComm.h:53
bool accept()
Definition: MpiComm.cpp:146
bool connect(std::string port)
Definition: MpiComm.cpp:116
void closePort()
Definition: MpiComm.h:76
yarp::os::Semaphore sema
Definition: MpiComm.h:60
int rank()
Definition: MpiComm.h:79
MPI_Comm comm
Definition: MpiComm.h:59
bool notLocal(std::string other)
Definition: MpiComm.cpp:108
char unique_id[10+MPI_MAX_PROCESSOR_NAME]
Definition: MpiComm.h:58
void disconnect(bool disconn)
Definition: MpiComm.cpp:168
MpiComm(std::string name)
Definition: MpiComm.cpp:83
void openPort()
Definition: MpiComm.h:73
char port_name[MPI_MAX_PORT_NAME]
Definition: MpiComm.h:57
~MpiComm()
Definition: MpiComm.h:64
void finalize()
Definition: MpiComm.h:31
bool threadInit() override
Initialization method.
Definition: MpiComm.cpp:53
void run() override
Main body of the new thread.
Definition: MpiComm.h:34
void threadRelease() override
Release method.
Definition: MpiComm.cpp:47
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
static void delaySystem(double seconds)
Definition: SystemClock.cpp:32
An abstraction for a thread of execution.
Definition: Thread.h:25
#define yCTrace(component,...)
Definition: LogComponent.h:88