OpenMPCD
MPI.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OpenMPCD::MPI` class.
4  */
5 
6 #ifndef OPENMPCD_MPI_HPP
7 #define OPENMPCD_MPI_HPP
8 
9 #include <cstddef>
10 
11 #include <mpi.h>
12 
13 namespace OpenMPCD
14 {
15 
16 /**
17  * Wraps MPI functionality.
18  *
19  * The first instance of this class that is constructed in a program will
20  * initialize the MPI library, and will also finalize it once it is destructed.
21  * After that point, no new instances of this class may be constructed, and
22  * at that point, no other instances of this class may exist.
23  *
24  * All functions can throw `OpenMPCD::Exception` in case of an MPI error.
25  */
26 class MPI
27 {
28 public:
29  /**
30  * The constructor.
31  */
32  MPI();
33 
34  /**
35  * The destructor.
36  */
37  ~MPI();
38 
39 public:
40  /**
41  * Returns the total number of processes.
42  */
43  std::size_t getNumberOfProcesses() const;
44 
45  /**
46  * Returns the rank of the current process.
47  */
48  std::size_t getRank() const;
49 
50  /**
51  * Implements the `MPI_BCAST` functionality.
52  *
53  * The MPI communicator used is `MPI_COMM_WORLD`.
54  *
55  * @throw OpenMPCD::NULLPointerException
56  * If `OPENMPCD_DEBUG` is defined, throws if
57  * `count != 0 && buffer == nullptr`.
58  * @throw OpenMPCD::OutOfBoundsException
59  * If `OPENMPCD_DEBUG` is defined, throws if
60  * `root >= getNumberOfProcesses()`.
61  *
62  * @tparam T The type of elements to broadcast.
63  *
64  * @param[in,out] buffer
65  * If `root == getRank()`, this designates the buffer to send
66  * from; otherwise, this designates the buffer to receive
67  * into. In either case, the buffer must be able to hold at
68  * least `count` elements, unless `count == 0`.
69  * @param[in] count
70  * The number of elements to broadcast. May be `0`, in which
71  * case this function does nothing.
72  * @param[in] root
73  * The root process rank.
74  */
75  template<typename T>
76  void broadcast(
77  T* const buffer, const std::size_t count,
78  const std::size_t root) const;
79 
80  /**
81  * Implements the `MPI_BARRIER` functionality.
82  *
83  * The communicator is taken to be `MPI_COMM_WORLD`.
84  */
85  void barrier();
86 
87  /**
88  * Calls `MPI_TEST` on the given request.
89  *
90  * @throw OpenMPCD::NULLPointerException
91  * If `OPENMPCD_DEBUG` is defined, throws if `request == nullptr`.
92  * @throw OpenMPCD::InvalidArgumentException
93  * If `OPENMPCD_DEBUG` is defined, throws if
94  * `*request == MPI_REQUEST_NULL`.
95  *
96  * @param[in,out] request
97  * The request to test; if completed, the pointee will be set
98  * `MPI_REQUEST_NULL`.
99  * @param[out] sender
100  * If not `nullptr` and if the request is completed, the
101  * pointee is set to the rank of the sender of the received
102  * message, if such information is available.
103  *
104  * @return Returns true if the request has been completed, false otherwise.
105  */
106  bool test(
107  MPI_Request* const request,
108  std::size_t* const sender = NULL) const;
109 
110 private:
111  bool initializedByThisInstance;
112  ///< Holds whether this instance initialized MPI.
113 }; //class MPI
114 
115 } //namespace OpenMPCD
116 
118 
119 #endif //OPENMPCD_MPI_HPP
OpenMPCD::MPI::getRank
std::size_t getRank() const
Returns the rank of the current process.
Definition: MPI.cpp:100
OpenMPCD::MPI
Wraps MPI functionality.
Definition: MPI.hpp:26
OpenMPCD::MPI::~MPI
~MPI()
The destructor.
Definition: MPI.cpp:44
OpenMPCD::MPI::MPI
MPI()
The constructor.
Definition: MPI.cpp:12
OpenMPCD::MPI::getNumberOfProcesses
std::size_t getNumberOfProcesses() const
Returns the total number of processes.
Definition: MPI.cpp:79
MPI.hpp
OpenMPCD::MPI::barrier
void barrier()
Implements the MPI_BARRIER functionality.
Definition: MPI.cpp:121
OpenMPCD::MPI::test
bool test(MPI_Request *const request, std::size_t *const sender=NULL) const
Calls MPI_TEST on the given request.
Definition: MPI.cpp:127
OpenMPCD::MPI::broadcast
void broadcast(T *const buffer, const std::size_t count, const std::size_t root) const
Implements the MPI_BCAST functionality.
Definition: ImplementationDetails/MPI.hpp:19