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::finalize
void finalize()
Definition: MpiComm.h:31
SystemClock.h
MpiControlThread::MpiControlThread
MpiControlThread()
Definition: MpiComm.h:30
MpiComm::connect
bool connect(std::string port)
Definition: MpiComm.cpp:116
yarp::os::Semaphore
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
MpiControl
MpiControlThread * MpiControl
Definition: MpiComm.cpp:25
MpiControlThread::run
void run() override
Main body of the new thread.
Definition: MpiComm.h:34
MpiComm::comm
MPI_Comm comm
Definition: MpiComm.h:59
yarp::os::Thread
An abstraction for a thread of execution.
Definition: Thread.h:25
MpiComm::rank
int rank()
Definition: MpiComm.h:79
MpiComm::accept
bool accept()
Definition: MpiComm.cpp:146
MpiControlThread
Definition: MpiComm.h:27
yarp::os::SystemClock::delaySystem
static void delaySystem(double seconds)
Definition: SystemClock.cpp:32
MpiComm::unique_id
char unique_id[10+MPI_MAX_PROCESSOR_NAME]
Definition: MpiComm.h:58
MpiComm::openPort
void openPort()
Definition: MpiComm.h:73
MpiComm::closePort
void closePort()
Definition: MpiComm.h:76
MpiComm::notLocal
bool notLocal(std::string other)
Definition: MpiComm.cpp:108
Thread.h
MpiControlThread::threadRelease
void threadRelease() override
Release method.
Definition: MpiComm.cpp:47
MpiLogComponent.h
Semaphore.h
MpiComm::MpiComm
MpiComm(std::string name)
Definition: MpiComm.cpp:83
MpiComm::sema
yarp::os::Semaphore sema
Definition: MpiComm.h:60
MpiComm::disconnect
void disconnect(bool disconn)
Definition: MpiComm.cpp:168
MpiComm::port_name
char port_name[MPI_MAX_PORT_NAME]
Definition: MpiComm.h:57
MpiComm::~MpiComm
~MpiComm()
Definition: MpiComm.h:64
MpiControlThread::threadInit
bool threadInit() override
Initialization method.
Definition: MpiComm.cpp:53
MpiComm
Wrapper for MPI_Comm communicator.
Definition: MpiComm.h:53
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
MPI_CARRIER
const yarp::os::LogComponent & MPI_CARRIER()
Definition: MpiLogComponent.cpp:16