-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathansv.hpp
2054 lines (1860 loc) · 86.2 KB
/
ansv.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
*
* http://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.
*/
#ifndef ANSV_HPP
#define ANSV_HPP
#include <vector>
#include <deque>
#include <cxx-prettyprint/prettyprint.hpp>
#include <mxx/comm.hpp>
#include <mxx/timer.hpp>
#include <mxx/algos.hpp>
#include "ansv_common.hpp"
#include "ansv_merge.hpp"
// for debugging
//#define SDEBUG(x) mxx::sync_cerr(comm) << "[" << comm.rank() << "]: " #x " = " << (x) << std::endl
#define SDEBUG(x)
/**
* @brief Solves the ANSV problem sequentially in one direction.
*
* @tparam T Type of input elements.
* @param in Vector of input elemens
* @param left Whether to find left or right smaller value. `True` denotes
* left, and `False` denotes finding the smaller value to the right.
*
* @return The nearest smaler value for each element in `in` to the direction
* given by `left`.
*/
template <typename T>
std::vector<size_t> ansv_sequential(const std::vector<T>& in, bool left, size_t nonsv = 0) {
std::vector<size_t> nsv(in.size());
std::deque<size_t> q;
for (size_t i = 0; i < in.size(); ++i) {
size_t idx = left ? in.size() - 1 - i : i;
while (!q.empty() && in[idx] < in[q.back()]) {
// current element is the min for in[i-1]
nsv[q.back()] = idx;
q.pop_back();
}
q.push_back(idx);
}
for (auto& x : q) {
nsv[x] = nonsv;
}
return nsv;
}
template <typename T, int type>
inline void update_nsv_queue(std::vector<size_t>& nsv, std::deque<std::pair<T,size_t>>& q, const T& next_value, size_t idx, size_t prefix) {
while (!q.empty() && next_value < q.back().first) {
// current element is the min for the last element in the queue
nsv[q.back().second-prefix] = idx;
if (type == furthest_eq) {
// if that element is followed in the queue by equal elements,
// set them to the furthest
std::pair<T,size_t> furthest = q.back();
q.pop_back();
while (!q.empty() && furthest.first == q.back().first) {
nsv[q.back().second-prefix] = furthest.second;
q.pop_back();
}
} else {
q.pop_back();
}
}
if (type == nearest_eq) {
if (!q.empty() && next_value == q.back().first) {
// replace the equal element
nsv[q.back().second-prefix] = idx;
q.pop_back();
}
}
}
// TODO: this is yet uncorrect for furthest_eq
template <typename Iterator, typename T, int nsv_type, bool dir>
void local_indexing_nsv(Iterator begin, Iterator end, std::vector<std::pair<T, size_t>>& unmatched, std::vector<size_t>& nsv) {
size_t n = std::distance(begin, end);
std::deque<std::pair<T,size_t> > q;
for (Iterator it = begin; it != end; ++it) {
size_t idx = (dir == dir_left) ? n - std::distance(begin,it) - 1 : std::distance(begin,it);
size_t prefix = 0;
update_nsv_queue<T, nsv_type>(nsv, q, *it, idx, prefix);
q.push_back(std::pair<T, size_t>(*it, prefix+idx));
}
size_t prev_size = unmatched.size();
if (dir == dir_left) {
unmatched = std::vector<std::pair<T,size_t>>(q.rbegin(), q.rend());
} else {
unmatched.insert(unmatched.end(), q.begin(), q.end());
}
for (size_t i = prev_size; i < unmatched.size(); ++i) {
nsv[unmatched[i].second] = n + i;
}
}
template <typename Iterator, typename T, int nsv_type, bool dir>
void local_indexing_nsv_deque(Iterator begin, Iterator end, std::vector<std::pair<T, size_t>>& unmatched, std::vector<size_t>& nsv) {
size_t n = std::distance(begin, end);
std::deque<std::pair<T,size_t> > q;
std::deque<std::pair<T,size_t> > eq;
size_t prev_size = unmatched.size();
for (Iterator it = begin; it != end; ++it) {
size_t idx = (dir == dir_left) ? n - std::distance(begin,it) - 1 : std::distance(begin,it);
size_t prefix = 0;
while (!q.empty() && *it < q.back().first) {
// this is only works for nearest_sm and nearest_eq
// furthest_eq requires a post-processing of _all_ items at the end
nsv[q.back().second-prefix] = idx;
if (nsv_type == nearest_sm) {
if (q.size() >= 2 && q.back().first == (q.end()-2)->first) {
while (!eq.empty() && eq.back().first == q.back().first) {
nsv[eq.back().second-prefix] = idx;
eq.pop_back();
}
q.pop_back();
nsv[q.back().second-prefix] = idx;
}
}
if (nsv_type == nearest_eq) {
// if there is a second element with the same value
// pop that one as well, since it alreay has its
// nsv set correctly
if (q.size() >= 2 && q.back().first == (q.end()-2)->first) {
q.pop_back();
}
}
q.pop_back();
}
if (q.size() >= 2 && *it == q.back().first && *it == (q.end()-2)->first) {
// there are already two equal elements in the queue
// -> replace the last one with me
if (nsv_type == nearest_eq) {
nsv[q.back().second-prefix] = idx;
} else if (nsv_type == nearest_sm) {
// local indexing and add to equal range queue
size_t local_index = n + prev_size + (q.size()-1);
if (dir == dir_left)
local_index = n - q.size();
nsv[q.back().second-prefix] = local_index;
eq.push_back(q.back());
}
q.pop_back();
} else if (nsv_type == nearest_eq && !q.empty() && *it == q.back().first) {
// there's only 1 equal element in the queue so far
// i'm its closest neighbor
nsv[q.back().second-prefix] = idx;
}
q.push_back(std::pair<T, size_t>(*it, prefix+idx));
}
if (dir == dir_left) {
unmatched = std::vector<std::pair<T,size_t>>(q.rbegin(), q.rend());
} else {
unmatched.insert(unmatched.end(), q.begin(), q.end());
}
for (size_t i = prev_size; i < unmatched.size(); ++i) {
if (nsv_type == nearest_eq) {
// if nearest_eq, don't do this for the second of an equal range
if (dir == dir_left && i > prev_size && unmatched[i-1].first == unmatched[i].first) {
// nsv is already set, don't change
} else if (dir == dir_right && i+1 < unmatched.size() && unmatched[i].first == unmatched[i+1].first) {
// nsv is already set, don't change
} else {
nsv[unmatched[i].second] = n + i;
}
} else {
nsv[unmatched[i].second] = n + i;
}
}
if (dir == dir_left && nsv_type == nearest_sm) {
size_t n_left_mins = unmatched.size();
for (auto& x : eq) {
nsv[x.second] += n_left_mins;
}
}
}
template <typename Iterator, typename T, int nsv_type, bool dir>
void local_indexing_nsv_2(Iterator begin, Iterator end, std::vector<std::pair<T, size_t>>& unmatched, std::vector<size_t>& nsv) {
size_t n = std::distance(begin, end);
//std::deque<std::pair<T,size_t> > q;
// q will have maximum size `n`
std::vector<std::pair<T, size_t> > q(n+2);
std::pair<T,size_t>* qlast = &q[0]-1;
size_t qsize = 0;
std::deque<std::pair<T,size_t> > eq;
size_t prev_size = unmatched.size();
for (Iterator it = begin; it != end; ++it) {
size_t idx = (dir == dir_left) ? n - std::distance(begin,it) - 1 : std::distance(begin,it);
size_t prefix = 0;
const T cur_el = *it;
while (qsize != 0 && cur_el < qlast->first) {
// this is only works for nearest_sm and nearest_eq
// furthest_eq requires a post-processing of _all_ items at the end
nsv[qlast->second-prefix] = idx;
if (nsv_type == nearest_sm) {
if (qsize >= 2 && qlast->first == (qlast-1)->first) {
while (!eq.empty() && eq.back().first == qlast->first) {
nsv[eq.back().second-prefix] = idx;
eq.pop_back();
}
--qsize; --qlast;
nsv[qlast->second-prefix] = idx;
}
}
if (nsv_type == nearest_eq) {
// if there is a second element with the same value
// pop that one as well, since it alreay has its
// nsv set correctly
if (qsize >= 2 && qlast->first == (qlast-1)->first) {
--qsize; --qlast;
}
}
--qsize; --qlast;
}
if (qsize >= 2 && cur_el == qlast->first && cur_el == (qlast-1)->first) {
// there are already two equal elements in the queue
// -> replace the last one with me
if (nsv_type == nearest_eq) {
nsv[qlast->second-prefix] = idx;
} else if (nsv_type == nearest_sm) {
// local indexing and add to equal range queue
size_t local_index = n + prev_size + (qsize-1);
if (dir == dir_left)
local_index = n - qsize;
nsv[qlast->second-prefix] = local_index;
eq.push_back(*qlast);
}
--qsize; --qlast;
} else if (nsv_type == nearest_eq && qsize > 0 && cur_el == qlast->first) {
// there's only 1 equal element in the queue so far
// i'm its closest neighbor
nsv[qlast->second-prefix] = idx;
}
//q.push_back(std::pair<T, size_t>(*it, prefix+idx));
++qlast; ++qsize;
qlast->first = cur_el;
qlast->second = prefix+idx;
}
if (dir == dir_left) {
//unmatched = std::vector<std::pair<T,size_t>>(q.rbegin(), q.rend());
// reverse copy
unmatched.resize(qsize);
for (size_t i = 0; i < qsize; ++i) {
unmatched[qsize-i-1] = q[i];
}
} else {
unmatched.insert(unmatched.end(), q.begin(), q.begin()+qsize);
}
for (size_t i = prev_size; i < unmatched.size(); ++i) {
if (nsv_type == nearest_eq) {
// if nearest_eq, don't do this for the second of an equal range
if (dir == dir_left && i > prev_size && unmatched[i-1].first == unmatched[i].first) {
// nsv is already set, don't change
} else if (dir == dir_right && i+1 < unmatched.size() && unmatched[i].first == unmatched[i+1].first) {
// nsv is already set, don't change
} else {
nsv[unmatched[i].second] = n + i;
}
} else {
nsv[unmatched[i].second] = n + i;
}
}
if (dir == dir_left && nsv_type == nearest_sm) {
size_t n_left_mins = unmatched.size();
for (auto& x : eq) {
nsv[x.second] += n_left_mins;
}
}
}
template <typename T, int nsv_type, bool direction>
void local_indexing_nsv_4(const std::vector<T>& in, std::vector<std::pair<T, size_t>>& unmatched, std::vector<size_t>& nsv) {
// to the first equal or smaller element in the queue
//std::deque<std::pair<T, size_t>> q;
std::vector<size_t> q(in.size());
//q.reserve(in.size());
std::deque<size_t> e;
size_t prev_size = unmatched.size();
if (direction == dir_left) {
unmatched.emplace_back(in[0], 0);
nsv[0] = in.size();
q[0] = 0;
} else {
unmatched.emplace_back(in[in.size()-1], in.size()-1);
nsv[in.size()-1] = prev_size + in.size();
q[0] = in.size()-1;
}
size_t* qlast = &q[0];
const size_t * const qstart = qlast;
for (size_t idx = 1; idx < in.size(); ++idx) {
size_t i = (direction == dir_left) ? idx : in.size() - idx - 1;
while (qlast >= qstart && in[i] < in[*qlast]) {
--qlast;
}
if (qlast < qstart) {
// this is a new minimum -> no left match
unmatched.emplace_back(in[i], i);
nsv[i] = in.size() + unmatched.size()-1;
// also add to queue
//q.emplace_back(i);
++qlast; *qlast = i;
} else {
if (nsv_type == furthest_eq) {
//nsv[i] = q.back();
nsv[i] = *qlast;
if (in[i] > in[*qlast]) {
// don't push equal elements
//q.emplace_back(i);
++qlast; *qlast = i;
}
} else if (nsv_type == nearest_sm) {
// if unmatched is equal
if (unmatched.back().first == in[i]) {
// this element has the same left
// match as the previous added one
// TODO: handle the case there there are two?
// in `unmatched`
if (unmatched.size() - prev_size >= 2 && unmatched[unmatched.size()-2].first == in[i]) {
// replace the index and remember the one we replaced
if (direction == dir_right)
e.emplace_back(unmatched.back().second);
unmatched.back().second = i;
} else {
// insert last
unmatched.emplace_back(in[i], i);
}
// nsv is last added unmatched
nsv[i] = in.size() + unmatched.size()-1;
//q.back() = i;
*qlast = i;
} else {
// also remove equal elements from queue
while (qlast >= qstart && in[i] == in[*qlast]) {
//q.pop_back();
--qlast;
}
//nsv[i] = q.back();
//q.emplace_back(i);
nsv[i] = *qlast;
++qlast; *qlast = i;
}
} else if (nsv_type == nearest_eq) {
// the queue contains my match
//nsv[i] = q.back();
nsv[i] = *qlast;
// pop if previous element is equal
if (in[i] == in[*qlast]) {
//q.pop_back();
--qlast;
}
//q.emplace_back(i);
++qlast;
*qlast = i;
}
// add the last element of equal range to unmatched
if (nsv_type != nearest_sm) {
if (unmatched.back().first == in[i]) {
if (unmatched.size()-prev_size >= 2 && unmatched[unmatched.size()-2].first == in[i]) {
unmatched.back().second = i;
} else {
// insert last
unmatched.emplace_back(in[i], i);
}
}
}
}
}
if (direction == dir_right) {
size_t n_right = unmatched.size() - prev_size;
std::reverse(unmatched.begin()+prev_size, unmatched.end());
// TODO: fix addressing for all in unmatched
// plus those replaced (in `e`)
for (size_t i = prev_size; i < unmatched.size(); ++i) {
if (nsv[unmatched[i].second] >= in.size())
nsv[unmatched[i].second] = (n_right - (nsv[unmatched[i].second] - in.size() - prev_size) - 1) + in.size() + prev_size;
}
if (nsv_type == nearest_sm) {
for (size_t x : e) {
nsv[x] = (n_right - (nsv[x] - in.size() - prev_size) - 1) + in.size() + prev_size;
}
}
}
}
// solve all locals with correct local indexing
// unmatched elements point into the lr_mins
// later only lr_mins are used for getting global solutions
/*
template <typename T, int left_type = nearest_sm, int right_type = nearest_sm>
size_t local_ansv_unmatched_nsv(const std::vector<T>& in, const size_t prefix, std::vector<std::pair<T, size_t>>& unmatched, std::vector<std::pair<T, size_t>>& left_nsv, std::vector<std::pair<T,size_t>>& right_nsv) {
// backwards direction (left mins)
std::deque<std::pair<T,size_t> > q;
for (size_t i = in.size(); i > 0; --i) {
while (!q.empty() && in[i-1] < q.back().first) {
q.pop_back();
}
if (right_type == furthest_eq) {
// remove all equal elements but the last
while (q.size() >= 2 && in[i-1] == q.back().first && in[i-1] == (q.rbegin()+1)->first) {
q.pop_back();
}
} else {
while (!q.empty() && in[i-1] == q.back().first)
q.pop_back();
}
q.push_back(std::pair<T, size_t>(in[i-1], prefix+i-1));
}
// add results to left_mins
unmatched = std::vector<std::pair<T,size_t>>(q.rbegin(), q.rend());
size_t n_left_mins = q.size();
assert(n_left_mins >= 1);
q.clear();
// forward direction (right mins)
for (size_t i = 0; i < in.size(); ++i) {
while (!q.empty() && in[i] < q.back().first)
q.pop_back();
if (left_type == furthest_eq) {
while (q.size() >= 2 && in[i] == q.back().first && in[i] == (q.rbegin()+1)->first) {
// remove all but the last one that is equal, TODO: can be `if` instead
q.pop_back();
}
} else {
while (!q.empty() && in[i] == q.back().first)
q.pop_back();
}
q.push_back(std::pair<T, size_t>(in[i], prefix+i));
}
// add results to right_mins
size_t n_right_mins = q.size();
unmatched.insert(unmatched.end(), q.begin(), q.end());
MXX_ASSERT(n_right_mins >= 1);
if (n_right_mins + n_left_mins != unmatched.size()) {
MXX_ASSERT(false);
}
return n_left_mins;
}
*/
template <typename T, int left_type = nearest_sm, int right_type = nearest_sm>
size_t local_ansv_unmatched(const std::vector<T>& in, const size_t prefix, std::vector<std::pair<T, size_t>>& unmatched) {
// backwards direction (left mins)
std::deque<std::pair<T,size_t> > q;
for (size_t i = in.size(); i > 0; --i) {
while (!q.empty() && in[i-1] < q.back().first)
q.pop_back();
if (right_type == furthest_eq) {
// remove all equal elements but the last
while (q.size() >= 2 && in[i-1] == q.back().first && in[i-1] == (q.rbegin()+1)->first) {
q.pop_back();
}
} else {
while (!q.empty() && in[i-1] == q.back().first)
q.pop_back();
}
q.push_back(std::pair<T, size_t>(in[i-1], prefix+i-1));
}
// add results to left_mins
unmatched = std::vector<std::pair<T,size_t>>(q.rbegin(), q.rend());
size_t n_left_mins = q.size();
assert(n_left_mins >= 1);
q.clear();
// forward direction (right mins)
for (size_t i = 0; i < in.size(); ++i) {
while (!q.empty() && in[i] < q.back().first)
q.pop_back();
if (left_type == furthest_eq) {
while (q.size() >= 2 && in[i] == q.back().first && in[i] == (q.rbegin()+1)->first) {
// remove all but the last one that is equal, TODO: can be `if` instead
q.pop_back();
}
} else {
while (!q.empty() && in[i] == q.back().first)
q.pop_back();
}
q.push_back(std::pair<T, size_t>(in[i], prefix+i));
}
// add results to right_mins
size_t n_right_mins = q.size();
unmatched.insert(unmatched.end(), q.begin(), q.end());
MXX_ASSERT(n_right_mins >= 1);
if (n_right_mins + n_left_mins != unmatched.size()) {
MXX_ASSERT(false);
}
return n_left_mins;
}
template <typename T, int left_type, int right_type>
void ansv_comm_allpairs_params(const std::vector<std::pair<T, size_t>>& lr_mins,
size_t n_left_mins,
std::vector<size_t>& send_counts,
std::vector<size_t>& send_displs,
const mxx::comm& comm) {
// allgather min and max of all left and right mins
T local_min = lr_mins[n_left_mins].first;
std::vector<T> allmins = mxx::allgather(local_min, comm);
send_counts = std::vector<size_t>(comm.size(), 0);
send_displs = std::vector<size_t>(comm.size(), 0);
// calculate which processor to exchange unmatched left_mins and right_mins with
// 1) calculate communication parameters for left processes
if (comm.rank() > 0) {
size_t left_idx = 0;
T prev_mins_min = allmins[comm.rank()-1];
for (int i = comm.rank()-1; i >= 0; --i) {
size_t start_idx = left_idx;
// move start back to other `equal` elements (there can be at most 2)
if (right_type == furthest_eq && i < comm.rank()-1) {
while (start_idx > 0 && lr_mins[start_idx-1].first <= prev_mins_min) {
--start_idx;
}
}
while (left_idx+1 < n_left_mins && lr_mins[left_idx].first >= allmins[i]) {
++left_idx;
}
if (right_type == furthest_eq) {
while (left_idx+1 < n_left_mins && lr_mins[left_idx].first == lr_mins[left_idx+1].first) {
++left_idx;
}
}
// remember most min we have seen so far
if (allmins[i] < prev_mins_min) {
prev_mins_min = allmins[i];
}
// send all from [left_idx, start_idx] (inclusive) to proc `i`
send_counts[i] = left_idx - start_idx + 1;
send_displs[i] = start_idx;
// set comm parameters
if (allmins[i] < local_min) {
break;
}
}
}
// 2) calculate communication parameters for right process
if (comm.rank() < comm.size()-1) {
size_t right_idx = lr_mins.size()-1; // n_left_mins;
T prev_mins_min = allmins[comm.rank()+1];
for (int i = comm.rank()+1; i < comm.size(); ++i) {
// find the range which should be send
size_t end_idx = right_idx;
if (left_type == furthest_eq && i > comm.rank()+1) {
// move start back to other `equal` elements
while (end_idx+1 < lr_mins.size() && lr_mins[end_idx+1].first <= prev_mins_min) {
++end_idx;
}
}
while (right_idx > n_left_mins && lr_mins[right_idx].first >= allmins[i]) {
--right_idx;
}
if (left_type == furthest_eq) {
while (right_idx > n_left_mins && lr_mins[right_idx].first == lr_mins[right_idx-1].first) {
--right_idx;
}
}
// remember most min we have seen so far
if (prev_mins_min > allmins[i]) {
prev_mins_min = allmins[i];
}
// now right_idx is the first one that is smaller than the min of `i`
// send all elements from [start_idx, right_idx]
send_counts[i] = end_idx - right_idx + 1;
send_displs[i] = right_idx;
// can stop if an overall smaller min than my own has been found
if (allmins[i] < local_min) {
break;
}
}
}
}
template <typename T, int left_type, int right_type>
size_t ansv_communicate_allpairs(const std::vector<std::pair<T,size_t>>& lr_mins, size_t n_left_mins, std::vector<std::pair<T, size_t>>& remote_seqs, const mxx::comm& comm) {
mxx::section_timer t(std::cerr, comm);
std::vector<size_t> send_counts;
std::vector<size_t> send_displs;
ansv_comm_allpairs_params<T, left_type, right_type>(lr_mins, n_left_mins, send_counts, send_displs, comm);
// exchange lr_mins via all2all
std::vector<size_t> recv_counts = mxx::all2all(send_counts, comm);
std::vector<size_t> recv_displs = mxx::impl::get_displacements(recv_counts);
size_t recv_size = recv_counts.back() + recv_displs.back();
remote_seqs = std::vector<std::pair<T, size_t>>(recv_size);
t.end_section("ANSV: calc comm params");
mxx::all2allv(&lr_mins[0], send_counts, send_displs, &remote_seqs[0], recv_counts, recv_displs, comm);
t.end_section("ANSV: all2allv");
// solve locally given the exchanged values (by prefilling min-queue before running locally)
size_t n_left_recv = recv_displs[comm.rank()];
return n_left_recv;
}
template <typename T, int left_type, int right_type>
void x_ansv_local(const std::vector<T>& in,
std::vector<std::pair<T, size_t>>& lr_mins,
std::vector<size_t>& left_nsv, std::vector<size_t>& right_nsv) {
lr_mins = std::vector<std::pair<T, size_t>>(in.size());
size_t q = 0;
// left scan = find left matches
for (size_t i = 0; i < in.size(); ++i) {
size_t idx = in.size() - i - 1;
if (q > 0) {
if (in[idx] < lr_mins[q-1].first) {
// a new minimum (potentially)
if (left_type == furthest_eq) {
// TODO
}
}
}
while (q > 0 && in[idx] < lr_mins[q-1].first) {
left_nsv[lr_mins[q-1].second] = idx;
--q;
}
}
// right scan = find right matches
for (size_t i = 0; i < in.size(); ++i) {
while (q > 0 && in[i] < lr_mins[q-1].first) {
right_nsv[lr_mins[q-1].second] = i;
--q;
}
}
}
template <typename T, bool direction, bool tail_direction, int indexing_type, typename Iterator>
void ansv_local_finish_furthest_eq(const std::vector<T>& in, Iterator tail_begin, Iterator tail_end, size_t prefix, size_t tail_prefix, size_t nonsv, std::vector<size_t>& nsv) {
std::deque<std::pair<T,size_t> > q;
const size_t iprefix = (indexing_type == global_indexing) ? prefix : 0;
// iterate forwards through the received elements to fill the queue
// TODO: don't need a queue if this is a non-decreasing sequence
// since we never pop, we just need to keep track of an iterator position
size_t n_tail = std::distance(tail_begin, tail_end);
for (size_t i = 0; i < n_tail; ++i) {
auto r = (tail_direction == dir_left) ? tail_begin+i : tail_begin+(n_tail-i-1);
// TODO: I should be able to assume that the sequence is non-decreasing
while (!q.empty() && r->first < q.back().first) {
q.pop_back();
}
if (q.empty() || r->first > q.back().first) {
// push only if this element is larger (not equal)
if (indexing_type == global_indexing) {
q.push_back(*r);
} else {
size_t rcv_idx = tail_prefix + std::distance(tail_begin, r);
q.push_back(std::pair<T, size_t>(r->first, in.size() + rcv_idx));
}
}
}
// iterate forward through the local items and set their nsv
// to the first equal or smaller element in the queue
for (size_t idx = 0; idx < in.size(); ++idx) {
size_t i = (direction == dir_left) ? idx : in.size() - idx - 1;
while (!q.empty() && in[i] < q.back().first) {
q.pop_back();
}
if (q.empty()) {
nsv[i] = nonsv;
} else {
nsv[i] = q.back().second;
}
if (q.empty() || in[i] > q.back().first) {
q.push_back(std::pair<T, size_t>(in[i], iprefix+i));
}
}
}
template <typename T, int left_type, int right_type, int indexing_type>
void ansv_local_finish_all(const std::vector<T>& in, const std::vector<std::pair<T,size_t>>& recved, size_t n_left_recv, size_t prefix, size_t nonsv, std::vector<size_t>& left_nsv, std::vector<size_t>& right_nsv) {
left_nsv.resize(in.size());
right_nsv.resize(in.size());
size_t local_size = in.size();
size_t n_right_recv = recved.size() - n_left_recv;
std::deque<std::pair<T,size_t> > q;
const size_t iprefix = (indexing_type == global_indexing) ? prefix : 0;
if (left_type == furthest_eq) {
ansv_local_finish_furthest_eq<T, dir_left, dir_left, indexing_type>(in, recved.begin(), recved.begin()+n_left_recv, prefix, 0, nonsv, left_nsv);
} else {
// iterate backwards to get the nearest smaller element to left for each element
for (size_t i = in.size(); i > 0; --i) {
if (indexing_type == global_indexing) {
update_nsv_queue<T,left_type>(left_nsv, q, in[i-1], prefix+i-1, prefix);
q.push_back(std::pair<T, size_t>(in[i-1], prefix+i-1));
} else { // indexing_type == local_indexing
// prefix=0 for local indexing
update_nsv_queue<T,left_type>(left_nsv, q, in[i-1], i-1, 0);
q.push_back(std::pair<T, size_t>(in[i-1], i-1));
}
}
// now go backwards through the right-mins from the previous processors,
// in order to resolve all local elements
for (size_t i = 0; i < n_left_recv; ++i) {
if (q.empty()) {
break;
}
size_t rcv_idx = n_left_recv - i - 1;
// set nsv for all larger elements in the queue
if (indexing_type == global_indexing) {
update_nsv_queue<T,left_type>(left_nsv, q, recved[rcv_idx].first, recved[rcv_idx].second, prefix);
} else { // indexing_type == local_indexing
// prefix = 0, recv.2nd = local_size+rcv_idx,
update_nsv_queue<T,left_type>(left_nsv, q, recved[rcv_idx].first, local_size + rcv_idx, 0);
}
}
// elements still in the queue do not have a smaller value to the left
// -> set these to a special value and handle case if furthest_eq
// elements are still waiting in queue
for (auto it = q.rbegin(); it != q.rend(); ++it) {
left_nsv[it->second - iprefix] = nonsv;
}
}
// iterate forwards to get the nearest smaller value to the right for each element
q.clear();
if (right_type == furthest_eq) {
ansv_local_finish_furthest_eq<T, dir_right, dir_right, indexing_type>(in, recved.begin()+n_left_recv, recved.end(), prefix, n_left_recv, nonsv, right_nsv);
} else {
for (size_t i = 0; i < in.size(); ++i) {
if (indexing_type == global_indexing) {
update_nsv_queue<T,right_type>(right_nsv, q, in[i], prefix+i, prefix);
q.push_back(std::pair<T, size_t>(in[i], prefix+i));
} else { // indexing_type == local_indexing
// handle as if prefix = 0
update_nsv_queue<T,right_type>(right_nsv, q, in[i], i, 0);
q.push_back(std::pair<T, size_t>(in[i], i));
}
}
// now go forwards through left-mins of succeeding processors
for (size_t i = 0; i < n_right_recv; ++i) {
size_t rcv_idx = n_left_recv + i;
if (q.empty()) {
break;
}
// set nsv for all larger elements in the queue
if (indexing_type == global_indexing) {
update_nsv_queue<T,right_type>(right_nsv, q, recved[rcv_idx].first, recved[rcv_idx].second, prefix);
} else { // indexing_type == local_indexing
update_nsv_queue<T,right_type>(right_nsv, q, recved[rcv_idx].first, local_size+rcv_idx, 0);
}
}
// elements still in the queue do not have a smaller value to the left
// -> set these to a special value and handle case if furthest_eq elements
// are still waiting in queue
for (auto it = q.rbegin(); it != q.rend(); ++it) {
right_nsv[it->second - iprefix] = nonsv;
}
}
}
// parallel all nearest smallest value
// TODO: iterator version??
// TODO: comparator type
// TODO: more compact via index_t template instead of size_t
template <typename T, int left_type, int right_type, int indexing_type>
void old_gansv(const std::vector<T>& in, std::vector<size_t>& left_nsv, std::vector<size_t>& right_nsv, std::vector<std::pair<T,size_t> >& lr_mins, const mxx::comm& comm, size_t nonsv = 0) {
mxx::section_timer t(std::cerr, comm);
size_t local_size = in.size();
size_t prefix = mxx::exscan(local_size, comm);
/*****************************************************************
* Step 1: Locally calculate ANSV and save un-matched elements *
*****************************************************************/
size_t n_left_mins = local_ansv_unmatched<T, left_type, right_type>(in, prefix, lr_mins);
t.end_section("ANSV: local ansv");
/***************************************************************
* Step 2: communicate un-matched elements to correct target *
***************************************************************/
std::vector<std::pair<T, size_t>> recved;
size_t n_left_recv = ansv_communicate_allpairs<T, left_type, right_type>(lr_mins, n_left_mins, recved, comm);
t.end_section("ANSV: communicate all");
/***************************************************************
* Step 3: Again solve ANSV locally and use lr_mins as tails *
***************************************************************/
ansv_local_finish_all<T, left_type, right_type, indexing_type>(in, recved, n_left_recv, prefix, nonsv, left_nsv, right_nsv);
lr_mins = recved;
t.end_section("ANSV: finish ansv local");
}
template <typename T>
void my_ansv(const std::vector<T>& in, std::vector<size_t>& left_nsv, std::vector<size_t>& right_nsv, std::vector<std::pair<T,size_t> >& lr_mins, const mxx::comm& comm, size_t nonsv = 0) {
mxx::section_timer t(std::cerr, comm);
size_t local_size = in.size();
size_t prefix = mxx::exscan(local_size, comm);
/*****************************************************************
* Step 1: Locally calculate ANSV and save un-matched elements *
*****************************************************************/
if (left_nsv.size() != in.size())
left_nsv.resize(in.size());
if (right_nsv.size() != in.size())
right_nsv.resize(in.size());
//size_t n_left_mins = local_ansv_unmatched<T, left_type, right_type>(in, prefix, lr_mins);
local_indexing_nsv<decltype(in.rbegin()), T, nearest_sm, dir_left>(in.rbegin(), in.rend(), lr_mins, left_nsv);
size_t n_left_mins = lr_mins.size();
local_indexing_nsv<decltype(in.begin()), T, nearest_sm, dir_right>(in.begin(), in.end(), lr_mins, right_nsv);
// change lrmin indexing to global
for (size_t i = 0; i < lr_mins.size(); ++i) {
lr_mins[i].second += prefix;
}
t.end_section("ANSV: local ansv");
/***************************************************************
* Step 2: communicate un-matched elements to correct target *
***************************************************************/
std::vector<std::pair<T, size_t>> recved;
size_t n_left_recv = ansv_communicate_allpairs<T, nearest_sm, nearest_sm>(lr_mins, n_left_mins, recved, comm);
t.end_section("ANSV: communicate all");
/***************************************************************
* Step 3: Again solve ANSV locally and use lr_mins as tails *
***************************************************************/
ansv_merge<nearest_sm, nearest_sm>(recved.begin(), recved.begin()+n_left_recv, lr_mins.begin(), lr_mins.begin()+n_left_mins);
ansv_merge<nearest_sm, nearest_sm>(lr_mins.begin()+n_left_mins, lr_mins.end(), recved.begin()+n_left_recv, recved.end());
//ansv_local_finish_all<T, left_type, right_type, indexing_type>(in, recved, n_left_recv, prefix, nonsv, left_nsv, right_nsv);
// local to global indexing transformation
for (size_t i = 0; i < in.size(); ++i) {
if(left_nsv[i] >= local_size) {
if (lr_mins[left_nsv[i]-local_size].second == i+prefix) {
left_nsv[i] = nonsv;
} else {
left_nsv[i] = lr_mins[left_nsv[i]-local_size].second;
}
} else {
left_nsv[i] += prefix;
}
if (right_nsv[i] >= local_size) {
if (lr_mins[right_nsv[i]-local_size].second == i+prefix) {
right_nsv[i] = nonsv;
} else {
right_nsv[i] = lr_mins[right_nsv[i]-local_size].second;
}
} else {
right_nsv[i] += prefix;
}
}
t.end_section("ANSV: finish ansv local");
}
template <typename T>
void my_ansv_minpair(const std::vector<T>& in, std::vector<size_t>& left_nsv, std::vector<size_t>& right_nsv, std::vector<std::pair<T,size_t> >& lr_mins, const mxx::comm& comm, size_t nonsv = 0) {
mxx::section_timer t(std::cerr, comm);
size_t local_size = in.size();
size_t prefix = mxx::exscan(local_size, comm);
/*****************************************************************
* Step 1: Locally calculate ANSV and save un-matched elements *
*****************************************************************/
if (left_nsv.size() != in.size())
left_nsv.resize(in.size());
if (right_nsv.size() != in.size())
right_nsv.resize(in.size());
//size_t n_left_mins = local_ansv_unmatched<T, left_type, right_type>(in, prefix, lr_mins);
local_indexing_nsv<decltype(in.rbegin()), T, nearest_sm, dir_left>(in.rbegin(), in.rend(), lr_mins, left_nsv);
size_t n_left_mins = lr_mins.size();
local_indexing_nsv<decltype(in.begin()), T, nearest_sm, dir_right>(in.begin(), in.end(), lr_mins, right_nsv);
// change lrmin indexing to global
for (size_t i = 0; i < lr_mins.size(); ++i) {
lr_mins[i].second += prefix;
}
t.end_section("ANSV: local ansv");
/***************************************************************
* Step 2: communicate un-matched elements to correct target *
***************************************************************/
std::vector<std::pair<T, size_t>> recved;
//size_t n_left_recv = ansv_communicate_allpairs<T, nearest_sm, nearest_sm>(lr_mins, n_left_mins, recved, comm);
t.end_section("ANSV: communicate all");
std::vector<size_t> send_counts;
std::vector<size_t> send_displs;
ansv_comm_allpairs_params<T, furthest_eq, furthest_eq>(lr_mins, n_left_mins, send_counts, send_displs, comm);
// determine min/max for each communication pair
std::vector<size_t> min_send_counts(send_counts.begin(), send_counts.end());
std::vector<size_t> min_recv_counts = mxx::all2all(min_send_counts, comm);
// TODO: modify send/recv counts by selecting the minimum partner
for (int i = 0; i < comm.size(); ++i) {
if (min_recv_counts[i] != 0 && min_send_counts[i] != 0) {
// choose the min and set the other one to 0
if (min_recv_counts[i] < min_send_counts[i]) {
min_send_counts[i] = 0;
} else if (min_recv_counts[i] == min_send_counts[i]) {
// need to handle this case so that its symmetric
if (i < comm.rank()) {
min_recv_counts[i] = 0;
} else {
min_send_counts[i] = 0;
}
} else {
min_recv_counts[i] = 0;
}
}
}
SDEBUG(lr_mins);
SDEBUG(send_counts);
SDEBUG(min_send_counts);
SDEBUG(min_recv_counts);
std::vector<size_t> recv_displs = mxx::local_exscan(min_recv_counts);
size_t recv_size = min_recv_counts.back() + recv_displs.back();
recved = std::vector<std::pair<T, size_t>>(recv_size);
// communicate through an all2all TODO: replace by send/recv + barrier?
t.end_section("ANSV: calc comm params");
mxx::all2allv(&lr_mins[0], min_send_counts, send_displs, &recved[0], min_recv_counts, recv_displs, comm);
t.end_section("ANSV: all2allv");
SDEBUG(recved);
/***************************************************************
* Step 3: Again solve ANSV locally and use lr_mins as tails *
***************************************************************/
// use original send_counts as the range for the merge, and merge one sequence at a time
// at the same time determine the send_counts for sending back solutions
std::vector<size_t> ret_send_counts(min_recv_counts);
std::vector<size_t> ret_send_displs(recv_displs);
typedef typename std::vector<std::pair<T, size_t>>::iterator pair_it;
for (int i = comm.rank()-1; i >= 0; --i) {
// local_range = send_displs[i] + [0, send_counts[i])
if (min_recv_counts[i] > 0) {
// local merge
pair_it rec_begin = recved.begin()+recv_displs[i];
pair_it rec_end = recved.begin()+recv_displs[i]+min_recv_counts[i];
pair_it loc_begin = lr_mins.begin()+send_displs[i];
pair_it loc_end = lr_mins.begin()+send_displs[i]+send_counts[i];
pair_it rec_merge_end, loc_merge_end;
std::tie(rec_merge_end, loc_merge_end) = ansv_merge<nearest_sm, nearest_sm>(rec_begin, rec_end, loc_begin, loc_end);
ret_send_counts[i] = min_recv_counts[i] - (rec_merge_end - rec_begin + 1);
ret_send_displs[i] += (rec_merge_end - rec_begin + 1);
} else {
// this section is waiting for the result from the remote
}
}
for (int i = comm.rank()+1; i < comm.size(); ++i) {
if (min_recv_counts[i] > 0) {
pair_it loc_begin = lr_mins.begin()+send_displs[i];
pair_it loc_end = lr_mins.begin()+send_displs[i]+send_counts[i];
pair_it rec_begin = recved.begin()+recv_displs[i];
pair_it rec_end = recved.begin()+recv_displs[i]+min_recv_counts[i];
pair_it loc_merge_end, rec_merge_end;
std::tie(loc_merge_end, rec_merge_end) = ansv_merge<nearest_sm, nearest_sm>(loc_begin, loc_end, rec_begin, rec_end);
ret_send_counts[i] = min_recv_counts[i] - (rec_end - rec_merge_end);
// displacements stay the same, since we take elements away at the end of the sequence
} else {
// remote is answering my queries, so nothing to do here
}
}
// get receive counts via all2all
std::vector<size_t> ret_recv_counts = mxx::all2all(ret_send_counts, comm);
std::vector<size_t> ret_recv_displs(send_displs);