-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathp6est.c
2634 lines (2278 loc) · 83.7 KB
/
p6est.c
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 p4est.
p4est is a C library to manage a collection (a forest) of multiple
connected adaptive quadtrees or octrees in parallel.
Copyright (C) 2010 The University of Texas System
Additional copyright (C) 2011 individual authors
Written by Carsten Burstedde, Lucas C. Wilcox, and Tobin Isaac
p4est 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.
p4est 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 p4est; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <p6est.h>
#include <p6est_ghost.h>
#include <p6est_profile.h>
#include <p6est_communication.h>
#include <p4est_lnodes.h>
#include <p8est.h>
#include <p4est_extended.h>
#include <p4est_algorithms.h>
#include <sc_containers.h>
#include <p4est_communication.h>
#include <sc_io.h>
#include <p6est_extended.h>
/* htonl is in either of these two */
#ifdef P4EST_HAVE_ARPA_NET_H
#include <arpa/inet.h>
#endif
#ifdef P4EST_HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef P4EST_HAVE_ZLIB
#include <zlib.h>
#endif
p6est_connectivity_t *
p6est_connectivity_new (p4est_connectivity_t * conn4,
double *top_vertices, double height[3])
{
p6est_connectivity_t *conn = P4EST_ALLOC (p6est_connectivity_t, 1);
conn->conn4 = p4est_connectivity_new_copy (conn4->num_vertices,
conn4->num_trees,
conn4->num_corners,
conn4->vertices,
conn4->tree_to_vertex,
conn4->tree_to_tree,
conn4->tree_to_face,
conn4->tree_to_corner,
conn4->ctt_offset,
conn4->corner_to_tree,
conn4->corner_to_corner);
if (top_vertices != NULL) {
conn->top_vertices = P4EST_ALLOC (double, 3 * conn4->num_vertices);
memcpy (conn->top_vertices, top_vertices,
3 * conn4->num_vertices * sizeof (double));
}
else {
conn->top_vertices = NULL;
P4EST_ASSERT (height != NULL);
conn->height[0] = height[0];
conn->height[1] = height[1];
conn->height[2] = height[2];
}
return conn;
}
void
p6est_connectivity_destroy (p6est_connectivity_t * conn)
{
p4est_connectivity_destroy (conn->conn4);
if (conn->top_vertices != NULL) {
P4EST_FREE (conn->top_vertices);
}
P4EST_FREE (conn);
}
void
p6est_tree_get_vertices (p6est_connectivity_t * conn,
p4est_topidx_t which_tree, double vertices[24])
{
p4est_connectivity_t *conn4 = conn->conn4;
const double *btv = conn4->vertices;
const double *ttv = conn->top_vertices;
int i, j, k;
double *height = conn->height;
double zerooff[3] = { 0., 0., 0. };
P4EST_ASSERT (conn4->num_vertices > 0);
P4EST_ASSERT (btv != NULL);
P4EST_ASSERT (which_tree >= 0 && which_tree < conn4->num_trees);
P4EST_ASSERT (vertices != NULL);
for (k = 0; k < 2; k++) {
const double *verts = k ? ttv : btv;
const double *off = (verts != NULL) ? zerooff : height;
if (verts == NULL) {
verts = btv;
}
for (i = 0; i < P4EST_CHILDREN; i++) {
p4est_topidx_t vert;
vert = conn4->tree_to_vertex[P4EST_CHILDREN * which_tree + i];
for (j = 0; j < 3; j++) {
vertices[3 * (P4EST_CHILDREN * k + i) + j] = verts[3 * vert + j] +
off[j];
}
}
}
}
void
p6est_qcoord_to_vertex (p6est_connectivity_t * conn,
p4est_topidx_t treeid,
p4est_qcoord_t x, p4est_qcoord_t y,
p4est_qcoord_t z, double vxyz[3])
{
double bottom[3], top[3];
double eta = (double) z / (double) P4EST_ROOT_LEN;
p4est_qcoord_to_vertex (conn->conn4, treeid, x, y, bottom);
if (conn->top_vertices != NULL) {
double *orig = conn->conn4->vertices;
conn->conn4->vertices = conn->top_vertices;
p4est_qcoord_to_vertex (conn->conn4, treeid, x, y, top);
conn->conn4->vertices = orig;
}
else {
top[0] = bottom[0] + conn->height[0];
top[1] = bottom[1] + conn->height[1];
top[2] = bottom[2] + conn->height[2];
}
vxyz[0] = (1. - eta) * bottom[0] + eta * top[0];
vxyz[1] = (1. - eta) * bottom[1] + eta * top[1];
vxyz[2] = (1. - eta) * bottom[2] + eta * top[2];
}
size_t
p6est_connectivity_memory_used (p6est_connectivity_t * conn)
{
return
p4est_connectivity_memory_used (conn->conn4) +
conn->top_vertices == NULL ? 0 :
(conn->conn4->num_vertices * 3 * sizeof (double));
}
int
p6est_connectivity_extra_sink (p6est_connectivity_t * conn,
sc_io_sink_t * sink)
{
int retval;
size_t u64z, tcount;
double *v;
uint64_t num_vertices;
u64z = sizeof (uint64_t);
num_vertices = (conn->top_vertices != NULL) ?
(uint64_t) conn->conn4->num_vertices : 0;
retval = sc_io_sink_write (sink, &num_vertices, 1 * u64z);
if (conn->top_vertices != NULL) {
tcount = (size_t) (3 * conn->conn4->num_vertices);
v = conn->top_vertices;
}
else {
tcount = 3;
v = conn->height;
}
retval = retval || sc_io_sink_write (sink, v, tcount * sizeof (double));
return retval;
}
p6est_connectivity_t *
p6est_connectivity_extra_source (p4est_connectivity_t * conn4,
sc_io_source_t * source)
{
p6est_connectivity_t *conn;
int retval;
size_t u64z;
uint64_t num_vertices;
double *top_vertices;
double height[3];
u64z = sizeof (uint64_t);
retval = sc_io_source_read (source, &num_vertices, 1 * u64z, NULL);
if (retval) {
return NULL;
}
if (num_vertices > 0) {
height[0] = height[1] = height[2] = 0.;
if (num_vertices != (uint64_t) conn4->num_vertices) {
return NULL;
}
top_vertices = P4EST_ALLOC (double, 3 * num_vertices);
retval = sc_io_source_read (source, top_vertices,
3 * num_vertices * sizeof (double), NULL);
if (retval) {
P4EST_FREE (top_vertices);
return NULL;
}
}
else {
top_vertices = NULL;
retval = sc_io_source_read (source, height, 3 * sizeof (double), NULL);
if (retval) {
return NULL;
}
}
conn = P4EST_ALLOC (p6est_connectivity_t, 1);
conn->conn4 = conn4;
conn->top_vertices = top_vertices;
conn->height[0] = height[0];
conn->height[1] = height[1];
conn->height[2] = height[2];
return conn;
}
int
p6est_connectivity_sink (p6est_connectivity_t * conn, sc_io_sink_t * sink)
{
int retval;
retval = p4est_connectivity_sink (conn->conn4, sink);
retval = retval || p6est_connectivity_extra_sink (conn, sink);
return retval;
}
p6est_connectivity_t *
p6est_connectivity_source (sc_io_source_t * source)
{
p4est_connectivity_t *conn4;
conn4 = p4est_connectivity_source (source);
if (conn4 == NULL) {
return NULL;
}
return p6est_connectivity_extra_source (conn4, source);
}
int
p6est_connectivity_save (const char *filename, p6est_connectivity_t * conn)
{
int retval;
sc_io_sink_t *sink;
sink = sc_io_sink_new (SC_IO_TYPE_FILENAME, SC_IO_MODE_WRITE,
SC_IO_ENCODE_NONE, filename);
if (sink == NULL) {
return -1;
}
/* Close file even on earlier write error */
retval = p6est_connectivity_sink (conn, sink);
retval = sc_io_sink_destroy (sink) || retval;
return retval;
}
p6est_connectivity_t *
p6est_connectivity_load (const char *filename, size_t * bytes)
{
int retval;
size_t bytes_in;
sc_io_source_t *source;
p6est_connectivity_t *conn;
source = sc_io_source_new (SC_IO_TYPE_FILENAME,
SC_IO_ENCODE_NONE, filename);
if (source == NULL) {
return NULL;
}
/* Get byte length and close file even on earlier read error */
conn = p6est_connectivity_source (source);
retval = sc_io_source_complete (source, &bytes_in, NULL) || conn == NULL;
retval = sc_io_source_destroy (source) || retval;
if (retval) {
if (conn != NULL) {
p6est_connectivity_destroy (conn);
}
return NULL;
}
if (bytes != NULL) {
*bytes = bytes_in;
}
return conn;
}
size_t
p6est_memory_used (p6est_t * p6est)
{
size_t size;
size = p4est_memory_used (p6est->columns);
size += sc_array_memory_used (p6est->layers, 1);
if (p6est->data_size > 0) {
size += sc_mempool_memory_used (p6est->user_data_pool);
}
size += sc_mempool_memory_used (p6est->layer_pool);
return size;
}
typedef struct p6est_init_data
{
int min_zlevel;
int num_zroot;
sc_array_t *layers;
p6est_init_t init_fn;
void *user_pointer;
}
p6est_init_data_t;
static void
p6est_init_fn (p4est_t * p4est, p4est_topidx_t which_tree,
p4est_quadrant_t * col)
{
p6est_t *p6est = (p6est_t *) p4est->user_pointer;
p6est_init_data_t *init_data = (p6est_init_data_t *) p6est->user_pointer;
int log_zroot = SC_LOG2_32 (init_data->num_zroot - 1) + 1;
int nlayers =
(1 << (init_data->min_zlevel - log_zroot)) * init_data->num_zroot;
sc_array_t *layers = init_data->layers;
size_t incount = layers->elem_count, zz;
size_t last = incount + nlayers;
p2est_quadrant_t *layer;
/* we have to sneak the user_pointer in */
p6est->user_pointer = init_data->user_pointer;
P6EST_COLUMN_SET_RANGE (col, layers->elem_count, last);
layer = (p2est_quadrant_t *) sc_array_push_count (layers, nlayers);
for (zz = incount; zz < last; zz++, layer++) {
P2EST_QUADRANT_INIT (layer);
layer->level = init_data->min_zlevel;
layer->z = (zz - incount) * P4EST_QUADRANT_LEN (layer->level);
p6est_layer_init_data (p6est, which_tree, col, layer, init_data->init_fn);
}
/* we have to sneak the user_pointer out */
p6est->user_pointer = (void *) init_data;
}
p6est_t *
p6est_new_ext (sc_MPI_Comm mpicomm, p6est_connectivity_t * connectivity,
p4est_locidx_t min_quadrants, int min_level, int min_zlevel,
int num_zroot,
int fill_uniform, size_t data_size, p6est_init_t init_fn,
void *user_pointer)
{
p6est_t *p6est = P4EST_ALLOC (p6est_t, 1);
p4est_t *p4est;
sc_array_t *layers;
sc_mempool_t *user_data_pool;
p6est_init_data_t init_data;
int mpiret, num_procs, rank;
int log_zroot = SC_LOG2_32 (num_zroot - 1) + 1;
int quadpercol =
(1 << (min_zlevel - log_zroot)) * num_zroot;
int i;
P4EST_GLOBAL_PRODUCTIONF
("Into p6est_new with min layers %lld z-level %d\n",
(long long) min_quadrants, SC_MAX (min_zlevel, 0));
p4est_log_indent_push ();
layers = sc_array_new (sizeof (p2est_quadrant_t));
if (data_size > 0) {
user_data_pool = sc_mempool_new (data_size);
}
else {
user_data_pool = NULL;
}
p6est->layer_pool = sc_mempool_new (sizeof (p2est_quadrant_t));
p6est->data_size = data_size;
p6est->user_pointer = user_pointer;
p6est->connectivity = connectivity;
p6est->layers = layers;
p6est->user_data_pool = user_data_pool;
p6est->root_len = num_zroot * P4EST_QUADRANT_LEN (log_zroot);
p6est_comm_parallel_env_assign (p6est, mpicomm);
mpicomm = p6est->mpicomm;
mpiret = sc_MPI_Comm_size (mpicomm, &num_procs);
SC_CHECK_MPI (mpiret);
mpiret = sc_MPI_Comm_rank (mpicomm, &rank);
SC_CHECK_MPI (mpiret);
P4EST_ASSERT (min_zlevel <= P4EST_QMAXLEVEL);
P4EST_ASSERT (num_zroot >= 1);
P4EST_ASSERT (min_zlevel >= log_zroot);
init_data.min_zlevel = min_zlevel;
init_data.num_zroot = num_zroot;
init_data.layers = layers;
init_data.init_fn = init_fn;
init_data.user_pointer = user_pointer;
p6est->user_pointer = &init_data;
P4EST_GLOBAL_VERBOSE ("Creating p4est for columns\n");
p4est =
p4est_new_ext (mpicomm, connectivity->conn4, min_quadrants / quadpercol,
min_level, fill_uniform, 0, p6est_init_fn, (void *) p6est);
p6est->user_pointer = user_pointer;
p6est->columns = p4est;
p6est->global_first_layer = P4EST_ALLOC (p4est_gloidx_t, num_procs + 1);
for (i = 0; i <= num_procs; i++) {
p6est->global_first_layer[i] =
quadpercol * p4est->global_first_quadrant[i];
}
/* print more statistics */
P4EST_VERBOSEF ("total local layers %lld\n",
(long long) layers->elem_count);
p4est_log_indent_pop ();
P4EST_GLOBAL_PRODUCTIONF
("Done p6est_new with %lld total layers in %lld total columns\n",
(long long) p6est->global_first_layer[p6est->mpisize],
(long long) p6est->columns->global_num_quadrants);
return p6est;
}
p6est_t *
p6est_new (sc_MPI_Comm mpicomm, p6est_connectivity_t * connectivity,
size_t data_size, p6est_init_t init_fn, void *user_pointer)
{
return p6est_new_ext (mpicomm, connectivity, 0, 0, 0, 1, 1,
data_size, init_fn, user_pointer);
}
p6est_t *
p6est_new_from_p4est (p4est_t * p4est, double *top_vertices, double height[3],
int min_zlevel, size_t data_size, p6est_init_t init_fn,
void *user_pointer)
{
p6est_t *p6est = P4EST_ALLOC (p6est_t, 1);
p6est_connectivity_t *conn;
sc_array_t *layers;
sc_mempool_t *user_data_pool;
p6est_init_data_t init_data;
int num_procs;
int quadpercol = (1 << min_zlevel);
int i;
P4EST_GLOBAL_PRODUCTIONF
("Into p6est_new_from_p4est with z-level %d\n", SC_MAX (min_zlevel, 0));
p4est_log_indent_push ();
layers = sc_array_new (sizeof (p2est_quadrant_t));
if (data_size > 0) {
user_data_pool = sc_mempool_new (data_size);
}
else {
user_data_pool = NULL;
}
conn = p6est_connectivity_new (p4est->connectivity, top_vertices, height);
p6est->layer_pool = sc_mempool_new (sizeof (p2est_quadrant_t));
p6est->data_size = data_size;
p6est->user_pointer = user_pointer;
p6est->connectivity = conn;
p6est->layers = layers;
p6est->user_data_pool = user_data_pool;
p6est->columns = p4est_copy (p4est, 0);
p6est->columns->connectivity = conn->conn4;
p6est->root_len = P4EST_ROOT_LEN;
p6est_comm_parallel_env_assign (p6est, p4est->mpicomm);
num_procs = p6est->mpisize;
P4EST_ASSERT (min_zlevel <= P4EST_QMAXLEVEL);
init_data.min_zlevel = min_zlevel;
init_data.layers = layers;
init_data.init_fn = init_fn;
init_data.user_pointer = user_pointer;
init_data.num_zroot = 1;
p6est->user_pointer = &init_data;
p4est_reset_data (p6est->columns, 0, p6est_init_fn, (void *) p6est);
p6est->user_pointer = user_pointer;
p6est->global_first_layer = P4EST_ALLOC (p4est_gloidx_t, num_procs + 1);
for (i = 0; i <= num_procs; i++) {
p6est->global_first_layer[i] =
quadpercol * p4est->global_first_quadrant[i];
}
/* print more statistics */
P4EST_VERBOSEF ("total local layers %lld\n",
(long long) layers->elem_count);
p4est_log_indent_pop ();
P4EST_GLOBAL_PRODUCTIONF
("Done p6est_new with %lld total layers in %lld total columns\n",
(long long) p6est->global_first_layer[p6est->mpisize],
(long long) p6est->columns->global_num_quadrants);
return p6est;
}
void
p6est_destroy (p6est_t * p6est)
{
sc_array_t *layers = p6est->layers;
size_t layercount = layers->elem_count;
size_t zz;
for (zz = 0; zz < layercount; zz++) {
p2est_quadrant_t *layer = p2est_quadrant_array_index (layers, zz);
p6est_layer_free_data (p6est, layer);
}
sc_array_destroy (p6est->layers);
if (p6est->columns) {
p4est_destroy (p6est->columns);
}
if (p6est->user_data_pool != NULL) {
sc_mempool_destroy (p6est->user_data_pool);
}
sc_mempool_destroy (p6est->layer_pool);
p6est_comm_parallel_env_release (p6est);
P4EST_FREE (p6est->global_first_layer);
P4EST_FREE (p6est);
}
p6est_t *
p6est_copy (p6est_t * input, int copy_data)
{
return p6est_copy_ext (input, copy_data, 0);
}
p6est_t *
p6est_copy_ext (p6est_t * input, int copy_data, int duplicate_comm)
{
p6est_t *p6est = P4EST_ALLOC (p6est_t, 1);
size_t zz, qcount = input->layers->elem_count;
memcpy (p6est, input, sizeof (p6est_t));
/* set parallel environment */
p6est_comm_parallel_env_assign (p6est, input->mpicomm);
if (duplicate_comm) {
p6est_comm_parallel_env_duplicate (p6est);
}
p6est->layers =
sc_array_new_size (input->layers->elem_size, input->layers->elem_count);
sc_array_copy (p6est->layers, input->layers);
p6est->columns = p4est_copy (input->columns, 0);
p4est_comm_parallel_env_assign (p6est->columns, p6est->mpicomm);
p6est->columns->user_pointer = p6est;
if (copy_data && p6est->data_size > 0) {
p6est->user_data_pool = sc_mempool_new (p6est->data_size);
}
else {
p6est->data_size = 0;
}
p6est->layer_pool = sc_mempool_new (sizeof (p2est_quadrant_t));
if (p6est->data_size > 0) {
P4EST_ASSERT (copy_data);
for (zz = 0; zz < qcount; zz++) {
p2est_quadrant_t *inlayer =
p2est_quadrant_array_index (input->layers, zz);
p2est_quadrant_t *outlayer =
p2est_quadrant_array_index (p6est->layers, zz);
outlayer->p.user_data = sc_mempool_alloc (p6est->user_data_pool);
memcpy (outlayer->p.user_data, inlayer->p.user_data, p6est->data_size);
}
}
p6est->global_first_layer =
P4EST_ALLOC (p4est_gloidx_t, p6est->mpisize + 1);
memcpy (p6est->global_first_layer, input->global_first_layer,
(p6est->mpisize + 1) * sizeof (p4est_gloidx_t));
return p6est;
}
void
p6est_reset_data (p6est_t * p6est, size_t data_size, p6est_init_t init_fn,
void *user_pointer)
{
int doresize;
size_t zz, zy, first, last;
p4est_topidx_t jt;
p4est_quadrant_t *col;
p2est_quadrant_t *q;
p4est_tree_t *tree;
sc_array_t *tquadrants;
doresize = (p6est->data_size != data_size);
p6est->data_size = data_size;
p6est->user_pointer = user_pointer;
if (doresize) {
if (p6est->user_data_pool != NULL) {
sc_mempool_destroy (p6est->user_data_pool);
}
if (p6est->data_size > 0) {
p6est->user_data_pool = sc_mempool_new (p6est->data_size);
}
else {
p6est->user_data_pool = NULL;
}
}
for (jt = p6est->columns->first_local_tree;
jt <= p6est->columns->last_local_tree; ++jt) {
tree = p4est_tree_array_index (p6est->columns->trees, jt);
tquadrants = &tree->quadrants;
for (zz = 0; zz < tquadrants->elem_count; ++zz) {
col = p4est_quadrant_array_index (tquadrants, zz);
P6EST_COLUMN_GET_RANGE (col, &first, &last);
for (zy = first; zy < last; zy++) {
q = p2est_quadrant_array_index (p6est->layers, zy);
if (doresize) {
if (p6est->data_size > 0) {
q->p.user_data = sc_mempool_alloc (p6est->user_data_pool);
}
else {
q->p.user_data = NULL;
}
}
if (init_fn != NULL) {
init_fn (p6est, jt, col, q);
}
}
}
}
}
void
p6est_save (const char *filename, p6est_t * p6est, int save_data)
{
p6est_save_ext (filename, p6est, save_data, 1);
}
void
p6est_save_ext (const char *filename, p6est_t * p6est,
int save_data, int save_partition)
{
FILE *file;
int rank = p6est->mpirank;
long fpos = -1, foffset;
int align = 32;
uint64_t u64a;
sc_io_sink_t *sink;
int retval, mpiret;
p4est_t *savecolumns;
#ifdef P4EST_MPIIO_WRITE
sc_MPI_File mpifile;
sc_MPI_Offset mpipos;
sc_MPI_Offset mpithis;
#else
long fthis;
sc_MPI_Status mpistatus;
#endif
int num_procs = p6est->mpisize;
size_t comb_size, data_size = p6est->data_size;
size_t zz, nlayers = p6est->layers->elem_count;
char *lbuf, *bp;
P4EST_GLOBAL_PRODUCTION ("Into p6est_save\n");
p4est_log_indent_push ();
savecolumns = p4est_copy (p6est->columns, 0);
p4est_reset_data (savecolumns, 2 * sizeof (p4est_locidx_t), NULL, NULL);
if (!data_size) {
save_data = 0;
}
if (!save_data) {
data_size = 0;
}
comb_size = 2 * sizeof (p4est_qcoord_t) + data_size;
{
p4est_topidx_t jt;
p4est_tree_t *tree, *savetree;
sc_array_t *tquadrants, *savetquadrants;
p4est_quadrant_t *col, *savecol;
size_t first, last;
p4est_locidx_t lfirst, llast;
for (jt = p6est->columns->first_local_tree;
jt <= p6est->columns->last_local_tree; ++jt) {
tree = p4est_tree_array_index (p6est->columns->trees, jt);
savetree = p4est_tree_array_index (savecolumns->trees, jt);
tquadrants = &tree->quadrants;
savetquadrants = &savetree->quadrants;
for (zz = 0; zz < tquadrants->elem_count; ++zz) {
p4est_locidx_t *savedata;
col = p4est_quadrant_array_index (tquadrants, zz);
savecol = p4est_quadrant_array_index (savetquadrants, zz);
P6EST_COLUMN_GET_RANGE (col, &first, &last);
lfirst = (p4est_locidx_t) first;
llast = (p4est_locidx_t) last;
savedata = (p4est_locidx_t *) savecol->p.user_data;
savedata[0] = lfirst;
savedata[1] = llast;
}
}
}
/* save the columns; this function calls sc_MPI_Barrier at the end */
p4est_save_ext (filename, savecolumns, 1, save_partition);
p4est_destroy (savecolumns);
if (rank == 0) {
file = fopen (filename, "ab");
SC_CHECK_ABORT (file != NULL, "file open");
/* align */
fpos = ftell (file);
SC_CHECK_ABORT (fpos > 0, "first file tell");
while (fpos % align != 0) {
retval = fputc ('\0', file);
SC_CHECK_ABORT (retval == 0, "first file align");
++fpos;
}
/* write the p6est_connectivity extra data */
sink = sc_io_sink_new (SC_IO_TYPE_FILEFILE, SC_IO_MODE_APPEND,
SC_IO_ENCODE_NONE, file);
SC_CHECK_ABORT (sink != NULL, "file sink");
retval = p6est_connectivity_extra_sink (p6est->connectivity, sink);
SC_CHECK_ABORT (retval == 0, "sink connectivity");
retval = sc_io_sink_destroy (sink);
SC_CHECK_ABORT (retval == 0, "destroy sink");
fpos = ftell (file);
SC_CHECK_ABORT (fpos > 0, "second file tell");
while (fpos % align != 0) {
retval = fputc ('\0', file);
SC_CHECK_ABORT (retval == 0, "second file align");
++fpos;
}
/* write the data size */
u64a = (uint64_t) data_size;
sc_fwrite (&u64a, sizeof (uint64_t), 1, file, "write data size");
fpos = ftell (file);
SC_CHECK_ABORT (fpos > 0, "third file tell");
while (fpos % align != 0) {
retval = fputc ('\0', file);
SC_CHECK_ABORT (retval == 0, "third file align");
++fpos;
}
#ifdef P4EST_MPIIO_WRITE
/* we will close the sequential access to the file */
sc_fflush_fsync_fclose (file);
file = NULL;
#else
/* file is still open for sequential write mode */
#endif
}
else {
file = NULL;
}
#ifndef P4EST_MPIIO_WRITE
if (rank > 0) {
/* wait for sequential synchronization */
mpiret = sc_MPI_Recv (&fpos, 1, sc_MPI_LONG, rank - 1, P4EST_COMM_SAVE,
p6est->mpicomm, &mpistatus);
SC_CHECK_MPI (mpiret);
/* open file after all previous processors have written to it */
file = fopen (filename, "rb+");
SC_CHECK_ABORT (file != NULL, "file open");
}
#else
/* Every core opens the file in append mode */
mpiret = MPI_File_open (p6est->mpicomm, (char *) filename,
MPI_MODE_WRONLY | MPI_MODE_APPEND |
MPI_MODE_UNIQUE_OPEN, MPI_INFO_NULL, &mpifile);
SC_CHECK_MPI (mpiret);
mpiret = MPI_File_get_position (mpifile, &mpipos);
SC_CHECK_MPI (mpiret);
#endif
if (rank > 0) {
/* seek to the beginning of this processor's storage */
foffset = (long) (p6est->global_first_layer[rank] * comb_size);
#ifndef P4EST_MPIIO_WRITE
fthis = fpos + foffset;
retval = fseek (file, fthis, SEEK_SET);
SC_CHECK_ABORT (retval == 0, "seek data");
#else
mpithis = mpipos + (MPI_Offset) foffset;
mpiret = MPI_File_seek (mpifile, mpithis, MPI_SEEK_SET);
SC_CHECK_MPI (mpiret);
#endif
}
/* write layers and data interleaved */
bp = lbuf = P4EST_ALLOC (char, comb_size * nlayers);
for (zz = 0; zz < nlayers; zz++) {
p2est_quadrant_t *layer;
p4est_qcoord_t *qpos;
qpos = (p4est_locidx_t *) bp;
layer = p2est_quadrant_array_index (p6est->layers, zz);
*qpos++ = layer->z;
*qpos++ = (p4est_qcoord_t) layer->level;
if (save_data) {
memcpy (qpos, layer->p.user_data, data_size);
}
bp += comb_size;
}
#ifndef P4EST_MPIIO_WRITE
sc_fwrite (lbuf, comb_size, nlayers, file, "write quadrants");
#else
sc_mpi_write (mpifile, lbuf, comb_size * nlayers, MPI_BYTE,
"write quadrants");
#endif
P4EST_FREE (lbuf);
#ifndef P4EST_MPIIO_WRITE
sc_fflush_fsync_fclose (file);
file = NULL;
/* initiate sequential synchronization */
if (rank < num_procs - 1) {
mpiret = sc_MPI_Send (&fpos, 1, sc_MPI_LONG, rank + 1, P4EST_COMM_SAVE,
p6est->mpicomm);
SC_CHECK_MPI (mpiret);
}
#else
mpiret = MPI_File_close (&mpifile);
SC_CHECK_MPI (mpiret);
#endif
/* make sure subsequent code finds the final result on disk */
mpiret = sc_MPI_Barrier (p6est->mpicomm);
SC_CHECK_MPI (mpiret);
p4est_log_indent_pop ();
P4EST_GLOBAL_PRODUCTION ("Done p6est_save\n");
}
p6est_t *
p6est_load (const char *filename, sc_MPI_Comm mpicomm, size_t data_size,
int load_data, void *user_pointer,
p6est_connectivity_t ** connectivity)
{
return p6est_load_ext (filename, mpicomm, data_size, load_data,
0, 0, user_pointer, connectivity);
}
p6est_t *
p6est_load_ext (const char *filename, sc_MPI_Comm mpicomm, size_t data_size,
int load_data, int autopartition, int broadcasthead,
void *user_pointer, p6est_connectivity_t ** connectivity)
{
int align = 32;
int retval;
sc_io_source_t *src;
p4est_t *loadcolumns, *columns;
p4est_connectivity_t *conn4;
p6est_connectivity_t *conn;
p4est_topidx_t jt;
p4est_tree_t *tree, *loadtree;
p4est_quadrant_t *col, *loadcol;
sc_array_t *tquadrants, *loadtquadrants;
size_t zz, first, last, zcount, zpadding;
p4est_locidx_t *loaddata, lfirst, llast, nlayers;
p6est_t *p6est;
size_t save_data_size, comb_size;
uint64_t u64a;
p4est_gloidx_t *gfl;
int rank;
P4EST_GLOBAL_PRODUCTIONF ("Into p6est_load %s\n", filename);
p4est_log_indent_push ();
/* open file on all processors */
src = sc_io_source_new (SC_IO_TYPE_FILENAME, SC_IO_ENCODE_NONE, filename);
SC_CHECK_ABORT (src != NULL, "file source");
loadcolumns = p4est_source_ext (src, mpicomm, 2 * sizeof (p4est_locidx_t),
1, autopartition, broadcasthead, NULL,
&conn4);
columns = p4est_copy (loadcolumns, 0);
nlayers = 0;
for (jt = columns->first_local_tree; jt <= columns->last_local_tree; ++jt) {
tree = p4est_tree_array_index (columns->trees, jt);
loadtree = p4est_tree_array_index (loadcolumns->trees, jt);
tquadrants = &tree->quadrants;
loadtquadrants = &loadtree->quadrants;
for (zz = 0; zz < tquadrants->elem_count; ++zz) {
col = p4est_quadrant_array_index (tquadrants, zz);
loadcol = p4est_quadrant_array_index (loadtquadrants, zz);
loaddata = (p4est_locidx_t *) loadcol->p.user_data;
lfirst = loaddata[0];
llast = loaddata[1];
first = (size_t) nlayers;
nlayers += llast - lfirst;
last = first + (size_t) (llast - lfirst);
P6EST_COLUMN_SET_RANGE (col, first, last);
}
}
columns->connectivity = conn4;
p4est_destroy (loadcolumns);
/* padding */
zcount = src->bytes_out;
zpadding = (align - zcount % align) % align;
retval = sc_io_source_read (src, NULL, zpadding, NULL);
SC_CHECK_ABORT (!retval, "source padding");
conn = p6est_connectivity_extra_source (columns->connectivity, src);
if (connectivity != NULL) {
*connectivity = conn;
}
/* padding */
zcount = src->bytes_out;
zpadding = (align - zcount % align) % align;
retval = sc_io_source_read (src, NULL, zpadding, NULL);
SC_CHECK_ABORT (!retval, "source padding");
/* read data size */
retval = sc_io_source_read (src, &u64a, sizeof (uint64_t), NULL);
SC_CHECK_ABORT (!retval, "source data size");
save_data_size = (size_t) u64a;
if (load_data) {
SC_CHECK_ABORT (save_data_size == data_size, "data size mismatch");
}
/* padding */
zcount = src->bytes_out;
zpadding = (align - zcount % align) % align;
retval = sc_io_source_read (src, NULL, zpadding, NULL);
SC_CHECK_ABORT (!retval, "source padding");
p6est = P4EST_ALLOC (p6est_t, 1);
columns->user_pointer = p6est;
p6est->columns = columns;
p6est->connectivity = conn;
p6est->data_size = data_size;
p6est_comm_parallel_env_assign (p6est, mpicomm);
mpicomm = p6est->mpicomm;
rank = p6est->mpirank;
p6est->global_first_layer = gfl = P4EST_ALLOC (p4est_gloidx_t,
p6est->mpisize + 1);
p6est->layers =