IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
Window.hpp
Go to the documentation of this file.
1//
2// Class Window
3// Defines an interface to perform one-sided communication.
4// The term RMA stands for remote memory accesss.
5//
7
8namespace ippl {
9 namespace mpi {
10 namespace rma {
11
12 template <TargetComm Target>
14 if (win_m != MPI_WIN_NULL) {
15 MPI_Win_free(&win_m);
16 win_m = MPI_WIN_NULL;
17 }
18 }
19
20 template <TargetComm Target>
21 template <std::contiguous_iterator Iter>
22 bool Window<Target>::create(const Communicator& comm, Iter first, Iter last) {
24 "No active target communication window");
25
26 if (allocated_m) {
27 return false;
28 }
29 allocated_m = true;
30
31 count_m = std::distance(first, last);
32 int dispUnit = sizeof(typename Iter::value_type);
33 MPI_Aint size = (MPI_Aint)count_m * dispUnit;
34 MPI_Win_create(&(*first), size, dispUnit, MPI_INFO_NULL, comm, &win_m);
35
36 return allocated_m;
37 }
38
39 template <TargetComm Target>
40 template <std::contiguous_iterator Iter>
41 bool Window<Target>::attach(const Communicator& comm, Iter first, Iter last) {
42 if (attached_m) {
43 return false;
44 }
45 attached_m = true;
46
47 if (!allocated_m) {
48 MPI_Win_create_dynamic(MPI_INFO_NULL, comm, &win_m);
49 allocated_m = true;
50 }
52 count_m = std::distance(first, last);
53 MPI_Aint size = (MPI_Aint)count_m * sizeof(typename Iter::value_type);
54 MPI_Win_attach(win_m, &(*first), size);
55
56 return attached_m;
57 }
58
59 template <TargetComm Target>
60 template <std::contiguous_iterator Iter>
62 if (!attached_m) {
63 return false;
64 }
65 attached_m = false;
66 MPI_Win_detach(win_m, &(*first));
67 return true;
68 }
69
70 template <TargetComm Target>
71 void Window<Target>::fence(int asrt) {
73 "No active target communication window");
74 MPI_Win_fence(asrt, win_m);
75 }
76
77 template <TargetComm Target>
78 template <std::contiguous_iterator Iter>
79 void Window<Target>::put(Iter first, Iter last, int dest, unsigned int pos,
80 Request* request) {
81 MPI_Datatype datatype = get_mpi_datatype<typename Iter::value_type>(*first);
82 auto count = std::distance(first, last);
83 if (count > count_m) {
84 throw IpplException("Window::put", "Count exceeds RMA window size.");
85 }
86 if (request == nullptr) {
87 MPI_Put(&(*first), count, datatype, dest, (MPI_Aint)pos, count, datatype,
89 } else {
90 MPI_Rput(&(*first), count, datatype, dest, (MPI_Aint)pos, count, datatype,
91 win_m, *request);
92 }
93 }
94
95 template <TargetComm Target>
96 template <typename T>
97 void Window<Target>::put(const T* value, int dest, unsigned int pos, Request* request) {
98 MPI_Datatype datatype = get_mpi_datatype<T>(*value);
99 if (request == nullptr) {
100 MPI_Put(value, 1, datatype, dest, (MPI_Aint)pos, 1, datatype, win_m);
101 } else {
102 MPI_Rput(value, 1, datatype, dest, (MPI_Aint)pos, 1, datatype, win_m, *request);
103 }
104 }
105
106 template <TargetComm Target>
107 template <std::contiguous_iterator Iter>
108 void Window<Target>::get(Iter first, Iter last, int source, unsigned int pos,
109 Request* request) {
110 MPI_Datatype datatype = get_mpi_datatype<typename Iter::value_type>(*first);
111 auto count = std::distance(first, last);
112 if (count > count_m) {
113 throw IpplException("Window::put", "Count exceeds RMA window size.");
114 }
115 if (request == nullptr) {
116 MPI_Get(&(*first), count, datatype, source, (MPI_Aint)pos, count, datatype,
117 win_m);
118 } else {
119 MPI_Rget(&(*first), count, datatype, source, (MPI_Aint)pos, count, datatype,
120 win_m, *request);
121 }
122 }
123
124 template <TargetComm Target>
125 template <typename T>
126 void Window<Target>::get(T* value, int source, unsigned int pos, Request* request) {
127 MPI_Datatype datatype = get_mpi_datatype<T>(*value);
128 if (request == nullptr) {
129 MPI_Get(value, 1, datatype, source, (MPI_Aint)pos, 1, datatype, win_m);
130 } else {
131 MPI_Rget(value, 1, datatype, source, (MPI_Aint)pos, 1, datatype, win_m,
132 *request);
133 }
134 }
135
136 /*
137 * Passive target communication:
138 */
139 template <TargetComm Target>
140 void Window<Target>::flush(int rank) {
141 static_assert(!isActiveTarget<Target>::value,
142 "No passive target communication window");
143 MPI_Win_flush(rank, win_m);
144 }
145
146 template <TargetComm Target>
148 static_assert(!isActiveTarget<Target>::value,
149 "No passive target communication window");
150 MPI_Win_flush_all(win_m);
151 }
152
153 template <TargetComm Target>
154 void Window<Target>::lock(int locktype, int rank, int asrt) {
155 static_assert(!isActiveTarget<Target>::value,
156 "No passive target communication window");
157 MPI_Win_lock(locktype, rank, asrt, win_m);
158 }
159
160 template <TargetComm Target>
161 void Window<Target>::lockall(int asrt) {
162 static_assert(!isActiveTarget<Target>::value,
163 "No passive target communication window");
164 MPI_Win_lock_all(asrt, win_m);
165 }
166
167 template <TargetComm Target>
168 void Window<Target>::unlock(int rank) {
169 static_assert(!isActiveTarget<Target>::value,
170 "No passive target communication window");
171 MPI_Win_unlock(rank, win_m);
172 }
173
174 template <TargetComm Target>
176 static_assert(!isActiveTarget<Target>::value,
177 "No passive target communication window");
178 MPI_Win_unlock_all(win_m);
179 }
180
181 } // namespace rma
182 } // namespace mpi
183} // namespace ippl
constexpr KOKKOS_INLINE_FUNCTION auto first()
Definition AbsorbingBC.h:10
Definition Archive.h:20
MPI_Datatype get_mpi_datatype(const T &)
Definition DataTypes.h:65
bool detach(Iter first)
Definition Window.hpp:61
void get(Iter first, Iter last, int source, unsigned int pos, Request *request=nullptr)
Definition Window.hpp:108
void lockall(int asrt=0)
Definition Window.hpp:161
void flush(int rank)
Definition Window.hpp:140
bool create(const Communicator &comm, Iter first, Iter last)
Definition Window.hpp:22
void fence(int asrt=0)
Definition Window.hpp:71
bool attach(const Communicator &comm, Iter first, Iter last)
Definition Window.hpp:41
void lock(int locktype, int rank, int asrt=0)
Definition Window.hpp:154
void unlock(int rank)
Definition Window.hpp:168
void put(Iter first, Iter last, int dest, unsigned int pos, Request *request=nullptr)
Definition Window.hpp:79