forked from fmihpc/dccrg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdccrg.hpp
12852 lines (10916 loc) · 331 KB
/
dccrg.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
/*
A distributed cartesian cell-refinable grid.
Copyright 2009, 2010, 2011, 2012, 2013,
2014, 2015, 2016, 2018, 2019 Finnish Meteorological Institute
Copyright 2014, 2015, 2016, 2018 Ilja Honkonen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DCCRG_HPP
#define DCCRG_HPP
/*!
\mainpage Distributed Cartesian Cell-Refinable Grid.
See https://github.com/fmihpc/dccrg/wiki/Basics for the basics and
dccrg::Dccrg for a starting point in the API.
*/
#include "algorithm"
#include "array"
#include "cstdint"
#include "cstdio"
#include "cstdlib"
#include "fstream"
#include "functional"
#include "iterator"
#include "limits"
#include "map"
#include "set"
#include "stdexcept"
#include "tuple"
#include "type_traits"
#include "utility"
#include "unordered_map"
#include "unordered_set"
#include "vector"
#include "mpi.h"
#include "zoltan.h"
#ifdef USE_SFC
#include "sfc++.hpp"
#endif
/*
If compilation fails with a complaint about
MPI_UNSIGNED_LONG_LONG try replacing it with
MPI_UNSIGNED_LONG in the following:
*/
#ifndef MPI_UINT64_T
#define MPI_UINT64_T MPI_UNSIGNED_LONG_LONG
#endif
#include "dccrg_get_cell_datatype.hpp"
#include "dccrg_no_geometry.hpp"
#include "dccrg_mapping.hpp"
#include "dccrg_mpi_support.hpp"
#include "dccrg_topology.hpp"
#include "dccrg_types.hpp"
/*!
Namespace where all dccrg classes, functions, etc are defined.
*/
namespace dccrg
{
static const int
/*! @var
*/
/*!
Id of the default neighborhood created when dccrg is initialized
\see Dccrg::initialize() Dccrg::add_neighborhood()
*/
default_neighborhood_id = -0xdcc,
/*!
This bit is set for a cell that does not consider any cell as a
neighbor and is not considered as a neighbor by any cell
\see Dccrg::get_cells()
*/
has_no_neighbor = 0,
/*!
This bit is set for a cell that considers a cell on this
process as a neighbor
\see Dccrg::get_cells()
*/
has_local_neighbor_of = (1 << 0),
/*!
This bit is set for a cell that is considered as a neighbor
by a cell on this process
\see Dccrg::get_cells()
*/
has_local_neighbor_to = (1 << 1),
/*!
This bit is set for a cell that considers a cell on another
process as a neighbor
\see Dccrg::get_cells()
*/
has_remote_neighbor_of = (1 << 2),
/*!
This bit is set for a cell that c is considered as a neighbor
by a cell on another process
\see Dccrg::get_cells()
*/
has_remote_neighbor_to = (1 << 3),
/*!
This bit is set for a cell which is both dccrg::has_local_neighbor_of
and dccrg::has_local_neighbor_to
\see Dccrg::get_cells()
*/
has_local_neighbor_both = has_local_neighbor_of | has_local_neighbor_to,
/*!
This bit is set for a cell which is both dccrg::has_remote_neighbor_of
and dccrg::has_remote_neighbor_to
\see Dccrg::get_cells()
*/
has_remote_neighbor_both = has_remote_neighbor_of | has_remote_neighbor_to;
template <
class Cell_Data,
class Geometry = No_Geometry,
class Additional_Cell_Items = std::tuple<>,
class Additional_Neighbor_Items = std::tuple<>
> class Dccrg;
/*!
\brief Main class of dccrg, instantiate and initialize() to create a parallel simulation grid.
Cell_Data class will be stored in all local cells (and their remote neighbors after
a call to e.g. update_copies_of_remote_neighbors()) and must provide a way for dccrg to query
what data to send between processes using MPI-2.2 (http://www.mpi-forum.org/docs/).
Cell_Data must have one of the following member functions (as const or not):
\verbatim
std::tuple<
void*,
int,
MPI_Datatype
> get_mpi_datatype();
\endverbatim
or
\verbatim
std::tuple<
void*,
int,
MPI_Datatype
> get_mpi_datatype(
const uint64_t cell_id,
const int sender,
const int receiver,
const bool receiving,
const int neighborhood_id
);
\endverbatim
The get_mpi_datatype() function of the Cell_Data class decides what data
dccrg should transfer between processes or when the grid is written
to/read from a file. Internally the result of get_mpi_datatype()
from all cells being transferred is collected into one MPI_Datatype and
passed to MPI functions (see also set_send_single_cells()).
If needed, the arguments to get_mpi_datatype() provide additional information:
- cell_id is the id of the cell whose get_mpi_datatype is being called
- sender is the MPI process number sending the cell's data
- this can be negative, e.g. when loading grid data from a file,
see load_grid_data()
- receiver is the MPI process number receiving the cell's data
- this can be negative, e.g. when saving grid data to a file,
see save_grid_data()
- receiving is true on the receiving process and false on the sending
- neighborhood_id is the neighborhood id for which communication is
being done, see add_neighborhood()
- neighborhood_id is undefined when saving/loading grid data
from a file and balancing the computational load
Geometry class decides the physical size, shape, etc of the grid.
Cell_Data transfers between processes are determined by which cells are
considered as neighbors of other cells, which is defined by each cells'
neighbors_of list. Process owning a cell receives Cell_Data of all
neighbors_of of that cell from other processes.
\see Dccrg() to instantiate a new grid object.
*/
template <
class Cell_Data,
class Geometry,
class... Additional_Cell_Items,
class... Additional_Neighbor_Items
> class Dccrg<
Cell_Data,
Geometry,
std::tuple<Additional_Cell_Items...>,
std::tuple<Additional_Neighbor_Items...>
> {
public:
using cell_data_type = Cell_Data;
using geometry_type = Geometry;
private:
/*!
Read-write version of topology for internal use.
*/
Grid_Topology topology_rw;
public:
/*!
Public read-only version of the grid's topology.
\see Grid_Topology
*/
const Grid_Topology& topology = this->topology_rw;
private:
/*!
Read-write version of cell id mapping for internal use.
*/
Mapping mapping_rw;
public:
/*!
Public read-only version of the mapping of a
cell's ids to its size and location in the grid.
\see Mapping
*/
const Mapping& mapping = this->mapping_rw;
/*!
Public read-only version of the grid's length in cells of refinement level 0.
\see Grid_Length
*/
const Grid_Length& length = this->mapping.length;
private:
/*!
Read-write version of the grid's geometry for internal use.
*/
Geometry geometry_rw;
public:
/*!
The geometry of the grid.
\see
Cartesian_Geometry
Dccrg()
*/
const Geometry& geometry = this->geometry_rw;
/*!
Parameter type of the geometry class that is given
to dccrg as a template parameter.
\see For example Cartesian_Geometry_Parameters
*/
typedef typename Geometry::Parameters Geometry_Parameters;
/*!
Helper type for iterating over local cells and their data
\see
operator[]()
begin()
get_cells()
*/
typedef typename std::pair<const uint64_t&, const Cell_Data&> cell_and_data_pair_t;
/*!
Creates an uninitialized instance of dccrg.
The instance's initialize function must be called
before doing anything else with the instance,
otherwise the result is undefined.
Use the set function of the grid's geometry
variable to give the grid specific physical
parameters such as starting location and size.
For an example see the file simple_game_of_life.cpp
in the examples directory.
set_initial_length()
Dccrg(const Dccrg<Other_Cell_Data, Other_Geometry>& other)
*/
Dccrg():geometry_rw(length, mapping, topology){}
/*!
Creates an instance of the grid based on another instance of the grid.
Call this with all processes unless you really know what you are doing.
Must not be used while the instance being copied is updating remote
neighbor data or balancing the load.
The Cell_Data class can differ between the two grids.
The geometry of this grid must be compatible with the other
grid's geometry (it must have a copy contructor taking the
other grid's geometry as an argument).
The following data is not included in the new dccrg instance:
- Other_Cell_Data, in other words the other grid's cell data
- refined/unrefined cells
- everything related to load balancing or remote neighbor updates
- including user defined neighborhoods
A dccrg instance created this way is already initialized.
*/
template<
class Other_Cell_Data,
class Other_Geometry,
class... Other_Additional_Cell_Items,
class... Other_Additional_Neighbor_Items
> Dccrg(
const Dccrg<
Other_Cell_Data,
Other_Geometry,
std::tuple<Other_Additional_Cell_Items...>,
std::tuple<Other_Additional_Neighbor_Items...>
>& other
) :
topology_rw(other.topology),
mapping_rw(other.mapping),
geometry_rw(length, mapping, topology),
mapping_initialized(other.get_mapping_initialized()),
grid_initialized(other.get_initialized()),
neighborhood_length(other.get_neighborhood_length()),
neighbors_(other.get_neighbors_()),
max_tag(other.get_max_tag()),
send_single_cells(other.get_send_single_cells()),
comm(other.get_communicator()),
rank(uint64_t(other.get_rank())),
comm_size(uint64_t(other.get_comm_size())),
neighbors_of(other.get_all_neighbors_of()),
neighborhood_of(other.get_neighborhood_of()),
neighborhood_to(other.get_neighborhood_to()),
user_hood_of(other.get_user_hood_of()),
user_hood_to(other.get_user_hood_to()),
neighbors_to(other.get_all_neighbors_to()),
user_neigh_of(other.get_all_user_neigh_of()),
user_neigh_to(other.get_all_user_neigh_to()),
cell_process(other.get_cell_process()),
local_cells_on_process_boundary(other.get_local_cells_on_process_boundary_internal()),
remote_cells_on_process_boundary(other.get_remote_cells_on_process_boundary_internal()),
user_local_cells_on_process_boundary(other.get_user_local_cells_on_process_boundary()),
user_remote_cells_on_process_boundary(other.get_user_remote_cells_on_process_boundary()),
cells_to_send(other.get_cells_to_send()),
cells_to_receive(other.get_cells_to_receive()),
user_neigh_cells_to_send(other.get_user_neigh_cells_to_send()),
user_neigh_cells_to_receive(other.get_user_neigh_cells_to_receive()),
pin_requests(other.get_pin_requests()),
new_pin_requests(other.get_new_pin_requests()),
processes_per_part(other.get_processes_per_part()),
partitioning_options(other.get_partitioning_options()),
no_load_balancing(other.get_no_load_balancing()),
reserved_options(other.get_reserved_options()),
cell_weights(other.get_cell_weights()),
neighbor_processes(other.get_neighbor_processes()),
balancing_load(other.get_balancing_load())
{
if (other.get_balancing_load()) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Copy constructor called while the instance being copied is balancing load"
<< std::endl;
abort();
}
if (!this->geometry_rw.set(other.geometry)) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't set geometry while copy constructing"
<< std::endl;
abort();
}
if (!this->mapping_rw.set_maximum_refinement_level(other.mapping.get_maximum_refinement_level())) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Couldn't set maximum refinement level when copy constructing"
<< std::endl;
abort();
}
this->zoltan = Zoltan_Copy(other.get_zoltan());
// default construct Other_Cell_Data of local cells
for (typename std::unordered_map<uint64_t, Other_Cell_Data>::const_iterator
cell_item = other.get_cell_data().begin();
cell_item != other.get_cell_data().end();
cell_item++
) {
this->cell_data[cell_item->first];
}
this->allocate_copies_of_remote_neighbors();
try {
this->update_cell_pointers(
this->cells_rw,
this->neighbors_rw,
this->inner_cells_default,
this->outer_cells_default,
this->local_cells_default,
this->remote_cells_default,
this->all_cells_default,
default_neighborhood_id
);
} catch (...) {
throw std::runtime_error(__FILE__ "(" + std::to_string(__LINE__) + ")");
}
}
/*!
Initializes the instance of the grid with given parameters.
Zoltan library must have been initialized with Zoltan_Initialize
before calling this function. The geometry of the grid must not
have been set before calling this function.
comm: the grid will span all the processes in the communicator comm
Throws on failure (on one or more processes).
\see
Dccrg()
set_initial_length()
set_neighborhood_length()
set_load_balancing_method()
set_maximum_refinement_level()
set_periodic()
set_geometry()
get_cells()
get_existing_cell()
operator[]()
get_neighbors_of()
update_copies_of_remote_neighbors()
balance_load()
add_neighborhood()
refine_completely()
is_local()
set_send_single_cells()
load_grid_data()
*/
Dccrg<
Cell_Data,
Geometry,
std::tuple<Additional_Cell_Items...>,
std::tuple<Additional_Neighbor_Items...>
>& initialize(
const MPI_Comm& given_comm,
const uint64_t sfc_caching_batches = 1
) {
using std::to_string;
if (this->grid_initialized) {
throw std::invalid_argument(
"\n" __FILE__ "(" + to_string(__LINE__) + "): "
+ "Initialize function called for an already initialized dccrg"
);
}
this->grid_initialized = true;
if (not this->mapping_initialized) {
throw std::invalid_argument(
"\n" __FILE__ "(" + to_string(__LINE__) + "): "
+ "Initialize function called before set_initial_length()."
);
}
if (!this->initialize_mpi(given_comm)) {
throw std::invalid_argument(
"\n" __FILE__ "(" + to_string(__LINE__) + "): "
+ "Couldn't initialize MPI"
);
}
if (!this->initialize_zoltan()) {
throw std::invalid_argument(
"\n" __FILE__ "(" + to_string(__LINE__) + "): "
+ "Couldn't set up Zoltan"
);
}
this->initialize_neighborhoods();
if (!this->create_level_0_cells(sfc_caching_batches)) {
throw std::invalid_argument(
"\n" __FILE__ "(" + to_string(__LINE__) + "): "
+ "Couldn't create cells of refinement level 0"
);
}
this->update_neighbors_();
if (!this->initialize_neighbors()) {
throw std::invalid_argument(
"\n" __FILE__ "(" + to_string(__LINE__) + "): "
+ "Couldn't initialize neighbors"
);
}
this->allocate_copies_of_remote_neighbors();
try {
this->update_cell_pointers(
this->cells_rw,
this->neighbors_rw,
this->inner_cells_default,
this->outer_cells_default,
this->local_cells_default,
this->remote_cells_default,
this->all_cells_default,
default_neighborhood_id
);
} catch (const std::exception& e) {
throw std::runtime_error(
"\n" __FILE__ "(" + to_string(__LINE__) + "): "
+ "Couldn't update cell pointers: " + e.what()
);
}
return *this;
}
/*!
Sets the geometry of the grid to given values.
initialize() must have been called prior to calling this function.
Forwards the given values to the set function of the
Geometry class given as a template parameter to dccrg.
See the documentation of the Geometry class for details
of the Parameters required.
Available geometries:
- Cartesian_Geometry::set()
- Stretched_Cartesian_Geometry::set()
.
*/
Dccrg<
Cell_Data,
Geometry,
std::tuple<Additional_Cell_Items...>,
std::tuple<Additional_Neighbor_Items...>
>& set_geometry(const typename Geometry::Parameters& parameters)
{
if (not this->geometry_rw.set(parameters)) {
throw std::invalid_argument(
"\n" __FILE__ "(" + std::to_string(__LINE__) + "): "
+ "Couldn't set geometry"
);
}
return *this;
}
/*!
Returns cells without children on this process fulfilling given criteria.
By default returns all local cells. Otherwise only those local cells are
returned which match one or more of the given criteria.
Criteria represent which type of neighbors a cell must (and possibly must not)
have in order to be returned. Each criteria is a bitmask of possibly several
neighbor types.
If exact_match = false then a cell is returned as long as it has at least
one neighbor of the type given by any of the given criteria. If
exact_match == true a cell is returned only if it has neighbors of the
type(s) in any given criteria and no other types of neighbors in that
criteria.
For example to only get cells which:
- are not on the process boundary, i.e. don't have neighbors or only
consider cells on this process as a neighbor or are only considered
as a neighbor by cells on this process, give exact_match = true and
\verbatim
{
has_no_neighbor,
has_local_neighbor_of,
has_local_neighbor_to,
has_local_neighbor_both
}
\endverbatim
- are on the process boundary, i.e. consider a cell on another process
as a neighbor or are considered as a neighbor by a cell on another
process, give exact_match = false and
\verbatim
{has_remote_neighbor_both}
\endverbatim
- are considered as a neighbor by at least one local and at least one
remote cell but do not consider any local or remote cells as their
neighbors, give exact_match = true and
\verbatim
{has_local_neighor_to | has_remote_neighbor_to}
\endverbatim
- are considered as a neighbor by a local cell or by a remote cell
but do not consider any local of remote cells as their neighbors, give
exact_match = true and
\verbatim
{has_local_neighor_to, has_remote_neighbor_to}
\endverbatim
Cells' neighbors from the given neighborhood are used when checking for
neighbor types.
By default returned cells are in random order but if sorted == true
they are sorted using std::sort.
Returns nothing if:
- this process doesn't have any cells
- given neighborhood doesn't exist
\see
begin()
get_local_cells_on_process_boundary()
get_local_cells_not_on_process_boundary()
get_remote_cells_on_process_boundary()
*/
std::vector<uint64_t> get_cells(
const std::vector<int>& criteria = std::vector<int>(),
const bool exact_match = false,
const int neighborhood_id = default_neighborhood_id,
const bool sorted = false
) const {
#ifdef DEBUG
if (not this->grid_initialized) {
std::cerr << __FILE__ << ":" << __LINE__
<< " DCCRG not initialized before calling " << __func__
<< std::endl;
abort();
}
#endif
if (this->balancing_load) {
std::cerr << __FILE__ << ":" << __LINE__
<< " get_cells() must not be called while balancing load"
<< std::endl;
abort();
}
std::vector<uint64_t> ret_val;
if (neighborhood_id != default_neighborhood_id
&& this->user_hood_of.count(neighborhood_id) == 0) {
return ret_val;
}
for (const auto& item: this->cell_data) {
const uint64_t cell = item.first;
#ifdef DEBUG
if (this->cell_process.count(cell) == 0) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Cell " << cell
<< " shouldn't exist"
<< std::endl;
abort();
}
if (this->cell_process.at(cell) != this->rank) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Process " << this->rank
<< ": Cell " << cell
<< " should be on process " << this->cell_process.at(cell)
<< std::endl;
abort();
}
const uint64_t child = this->get_child(cell);
if (child == 0) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Process " << this->rank
<< ": Child == 0 for cell " << cell
<< std::endl;
abort();
}
if (child != cell) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Process " << this->rank
<< ": Cell " << cell
<< " has a child"
<< std::endl;
abort();
}
#endif
if (criteria.size() == 0) {
ret_val.push_back(cell);
continue;
}
if (this->is_neighbor_type_match(
cell,
criteria,
exact_match,
neighborhood_id
)) {
ret_val.push_back(cell);
}
}
if (sorted && ret_val.size() > 0) {
std::sort(ret_val.begin(), ret_val.end());
}
return ret_val;
}
/*!
Returns a pointer to the user supplied data of given cell.
The data of local cells is always available, including refined cells
before the next call to stop_refining.
The data of cells which are on other processes can also be available if:
- the cells are neighbors to a local cell and remote neighbor data has been updated
- the cells were unrefined and their parent is now a local cell
\see
get_cells()
*/
Cell_Data* operator [] (const uint64_t cell) const
{
if (this->cell_data.count(cell) > 0) {
return (Cell_Data*) &(this->cell_data.at(cell));
} else if (this->remote_neighbors.count(cell) > 0) {
return (Cell_Data*) &(this->remote_neighbors.at(cell));
} else if (this->refined_cell_data.count(cell) > 0) {
return (Cell_Data*) &(this->refined_cell_data.at(cell));
} else if (this->unrefined_cell_data.count(cell) > 0) {
return (Cell_Data*) &(this->unrefined_cell_data.at(cell));
} else {
return nullptr;
}
}
/*!
Returns the coordinate of the center of given cell.
*/
std::array<double, 3> get_center(const uint64_t cell) const
{
return this->geometry.get_center(cell);
}
/*!
Returns a pointer to the neighbors of given cell.
In case the grid is not periodic in one or more directions,
neighbors that would be outside of the grid are error_cell.
Some neighbors might be on another process, but have a copy of their data on this process.
The local copy of remote neighbors' data is updated, for example, by calling
update_copies_of_remote_neighbors().
The neighbors are always in the following order:
- if all neighbors are of the same size then they are in z order, e.g.
with a neighborhood size of 2 the first neighbor is at offset (-2, -2, -2)
from the given cell, the second one is at (-1, -2, -2), etc, in size units
of the given cell.
- if one or more of the cells in 1) is refined then instead of one cell
there are 8 which are again in z order.
For example with maximum refinement level 1 and neighborhood size of 1
the neighbors of a cell of refinement level 0 at indices (2, 2, 2) could
be in the following order: (0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0),
rest of refined cells..., (2, 0, 0), (3, 0, 0), (0, 2, 0), (2, 2, 0), ...
Offset (0, 0, 0) is skipped in all neighbor lists, so with a neighborhood
size of 2 the minimum length of neighbors lists is 124 and not 5^3 = 125.
If given a non-default neighborhood neighbors are in the same order as
the offsets in the given neighborhood.
Returns NULL if:
- neighborhood with given id doesn't exist
- given cell doesn't exist
- given cell is on another process
\see
get_neighbors_to()
update_copies_of_remote_neighbors()
get_neighbors_of_at_offset()
add_neighborhood()
*/
const std::vector<
std::pair<
uint64_t,
std::array<int, 3>
>
>* get_neighbors_of(
const uint64_t cell,
const int neighborhood_id = default_neighborhood_id
) const {
#ifdef DEBUG
if (not this->grid_initialized) {
std::cerr << __FILE__ << ":" << __LINE__
<< " DCCRG not initialized before calling " << __func__
<< std::endl;
abort();
}
#endif
if (this->cell_process.count(cell) > 0) {
if (neighborhood_id == default_neighborhood_id) {
#ifdef DEBUG
if (this->neighbors_of.count(cell) == 0) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Process " << this->rank
<< ": Neighbor list for cell " << cell
<< " owned by process "
<< this->cell_process.at(cell)
<< " doesn't exist" << std::endl;
abort();
}
#endif
return &(this->neighbors_of.at(cell));
} else if (this->user_hood_of.count(neighborhood_id) > 0) {
#ifdef DEBUG
if (this->user_neigh_of.at(neighborhood_id).count(cell) == 0) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Process " << this->rank
<< ": Neighbor list for cell " << cell
<< " doesn't exist for neighborhood id " << neighborhood_id
<< std::endl;
abort();
}
#endif
return &(this->user_neigh_of.at(neighborhood_id).at(cell));
}
}
return nullptr;
}
/*!
Returns a pointer to the cells that consider given cell as a neighbor.
This list doesn't include 0s even if the grid isn't periodic in some direction.
Returns NULL if given cell doesn't exist or is on another process.
Neighbors returned by this function are in no particular order.
Returns NULL if neighborhood with given id doesn't exist.
*/
const std::vector<
std::pair<
uint64_t,
std::array<int, 3>
>
>* get_neighbors_to(
const uint64_t cell,
const int neighborhood_id = default_neighborhood_id
) const {
#ifdef DEBUG
if (not this->grid_initialized) {
std::cerr << __FILE__ << ":" << __LINE__
<< " DCCRG not initialized before calling " << __func__
<< std::endl;
abort();
}
#endif
if (this->cell_process.count(cell) > 0) {
if (neighborhood_id == default_neighborhood_id) {
#ifdef DEBUG
if (this->neighbors_to.count(cell) == 0) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Neighbors_to list for cell " << cell
<< " doesn't exist"
<< std::endl;
abort();
}
#endif
return &(this->neighbors_to.at(cell));
} else if (this->user_hood_of.count(neighborhood_id) > 0) {
#ifdef DEBUG
if (this->user_neigh_to.at(neighborhood_id).count(cell) == 0) {
std::cerr << __FILE__ << ":" << __LINE__
<< " Neighbors_to list for cell " << cell
<< " doesn't exist for neighborhood id " << neighborhood_id
<< std::endl;
abort();
}
#endif
return &(this->user_neigh_to.at(neighborhood_id).at(cell));
}
}
return nullptr;
}
/*!
Updates the cell data of neighboring cells between processes.
Cell data of any local cell that a cell on another process
considers as a neighbor is sent to that process.
Cell data of any cell on another process that a local cell
considers as a neighbor is received from that process.
Afterwards a copy of the remote cells' data is available
through operator[].
Data of any cell is only exchanged between any two processes
once, even if a cell the neighbor of more than one cell on
another process.
The decision of which cells are neighbors is controlled by
the neighborhood. By default the neighborhood with which
this instance of dccrg was initialized is used.
Returns true if successful and false otherwise (on one or more
processes), for example if a neighborhood with the given id
has not been set.
Must be called simultaneously on all processes and with
identical neigbhorhood_id.
Must not be called while load balancing is underway.
\see
start_remote_neighbor_copy_updates()
add_neighborhood()
get_remote_cells_on_process_boundary()
set_send_single_cells()
*/
bool update_copies_of_remote_neighbors(
const int neighborhood_id = default_neighborhood_id
) {
#ifdef DEBUG
if (not this->grid_initialized) {
std::cerr << __FILE__ << ":" << __LINE__
<< " DCCRG not initialized before calling " << __func__
<< std::endl;
abort();
}
#endif
if (this->balancing_load) {
std::cerr << __FILE__ << ":" << __LINE__
<< " update_copies_of_remote_neighbors(...) called while balancing load"
<< std::endl;
abort();
}
bool ret_val = true;
if (this->user_hood_of.count(neighborhood_id) == 0) {
ret_val = false;
}
if (!this->start_remote_neighbor_copy_updates(neighborhood_id)) {
ret_val = false;
}
if (!this->wait_remote_neighbor_copy_updates(neighborhood_id)) {
ret_val = false;
}
return ret_val;
}