-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathMpiCallbacks.hpp
715 lines (630 loc) · 22.2 KB
/
MpiCallbacks.hpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Copyright (C) 2010-2019 The ESPResSo project
* Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
* Max-Planck-Institute for Polymer Research, Theory Group
*
* This file is part of ESPResSo.
*
* ESPResSo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ESPResSo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef COMMUNICATION_MPI_CALLBACKS
#define COMMUNICATION_MPI_CALLBACKS
#include <utils/NumeratedContainer.hpp>
#include <utils/as_const.hpp>
#include <utils/tuple.hpp>
#include <utils/type_traits.hpp>
#include <boost/mpi/collectives/all_reduce.hpp>
#include <boost/mpi/collectives/broadcast.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/range/algorithm/remove_if.hpp>
#include <functional>
#include <initializer_list>
#include <tuple>
#include <utility>
namespace Communication {
/**
* This namespace contains tag types
* to indicate what to do with the return
* values of callbacks.
*/
namespace Result {
/** Ignore result */
struct Ignore {};
constexpr auto ignore = Ignore{};
/** Return value from one rank */
struct OneRank {};
constexpr auto one_rank = OneRank{};
/** Reduce return value over all ranks */
struct Reduction {};
constexpr auto reduction = Reduction{};
} // namespace Result
namespace detail {
/**
* @brief Check if a type can be used as a callback argument.
*
* This checks is a type can be a parameter type for a MPI callback.
* Not allowed are pointers and non-const references, as output
* parameters can not work across ranks.
*/
template <class T>
using is_allowed_argument = std::integral_constant<
bool, not(std::is_pointer<T>::value ||
(!std::is_const<std::remove_reference_t<T>>::value &&
std::is_lvalue_reference<T>::value))>;
template <class... Args>
using are_allowed_arguments =
typename Utils::conjunction<is_allowed_argument<Args>...>::type;
/**
* @brief Invoke a callable with arguments from an mpi buffer.
*
* @tparam F A Callable that can be called with Args as parameters.
* @tparam Args Pack of arguments for @p F
*
* @param f Functor to be called
* @param ia Buffer to extract the parameters from
*
* @return Return value of calling @p f.
*/
template <class F, class... Args>
auto invoke(F f, boost::mpi::packed_iarchive &ia) {
static_assert(are_allowed_arguments<Args...>::value,
"Pointers and non-const references are not allowed as "
"arguments for callbacks.");
/* This is the local receive buffer for the parameters. We have to strip
away const so we can actually deserialize into it. */
std::tuple<std::remove_const_t<std::remove_reference_t<Args>>...> params;
Utils::for_each([&ia](auto &e) { ia >> e; }, params);
/* We add const here, so that parameters can only be by value
or const reference. Output parameters on callbacks are not
sensible because the changes are not propagated back, so
we make sure this does not compile. */
return Utils::apply(f, Utils::as_const(params));
}
/**
* @brief Type-erased interface for callbacks.
*
* This encapsulates the signature of the callback
* and the parameter transfer, so that it can be
* called without any type information on the parameters.
*/
struct callback_concept_t {
/**
* @brief Execute the callback.
*
* Unpack parameters for this callback, and then call it.
*/
virtual void operator()(boost::mpi::communicator const &,
boost::mpi::packed_iarchive &) const = 0;
virtual ~callback_concept_t() = default;
};
/**
* @brief Callback without a return value.
*
* This is an implementation of a callback for a specific callable
* @p F and a set of arguments to call it with, where the there
* is no return value.
*/
template <class F, class... Args>
struct callback_void_t final : public callback_concept_t {
F m_f;
callback_void_t(callback_void_t const &) = delete;
callback_void_t(callback_void_t &&) = delete;
template <class FRef>
explicit callback_void_t(FRef &&f) : m_f(std::forward<FRef>(f)) {}
void operator()(boost::mpi::communicator const &,
boost::mpi::packed_iarchive &ia) const override {
detail::invoke<F, Args...>(m_f, ia);
}
};
/**
* @brief Callback with a return value from one rank.
*
* This is an implementation of a callback for a specific callable
* @p F and a set of arguments to call it with, where the value from
* one rank is returned.
*/
template <class F, class... Args>
struct callback_one_rank_t final : public callback_concept_t {
F m_f;
callback_one_rank_t(callback_one_rank_t const &) = delete;
callback_one_rank_t(callback_one_rank_t &&) = delete;
template <class FRef>
explicit callback_one_rank_t(FRef &&f) : m_f(std::forward<FRef>(f)) {}
void operator()(boost::mpi::communicator const &comm,
boost::mpi::packed_iarchive &ia) const override {
auto const result = detail::invoke<F, Args...>(m_f, ia);
/* Check that there was exactly one rank that has returned
* a value. */
assert(1 == boost::mpi::all_reduce(comm, static_cast<int>(!!result),
std::plus<>()));
/* If this rank returned a result, send it to the head node. */
if (!!result) {
comm.send(0, 42, *result);
}
}
};
/**
* @brief Callback with return value reduction.
*
* This is an implementation of a callback for a specific callable
* @p F and a set of arguments to call it with, where the return
* value is reduced over the communicator.
*/
template <class Op, class F, class... Args>
struct callback_reduce_t final : public callback_concept_t {
Op m_op;
F m_f;
template <class OpRef, class FRef>
explicit callback_reduce_t(OpRef &&op, FRef &&f)
: m_op(std::forward<OpRef>(op)), m_f(std::forward<FRef>(f)) {}
/**
* @brief Execute the callback.
*
* Receive parameters for this callback, and then call it.
*
* @param comm The communicator to receive the parameters on.
*/
void operator()(boost::mpi::communicator const &comm,
boost::mpi::packed_iarchive &ia) const override {
/* Call the callback function, and reduce over the results with
* the stored reduction operation. */
boost::mpi::reduce(comm, detail::invoke<F, Args...>(m_f, ia), m_op, 0);
}
};
template <class F, class R, class... Args> struct FunctorTypes {
using functor_type = F;
using return_type = R;
using argument_types = std::tuple<Args...>;
};
template <class C, class R, class... Args>
auto functor_types_impl(R (C::*)(Args...) const) {
return FunctorTypes<C, R, Args...>{};
}
template <class F>
using functor_types =
decltype(functor_types_impl(&std::remove_reference_t<F>::operator()));
template <class CRef, class C, class R, class... Args>
auto make_model_impl(Result::Ignore, CRef &&c, FunctorTypes<C, R, Args...>) {
return std::make_unique<callback_void_t<C, Args...>>(std::forward<CRef>(c));
}
/**
* @brief Make a @ref callback_model_t for a functor or lambda.
*
* The signature is deduced from F::operator() const, which has
* to exist and can not be overloaded.
*/
template <typename F> auto make_model(F &&f) {
return make_model_impl(Result::Ignore{}, std::forward<F>(f),
functor_types<F>{});
}
/**
* @brief Make a @ref callback_model_t for a function pointer.
*
* This instantiates an implementation of a callback for a function
* pointer. The main task here is to transfer the signature from
* the pointer to the callback_model_t by template argument type
* deduction.
*/
template <class... Args> auto make_model(void (*f_ptr)(Args...)) {
return std::make_unique<callback_void_t<void (*)(Args...), Args...>>(f_ptr);
}
template <class Op, class R, class... Args>
auto make_model(Result::Reduction, R (*f_ptr)(Args...), Op &&op) {
return std::make_unique<
callback_reduce_t<std::remove_reference_t<Op>, R (*)(Args...), Args...>>(
std::forward<Op>(op), f_ptr);
}
template <class R, class... Args>
auto make_model(Result::OneRank, R (*f_ptr)(Args...)) {
return std::make_unique<callback_one_rank_t<R (*)(Args...), Args...>>(f_ptr);
}
} // namespace detail
/**
* @brief The interface of the MPI callback mechanism.
*/
class MpiCallbacks {
public:
/**
* @brief RAII handle for a callback.
*
* This is what the client gets for registering a
* dynamic (= not function pointer) callback.
* It manages the lifetime of the callback handle is
* needed to call it. The handle has a type derived
* from the signature of the callback, which makes
* it possible to do static type checking on the
* arguments. */
template <class... Args> class CallbackHandle {
public:
template <typename F, class = std::enable_if_t<std::is_same<
typename detail::functor_types<F>::argument_types,
std::tuple<Args...>>::value>>
CallbackHandle(MpiCallbacks *cb, F &&f)
: m_id(cb->add(std::forward<F>(f))), m_cb(cb) {}
CallbackHandle(CallbackHandle const &) = delete;
CallbackHandle(CallbackHandle &&rhs) noexcept = default;
CallbackHandle &operator=(CallbackHandle const &) = delete;
CallbackHandle &operator=(CallbackHandle &&rhs) noexcept = default;
private:
int m_id;
MpiCallbacks *m_cb;
public:
/**
* @brief Call the callback managed by this handle.
*
* The arguments are passed to the remote callees, it
* must be possible to call the function with the provided
* arguments, otherwise this will not compile.
*/
template <class... ArgRef>
auto operator()(ArgRef &&... args) const
/* Enable if a hypothetical function with signature void(Args..)
* could be called with the provided arguments. */
-> std::enable_if_t<
std::is_void<decltype(std::declval<void (*)(Args...)>()(
std::forward<ArgRef>(args)...))>::value> {
assert(m_cb), m_cb->call(m_id, std::forward<ArgRef>(args)...);
}
~CallbackHandle() {
if (m_cb)
m_cb->remove(m_id);
}
MpiCallbacks *cb() const { return m_cb; }
};
/* Avoid accidental copy, leads to mpi deadlock
or split brain */
MpiCallbacks(MpiCallbacks const &) = delete;
MpiCallbacks &operator=(MpiCallbacks const &) = delete;
private:
static auto &static_callbacks() {
static std::vector<
std::pair<void (*)(), std::unique_ptr<detail::callback_concept_t>>>
m_callbacks;
return m_callbacks;
}
public:
explicit MpiCallbacks(boost::mpi::communicator &comm,
bool abort_on_exit = true)
: m_abort_on_exit(abort_on_exit), m_comm(comm) {
/** Add a dummy at id 0 for loop abort. */
m_callback_map.add(nullptr);
for (auto &kv : static_callbacks()) {
m_func_ptr_to_id[kv.first] = m_callback_map.add(kv.second.get());
}
}
~MpiCallbacks() {
/* Release the clients on exit */
if (m_abort_on_exit && (m_comm.rank() == 0)) {
try {
abort_loop();
} catch (...) {
}
}
}
private:
/**
* @brief Add a new callback.
*
* Add a new callback to the system. This is a collective
* function that must be run on all nodes.
*
* @tparam F An object with a const call operator.
*
* @param f The callback function to add.
* @return A handle with which the callback can be called.
**/
template <typename F> auto add(F &&f) {
m_callbacks.emplace_back(detail::make_model(std::forward<F>(f)));
return m_callback_map.add(m_callbacks.back().get());
}
public:
/**
* @brief Add a new callback.
*
* Add a new callback to the system. This is a collective
* function that must be run on all nodes.
*
* @param fp Pointer to the static callback function to add.
**/
template <class... Args> void add(void (*fp)(Args...)) {
m_callbacks.emplace_back(detail::make_model(fp));
const int id = m_callback_map.add(m_callbacks.back().get());
m_func_ptr_to_id[reinterpret_cast<void (*)()>(fp)] = id;
}
/**
* @brief Add a new callback.
*
* Add a new callback to the system. This is a collective
* function that must be run on all nodes.
*
* @param fp Pointer to the static callback function to add.
**/
template <class... Args> static void add_static(void (*fp)(Args...)) {
static_callbacks().emplace_back(reinterpret_cast<void (*)()>(fp),
detail::make_model(fp));
}
/**
* @brief Add a new callback with a return value.
*
* Add a new callback to the system. This is a collective
* function that must be run on all nodes.
* Tag is one of the tag types from @namespace Communication::Result,
* which indicates what to do with the return values.
*
* @param tag Tag type indicating return operation
* @param tag_args Argument for the return operation, if any.
* @param fp Pointer to the static callback function to add.
**/
template <class Tag, class R, class... Args, class... TagArgs>
static void add_static(Tag tag, R (*fp)(Args...), TagArgs &&... tag_args) {
static_callbacks().emplace_back(
reinterpret_cast<void (*)()>(fp),
detail::make_model(tag, fp, std::forward<TagArgs>(tag_args)...));
}
private:
/**
* @brief Remove callback.
*
* Remove the callback id from the callback list.
* This is a collective call that must be run on all node.
*
* @param id Identifier of the callback to remove.
*/
void remove(int id) {
m_callbacks.erase(
boost::remove_if(m_callbacks,
[ptr = m_callback_map[id]](auto const &e) {
return e.get() == ptr;
}),
m_callbacks.end());
m_callback_map.remove(id);
}
private:
/**
* @brief call a callback.
*
* Call the callback id.
* The method can only be called on the master
* and has the prerequisite that the other nodes are
* in the MPI loop.
*
* @param id The callback to call.
* @param args Arguments for the callback.
*/
template <class... Args> void call(int id, Args &&... args) const {
/** Can only be call from master */
if (m_comm.rank() != 0) {
throw std::logic_error("Callbacks can only be invoked on rank 0.");
}
/** Check if callback exists */
if (m_callback_map.find(id) == m_callback_map.end()) {
throw std::out_of_range("Callback does not exists.");
}
/** Send request to slaves */
boost::mpi::packed_oarchive oa(m_comm);
oa << id;
/* Pack the arguments into a packed mpi buffer. */
Utils::for_each([&oa](auto &&e) { oa << e; },
std::forward_as_tuple(std::forward<Args>(args)...));
boost::mpi::broadcast(m_comm, oa, 0);
}
public:
/**
* @brief call a callback.
*
* Call a static callback by pointer.
* The method can only be called the master
* and has the prerequisite that the other nodes are
* in the MPI loop. Also the function has to be previously
* registered e.g. with the @ref REGISTER_CALLBACK macro.
* The callback is not called on the head node.
*
* @param fp Pointer to the function to call.
* @param args Arguments for the callback.
*/
template <class... Args, class... ArgRef>
auto call(void (*fp)(Args...), ArgRef &&... args) const ->
/* Enable only if fp can be called with the provided arguments,
* e.g. if fp(args...) is well-formed. */
std::enable_if_t<std::is_void<decltype(fp(args...))>::value> {
const int id = m_func_ptr_to_id.at(reinterpret_cast<void (*)()>(fp));
call(id, std::forward<ArgRef>(args)...);
}
/**
* @brief call a callback.
*
* Call a static callback by pointer.
* The method can only be called the master
* and has the prerequisite that the other nodes are
* in the MPI loop. Also the function has to be previously
* registered e.g. with the @ref REGISTER_CALLBACK macro.
* The callback is called on the head node.
*
* @param fp Pointer to the function to call.
* @param args Arguments for the callback.
*/
template <class... Args, class... ArgRef>
void call_all(void (*fp)(Args...), ArgRef &&... args) const {
call(fp, args...);
fp(args...);
}
/**
* @brief Call a callback and reduce the result over all nodes.
*
* This calls a callback on all nodes, including the head node,
* and does a mpi reduction with the registered operation.
*
* This method can only be called on the head node.
*/
template <class Op, class R, class... Args>
auto call(Result::Reduction, Op op, R (*fp)(Args...), Args... args) const
-> std::remove_reference_t<decltype(op(std::declval<R>(),
std::declval<R>()))> {
const int id = m_func_ptr_to_id.at(reinterpret_cast<void (*)()>(fp));
call(id, args...);
std::remove_cv_t<std::remove_reference_t<decltype(
op(std::declval<R>(), std::declval<R>()))>>
result{};
boost::mpi::reduce(m_comm, fp(args...), result, op, 0);
return result;
}
/**
* @brief Call a callback and reduce the result over all nodes.
*
* This calls a callback on all nodes, including the head node,
* and returns the value from the node which returned an engaged
* optional.
*
* This method can only be called on the head node.
*/
template <class R, class... Args, class... ArgRef>
auto call(Result::OneRank, boost::optional<R> (*fp)(Args...),
ArgRef... args) const -> std::remove_reference_t<R> {
const int id = m_func_ptr_to_id.at(reinterpret_cast<void (*)()>(fp));
call(id, args...);
auto const local_result = fp(std::forward<Args>(args)...);
assert(1 == boost::mpi::all_reduce(m_comm, static_cast<int>(!!local_result),
std::plus<>()));
if (!!local_result) {
return *local_result;
}
std::remove_cv_t<std::remove_reference_t<R>> result;
m_comm.recv(boost::mpi::any_source, boost::mpi::any_tag, result);
return result;
}
/**
* @brief Mpi slave loop.
*
* This is the callback loop for the slaves. They block
* on the MPI call and wait for a new callback request
* coming from the master.
* This should be run on the slaves and must be running
* so that the master can issue call().
*/
void loop() const {
for (;;) {
/** Communicate callback id and parameters */
boost::mpi::packed_iarchive ia(m_comm);
boost::mpi::broadcast(m_comm, ia, 0);
int request;
ia >> request;
/** id == 0 is loop_abort. */
if (request == LOOP_ABORT) {
break;
}
/** Call the callback */
m_callback_map[request]->operator()(m_comm, ia);
}
}
/**
* @brief Abort mpi loop.
*
* Make the slaves exit the MPI loop.
*/
void abort_loop() { call(LOOP_ABORT); }
/**
* @brief The boost mpi communicator used by this instance
*/
boost::mpi::communicator const &comm() const { return m_comm; }
private:
/**
* @brief Id for the loop_abort. Has to be 0.
*/
enum { LOOP_ABORT = 0 };
/**
* @brief If loop_abort should be called on destruction
* on the head node.
*/
bool m_abort_on_exit;
/**
* The MPI communicator used for the callbacks.
*/
boost::mpi::communicator &m_comm;
/**
* Internal storage for the callback functions.
*/
std::vector<std::unique_ptr<detail::callback_concept_t>> m_callbacks;
/**
* Map of ids to callbacks.
*/
Utils::NumeratedContainer<detail::callback_concept_t *> m_callback_map;
/**
* Mapping of function pointers to ids, so static callbacks can be
* called by their pointer.
*/
std::unordered_map<void (*)(), int> m_func_ptr_to_id;
};
template <class... Args>
using CallbackHandle = MpiCallbacks::CallbackHandle<Args...>;
/**
* @brief Helper class to add callbacks before main.
*
* Should not be used directly, but via @ref REGISTER_CALLBACK.
*/
class RegisterCallback {
public:
RegisterCallback() = delete;
template <class... Args> explicit RegisterCallback(void (*cb)(Args...)) {
MpiCallbacks::add_static(cb);
}
template <class Tag, class R, class... Args, class... TagArgs>
explicit RegisterCallback(Tag tag, R (*cb)(Args...), TagArgs &&... tag_args) {
MpiCallbacks::add_static(tag, cb, std::forward<TagArgs>(tag_args)...);
}
};
} /* namespace Communication */
/**
* @brief Register a static callback
*
* This registers a function as an mpi callback. The
* macro should be used at global scope.
*
* @param cb A function
*/
#define REGISTER_CALLBACK(cb) \
namespace Communication { \
static ::Communication::RegisterCallback register_##cb(&(cb)); \
}
/**
* @brief Register a static callback
*
* This registers a function as an mpi callback with
* reduction of the return values from the nodes.
* The macro should be used at global scope.
*
* @param cb A function
* @param op Reduction operation
*/
#define REGISTER_CALLBACK_REDUCTION(cb, op) \
namespace Communication { \
static ::Communication::RegisterCallback \
register_reduction_##cb(::Communication::Result::Reduction{}, &(cb), \
(op)); \
}
/**
* @brief Register a static callback
*
* This registers a function as an mpi callback with
* reduction of the return values from one node
* where the value of the optional is set.
* The macro should be used at global scope.
*
* @param cb A function
*/
#define REGISTER_CALLBACK_ONE_RANK(cb) \
namespace Communication { \
static ::Communication::RegisterCallback \
register_one_rank_##cb(::Communication::Result::OneRank{}, &(cb)); \
}
#endif