-
Notifications
You must be signed in to change notification settings - Fork 17
/
reduction.hpp
1087 lines (937 loc) · 37.3 KB
/
reduction.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015 Georgia Institute of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file reduction.hpp
* @author Patrick Flick <[email protected]>
* @brief Reduction operations.
*/
#ifndef MXX_REDUCTION_HPP
#define MXX_REDUCTION_HPP
#include <mpi.h>
#include <vector>
#include <iterator>
#include <limits>
#include <functional>
#include <mutex>
#include <atomic>
#include <assert.h> // TODO: replace with own assert (calling MPI_Abort)
#include <cstring> // for memcpy
// mxx includes
#include "datatypes.hpp"
#include "comm_fwd.hpp"
#include "shift.hpp"
namespace mxx {
/*
* Add any (key,value) pair to any MPI_Datatype using MPI caching (keyval)
* functionality (used internally to mxx for adding std::function objects to
* MPI datatypes, required for custom operators)
*/
template <typename K, typename T>
class attr_map {
private:
// static "variables" wrapped into static member functions
static int& key(){ static int k=0; return k; }
static std::mutex& mut(){ static std::mutex m; return m; }
static std::map<K, int>& keymap(){ static std::map<K, int> m; return m; }
static int copy_attr(MPI_Datatype, int, void*, void *attribute_val_in, void *attribute_val_out, int *flag) {
T** out = (T**) attribute_val_out;
// copy via copy constructor
*out = new T(*(T*)(attribute_val_in));
*flag = 1;
return MPI_SUCCESS;
}
static int del_attr(MPI_Datatype, int, void *attribute_val, void *) {
delete (T*)attribute_val;
return MPI_SUCCESS;
}
public:
static void set(const MPI_Datatype& dt, const K& k, const T& value) {
std::lock_guard<std::mutex> lock(mut());
if (keymap().find(k) == keymap().end()) {
// create new keyval with MPI
keymap()[k] = 0;
MPI_Type_create_keyval(&attr_map<K,T>::copy_attr,
&attr_map<K,T>::del_attr,
&(keymap()[k]), (void*)NULL);
}
// insert new key into keymap
// set the keyval pair
T* val = new T(value); // copy construct
MPI_Type_set_attr(dt, keymap()[k], (void*)val);
}
static T& get(const MPI_Datatype& dt, const K& k) {
std::lock_guard<std::mutex> lock(mut());
if (keymap().find(k) == keymap().end()) {
throw std::out_of_range("Key not mapped.");
}
int key = keymap()[k];
T* result;
int flag;
MPI_Type_get_attr(dt, key, &result, &flag);
if (!flag) {
throw std::out_of_range("Key not mapped.");
}
return *result;
}
};
/*********************************************************************
* User supplied functions: *
*********************************************************************/
// std::min and std::max are overloaded functions, and thus can't easily
// be passed as functors.
// Thus we define functors mxx::min<T> and mxx::max<T> with similar
// declarations as std::plus, std::multiply etc
template <typename T>
struct min {
inline const T& operator()(const T& x, const T& y) const {
return std::min(x, y);
}
};
template <typename T>
struct max {
inline const T& operator()(const T& x, const T& y) const {
return std::max(x, y);
}
};
// an Op is not built-in in general:
template <typename T, typename Func>
struct get_builtin_op {
static MPI_Op op(Func) {
return MPI_OP_NULL;
}
};
// define all C++ functors that map to a MPI builtin MPI_Op to directly map
// to that builtin MPI_Op
#define MXX_BUILTIN_OP(mpi_op, cpp_functor) \
template <typename T> \
struct get_builtin_op<T, cpp_functor<T> > { \
static MPI_Op op(cpp_functor<T>) { \
return mpi_op; \
} \
}; \
MXX_BUILTIN_OP(MPI_SUM, std::plus)
MXX_BUILTIN_OP(MPI_PROD, std::multiplies)
MXX_BUILTIN_OP(MPI_LAND, std::logical_and)
MXX_BUILTIN_OP(MPI_LOR, std::logical_or)
MXX_BUILTIN_OP(MPI_BOR, std::bit_or)
MXX_BUILTIN_OP(MPI_BXOR, std::bit_xor)
MXX_BUILTIN_OP(MPI_BAND, std::bit_and)
MXX_BUILTIN_OP(MPI_MAX, mxx::max)
MXX_BUILTIN_OP(MPI_MIN, mxx::min)
#undef MXX_BUILTIN_OP
// for std::min/std::max functions
template <typename T>
struct get_builtin_op<T, const T&(*) (const T&, const T&)> {
static MPI_Op op(const T& (*t)(const T&, const T&)) {
// check if function is std::min or std::max
if (t == static_cast<const T&(*)(const T&, const T&)>(std::min<T>)){
return MPI_MIN;
} else if (t == static_cast<const T&(*)(const T&, const T&)>(std::max<T>)){
return MPI_MAX;
} else {
// otherwise return NULL
return MPI_OP_NULL;
}
}
};
/**
* @brief Wrapps a binary combination/reduction operator for MPI use in
* custom operators.
*
* @note This assumes that the operator is commutative.
*
* @tparam T The input and ouput datatype of the binary operator.
*/
template <typename T>
class custom_op {
public:
/**
* @brief Creates a custom operator given a functor and the associated
* `MPI_Datatype`.
*
* @tparam Func Type of the functor, can be a function pointer, lambda
* function, or std::function or any object with a
* `T operator(T& x, T& y)` member.
* @param func The instance of the functor.
* @param commutative Whether or not the operation is commutative (default = true).
*/
template <typename Func>
custom_op(Func func, const bool commutative = true) : m_builtin(false) {
if (mxx::is_builtin_type<T>::value) {
// check if the operator is MPI built-in (in case the type
// is also a MPI built-in type)
MPI_Op op = get_builtin_op<T, Func>::op(std::forward<Func>(func));
if (op != MPI_OP_NULL) {
// this op is builtin, save it as such and don't copy built-in type
m_builtin = true;
m_op = op;
mxx::datatype dt = mxx::get_datatype<T>();
m_type_copy = dt.type();
}
}
if (!m_builtin) {
// create user function
namespace ph = std::placeholders;
m_user_func = std::bind(custom_op::custom_function<Func>,
std::forward<Func>(func),
ph::_1, ph::_2, ph::_3, ph::_4);
// get datatype associated with the type `T`
mxx::datatype dt = mxx::get_datatype<T>();
// attach function to a copy of the datatype
MPI_Type_dup(dt.type(), &m_type_copy);
attr_map<int, func_t>::set(m_type_copy, 1347, m_user_func);
// create op
MPI_Op_create(&custom_op::mpi_user_function, commutative, &m_op);
}
}
/**
* @brief Returns the MPI_Datatype which has to be used in conjuction
* with the MPI_Op operator.
*
* The custom operators are wrapped into `std::function` objects and
* saved/attached to a duplicated MPI_Datatype as MPI attribute.
*
* When MPI calls the custom user function, the MPI_Datatype is supplied
* and thus the `std::function` object can be accessed and executed.
*
* @returns The `MPI_Datatype` which has to be used in conjuction with the
* `MPI_Op` returned by `get_op()` for all MPI reduction operations.
*/
MPI_Datatype get_type() const {
return m_type_copy;
}
/**
* @brief Returns the `MPI_Op` operator for reduction operations.
*
* @note
* The MPI operator `MPI_Op` returned by this function can only be used in
* conjunction with the `MPI_Datatype` returned by `get_type()`.
*
* @returns The MPI operator as `MPI_Op` object.
*/
MPI_Op get_op() const {
return m_op;
}
/// Destructor: cleanup MPI objects
virtual ~custom_op() {
if (!m_builtin) {
// clean-up (only if this wasn't a built-in MPI_Op)
MPI_Op_free(&m_op);
MPI_Type_free(&m_type_copy);
}
}
private:
// Apply the user provided function to all elements passed by MPI.
// The user provided function (lambda, function pointer, functor)
// is bound to this function via std::bind, and the resulting object
// saved in the MPI_Datatype
template <typename Func>
static void custom_function(Func func, void* invec, void* inoutvec, int* len, MPI_Datatype* dt) {
if (*len > 1) {
T* in = (T*) invec;
T* inout = (T*) inoutvec;
for (int i = 0; i < *len-1; ++i) {
inout[i] = func(in[i], inout[i]);
}
}
// only read and write the `true_extent` of the datatype
// for the last item (otherwise we might encounter a memory error)
// [see github OpenMPI issue #1462]
T in_buf;
T inout_buf;
MPI_Aint true_extent, true_lb;
MPI_Type_get_true_extent(*dt, &true_lb, &true_extent);
std::memcpy((char*)&in_buf+true_lb, (char*)invec+(sizeof(T)*(*len-1)), true_extent);
std::memcpy((char*)&inout_buf+true_lb, (char*)inoutvec+(sizeof(T)*(*len-1)), true_extent);
// now do the operation on our local buffers
inout_buf = func(in_buf, inout_buf);
// copy the results back, again only to true_extent
std::memcpy((char*)inoutvec+(sizeof(T)*(*len - 1)), (char*)&inout_buf+true_lb, true_extent);
}
// MPI custom Op function: (of type MPI_User_function)
// This function is called from within MPI
static void mpi_user_function(void* in, void* inout, int* n, MPI_Datatype* dt) {
// get the std::function from the MPI_Datatype and call it
func_t f = attr_map<int, func_t>::get(*dt, 1347);
f(in, inout, n, dt);
}
// the std::function user function wrapper, which is called from the mpi user function
typedef std::function<void(void*,void*,int*, MPI_Datatype*)> func_t;
func_t m_user_func;
/// Whether the MPI_Op is a builtin operator (e.g. MPI_SUM)
bool m_builtin;
/// The copy (Type_dup) of the MPI_Datatype to work on
MPI_Datatype m_type_copy;
/// The MPI user operator
MPI_Op m_op;
};
/*********************************************************************
* Reductions *
*********************************************************************/
/*********************************************************************
* Reduce *
*********************************************************************/
template <typename T, typename Func>
inline void reduce(const T* in, size_t n, T* out, int root, Func func, const mxx::comm& comm = mxx::comm()) {
// get custom op
mxx::custom_op<T> op(std::forward<Func>(func));
MPI_Reduce(const_cast<T*>(in), out, n, op.get_type(), op.get_op(), root, comm);
}
template <typename T>
inline void reduce(const T* in, size_t n, T* out, int root, const mxx::comm& comm = mxx::comm()) {
reduce(in, n, out, root, std::plus<T>(), comm);
}
template <typename T, typename Func>
inline std::vector<T> reduce(const T* in, size_t n, int root, Func func, const mxx::comm& comm = mxx::comm()) {
std::vector<T> result;
if (comm.rank() == root)
result.resize(n);
reduce(in, n, &result[0], root, func, comm);
return result;
}
template <typename T>
inline std::vector<T> reduce(const T* in, size_t n, int root, const mxx::comm& comm = mxx::comm()) {
return reduce(in, n, root, std::plus<T>(), comm);
}
template <typename T, typename Func>
inline std::vector<T> reduce(const std::vector<T>& in, int root, Func func, const mxx::comm& comm = mxx::comm()) {
return reduce(&in[0], in.size(), root, func, comm);
}
template <typename T>
inline std::vector<T> reduce(const std::vector<T>& in, int root, const mxx::comm& comm = mxx::comm()) {
return reduce(in, root, std::plus<T>(), comm);
}
template <typename T, typename Func>
inline T reduce(const T& x, int root, Func func, const mxx::comm& comm = mxx::comm()) {
// get custom op (and type for custom op)
mxx::custom_op<T> op(std::forward<Func>(func));
T result = T();
MPI_Reduce(const_cast<T*>(&x), &result, 1, op.get_type(), op.get_op(), root, comm);
return result;
}
template <typename T>
inline T reduce(const T& x, int root, const mxx::comm& comm = mxx::comm()) {
return reduce(x, root, std::plus<T>(), comm);
}
/*********************************************************************
* Allreduce *
*********************************************************************/
template <typename T, typename Func>
inline void allreduce(const T* in, size_t n, T* out, Func func, const mxx::comm& comm = mxx::comm()) {
// get custom op
mxx::custom_op<T> op(std::forward<Func>(func));
MPI_Allreduce(const_cast<T*>(in), out, n, op.get_type(), op.get_op(), comm);
}
template <typename T>
inline void allreduce(const T* in, size_t n, T* out, const mxx::comm& comm = mxx::comm()) {
allreduce(in, n, out, std::plus<T>(), comm);
}
template <typename T, typename Func>
inline std::vector<T> allreduce(const T* in, size_t n, Func func, const mxx::comm& comm = mxx::comm()) {
std::vector<T> result(n);
allreduce(in, n, &result[0], func, comm);
return result;
}
template <typename T>
inline std::vector<T> allreduce(const T* in, size_t n, const mxx::comm& comm = mxx::comm()) {
return allreduce(in, n, std::plus<T>(), comm);
}
template <typename T, typename Func>
inline std::vector<T> allreduce(const std::vector<T>& in, Func func, const mxx::comm& comm = mxx::comm()) {
return allreduce(&in[0], in.size(), func, comm);
}
template <typename T>
inline std::vector<T> allreduce(const std::vector<T>& in, const mxx::comm& comm = mxx::comm()) {
return allreduce(in, std::plus<T>(), comm);
}
template <typename T, typename Func>
inline T allreduce(const T& x, Func func, const mxx::comm& comm = mxx::comm()) {
// get custom op (and type for custom op)
mxx::custom_op<T> op(std::forward<Func>(func));
// perform reduction
T result;
MPI_Allreduce(const_cast<T*>(&x), &result, 1, op.get_type(), op.get_op(), comm);
return result;
}
template <typename T>
inline T allreduce(const T& x, const mxx::comm& comm = mxx::comm()) {
return allreduce(x, std::plus<T>(), comm);
}
/************************
* Boolean reductions *
************************/
// useful for testing global conditions, such as termination conditions
inline bool all_of(bool x, const mxx::comm& comm = mxx::comm()) {
int i = x ? 1 : 0;
int result;
MPI_Allreduce(&i, &result, 1, MPI_INT, MPI_LAND, comm);
return result != 0;
}
inline bool any_of(bool x, const mxx::comm& comm = mxx::comm()) {
int i = x ? 1 : 0;
int result;
MPI_Allreduce(&i, &result, 1, MPI_INT, MPI_LOR, comm);
return result != 0;
}
inline bool none_of(bool x, const mxx::comm& comm = mxx::comm()) {
int i = x ? 1 : 0;
int result;
MPI_Allreduce(&i, &result, 1, MPI_INT, MPI_LAND, comm);
return result == 0;
}
template <typename T>
inline bool all_same(const T& x, const mxx::comm& comm = mxx::comm()) {
T y = mxx::right_shift(x, comm);
bool same = comm.rank() == 0 || y == x;
return all_of(same, comm);
}
/*********************************************************************
* Local and Global reductions *
*********************************************************************/
// local reduce
template <typename Iterator, typename Func>
inline typename std::iterator_traits<Iterator>::value_type local_reduce(Iterator begin, Iterator end, Func func) {
assert(std::distance(begin, end) >= 1);
typedef typename std::iterator_traits<Iterator>::value_type T;
T init = std::accumulate(begin+1, end, *begin, func);
return init;
}
template <typename Iterator>
inline typename std::iterator_traits<Iterator>::value_type local_reduce(Iterator begin, Iterator end) {
return local_reduce(begin, end, std::plus<typename std::iterator_traits<Iterator>::value_type>());
}
// overloads for std::vector
template <typename T, typename Func>
inline T local_reduce(const std::vector<T>& in, Func func) {
assert(in.size() >= 1);
T init = std::accumulate(in.begin()+1, in.end(), in.front(), func);
return init;
}
template <typename T>
inline T local_reduce(const std::vector<T>& in) {
return local_reduce(in, std::plus<T>());
}
// global reduce (= local_reduce + allreduce)
template <typename Iterator, typename Func>
inline typename std::iterator_traits<Iterator>::value_type global_reduce(Iterator begin, Iterator end, Func func, const mxx::comm& comm = mxx::comm()) {
size_t n = std::distance(begin, end);
typedef typename std::iterator_traits<Iterator>::value_type T;
if (mxx::allreduce((int)(n >= 1), [](int x, int y) { return (int)(x && y); }, comm) == 0) {
// some processors have 0 elements
mxx::comm nonzero_comm = comm.split(n >= 1);
if (n == 0 && nonzero_comm.size() == comm.size()) {
// all processes have zero elements, thus return default value
return T();
}
// otherwise reduce only over nonzero processors (subcommunicator)
int bcast_rank = -1;
T result;
if (n >= 1) {
// local reduction
result = std::accumulate(begin+1, end, *begin, func);
// reduction in nonzero subcommunicator
result = reduce(result, 0, func, nonzero_comm);
// determine rank of first element of nonzero comm
if (nonzero_comm.rank() == 0)
bcast_rank = comm.rank();
}
// get rank of processor for bcast
int bcast_src = mxx::allreduce(bcast_rank, mxx::max<int>(), comm);
mxx::datatype dt = mxx::get_datatype<T>();
MPI_Bcast(&result, 1, dt.type(), bcast_src, comm);
return result;
} else {
assert(n >= 1);
T init = std::accumulate(begin+1, end, *begin, func);
return allreduce(init, func, comm);
}
}
template <typename Iterator>
inline typename std::iterator_traits<Iterator>::value_type global_reduce(Iterator begin, Iterator end, const mxx::comm& comm = mxx::comm()) {
return global_reduce(begin, end, std::plus<typename std::iterator_traits<Iterator>::value_type>(), comm);
}
// overloads for std::vector
template <typename T, typename Func>
inline T global_reduce(const std::vector<T>& in, Func func, const mxx::comm& comm = mxx::comm()) {
return global_reduce(in.begin(), in.end(), func, comm);
}
template <typename T>
inline T global_reduce(const std::vector<T>& in, const mxx::comm& comm = mxx::comm()) {
return global_reduce(in.begin(), in.end(), std::plus<T>(), comm);
}
/*********************************************************************
* max/min with location *
*********************************************************************/
template <typename T>
inline std::pair<T, int> max_element(const T& x, const mxx::comm& comm = mxx::comm()) {
if (mxx::is_builtin_pair_type<T>::value) {
MPI_Datatype dt = mxx::datatype_pair<T>::get_type();
struct {
T value;
int rank;
} in, out;
in.value = x;
in.rank = comm.rank();
MPI_Allreduce(&in, &out, 1, dt, MPI_MAXLOC, comm);
return std::make_pair(out.value, out.rank);
} else {
// use custom operator
std::pair<T, int> in = std::make_pair(x, comm.rank());
return mxx::allreduce(in, [](const std::pair<T, int>& x, const std::pair<T, int>& y) { return x.first < y.first ? y : x;}, comm);
}
}
// vector operation
template <typename T>
inline std::vector<std::pair<T, int>> max_element(const std::vector<T>& in, const mxx::comm& comm = mxx::comm()) {
std::vector<std::pair<T, int> > pairin(in.size());
MXX_ASSERT(mxx::all_same(in.size(), comm));
for (size_t i = 0; i < in.size(); ++i) {
pairin[i] = std::make_pair(in[i], comm.rank());
}
// don't use MPI_MAXLOC, because it requires re-packing of the data into structs
return mxx::allreduce(pairin, [](const std::pair<T, int>& x, const std::pair<T, int>& y) { return x.first < y.first ? y : x;}, comm);
}
template <typename T>
inline std::pair<T, int> min_element(const T& x, const mxx::comm& comm = mxx::comm()) {
if (mxx::is_builtin_pair_type<T>::value) {
MPI_Datatype dt = mxx::datatype_pair<T>::get_type();
struct {
T value;
int rank;
} in, out;
in.value = x;
in.rank = comm.rank();
MPI_Allreduce(&in, &out, 1, dt, MPI_MINLOC, comm);
return std::make_pair(out.value, out.rank);
} else {
// use custom operator
std::pair<T, int> in = std::make_pair(x, comm.rank());
return mxx::allreduce(in, [](const std::pair<T, int>& x, const std::pair<T, int>& y) { return x.first > y.first ? y : x;}, comm);
}
}
// vector operation
template <typename T>
inline std::vector<std::pair<T, int>> min_element(const std::vector<T>& in, const mxx::comm& comm = mxx::comm()) {
std::vector<std::pair<T, int> > pairin(in.size());
MXX_ASSERT(mxx::all_same(in.size(), comm));
for (size_t i = 0; i < in.size(); ++i) {
pairin[i] = std::make_pair(in[i], comm.rank());
}
// don't use MPI_MAXLOC, because it requires re-packing of the data into structs
return mxx::allreduce(pairin, [](const std::pair<T, int>& x, const std::pair<T, int>& y) { return x.first > y.first ? y : x;}, comm);
}
/*********************************************************************
* Scan *
*********************************************************************/
// reduce over vectors
template <typename T, typename Func>
inline void scan_vec(const T* in, size_t n, T* out, Func func, const mxx::comm& comm = mxx::comm()) {
// get op
mxx::custom_op<T> op(std::forward<Func>(func));
MPI_Scan(const_cast<T*>(in), out, n, op.get_type(), op.get_op(), comm);
}
template <typename T, typename Func>
inline std::vector<T> scan_vec(const T* in, size_t n, Func func, const mxx::comm& comm = mxx::comm()) {
std::vector<T> result(n);
scan_vec(in, n, &result[0], func, comm);
return result;
}
template <typename T, typename Func>
inline std::vector<T> scan_vec(const std::vector<T>& x, Func func, const mxx::comm& comm = mxx::comm()) {
return scan_vec(&x[0], x.size(), func, comm);
}
// single element per processor
template <typename T, typename Func>
inline T scan(const T& x, Func func, const mxx::comm& comm = mxx::comm()) {
// get op
mxx::custom_op<T> op(std::forward<Func>(func));
T result;
MPI_Scan(const_cast<T*>(&x), &result, 1, op.get_type(), op.get_op(), comm);
return result;
}
template <typename T>
inline T scan(const T& x, const mxx::comm& comm = mxx::comm()) {
return scan(x, std::plus<T>(), comm);
}
// local scan
template <typename InIterator, typename OutIterator, typename Func>
void local_scan(InIterator begin, InIterator end, OutIterator out, Func func) {
// return if there's nothing here
if (std::distance(begin, end) == 0)
return;
typedef typename std::iterator_traits<OutIterator>::value_type T;
// start from first element
T val = *begin;
*out = *begin;
++begin;
++out;
// calculate the inclusive prefix sum
while (begin != end) {
val = func(val,*begin);
*out = val;
++begin;
++out;
}
}
// inplace!
template <typename Iterator, typename Func>
void local_scan_inplace(Iterator begin, Iterator end, Func func) {
// return if there's nothing here
if (std::distance(begin, end) == 0)
return;
typedef typename std::iterator_traits<Iterator>::value_type T;
// start from first element
T val = *begin;
++begin;
// calculate the inclusive prefix sum
while (begin != end) {
val = func(val,*begin);
*begin = val;
++begin;
}
}
template <typename InIterator, typename OutIterator>
inline void local_scan(InIterator begin, InIterator end, OutIterator out) {
return local_scan(begin, end, out, std::plus<typename std::iterator_traits<OutIterator>::value_type>());
}
template <typename Iterator>
inline void local_scan_inplace(Iterator begin, Iterator end) {
return local_scan_inplace(begin, end, std::plus<typename std::iterator_traits<Iterator>::value_type>());
}
// std::vector overloads
template <typename T, typename Func>
inline void local_scan_inplace(std::vector<T>& in, Func func) {
local_scan_inplace(in.begin(), in.end(), func);
}
template <typename T>
inline void local_scan_inplace(std::vector<T>& in) {
local_scan_inplace(in.begin(), in.end(), std::plus<T>());
}
template <typename T, typename Func>
inline std::vector<T> local_scan(const std::vector<T>& in, Func func) {
std::vector<T> result(in.size());
local_scan(in.begin(), in.end(), result.begin(), func);
return result;
}
template <typename T>
inline std::vector<T> local_scan(const std::vector<T>& in) {
std::vector<T> result(in.size());
local_scan(in.begin(), in.end(), result.begin(), std::plus<T>());
return result;
}
// global scans
template <typename InIterator, typename OutIterator, typename Func>
void global_scan(InIterator begin, InIterator end, OutIterator out, Func func, const mxx::comm& comm = mxx::comm(), const bool commutative = true) {
OutIterator o = out;
size_t n = std::distance(begin, end);
// create subcommunicator for those processes which contain elements
mxx::comm nonzero_comm = comm.split(n > 0);
if (n > 0) {
// local scan
local_scan(begin, end, out, func);
// mxx::scan
typedef typename std::iterator_traits<OutIterator>::value_type T;
T sum = T();
if (n > 0)
sum = *(out+(n-1));
T presum = exscan(sum, func, nonzero_comm, commutative);
// accumulate previous sum on all local elements
for (size_t i = 0; i < n; ++i) {
*o = func(presum, *o);
++o;
}
}
}
// inplace!
template <typename Iterator, typename Func>
inline void global_scan_inplace(Iterator begin, Iterator end, Func func, const mxx::comm& comm = mxx::comm(), const bool commutative = true) {
Iterator o = begin;
size_t n = std::distance(begin, end);
mxx::comm nonzero_comm = comm.split(n > 0);
if (n > 0) {
// local inplace scan
local_scan_inplace(begin, end, func);
// mxx::exscan
typedef typename std::iterator_traits<Iterator>::value_type T;
T sum = *(begin + (n-1));
T presum = exscan(sum, func, nonzero_comm, commutative);
// accumulate previous sum on all local elements
for (size_t i = 0; i < n; ++i) {
*o = func(presum, *o);
++o;
}
}
}
template <typename InIterator, typename OutIterator>
inline void global_scan(InIterator begin, InIterator end, OutIterator out, const mxx::comm& comm = mxx::comm()) {
return global_scan(begin, end, out, std::plus<typename std::iterator_traits<OutIterator>::value_type>(), comm);
}
template <typename Iterator>
inline void global_scan_inplace(Iterator begin, Iterator end, const mxx::comm& comm = mxx::comm()) {
return global_scan_inplace(begin, end, std::plus<typename std::iterator_traits<Iterator>::value_type>(), comm);
}
// std::vector overloads
template <typename T, typename Func>
inline void global_scan_inplace(std::vector<T>& in, Func func, const mxx::comm& comm = mxx::comm(), const bool commutative = true) {
global_scan_inplace(in.begin(), in.end(), func, comm, commutative);
}
template <typename T>
inline void global_scan_inplace(std::vector<T>& in, const mxx::comm& comm = mxx::comm()) {
global_scan_inplace(in.begin(), in.end(), std::plus<T>(), comm);
}
template <typename T, typename Func>
inline std::vector<T> global_scan(const std::vector<T>& in, Func func, const mxx::comm& comm = mxx::comm(), const bool commutative = true) {
std::vector<T> result(in.size());
global_scan(in.begin(), in.end(), result.begin(), func, comm, commutative);
return result;
}
template <typename T>
inline std::vector<T> global_scan(const std::vector<T>& in, const mxx::comm& comm = mxx::comm()) {
std::vector<T> result(in.size());
global_scan(in.begin(), in.end(), result.begin(), std::plus<T>(), comm);
return result;
}
/*********************************************************************
* Exscan *
*********************************************************************/
// reduce over vectors
template <typename T, typename Func>
inline void exscan_vec(const T* in, size_t n, T* out, Func func, const mxx::comm& comm = mxx::comm()) {
// get op
mxx::custom_op<T> op(std::forward<Func>(func));
MPI_Exscan(const_cast<T*>(in), out, n, op.get_type(), op.get_op(), comm);
}
template <typename T, typename Func>
inline std::vector<T> exscan_vec(const T* in, size_t n, Func func, const mxx::comm& comm = mxx::comm()) {
std::vector<T> result(n, T());
exscan_vec(in, n, &result[0], func, comm);
return result;
}
template <typename T, typename Func>
inline std::vector<T> exscan_vec(const std::vector<T>& x, Func func, const mxx::comm& comm = mxx::comm()) {
return exscan_vec(&x[0], x.size(), func, comm);
}
// single element
template <typename T, typename Func>
T exscan(const T& x, Func func, const mxx::comm& comm = mxx::comm(), const bool commutative = true) {
// get op
mxx::custom_op<T> op(std::forward<Func>(func), commutative);
// perform reduction
T result;
MPI_Exscan(const_cast<T*>(&x), &result, 1, op.get_type(), op.get_op(), comm);
if (comm.rank() == 0)
result = T();
return result;
}
template <typename T>
T exscan(const T& x, const mxx::comm& comm = mxx::comm()) {
return exscan(x, std::plus<T>(), comm);
}
// local exscan
template <typename InIterator, typename OutIterator, typename Func>
void local_exscan(InIterator begin, InIterator end, OutIterator out, Func func) {
// return if there's nothing here
if (std::distance(begin, end) == 0)
return;
typedef typename std::iterator_traits<OutIterator>::value_type T;
// start from first element
T val = *begin;
*out = T();
++begin;
++out;
// calculate the exclusive prefix sum
while (begin != end) {
T tmp = val;
val = func(val,*begin);
*out = tmp;
++begin;
++out;
}
}
// inplace!
template <typename Iterator, typename Func>
void local_exscan_inplace(Iterator begin, Iterator end, Func func) {
// return if there's nothing here
if (std::distance(begin, end) == 0)
return;
typedef typename std::iterator_traits<Iterator>::value_type T;
// start from first element
T val = *begin;
*begin = T();
++begin;
// calculate the exclusive prefix sum
while (begin != end) {
T tmp = val;
val = func(val,*begin);
*begin = tmp;
++begin;
}
}
template <typename InIterator, typename OutIterator>
inline void local_exscan(InIterator begin, InIterator end, OutIterator out) {
return local_exscan(begin, end, out, std::plus<typename std::iterator_traits<OutIterator>::value_type>());
}
template <typename Iterator>
inline void local_exscan_inplace(Iterator begin, Iterator end) {
return local_exscan_inplace(begin, end, std::plus<typename std::iterator_traits<Iterator>::value_type>());
}
// std::vector overloads
template <typename T, typename Func>
inline void local_exscan_inplace(std::vector<T>& in, Func func) {
local_exscan_inplace(in.begin(), in.end(), func);
}
template <typename T>
inline void local_exscan_inplace(std::vector<T>& in) {
local_exscan_inplace(in.begin(), in.end(), std::plus<T>());
}
template <typename T, typename Func>
inline std::vector<T> local_exscan(const std::vector<T>& in, Func func) {
std::vector<T> result(in.size());
local_exscan(in.begin(), in.end(), result.begin(), func);
return result;
}
template <typename T>
inline std::vector<T> local_exscan(const std::vector<T>& in) {
std::vector<T> result(in.size());
local_exscan(in.begin(), in.end(), result.begin(), std::plus<T>());
return result;
}
// global scans
template <typename InIterator, typename OutIterator, typename Func>
void global_exscan(InIterator begin, InIterator end, OutIterator out, Func func, const mxx::comm& comm = mxx::comm()) {
OutIterator o = out;
size_t n = std::distance(begin, end);
mxx::comm nonzero_comm = comm.split(n > 0);
if (n > 0) {
typedef typename std::iterator_traits<OutIterator>::value_type T;
T sum = *(begin+(n-1));
// local scan
local_exscan(begin, end, out, func);
// mxx::scan
sum += *(o + (n-1));
T presum = exscan(sum, func, nonzero_comm);
*o++ = presum;
// accumulate previous sum on all local elements
for (size_t i = 1; i < n; ++i) {
*o = func(presum, *o);
++o;
}
}
}
// inplace!
template <typename Iterator, typename Func>
inline void global_exscan_inplace(Iterator begin, Iterator end, Func func, const mxx::comm& comm = mxx::comm()) {
Iterator o = begin;
size_t n = std::distance(begin, end);
mxx::comm nonzero_comm = comm.split(n > 0);
if (n > 0) {
typedef typename std::iterator_traits<Iterator>::value_type T;
T sum = *(begin+(n-1));
// local inplace scan
local_exscan_inplace(begin, end, func);
sum += *(begin+(n-1));
// mxx::exscan
T presum = exscan(sum, func, nonzero_comm);
*o++ = presum;
// accumulate previous sum on all local elements
for (size_t i = 1; i < n; ++i) {
*o = func(presum, *o);
++o;
}
}
}
template <typename InIterator, typename OutIterator>
inline void global_exscan(InIterator begin, InIterator end, OutIterator out, const mxx::comm& comm = mxx::comm()) {
return global_exscan(begin, end, out, std::plus<typename std::iterator_traits<OutIterator>::value_type>(), comm);
}
template <typename Iterator>
inline void global_exscan_inplace(Iterator begin, Iterator end, const mxx::comm& comm = mxx::comm()) {
return global_exscan_inplace(begin, end, std::plus<typename std::iterator_traits<Iterator>::value_type>(), comm);
}
// std::vector overloads
template <typename T, typename Func>