forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Communicator.h
108 lines (90 loc) · 2.93 KB
/
Communicator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2017 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidParallel/DllConfig.h"
#include "MantidParallel/Request.h"
#include "MantidParallel/Status.h"
#include "MantidParallel/ThreadingBackend.h"
#ifdef MPI_EXPERIMENTAL
#include <boost/mpi/communicator.hpp>
#endif
namespace boost {
namespace mpi {
class environment;
}
} // namespace boost
namespace ParallelTestHelpers {
class ParallelRunner;
}
namespace Mantid {
namespace Parallel {
#ifdef MPI_EXPERIMENTAL
extern boost::mpi::environment environment;
#endif
/** Wrapper for boost::mpi::communicator. For non-MPI builds an equivalent
implementation with reduced functionality is provided.
@author Simon Heybrock
@date 2017
*/
class MANTID_PARALLEL_DLL Communicator {
public:
Communicator() = default;
#ifdef MPI_EXPERIMENTAL
explicit Communicator(const boost::mpi::communicator &comm);
#endif
int rank() const;
int size() const;
template <typename... T> void send(T &&... args) const;
template <typename... T> Status recv(T &&... args) const;
template <typename... T> Request isend(T &&... args) const;
template <typename... T> Request irecv(T &&... args) const;
#ifdef MPI_EXPERIMENTAL
operator const boost::mpi::communicator &() const;
#endif
bool hasBackend() const;
detail::ThreadingBackend &backend() const;
private:
Communicator(std::shared_ptr<detail::ThreadingBackend> backend,
const int rank);
#ifdef MPI_EXPERIMENTAL
boost::mpi::communicator m_communicator;
#endif
std::shared_ptr<detail::ThreadingBackend> m_backend;
int m_rank{0};
// For accessing constructor with threading backend.
friend class ParallelTestHelpers::ParallelRunner;
};
template <typename... T> void Communicator::send(T &&... args) const {
#ifdef MPI_EXPERIMENTAL
if (!hasBackend())
return m_communicator.send(std::forward<T>(args)...);
#endif
backend().send(m_rank, std::forward<T>(args)...);
}
template <typename... T> Status Communicator::recv(T &&... args) const {
#ifdef MPI_EXPERIMENTAL
if (!hasBackend())
return Status(m_communicator.recv(std::forward<T>(args)...));
#endif
return backend().recv(m_rank, std::forward<T>(args)...);
}
template <typename... T> Request Communicator::isend(T &&... args) const {
#ifdef MPI_EXPERIMENTAL
if (!hasBackend())
return m_communicator.isend(std::forward<T>(args)...);
#endif
return backend().isend(m_rank, std::forward<T>(args)...);
}
template <typename... T> Request Communicator::irecv(T &&... args) const {
#ifdef MPI_EXPERIMENTAL
if (!hasBackend())
return m_communicator.irecv(std::forward<T>(args)...);
#endif
return backend().irecv(m_rank, std::forward<T>(args)...);
}
} // namespace Parallel
} // namespace Mantid