-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy patht8_forest_ghost.cxx
More file actions
2123 lines (1921 loc) · 82.2 KB
/
t8_forest_ghost.cxx
File metadata and controls
2123 lines (1921 loc) · 82.2 KB
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
/*
This file is part of t8code.
t8code is a C library to manage a collection (a forest) of multiple
connected adaptive space-trees of general element classes in parallel.
Copyright (C) 2015 the developers
t8code is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
t8code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with t8code; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <t8_forest/t8_forest_ghost.h>
#include <t8_forest/t8_forest_partition.h>
#include <t8_forest/t8_forest_types.h>
#include <t8_forest/t8_forest_private.h>
#include <t8_forest/t8_forest_iterate.h>
#include <t8_forest/t8_forest_general.h>
#include <t8_forest/t8_forest_neighbor/t8_forest_element_face_neighbor.h>
#include <t8_schemes/t8_scheme.hxx>
#include <t8_cmesh/t8_cmesh_trees.h>
#include <t8_data/t8_containers.h>
#include <sc_statistics.h>
/* We want to export the whole implementation to be callable from "C" */
T8_EXTERN_C_BEGIN ();
/**
* The information for a remote process, what data we have to send to them.
*/
typedef struct
{
int recv_rank; /**< The rank to which we send. */
size_t num_bytes; /**< The number of bytes that we send. */
sc_MPI_Request *request; /**< Communication request, not owned by this struct. */
char *buffer; /**< The send buffer. */
} t8_ghost_mpi_send_info_t;
/**
* The information stored for the ghost trees
*/
typedef struct
{
t8_gloidx_t global_id; /**< Global id of the tree */
t8_locidx_t element_offset; /**< The count of all ghost elements in all smaller ghost trees */
t8_element_array_t elements; /**< The ghost elements of that tree */
t8_eclass_t eclass; /**< The trees element class */
} t8_ghost_tree_t;
/**
* The data structure stored in the global_tree_to_ghost_tree hash table.
*/
typedef struct
{
t8_gloidx_t global_id; /**< global tree id */
size_t index; /**< the index of that global tree in the ghost_trees array. */
} t8_ghost_gtree_hash_t;
/**
* The data structure stored in the process_offsets array.
*/
typedef struct
{
int mpirank; /**< rank of the process */
t8_locidx_t ghost_offset; /**< The number of ghost elements for all previous ranks */
size_t tree_index; /**< index of first ghost tree of this process in ghost_trees */
size_t first_element; /**< the index of the first element in the elements array of the ghost tree. */
} t8_ghost_process_hash_t;
/**
* The information stored for the remote trees.
* Each remote process stores an array of these
*/
typedef struct
{
t8_gloidx_t global_id; /**< global id of the tree */
int mpirank; /**< The mpirank of the remote process */
t8_element_array_t elements; /**< The remote elements of that tree */
sc_array_t element_indices; /**< The (tree) local indices of the ghost elements. */
t8_eclass_t eclass; /**< The trees element class */
} t8_ghost_remote_tree_t;
/**
* This struct stores information about the data that the current process needs from a specific remote_process
* as ghost data, such as the number of remote elements and the remote trees.
*/
typedef struct
{
int remote_rank; /**< The rank of the remote process */
t8_locidx_t num_elements; /**< The number of remote elements for this process */
sc_array_t remote_trees; /**< Array of the remote trees of this process */
} t8_ghost_remote_t;
/**
* The hash function for the global tree hash.
* As hash value we just return the global tree id.
*
* \param[in] ghost_gtree_hash Global tree hash.
* \param[in] data Unused dummy Argument to allow passing this function to sc_hash_new.
*
* \return The global tree id.
*/
static unsigned
t8_ghost_gtree_hash_function (const void *ghost_gtree_hash, [[maybe_unused]] const void *data)
{
const t8_ghost_gtree_hash_t *object = (const t8_ghost_gtree_hash_t *) ghost_gtree_hash;
return (unsigned) object->global_id;
}
/**
* The equal function for two global tree hash objects.
* Two t8_ghost_gtree_hash_t are considered equal if their global tree ids are the same.
*
* \param[in] ghost_gtreea Global tree hash object A
* \param[in] ghost_gtreeb Global tree hash object B
* \param[in] user Unused dummy Argument to allow passing this function to sc_hash_new.
*
* \return 1 if equal, 0 otherwise.
*/
static int
t8_ghost_gtree_equal_function (const void *ghost_gtreea, const void *ghost_gtreeb, [[maybe_unused]] const void *user)
{
const t8_ghost_gtree_hash_t *objecta = (const t8_ghost_gtree_hash_t *) ghost_gtreea;
const t8_ghost_gtree_hash_t *objectb = (const t8_ghost_gtree_hash_t *) ghost_gtreeb;
/* return true if and only if the global_ids are the same */
return objecta->global_id == objectb->global_id;
}
/**
* The hash value for an entry of the process_offsets hash is the processes mpirank.
*
* \param[in] process_data Process data as void pointer.
* \param[in] user_data Unused dummy Argument to allow passing this function to sc_hash_new.
*
* \return The process' MPI rank.
*
*/
static unsigned
t8_ghost_process_hash_function (const void *process_data, [[maybe_unused]] const void *user_data)
{
const t8_ghost_process_hash_t *process = (const t8_ghost_process_hash_t *) process_data;
return process->mpirank;
}
/**
* The equal function for the process_offsets array.
* Two entries are the same if their mpiranks are equal.
*
* \param[in] process_dataa Process offset array A
* \param[in] process_datab Process offset array B
* \param[in] user Unused dummy Argument to allow passing this function to sc_hash_new.
*
* \return 1 if equal, 0 if not.
*/
static int
t8_ghost_process_equal_function (const void *process_dataa, const void *process_datab,
[[maybe_unused]] const void *user)
{
const t8_ghost_process_hash_t *processa = (const t8_ghost_process_hash_t *) process_dataa;
const t8_ghost_process_hash_t *processb = (const t8_ghost_process_hash_t *) process_datab;
return processa->mpirank == processb->mpirank;
}
/**
* The hash function for the remote_ghosts hash table.
* The hash value for an mpirank is just the rank.
*
* \param[in] remote_data The remote ghost data as void pointer.
* \param[in] user_data Unused dummy Argument to allow passing this function to sc_hash_new.
*
* \return The mpi rank.
*/
static unsigned
t8_ghost_remote_hash_function (const void *remote_data, [[maybe_unused]] const void *user_data)
{
const t8_ghost_remote_t *remote = (const t8_ghost_remote_t *) remote_data;
return remote->remote_rank;
}
/**
* The equal function for the remote hash table.
* Two entries are the same if they have the same rank.
*
* \param[in] remote_dataa Remote hash table A.
* \param[in] remote_datab Remote hash table B.
* \param[in] user Unused dummy Argument to allow passing this function to sc_hash_new.
*
* \return 1 if the two have the same rank, 0 if not.
*/
static int
t8_ghost_remote_equal_function (const void *remote_dataa, const void *remote_datab, [[maybe_unused]] const void *user)
{
const t8_ghost_remote_t *remotea = (const t8_ghost_remote_t *) remote_dataa;
const t8_ghost_remote_t *remoteb = (const t8_ghost_remote_t *) remote_datab;
return remotea->remote_rank == remoteb->remote_rank;
}
/**
* This struct is used during a ghost data exchange.
* Since we use asynchronous communication, we store the
* send buffers and mpi requests until we end the communication.
*/
typedef struct
{
int num_remotes; /**< The number of processes, we send to. */
char **send_buffers; /**< For each remote the send buffer. */
sc_MPI_Request *send_requests; /**< For each process we send to, the MPI request used. */
sc_MPI_Request *recv_requests; /**< For each process we receive from, the MPI request used. */
} t8_ghost_data_exchange_t;
void
t8_forest_ghost_init (t8_forest_ghost_t *pghost, t8_ghost_type_t ghost_type)
{
t8_forest_ghost_t ghost;
/* We currently only support face-neighbor ghosts */
T8_ASSERT (ghost_type == T8_GHOST_FACES);
/* Allocate memory for ghost */
ghost = *pghost = T8_ALLOC_ZERO (t8_forest_ghost_struct_t, 1);
/* initialize the reference counter */
t8_refcount_init (&ghost->rc);
/* Set the ghost type */
ghost->ghost_type = ghost_type;
/* Allocate the trees array */
ghost->ghost_trees = sc_array_new (sizeof (t8_ghost_tree_t));
/* initialize the global_tree_to_ghost_tree hash table */
ghost->glo_tree_mempool = sc_mempool_new (sizeof (t8_ghost_gtree_hash_t));
ghost->global_tree_to_ghost_tree
= sc_hash_new (t8_ghost_gtree_hash_function, t8_ghost_gtree_equal_function, NULL, NULL);
/* initialize the process_offset hash table */
ghost->proc_offset_mempool = sc_mempool_new (sizeof (t8_ghost_process_hash_t));
ghost->process_offsets = sc_hash_new (t8_ghost_process_hash_function, t8_ghost_process_equal_function, NULL, NULL);
/* initialize the remote ghosts hash table */
ghost->remote_ghosts = sc_hash_array_new (sizeof (t8_ghost_remote_t), t8_ghost_remote_hash_function,
t8_ghost_remote_equal_function, NULL);
/* initialize the remote processes array */
ghost->remote_processes = sc_array_new (sizeof (int));
}
/**
* Return the remote struct of a given remote rank
*
* \param[in] forest A committed forest.
* \param[in] remote The rank of the remote process.
*
* \return The remote struct of the rank as \see t8_ghost_remote_t pointer.
*/
static t8_ghost_remote_t *
t8_forest_ghost_get_remote (t8_forest_t forest, int remote)
{
t8_ghost_remote_t remote_search;
#if T8_ENABLE_DEBUG
int ret;
#endif
size_t index;
T8_ASSERT (t8_forest_is_committed (forest));
remote_search.remote_rank = remote;
#if T8_ENABLE_DEBUG
ret =
#else
(void)
#endif
sc_hash_array_lookup (forest->ghosts->remote_ghosts, &remote_search, &index);
T8_ASSERT (ret);
return (t8_ghost_remote_t *) sc_array_index (&forest->ghosts->remote_ghosts->a, index);
}
/**
* Return a remote processes info about the stored ghost elements
*
* \param[in] forest A committed forest.
* \param[in] remote The rank of the remote.
*
* \return The remote process info about the stored ghost elements, as \see t8_ghost_process_hash_t.
*/
static t8_ghost_process_hash_t *
t8_forest_ghost_get_proc_info (t8_forest_t forest, int remote)
{
t8_ghost_process_hash_t proc_hash_search, **pproc_hash_found, *proc_hash_found;
#if T8_ENABLE_DEBUG
int ret;
#endif
T8_ASSERT (t8_forest_is_committed (forest));
proc_hash_search.mpirank = remote;
#if T8_ENABLE_DEBUG
ret =
#else
(void)
#endif
sc_hash_lookup (forest->ghosts->process_offsets, &proc_hash_search, (void ***) &pproc_hash_found);
T8_ASSERT (ret);
proc_hash_found = *pproc_hash_found;
T8_ASSERT (proc_hash_found->mpirank == remote);
return proc_hash_found;
}
/* Return the number of trees in a ghost */
t8_locidx_t
t8_forest_ghost_num_trees (const t8_forest_t forest)
{
if (forest->ghosts == NULL) {
return 0;
}
T8_ASSERT (forest->ghosts != NULL);
if (forest->ghosts->num_ghosts_elements <= 0) {
return 0;
}
T8_ASSERT (forest->ghosts->ghost_trees != NULL);
return forest->ghosts->ghost_trees->elem_count;
}
#if T8_ENABLE_DEBUG
static bool
t8_forest_tree_is_ghost (const t8_forest_t forest, const t8_locidx_t lghost_tree)
{
T8_ASSERT (t8_forest_is_committed (forest));
return 0 <= lghost_tree && lghost_tree < t8_forest_get_num_ghost_trees (forest);
}
#endif
/**
* Given an index into the ghost_trees array return the ghost tree
*
* \param[in] forest A committed forest.
* \param[in] lghost_tree Index of the tree within the ghost_trees array.
*
* \return The ghost tree.
*/
static t8_ghost_tree_t *
t8_forest_ghost_get_tree (const t8_forest_t forest, const t8_locidx_t lghost_tree)
{
t8_ghost_tree_t *ghost_tree;
t8_forest_ghost_t ghost;
T8_ASSERT (t8_forest_is_committed (forest));
ghost = forest->ghosts;
T8_ASSERT (ghost != NULL);
T8_ASSERT (ghost->ghost_trees != NULL);
T8_ASSERT (t8_forest_tree_is_ghost (forest, lghost_tree));
ghost_tree = (t8_ghost_tree_t *) t8_sc_array_index_locidx (ghost->ghost_trees, lghost_tree);
return ghost_tree;
}
t8_locidx_t
t8_forest_ghost_get_tree_element_offset (const t8_forest_t forest, const t8_locidx_t lghost_tree)
{
T8_ASSERT (t8_forest_is_committed (forest));
return t8_forest_ghost_get_tree (forest, lghost_tree)->element_offset;
}
/** Given an index in the ghost_tree array, return this tree's number of elements */
t8_locidx_t
t8_forest_ghost_tree_num_leaf_elements (t8_forest_t forest, t8_locidx_t lghost_tree)
{
t8_ghost_tree_t *ghost_tree;
T8_ASSERT (t8_forest_is_committed (forest));
ghost_tree = t8_forest_ghost_get_tree (forest, lghost_tree);
return t8_element_array_get_count (&ghost_tree->elements);
}
t8_element_array_t *
t8_forest_ghost_get_tree_leaf_elements (const t8_forest_t forest, const t8_locidx_t lghost_tree)
{
T8_ASSERT (t8_forest_is_committed (forest));
T8_ASSERT (forest->ghosts != NULL);
return &t8_forest_ghost_get_tree (forest, lghost_tree)->elements;
}
t8_locidx_t
t8_forest_ghost_get_ghost_treeid (t8_forest_t forest, t8_gloidx_t gtreeid)
{
t8_ghost_gtree_hash_t query, *found, **pfound;
T8_ASSERT (t8_forest_is_committed (forest));
T8_ASSERT (forest->ghosts != NULL);
query.global_id = gtreeid;
if (sc_hash_lookup (forest->ghosts->global_tree_to_ghost_tree, &query, (void ***) &pfound)) {
/* The tree was found */
found = *pfound;
return found->index;
}
else {
/* The tree was not found */
return -1;
}
}
/* Given an index in the ghost_tree array, return this tree's element class */
t8_eclass_t
t8_forest_ghost_get_tree_class (const t8_forest_t forest, const t8_locidx_t lghost_tree)
{
t8_ghost_tree_t *ghost_tree;
T8_ASSERT (t8_forest_is_committed (forest));
ghost_tree = t8_forest_ghost_get_tree (forest, lghost_tree);
return ghost_tree->eclass;
}
/* Given an index in the ghost_tree array, return this tree's global id */
t8_gloidx_t
t8_forest_ghost_get_global_treeid (const t8_forest_t forest, const t8_locidx_t lghost_tree)
{
t8_ghost_tree_t *ghost_tree;
T8_ASSERT (t8_forest_is_committed (forest));
ghost_tree = t8_forest_ghost_get_tree (forest, lghost_tree);
return ghost_tree->global_id;
}
/* Given an index into the ghost_trees array and for that tree an element index,
return the corresponding element. */
t8_element_t *
t8_forest_ghost_get_leaf_element (t8_forest_t forest, t8_locidx_t lghost_tree, t8_locidx_t lelement)
{
t8_ghost_tree_t *ghost_tree;
T8_ASSERT (t8_forest_is_committed (forest));
ghost_tree = t8_forest_ghost_get_tree (forest, lghost_tree);
T8_ASSERT (0 <= lelement && lelement < t8_forest_ghost_tree_num_leaf_elements (forest, lghost_tree));
/* TODO: In future, make return type const (and offer additional mutable version) and call t8_element_array_index_locidx (the const version). */
return t8_element_array_index_locidx_mutable (&ghost_tree->elements, lelement);
}
int
t8_forest_element_is_ghost (const t8_forest_t forest, const t8_element_t *element, const t8_locidx_t lghost_tree)
{
bool check_ghost = true;
T8_ASSERT (t8_forest_tree_is_ghost (forest, lghost_tree));
return t8_forest_element_is_leaf_or_ghost (forest, element, lghost_tree, check_ghost);
}
/** Initialize a t8_ghost_remote_tree_t.
*
* \param[in] forest The forest.
* \param[in] gtreeid The global ID of the remote tree.
* \param[in] remote_rank The rank of the reomte process holding the tree.
* \param[in] tree_class The eclass of the remote tree.
* \param[in, out] remote_tree A pointer to the t8_ghost_remote_tree_t to be initialized.
* Has to be non-NULL on input. On output, it is initialized.
*/
static void
t8_ghost_init_remote_tree (t8_forest_t forest, t8_gloidx_t gtreeid, int remote_rank, t8_eclass_t tree_class,
t8_ghost_remote_tree_t *remote_tree)
{
const t8_scheme *scheme = t8_forest_get_scheme (forest);
t8_locidx_t local_treeid;
T8_ASSERT (remote_tree != NULL);
local_treeid = gtreeid - t8_forest_get_first_local_tree_id (forest);
/* Set the entries of the new remote tree */
remote_tree->global_id = gtreeid;
remote_tree->mpirank = remote_rank;
remote_tree->eclass = t8_forest_get_eclass (forest, local_treeid);
/* Initialize the array to store the element */
t8_element_array_init (&remote_tree->elements, scheme, tree_class);
/* Initialize the array to store the element indices. */
sc_array_init (&remote_tree->element_indices, sizeof (t8_locidx_t));
}
/**
* Add a new element to the remote hash table (if not already in it).
* Must be called for elements in linear order
* element_index is the tree local index of this element.
*
* \param[in] forest The forest.
* \param[in] ghost The ghost structure.
* \param[in] remote_rank The remote rank.
* \param[in] ltreeid Local id of the tree within the forest.
* \param[in] elem The element to be added.
* \param[in] element_index The element's tree-local id.
*/
static void
t8_ghost_add_remote (t8_forest_t forest, t8_forest_ghost_t ghost, int remote_rank, t8_locidx_t ltreeid,
const t8_element_t *elem, t8_locidx_t element_index)
{
t8_ghost_remote_t remote_entry_lookup, *remote_entry;
t8_ghost_remote_tree_t *remote_tree;
t8_element_t *elem_copy;
sc_array_t *remote_array;
size_t index, element_count;
int *remote_process_entry;
int level, copy_level = 0;
/* Get the tree's element class and the scheme */
const t8_eclass_t tree_class = t8_forest_get_tree_class (forest, ltreeid);
const t8_scheme *scheme = t8_forest_get_scheme (forest);
const t8_gloidx_t gtreeid = t8_forest_get_first_local_tree_id (forest) + ltreeid;
/* Check whether the remote_rank is already present in the remote ghosts
* array. */
remote_entry_lookup.remote_rank = remote_rank;
/* clang-format off */
remote_entry = (t8_ghost_remote_t *) sc_hash_array_insert_unique (ghost->remote_ghosts, (void *) &remote_entry_lookup,
&index);
/* clang-format on */
if (remote_entry != NULL) {
/* The remote rank was not in the array and was inserted now */
remote_entry->remote_rank = remote_rank;
remote_entry->num_elements = 0;
/* Initialize the tree array of the new entry */
sc_array_init_size (&remote_entry->remote_trees, sizeof (t8_ghost_remote_tree_t), 1);
/* Get a pointer to the new entry */
remote_tree = (t8_ghost_remote_tree_t *) sc_array_index (&remote_entry->remote_trees, 0);
/* initialize the remote_tree */
t8_ghost_init_remote_tree (forest, gtreeid, remote_rank, tree_class, remote_tree);
/* Since the rank is a new remote rank, we also add it to the remote ranks array */
remote_process_entry = (int *) sc_array_push (ghost->remote_processes);
*remote_process_entry = remote_rank;
}
else {
/* The remote rank already is contained in the remotes array at position index. */
remote_array = &ghost->remote_ghosts->a;
remote_entry = (t8_ghost_remote_t *) sc_array_index (remote_array, index);
T8_ASSERT (remote_entry->remote_rank == remote_rank);
/* Check whether the tree has already an entry for this process.
* Since we only add in local tree order the current tree is either
* the last entry or does not have an entry yet. */
remote_tree = (t8_ghost_remote_tree_t *) sc_array_index (&remote_entry->remote_trees,
remote_entry->remote_trees.elem_count - 1);
if (remote_tree->global_id != gtreeid) {
/* The tree does not exist in the array. We thus need to add it and
* initialize it. */
remote_tree = (t8_ghost_remote_tree_t *) sc_array_push (&remote_entry->remote_trees);
t8_ghost_init_remote_tree (forest, gtreeid, remote_rank, tree_class, remote_tree);
}
}
/* remote_tree now points to a valid entry for the tree.
* We can add a copy of the element to the elements array
* if it does not exist already. If it exists it is the last entry in the array. */
#if T8_ENABLE_DEBUG
{
/* debugging assertion that the element is really not contained already */
int ielem;
int elem_count = t8_element_array_get_count (&remote_tree->elements);
for (ielem = 0; ielem < elem_count - 1; ielem++) {
const t8_element_t *test_el = t8_element_array_index_int (&remote_tree->elements, ielem);
SC_CHECK_ABORTF (!scheme->element_is_equal (tree_class, test_el, elem),
"Local element %i already in remote ghosts at pos %i\n", element_index, ielem);
}
}
#endif
elem_copy = NULL;
level = scheme->element_get_level (tree_class, elem);
element_count = t8_element_array_get_count (&remote_tree->elements);
if (element_count > 0) {
elem_copy = t8_element_array_index_locidx_mutable (&remote_tree->elements, element_count - 1);
copy_level = scheme->element_get_level (tree_class, elem_copy);
}
/* Check if the element was not contained in the array.
* If so, we add a copy of elem to the array.
* Otherwise, we do nothing. */
if (elem_copy == NULL || level != copy_level
|| scheme->element_get_linear_id (tree_class, elem_copy, copy_level)
!= scheme->element_get_linear_id (tree_class, elem, level)) {
/* Add the element */
elem_copy = t8_element_array_push (&remote_tree->elements);
scheme->element_copy (tree_class, elem, elem_copy);
/* Add the index of the element */
*(t8_locidx_t *) sc_array_push (&remote_tree->element_indices) = element_index;
remote_entry->num_elements++;
}
}
/**
* This struct stores the ghost boundary data of a forest.
*/
typedef struct
{
sc_array_t bounds_per_level; /**< For each level from the nca to the parent of the current element
* we store for each face the lower and upper bounds of the owners at
* this face. We also store bounds for the element's owners.
* Each entry is an array of 2 * (max_num_faces + 1) integers,
| * face_0 low | face_0 high | ... | face_n low | face_n high | owner low | owner high |
*/
sc_array_t face_owners; /**< Temporary storage for all owners at a leaf's face. */
const t8_scheme *scheme; /**< The scheme. */
t8_gloidx_t gtreeid; /**< The global tree id. */
int level_nca; /**< The refinement level of the root element in the search.
At position element_level - level_nca in bounds_per_level are the bounds
for the parent of element. */
int max_num_faces; /**< The maximum number of faces. */
t8_eclass_t eclass; /**< The element class. */
#if T8_ENABLE_DEBUG
t8_locidx_t left_out; /**< Count the elements for which we skip the search */
#endif
} t8_forest_ghost_boundary_data_t;
/**
* This function is used as callback search function within \ref t8_forest_search to check whether the neighbors of
* the current element are on another rank. If so, add the element to the ghost structures.
*
* \param[in] forest The forest.
* \param[in] ltreeid Local ID of the ghost tree.
* \param[in] element The current element.
* \param[in] is_leaf Switch indicating whether \a element is a leaf.
* \param[in] leaves The array of leaves (used for debug purposes only).
* \param[in] tree_leaf_index If the element is a leaf, its tree-local id.
*
* \return 0 if the element and its face neighbors are completely owned by the current rank; 1 otherwise.
*/
static int
t8_forest_ghost_search_boundary (t8_forest_t forest, t8_locidx_t ltreeid, const t8_element_t *element,
const int is_leaf, [[maybe_unused]] const t8_element_array_t *leaves,
const t8_locidx_t tree_leaf_index)
{
t8_forest_ghost_boundary_data_t *data = (t8_forest_ghost_boundary_data_t *) t8_forest_get_user_data (forest);
int num_faces, iface, faces_totally_owned, level;
int parent_face;
int lower, upper, *bounds, *new_bounds, parent_lower, parent_upper;
int el_lower, el_upper;
int element_is_owned, iproc, remote_rank;
/* First part: the search enters a new tree, we need to reset the user_data */
if (t8_forest_global_tree_id (forest, ltreeid) != data->gtreeid) {
int max_num_faces;
/* The search has entered a new tree, store its eclass and element scheme */
data->gtreeid = t8_forest_global_tree_id (forest, ltreeid);
data->eclass = t8_forest_get_eclass (forest, ltreeid);
data->scheme = t8_forest_get_scheme (forest);
data->level_nca = data->scheme->element_get_level (data->eclass, element);
data->max_num_faces = data->scheme->element_get_max_num_faces (data->eclass, element);
max_num_faces = data->max_num_faces;
sc_array_reset (&data->bounds_per_level);
sc_array_init_size (&data->bounds_per_level, 2 * (max_num_faces + 1) * sizeof (int), 1);
/* Set the (imaginary) owner bounds for the parent of the root element */
bounds = (int *) sc_array_index (&data->bounds_per_level, 0);
for (iface = 0; iface < max_num_faces + 1; iface++) {
bounds[iface * 2] = 0;
bounds[iface * 2 + 1] = forest->mpisize - 1;
}
/* TODO: compute bounds */
}
/* The level of the current element */
level = data->scheme->element_get_level (data->eclass, element);
/* Get a pointer to the owner at face bounds of this element, if there doesnt exist
* an entry for this in the bounds_per_level array yet, we allocate it */
T8_ASSERT (level >= data->level_nca);
if (data->bounds_per_level.elem_count <= (size_t) level - data->level_nca + 1) {
T8_ASSERT (data->bounds_per_level.elem_count == (size_t) level - data->level_nca + 1);
new_bounds = (int *) sc_array_push (&data->bounds_per_level);
}
else {
new_bounds = (int *) sc_array_index (&data->bounds_per_level, level - data->level_nca + 1);
}
/* Get a pointer to the owner bounds of the parent */
bounds = (int *) sc_array_index (&data->bounds_per_level, level - data->level_nca);
/* Get bounds for the element's parent's owners */
parent_lower = bounds[2 * data->max_num_faces];
parent_upper = bounds[2 * data->max_num_faces + 1];
/* Temporarily store them to serve as bounds for this element's owners */
el_lower = parent_lower;
el_upper = parent_upper;
/* Compute bounds for the element's owners */
t8_forest_element_owners_bounds (forest, data->gtreeid, element, data->eclass, &el_lower, &el_upper);
/* Set these as the new bounds */
new_bounds[2 * data->max_num_faces] = el_lower;
new_bounds[2 * data->max_num_faces + 1] = el_upper;
element_is_owned = (el_lower == el_upper);
num_faces = data->scheme->element_get_num_faces (data->eclass, element);
faces_totally_owned = 1;
/* TODO: we may not carry on with the face computations if the element is not
* totally owned and immediately return 1. However, how do we set the bounds for
* the face owners then?
*/
for (iface = 0; iface < num_faces; iface++) {
/* Compute the face number of the parent to reuse the bounds */
parent_face = data->scheme->element_face_get_parent_face (data->eclass, element, iface);
if (parent_face >= 0) {
/* This face was also a face of the parent, we reuse the computed bounds */
lower = bounds[parent_face * 2];
upper = bounds[parent_face * 2 + 1];
}
else {
/* this is an inner face, thus the face owners must be owners of the parent element */
lower = parent_lower;
upper = parent_upper;
}
if (!is_leaf) {
/* The element is not a leaf, we compute bounds for the face neighbor owners,
* if all face neighbors are owned by this rank, and the element is completely
* owned, then we do not continue the search. */
/* Compute the owners of the neighbor at this face of the element */
t8_forest_element_owners_at_neigh_face_bounds (forest, ltreeid, element, iface, &lower, &upper);
/* Store the new bounds at the entry for this element */
new_bounds[iface * 2] = lower;
new_bounds[iface * 2 + 1] = upper;
if (lower != upper or lower != forest->mpirank) {
faces_totally_owned = 0;
}
}
else {
/* The element is a leaf, we compute all of its face neighbor owners
* and add the element as a remote element to all of them. */
sc_array_resize (&data->face_owners, 2);
/* The first and second entry in the face_owners array serve as lower and upper bound */
*(int *) sc_array_index (&data->face_owners, 0) = lower;
*(int *) sc_array_index (&data->face_owners, 1) = upper;
t8_forest_element_owners_at_neigh_face (forest, ltreeid, element, iface, &data->face_owners);
/*TODO: add as remotes */
for (iproc = 0; iproc < (int) data->face_owners.elem_count; iproc++) {
remote_rank = *(int *) sc_array_index (&data->face_owners, iproc);
if (remote_rank != forest->mpirank) {
t8_ghost_add_remote (forest, forest->ghosts, remote_rank, ltreeid, element, tree_leaf_index);
}
}
}
} /* end face loop */
if (faces_totally_owned && element_is_owned) {
/* The element only has local descendants and all of its face neighbors are local as well.
* We do not continue the search */
#if T8_ENABLE_DEBUG
if (tree_leaf_index < 0) {
data->left_out += t8_element_array_get_count (leaves);
}
#endif
return 0;
}
/* Continue the top-down search if this element or its face neighbors are not completely owned by the rank. */
return 1;
}
/**
* Fill the remote ghosts of a ghost structure.
* We iterate through all elements and check if their neighbors
* lie on remote processes. If so, we add the element to the
* remote_ghosts array of ghost.
* We also fill the remote_processes here.
*
* \param[in,out] forest the forest.
*/
static void
t8_forest_ghost_fill_remote_v3 (t8_forest_t forest)
{
t8_forest_ghost_boundary_data_t data;
void *store_user_data = NULL;
/* Start with invalid entries in the user data.
* These are set in t8_forest_ghost_search_boundary each time a new tree is entered */
data.eclass = T8_ECLASS_COUNT;
data.gtreeid = -1;
data.scheme = NULL;
#if T8_ENABLE_DEBUG
data.left_out = 0;
#endif
sc_array_init (&data.face_owners, sizeof (int));
/* This is a dummy init, since we call sc_array_reset in ghost_search_boundary
* and we should not call sc_array_reset on a non-initialized array */
sc_array_init (&data.bounds_per_level, 1);
/* Store any user data that may reside on the forest */
store_user_data = t8_forest_get_user_data (forest);
/* Set the user data for the search routine */
t8_forest_set_user_data (forest, &data);
/* Loop over the trees of the forest */
t8_forest_search (forest, t8_forest_ghost_search_boundary, NULL, NULL);
/* Reset the user data from before search */
t8_forest_set_user_data (forest, store_user_data);
/* Reset the data arrays */
sc_array_reset (&data.face_owners);
sc_array_reset (&data.bounds_per_level);
#if T8_ENABLE_DEBUG
#endif
}
/**
* Fill the remote ghosts of a ghost structure.
* We iterate through all elements and check if their neighbors
* lie on remote processes. If so, we add the element to the
* remote_ghosts array of ghost.
* We also fill the remote_processes here.
* If ghost_method is 0, then we assume a balanced forest and
* construct the remote processes by looking at the half neighbors of an element.
* Otherwise, we use the owners_at_face method.
*
* \param[in] forest The forest.
* \param[in] ghost The forest's ghost.
* \param[in] ghost_method Switch indicating the ghost type.
*/
static void
t8_forest_ghost_fill_remote (t8_forest_t forest, t8_forest_ghost_t ghost, int ghost_method)
{
t8_element_t **half_neighbors = NULL;
t8_locidx_t num_local_trees, num_tree_elems;
t8_locidx_t itree, ielem;
t8_tree_t tree;
t8_eclass_t last_class;
t8_gloidx_t neighbor_tree;
int iface, num_faces;
int num_face_children, max_num_face_children = 0;
int ichild, owner;
sc_array_t owners, tree_owners;
int is_atom;
const t8_scheme *scheme = t8_forest_get_scheme (forest);
last_class = T8_ECLASS_COUNT;
num_local_trees = t8_forest_get_num_local_trees (forest);
if (ghost_method != 0) {
sc_array_init (&owners, sizeof (int));
sc_array_init (&tree_owners, sizeof (int));
}
/* Loop over the trees of the forest */
for (itree = 0; itree < num_local_trees; itree++) {
/* Get a pointer to the tree, the class of the tree, the
* scheme associated to the class and the number of elements in this tree. */
tree = t8_forest_get_tree (forest, itree);
const t8_eclass_t tree_class = t8_forest_get_tree_class (forest, itree);
/* Loop over the elements of this tree */
num_tree_elems = t8_forest_get_tree_leaf_element_count (tree);
for (ielem = 0; ielem < num_tree_elems; ielem++) {
/* Get the element of the tree */
const t8_element_t *elem = t8_forest_get_tree_leaf_element (tree, ielem);
num_faces = scheme->element_get_num_faces (tree_class, elem);
if (scheme->element_get_level (tree_class, elem) == scheme->get_maxlevel (tree_class)) {
/* flag to decide whether this element is at the maximum level */
is_atom = 1;
}
else {
is_atom = 0;
}
for (iface = 0; iface < num_faces; iface++) {
/* Get the element class of the neighbor tree */
const t8_eclass_t neigh_class = t8_forest_element_neighbor_eclass (forest, itree, elem, iface);
if (neigh_class != T8_ECLASS_INVALID) { // Only continue if a face neighbor exists
if (ghost_method == 0) {
/* Use half neighbors */
/* Get the number of face children of the element at this face */
num_face_children = scheme->element_get_num_face_children (tree_class, elem, iface);
/* regrow the half_neighbors array if necessary.
* We also need to reallocate it, if the element class of the neighbor
* changes */
if (max_num_face_children < num_face_children || last_class != neigh_class) {
half_neighbors = T8_ALLOC (t8_element_t *, num_face_children);
/* Allocate memory for the half size face neighbors */
scheme->element_new (neigh_class, num_face_children, half_neighbors);
max_num_face_children = num_face_children;
last_class = neigh_class;
}
if (!is_atom) {
/* Construct each half size neighbor */
neighbor_tree = t8_forest_element_half_face_neighbors (forest, itree, elem, half_neighbors, neigh_class,
iface, num_face_children, NULL);
}
else {
int dummy_neigh_face;
/* This element has maximum level, we only construct its neighbor */
neighbor_tree = t8_forest_element_face_neighbor (forest, itree, elem, half_neighbors[0], neigh_class,
iface, &dummy_neigh_face);
}
if (neighbor_tree >= 0) {
/* If there exist face neighbor elements (we are not at a domain boundary */
/* Find the owner process of each face_child */
for (ichild = 0; ichild < num_face_children; ichild++) {
/* find the owner */
owner = t8_forest_element_find_owner (forest, neighbor_tree, half_neighbors[ichild], neigh_class);
T8_ASSERT (0 <= owner && owner < forest->mpisize);
if (owner != forest->mpirank) {
/* Add the element as a remote element */
t8_ghost_add_remote (forest, ghost, owner, itree, elem, ielem);
}
}
}
scheme->element_destroy (neigh_class, num_face_children, half_neighbors);
T8_FREE (half_neighbors);
} /* end ghost_method 0 */
else {
size_t iowner;
/* Construct the owners at the face of the neighbor element */
t8_forest_element_owners_at_neigh_face (forest, itree, elem, iface, &owners);
/* Iterate over all owners and if any is not the current process,
* add this element as remote */
for (iowner = 0; iowner < owners.elem_count; iowner++) {
owner = *(int *) sc_array_index (&owners, iowner);
T8_ASSERT (0 <= owner && owner < forest->mpisize);
if (owner != forest->mpirank) {
/* Add the element as a remote element */
t8_ghost_add_remote (forest, ghost, owner, itree, elem, ielem);
}
}
sc_array_truncate (&owners);
}
}
} /* end face loop */
} /* end element loop */
} /* end tree loop */
if (forest->profile != NULL) {
/* If profiling is enabled, we count the number of remote processes. */
forest->profile->ghosts_remotes = ghost->remote_processes->elem_count;
}
/* Clean-up memory */
if (ghost_method != 0) {
sc_array_reset (&owners);
sc_array_reset (&tree_owners);
}
}
/**
* Begin sending the ghost elements from the remote ranks
* using non-blocking communication.
* Afterwards,
* t8_forest_ghost_send_end
* must be called to end the communication.
*
* \param[in] forest The forest.
* \param[in] ghost The forest's ghost.
* \param[out] requests The send requests as an array of pointers to sc_MPI_Requests.
*
* \return An array of mpi_send_info_t, holding one entry for each remote rank.
*/
static t8_ghost_mpi_send_info_t *
t8_forest_ghost_send_start (t8_forest_t forest, t8_forest_ghost_t ghost, sc_MPI_Request **requests)
{
int proc_index, remote_rank;
int num_remotes;
size_t remote_index;
t8_ghost_remote_t *remote_entry;
sc_array_t *remote_trees;
t8_ghost_remote_tree_t *remote_tree = NULL;
t8_ghost_mpi_send_info_t *send_info, *current_send_info;
char *current_buffer;
size_t bytes_written, element_bytes, element_count, element_size;
#if T8_ENABLE_DEBUG
size_t acc_el_count = 0;
#endif
int mpiret;
/* Allocate a send_buffer for each remote rank */
num_remotes = ghost->remote_processes->elem_count;
send_info = T8_ALLOC (t8_ghost_mpi_send_info_t, num_remotes);
*requests = T8_ALLOC (sc_MPI_Request, num_remotes);
/* Loop over all remote processes */
for (proc_index = 0; proc_index < (int) ghost->remote_processes->elem_count; proc_index++) {
current_send_info = send_info + proc_index;
/* Get the rank of the current remote process. */
remote_rank = *(int *) sc_array_index_int (ghost->remote_processes, proc_index);
t8_debugf ("Filling send buffer for process %i\n", remote_rank);
/* initialize the send_info for the current rank */
current_send_info->recv_rank = remote_rank;
current_send_info->num_bytes = 0;
current_send_info->request = *requests + proc_index;
/* Lookup the ghost elements for the first tree of this remote */
remote_entry = t8_forest_ghost_get_remote (forest, remote_rank);
T8_ASSERT (remote_entry->remote_rank == remote_rank);
/* Loop over all trees of the remote rank and count the bytes */
/* At first we store the number of remote trees in the buffer */
current_send_info->num_bytes += sizeof (size_t);
/* add padding before the eclass */
current_send_info->num_bytes += T8_ADD_PADDING (current_send_info->num_bytes);
/* TODO: put this in a function */
/* TODO: Use remote_entry to count the number of bytes while inserting
* the remote ghosts. */
remote_trees = &remote_entry->remote_trees;
for (remote_index = 0; remote_index < remote_trees->elem_count; remote_index++) {
/* Get the next remote tree. */
remote_tree = (t8_ghost_remote_tree_t *) sc_array_index (remote_trees, remote_index);
/* We will store the global tree id, the element class and the list