-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcorgi.h
1766 lines (1378 loc) · 49.3 KB
/
corgi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
#pragma once
// #include <fmt/format.h>
// #include <fmt/format.cc>
// #include <fmt/string.h>
// #include <fmt/ostream.h>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <memory>
#include <unordered_map>
#include <cassert>
#include <initializer_list>
#include <sstream>
#include <utility>
#include "internals.h"
#include "toolbox/sparse_grid.h"
#include "tile.h"
//#include "mpi.h"
#include <mpi4cpp/mpi.h>
#include "communication.h"
namespace corgi {
/*! Individual grid object that stores patches of grid in it.
*
* See:
* - https://github.com/maddouri/hyper_array/
* - https://github.com/astrobiology/orca_array
*/
template<std::size_t D>
class Grid
{
public:
// --------------------------------------------------
// definitions
using size_type = std::size_t;
using index_type = std::size_t;
using float_type = double;
protected:
// --------------------------------------------------
/// number of elements in each dimension
::std::array<size_type, D> _lengths;
/// start coordinates of each dimension
::std::array<float_type, D> _mins;
/// ending coordinates of each dimension
::std::array<float_type, D> _maxs;
/*! Global large scale block grid where information
* of all the mpi processes are stored
*/
corgi::tools::sparse_grid<int, D> _mpi_grid;
/// global large scale block grid where load balance
//information is stored
corgi::tools::sparse_grid<double, D> _work_grid;
// --------------------------------------------------
private:
// Mappings
using TileID_t = uint64_t;
using Tile_t = corgi::Tile<D>;
using Tileptr = std::shared_ptr<Tile_t>;
using Tile_map = std::unordered_map<TileID_t, Tileptr>;
public:
/// Map with tile_id & tile data
Tile_map tiles;
public:
// --------------------------------------------------
// Python bindings for mpi_grid
// get element
template<typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value, int >
py_get_mpi_grid(Indices... indices) /*const*/
{
return _mpi_grid(indices...);
}
// set element
template<typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value, void >
py_set_mpi_grid(int val, Indices... indices) {
_mpi_grid(indices...) = val;
}
// get element
template<typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value, double >
py_get_work_grid(Indices... indices) /*const*/
{
return _work_grid(indices...);
}
// set element
template<typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value, void >
py_set_work_grid(double val, Indices... indices) {
_work_grid(indices...) = val;
}
public:
// --------------------------------------------------
// constructors
/// mpi environment
mpi::environment env;
/// mpi communicator
mpi::communicator comm;
/// Uninitialized dimension lengths
Grid() :
env(),
comm()
{};
/// set dimensions during construction time
template<
typename... DimensionLength,
typename = corgi::internals::enable_if_t< (sizeof...(DimensionLength) == D) &&
corgi::internals::are_integral<DimensionLength...>::value, void
>
>
Grid(DimensionLength... dimension_lengths) :
_lengths {{static_cast<size_type>(dimension_lengths)...}},
_mpi_grid(dimension_lengths...),
_work_grid(dimension_lengths...),
env(),
comm()
{ }
/// Deallocate and free everything
virtual ~Grid() = default;
public:
//public:
// --------------------------------------------------
// iterators
/*
iterator begin() noexcept { return iterator(data()); }
const_iterator begin() const noexcept { return const_iterator(data()); }
iterator end() noexcept { return iterator(data() + size()); }
const_iterator end() const noexcept { return const_iterator(data() + size()); }
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
const_iterator cbegin() const noexcept { return const_iterator(data()); }
const_iterator cend() const noexcept { return const_iterator(data() + size()); }
const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
*/
public:
// --------------------------------------------------
// access grid configuration
/// number of dimension
static constexpr size_type dims() noexcept { return D; }
/// length (run-time)
size_type len(const size_type i) const
{
assert(i < D);
return _lengths[i];
}
/// reference to the _lengths array
const ::std::array<size_type, D>& lens() const noexcept
{
return _lengths;
}
/// starting location of i:th dimension (run-time)
float_type min(const size_type i) const
{
assert(i < D);
return _mins[i];
}
/// ending location of i:th dimension (run-time)
float_type max(const size_type i) const
{
assert(i < D);
return _maxs[i];
}
/// reference to the _mins array
const ::std::array<float_type, D>& mins() const noexcept
{
return _mins;
}
/// reference to the _maxs array
const ::std::array<float_type, D>& maxs() const noexcept
{
return _maxs;
}
// --------------------------------------------------
// indexing
private:
template <typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value,
::std::array<index_type, D>>
_validate_index_range(Indices... indices) const
{
::std::array<index_type, D> index_array = {{static_cast<index_type>(indices)...}};
// check all indices and prepare an exhaustive report (in oss)
// if some of them are out of bounds
std::ostringstream oss;
for (index_type i = 0; i < D; ++i)
{
if ((index_array[i] >= _lengths[i]) || (index_array[i] < 0))
{
oss << "Index #" << i << " [== " << index_array[i] << "]"
<< " is out of the [0, " << (_lengths[i]-1) << "] range. ";
std::cout << "Index #" << i << " [== " << index_array[i] << "]"
<< " is out of the [0, " << (_lengths[i]-1) << "] range. ";
}
}
// if nothing has been written to oss then all indices are valid
assert(oss.str().empty());
return index_array;
}
/*! Computes the index coefficients assuming column-major order
*
* what we compute:
* \f[
* \begin{cases}
* C_i = \prod_{j=i+1}^{n-1} L_j
* \\
* \begin{cases}
* i &\in [0, \text{Dimensions - 1}] \\
* C_i &: \text{\_coeffs[i]} \\
* L_j &: \text{\_lengths[j]}
* \end{cases}
* \end{cases}
* \f]
*
* For row-major switch to:
* coeffs[i] = ct_accumulate(dimension_lengths, i + 1, Dimensions - i - 1,
* static_cast<size_type>(1),
* ct_prod<size_type>);
*
*/
std::array<size_type, D>
compute_index_coeffs(const ::std::array<size_type, D>& dimension_lengths) const noexcept
{
std::array<size_type, D> coeffs;
for (size_type i = 0; i < D; ++i)
{
coeffs[i] = corgi::internals::ct_accumulate(
dimension_lengths,
0,
i,
static_cast<size_type>(1),
corgi::internals::ct_prod<size_type>);
}
return coeffs;
}
/// Actual Morton Z-ordering from index list
//
// what we compute: coeff . indices
//
// i.e., inner product of accumulated coefficients vector and index vector
constexpr index_type
_compute_index(
const ::std::array<index_type, D>& index_array) const noexcept
{
return corgi::internals::ct_inner_product(
compute_index_coeffs(_lengths), 0,
index_array, 0, D,
static_cast<index_type>(0),
corgi::internals::ct_plus<index_type>,
corgi::internals::ct_prod<index_type>);
}
public:
/// tile IDs
template<typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value, index_type >
id(Indices... indices) const
{
return _compute_index( _validate_index_range(indices...) );
}
/// auxiliary function to unpack tuples
template <size_t... Is>
index_type id_impl(
corgi::internals::tuple_of<D, size_t>& tuple,
std::index_sequence<Is...> /*unused*/)
{
return id( std::get<Is>(tuple)... );
}
template <size_t... Is>
index_type id_impl(
const corgi::internals::tuple_of<D, size_t>& tuple,
std::index_sequence<Is...> /*unused*/) const
{
return id( std::get<Is>(tuple)... );
}
/// unpack tuple into variadic argument list
template<typename Indices = std::make_index_sequence<D>>
index_type id( corgi::internals::tuple_of<D, size_t>& indices)
{
return id_impl(indices, Indices{} );
}
template<typename Indices = std::make_index_sequence<D>>
index_type id( const corgi::internals::tuple_of<D, size_t>& indices) const
{
return id_impl(indices, Indices{} );
}
/// Inverse Morton Z-code
//
// TODO: make N-dimensional
corgi::internals::tuple_of<1, index_type> id2index(
uint64_t cid,
std::array<size_type,1> /*lengths*/)
{
corgi::internals::tuple_of<1, index_type> indices = std::make_tuple(cid);
return indices;
}
corgi::internals::tuple_of<2, index_type> id2index(
uint64_t cid,
std::array<size_type,2> lengths)
{
corgi::internals::tuple_of<2, index_type> indices = std::make_tuple
(
cid % lengths[0],
(cid / lengths[0]) % (lengths[1] )
);
return indices;
}
corgi::internals::tuple_of<3, index_type> id2index(
uint64_t cid,
std::array<size_type,3> lengths)
{
corgi::internals::tuple_of<3, index_type> indices = std::make_tuple
(
cid % lengths[0],
(cid / lengths[0]) % (lengths[1] ),
cid /(lengths[0] * lengths[1] )
);
return indices;
}
public:
// --------------------------------------------------
// apply SFINAE to create some shortcuts (when appropriate)
// NOTE: valid up to D=3 with x/y/z
// return global grid sizes
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=1), T>
get_Nx() { return _lengths[0]; }
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=2), T>
get_Ny() { return _lengths[1]; }
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=3), T>
get_Nz() { return _lengths[2]; }
// return global grid limits
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=1), T>
get_xmin() { return _mins[0]; }
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=2), T>
get_ymin() { return _mins[1]; }
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=3), T>
get_zmin() { return _mins[2]; }
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=1), T>
get_xmax() { return _maxs[0]; }
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=2), T>
get_ymax() { return _maxs[1]; }
template<typename T = size_type>
corgi::internals::enable_if_t< (D>=3), T>
get_zmax() { return _maxs[2]; }
/// Set physical grid size
void set_grid_lims(
const ::std::array<float_type, D>& mins,
const ::std::array<float_type, D>& maxs
)
{
//_mins = std::move(mins);
//_maxs = std::move(maxs);
// explicitly avoid move semantics and copy
// this is to make sure that python garbage collector
// does not mess things up
_mins = mins;
_maxs = maxs;
}
public:
// --------------------------------------------------
// Tile addition etc. manipulation
/// Add local tile to the grid
// void add_tile(Tile& tile) {
//
// FIXME
void add_tile(
Tileptr tileptr,
corgi::internals::tuple_of<D, size_t> indices
)
{
// check that we are not appending nullptr
assert(tileptr);
// claim unique ownership of the tile (for unique_ptr)
// std::unique_ptr<corgi::Tile> tileptr = std::make_unique<corgi:Tile>(tile);
// Tileptr tileptr = std::make_unique<Tile_t>(tile);
// calculate unique global tile ID
uint64_t cid = id( indices );
// Erase any existing tiles to avoid emplace of doing nothing TODO: is this correct?
tiles.erase(cid);
tileptr->index = indices;
tileptr->cid = cid;
tileptr->communication.cid = cid;
tileptr->communication.owner = comm.rank();
//tileptr->communication.local = true; //TODO Catch error if tile is not already mine?
tileptr->lengths = _lengths;
// copy indices from tuple into D=3 array in Communication obj
auto tmp = corgi::internals::into_array(indices);
for(size_t i=0; i<D; i++) tileptr->communication.indices[i] = tmp[i];
// tiles.emplace(cid, std::move(tileptr)); // unique_ptr needs to be moved
tiles.emplace(cid, tileptr); // NOTE using c++14 emplace to avoid copying
//tiles.insert( std::make_pair(cid, tileptr) ); // NOTE using c++14 emplace to avoid copying
// add to my internal listing
_mpi_grid( indices ) = comm.rank();
}
/// Replace/add incoming tile
//
// FIXME
void replace_tile(
Tileptr tileptr,
corgi::internals::tuple_of<D, size_t> indices
)
{
// check that we are not appending nullptr
assert(tileptr);
// calculate unique global tile ID
uint64_t cid = id(indices);
// add tile if it does not exist
if(tiles.count(cid) == 0) return add_tile(tileptr, indices);
// else replace previous one; copy Communication object
auto& tile = get_tile(cid);
auto cm = tile.communication;
tileptr->index = indices;
tileptr->cid = cid;
tileptr->lengths = _lengths;
tiles.erase(cid);
tiles.emplace(cid, tileptr);
update_tile(cm);
}
/// Shortcut for creating raw tiles with only the internal meta info.
// to be used with message passing (w.r.t. add_tile that is for use with initialization)
// FIXME
void create_tile(Communication& cm)
{
//m_author = std::make_shared<Author>(t_author);
auto tileptr = std::make_shared<Tile_t>();
tileptr->load_metainfo(cm);
// additional grid info
tileptr->lengths = _lengths;
// owner
// local
// add
tiles.emplace(cm.cid, tileptr); // NOTE using c++14 emplace to avoid copying
_mpi_grid( tileptr->index ) = cm.owner;
}
/// Update tile metadata
// FIXME
void update_tile(Communication& cm)
{
auto& tile = get_tile(cm.cid);
tile.load_metainfo(cm);
_mpi_grid( tile.index ) = cm.owner;
}
/*! Return a vector of tile indices that fulfill a given criteria. */
std::vector<uint64_t> get_tile_ids(
const bool sorted=false )
{
std::vector<uint64_t> ret;
for (auto& it: tiles) ret.push_back( it.first );
// optional sort based on the tile id
if (sorted && !ret.empty()) {
std::sort(ret.begin(), ret.end());
}
return ret;
}
/*! \brief Get individual tile (as a reference)
*
* NOTE: from StackOverflow (recommended getter method):
* Other_t& get_othertype(const std::string& name)
* {
* auto it = otMap.find(name);
* if (it == otMap.end()) throw std::invalid_argument("entry not found");
* return *(it->second);
* }
*
* This way map retains its ownership of the tile and we avoid giving pointers
* away from the Class.
*/
Tile_t& get_tile(const uint64_t cid) {
auto it = tiles.find(cid);
if (it == tiles.end()) { throw std::invalid_argument("tile entry not found"); }
return *(it->second);
}
template<typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value,
Tile_t&>
get_tile_ind(const Indices... indices) {
uint64_t cid = id(indices...);
return get_tile(cid);
}
/// \brief Get individual tile (as a pointer)
Tileptr get_tileptr(const uint64_t cid) {
auto it = tiles.find(cid);
if (it == tiles.end()) { return nullptr; };
return it->second;
}
template<typename... Indices>
corgi::internals::enable_if_t< (sizeof...(Indices) == D) &&
corgi::internals::are_integral<Indices...>::value,
Tileptr>
get_tileptr_ind(const Indices... indices)
{
uint64_t cid = id(indices...);
return get_tileptr(cid);
}
Tileptr get_tileptr(const std::tuple<size_t> ind) {
size_t i = std::get<0>(ind);
return get_tileptr_ind(i);
}
Tileptr get_tileptr(const std::tuple<size_t, size_t> ind) {
size_t i = std::get<0>(ind);
size_t j = std::get<1>(ind);
return get_tileptr_ind(i, j);
}
Tileptr get_tileptr(const std::tuple<size_t, size_t, size_t> ind) {
size_t i = std::get<0>(ind);
size_t j = std::get<1>(ind);
size_t k = std::get<2>(ind);
return get_tileptr_ind(i, j, k);
}
/// Return all local tiles
std::vector<uint64_t> get_local_tiles(
const bool sorted=false ) {
std::vector<uint64_t> tile_list = get_tile_ids(sorted);
std::vector<uint64_t> ret;
ret.reserve(tile_list.size());
for(auto elem : tile_list) {
if(tiles.at( elem )->communication.owner == comm.rank() ) {
ret.push_back(elem);
}
}
return ret;
}
/// Return all tiles that are of VIRTUAL type.
std::vector<uint64_t> get_virtuals(
const bool sorted=false )
{
std::vector<uint64_t> tile_list = get_tile_ids(sorted);
std::vector<uint64_t> ret;
ret.reserve(tile_list.size());
for(auto elem : tile_list) {
if(tiles.at( elem )->communication.owner != comm.rank() ) {
ret.push_back(elem);
}
}
return ret;
}
/// Return all local boundary tiles
// TODO: update this to use the internal boundary_tile_map
std::vector<uint64_t> get_boundary_tiles(
const bool sorted=false ) {
std::vector<uint64_t> tile_list = get_tile_ids(sorted);
size_t i = 0, len = tile_list.size();
while(i < len) {
// remove if there are no virtual nbors and tile is not mine -> opposite means its boundary
if (tiles.at( tile_list[i] )-> communication.number_of_virtual_neighbors == 0 ||
tiles.at( tile_list[i] )-> communication.owner != comm.rank()
) {
std::swap(tile_list[i], tile_list.back());
tile_list.pop_back();
len -= 1;
} else {
i++;
}
}
return tile_list;
}
// /// Check if we have a tile with the given index
bool is_local(uint64_t cid) {
bool local = false;
// Do we have it on the list?
if (tiles.count( cid ) > 0) {
// is it local (i.e., not virtual)
if ( tiles.at(cid)->communication.owner == comm.rank() ) {
local = true;
}
}
return local;
}
/// return all virtual tiles around the given tile
std::vector<uint64_t> virtual_nhood(uint64_t cid)
{
auto& c = get_tile(cid);
auto neigs = c.nhood();
std::vector<uint64_t> vnhood;
for(auto& indx: neigs) {
if(_mpi_grid(indx) != comm.rank()) vnhood.push_back( id(indx) );
}
return vnhood;
}
/// return all owners of virtual tiles around the given tile
std::vector<int> virtual_nhood_owners(uint64_t cid)
{
auto& c = get_tile(cid);
auto neigs = c.nhood();
std::vector<int> virtual_owners;
for(auto& indx: neigs) {
int whoami = _mpi_grid(indx); // Get tile id from index notation
if(whoami != comm.rank()) virtual_owners.push_back( whoami );
}
return virtual_owners;
}
/// map of owners to exterior (=virtual) tiles
std::map<int, std::set<uint64_t> > virtual_tile_list;
/// map of my (local) tiles to exterior ranks
std::map<uint64_t, std::set<int> > boundary_tile_list;
// /*! Analyze my local boundary tiles that will be later on
// * send to the neighbors as virtual tiles.
// *
// * This is where the magic happens and we analyze what and who to send to.
// * These values *must* be same for everybody, this is why we use
// * mode of the owner list and in case of conflict pick the smaller value.
// * This way everybody knows what to expect and we avoid creating conflicts
// * in communication. This information is then being sent to other processes
// * together with the tiles and is analyzed there by others inside the
// * `rank_virtuals` function.
// * */
void analyze_boundaries() {
virtual_tile_list.clear();
boundary_tile_list.clear();
send_queue.clear();
send_queue_address.clear();
// analyze all of my local tiles
for(auto cid: get_local_tiles()) {
auto& c = get_tile(cid);
// analyze c's neighborhood
auto neigs = c.nhood();
for(auto& indx: neigs) {
int whoami = _mpi_grid(indx); // Get tile id from index notation
// if nbor tile is virtual
if(whoami != comm.rank()) {
// then the local tile cid is a boundary tile to this rank (=whoami)
boundary_tile_list[cid].insert(whoami);
// and then ncid is my virtual exterior tile
uint64_t ncid = id(indx);
virtual_tile_list[whoami].insert(ncid);
}
}
// mark completely local if no virtuals around this tile
// overwritten in next loop based on real values
// could also carry a flag through the loop above to check this...
c.communication.number_of_virtual_neighbors = 0;
c.communication.communications = 0;
}
// update this info into my local boundary tiles
for(auto&& elem : boundary_tile_list) {
auto& c = get_tile(elem.first);
// set into vector
std::vector<int> virtual_owners(elem.second.begin(), elem.second.end());
//c.communication.top_owner = top_owner; // can not be computed from set
c.communication.communications = virtual_owners.size();
c.communication.number_of_virtual_neighbors = virtual_owners.size(); // not same as original
//c.communication.virtual_owners = virtual_owners;
c.virtual_owners = virtual_owners;
// add to send queue
uint64_t cid = elem.first;
if (std::find( send_queue.begin(), send_queue.end(), cid) == send_queue.end()) {
send_queue.push_back( cid );
send_queue_address.push_back( virtual_owners );
}
}
//TODO: can also pre-create virtual tiles (if not existing in grid yet)
}
//void analyze_boundaries_old() {
// // old tile information update
// for(auto cid: get_local_tiles()) {
// //std::cout << comm.rank() << ": ab: " << cid << "\n";
// std::vector<int> virtual_owners = virtual_nhood_owners(cid);
// size_t N = virtual_owners.size();
// //std::cout << comm.rank() << ": ab: " << cid << "N:" << N << "\n";
// // If N > 0 then this is a local boundary tile.
// // other criteria could also apply but here we assume
// // neighborhood according to spatial distance.
// if (N > 0) {
// /* Now we analyze `owner` vector as:
// * - sort the vector
// * - compute mode of the list to see who owns most of them
// * - remove repeating elements creating a unique list. */
// // sort
// std::sort( virtual_owners.begin(), virtual_owners.end() );
// // compute mode by creating a frequency array
// // NOTE: in case of same frequency we implicitly pick smaller rank
// int max=0, top_owner = virtual_owners[0];
// for(size_t i=0; i<virtual_owners.size(); i++) {
// int co = (int)count(virtual_owners.begin(),
// virtual_owners.end(),
// virtual_owners[i]);
// if(co > max) {
// max = co;
// top_owner = virtual_owners[i];
// }
// }
// // remove duplicates
// virtual_owners.erase( unique( virtual_owners.begin(),
// virtual_owners.end()
// ), virtual_owners.end() );
// // update tile values
// auto& c = get_tile(cid);
// c.communication.top_virtual_owner = top_owner;
// c.communication.communications = virtual_owners.size();
// c.communication.number_of_virtual_neighbors = N;
// c.communication.virtual_owners = virtual_owners;
// if (std::find( send_queue.begin(), send_queue.end(), cid)
// == send_queue.end())
// {
// send_queue.push_back( cid );
// send_queue_address.push_back( virtual_owners );
// }
// } else { // else N == 0
// auto& c = get_tile(cid);
// c.communication.number_of_virtual_neighbors = 0;
// c.communication.communications = 0;
// }
// }
//}
// /// Clear send queue, issue this only after the send has been successfully done
void clear_send_queue() {
send_queue.clear();
send_queue_address.clear();
}
// // --------------------------------------------------
// // Send queues etc.
//
// /// list of tile id's that are to be sent to others
std::vector<uint64_t> send_queue;
// /// list containing lists to where the aforementioned send_queue tiles are to be sent
std::vector< std::vector<int> > send_queue_address;
// public:
// // --------------------------------------------------
std::vector<mpi::request> sent_info_messages;
std::vector<mpi::request> sent_tile_messages;
std::unordered_map<int, std::vector<mpi::request>> sent_data_messages;
std::vector<mpi::request> recv_info_messages;
std::vector<mpi::request> recv_tile_messages;
std::unordered_map<int, std::vector<mpi::request>> recv_data_messages;
std::vector<mpi::request> sent_adoption_messages;
std::vector<mpi::request> recv_adoption_messages;
// /// Broadcast master ranks mpi_grid to everybody
void bcast_mpi_grid() {
// total size
int N = 1;
for (size_t i = 0; i<D; i++) N *= _lengths[i];
std::vector<int> tmp;
if (comm.rank() == 0) {
tmp = _mpi_grid.serialize();
} else {
tmp.resize(N);
for(int k=0; k<N; k++) {tmp[k] = -1;};
}
MPI_Bcast(&tmp[0],
N,
MPI_INT,
0,
MPI_COMM_WORLD
);
// unpack
if(comm.rank() != 0) {
_mpi_grid.deserialize(tmp, _lengths);
}
}
/// update work arrays from other nodes and send mine
void allgather_work_grid()
{
// total size
int N = 1;
for (size_t i = 0; i<D; i++) N *= _lengths[i];
// message buffers
std::vector<double> recv(N*comm.size());
std::vector<double> orig = _work_grid.serialize();
// mask all work values that are not mine
std::vector<int> ranks = _mpi_grid.serialize();
for(size_t i=0; i<ranks.size(); i++) {
if( ranks[i] != comm.rank() ) orig[i] = -1.0;
}
MPI_Allgather(
&orig[0],
orig.size(),
MPI_DOUBLE,
&recv[0],
orig.size(),
MPI_DOUBLE,
MPI_COMM_WORLD
);
std::vector<double> new_work(N);
std::fill(new_work.begin(), new_work.end(), -1.0);
double val;