File: test_loadsave2.c

package info (click to toggle)
p4est 2.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,536 kB
  • sloc: ansic: 87,528; makefile: 855; sh: 635; perl: 272; python: 226; awk: 40; javascript: 23
file content (351 lines) | stat: -rw-r--r-- 11,449 bytes parent folder | download | duplicates (2)
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
/*
  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.
*/

#ifndef P4_TO_P8
#include <p4est_algorithms.h>
#include <p4est_bits.h>
#include <p4est_communication.h>
#include <p4est_extended.h>
#include <p4est_io.h>
#else
#include <p8est_algorithms.h>
#include <p8est_bits.h>
#include <p8est_communication.h>
#include <p8est_extended.h>
#include <p8est_io.h>
#endif
#include <sc_options.h>
#include <sc_statistics.h>

#ifndef P4_TO_P8
#define P4EST_CONN_SUFFIX "p4c"
#define P4EST_FOREST_SUFFIX "p4p"
static const int    default_refine_level = 7;
#else
#define P4EST_CONN_SUFFIX "p8c"
#define P4EST_FOREST_SUFFIX "p8p"
static const int    default_refine_level = 4;
#endif
static int          refine_level = 0;
static int          counter = 0;

static void
init_fn (p4est_t * p4est, p4est_topidx_t which_tree,
         p4est_quadrant_t * quadrant)
{
  int                *data = (int *) quadrant->p.user_data;

  *data = (counter = counter * 1664525 + 1013904223) + (int) which_tree;
}

static int
refine_fn (p4est_t * p4est, p4est_topidx_t which_tree,
           p4est_quadrant_t * quadrant)
{
  int                 cid;

  if (which_tree == 2 || which_tree == 3) {
    return 0;
  }

  cid = p4est_quadrant_child_id (quadrant);

  if (cid == P4EST_CHILDREN - 1 ||
      (quadrant->x >= P4EST_LAST_OFFSET (P4EST_MAXLEVEL - 2) &&
       quadrant->y >= P4EST_LAST_OFFSET (P4EST_MAXLEVEL - 2)
#ifdef P4_TO_P8
       && quadrant->z >= P4EST_LAST_OFFSET (P4EST_MAXLEVEL - 2)
#endif
      )) {
    return 1;
  }
  if ((int) quadrant->level >= (refine_level - (int) (which_tree % 3))) {
    return 0;
  }
  if (quadrant->level == 1 && cid == 2) {
    return 1;
  }
  if (quadrant->y == P4EST_QUADRANT_LEN (2) &&
      quadrant->x == P4EST_LAST_OFFSET (2)) {
    return 1;
  }
  if (quadrant->y >= P4EST_QUADRANT_LEN (2)) {
    return 0;
  }

  return 1;
}

enum
{
  STATS_CONN_LOAD,
  STATS_P4EST_SAVE1,
  STATS_P4EST_LOAD1a,
  STATS_P4EST_LOAD1b,
  STATS_P4EST_ELEMS,
  STATS_P4EST_SAVE2,
  STATS_P4EST_LOAD2,
  STATS_P4EST_SAVE3,
  STATS_P4EST_LOAD3,
  STATS_P4EST_LOAD4,
  STATS_COUNT
};

static void
test_deflate (p4est_t * p4est)
{
  p4est_gloidx_t     *pertree;
  p4est_t            *p4est2;
  sc_array_t         *qarr, *darr;

  pertree = P4EST_ALLOC (p4est_gloidx_t, p4est->connectivity->num_trees + 1);
  p4est_comm_count_pertree (p4est, pertree);
  darr = NULL;
  qarr = p4est_deflate_quadrants (p4est, p4est->data_size > 0 ? &darr : NULL);

  /* Data that describes the forest completely
     (a) shared data (identical on all processors):
     p4est->connectivity
     p4est->global_first_quadrant (does not need to be stored away)
     pertree
     (b) per-processor data (partition independent after allgatherv):
     qarr
     darr (if per-quadrant data size is greater 0 and it should be saved)
   */

  /* Create a forest from this information and compare */
  p4est2 = p4est_inflate (p4est->mpicomm, p4est->connectivity,
                          p4est->global_first_quadrant, pertree,
                          qarr, darr, p4est->user_pointer);
  SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 1), "de/inflate");
  p4est_destroy (p4est2);

  /* clean up allocated memory */
  P4EST_FREE (pertree);
  sc_array_destroy (qarr);
  if (darr != NULL) {
    sc_array_destroy (darr);
  }
}

static void
test_loadsave (p4est_connectivity_t * connectivity, const char *prefix,
               sc_MPI_Comm mpicomm, int mpirank)
{
  int                 mpiret, retval;
  unsigned            csum, csum2;
  double              elapsed, wtime;
  p4est_connectivity_t *conn2;
  p4est_t            *p4est, *p4est2;
  sc_statinfo_t       stats[STATS_COUNT];
  char                conn_name[BUFSIZ];
  char                p4est_name[BUFSIZ];

  snprintf (conn_name, BUFSIZ, "%s.%s", prefix, P4EST_CONN_SUFFIX);
  snprintf (p4est_name, BUFSIZ, "%s.%s", prefix, P4EST_FOREST_SUFFIX);
  P4EST_GLOBAL_INFOF ("Using file names %s and %s\n", conn_name, p4est_name);

  p4est = p4est_new_ext (mpicomm, connectivity, 0, 0, 0,
                         sizeof (int), init_fn, NULL);
  p4est_refine (p4est, 1, refine_fn, init_fn);
  test_deflate (p4est);

  /* save, synchronize, load connectivity and compare */
  if (mpirank == 0) {
    retval = p4est_connectivity_save (conn_name, connectivity);
    SC_CHECK_ABORT (retval == 0, "connectivity_save failed");
  }
  mpiret = sc_MPI_Barrier (mpicomm);
  SC_CHECK_MPI (mpiret);

  wtime = sc_MPI_Wtime ();
  conn2 = p4est_connectivity_load (conn_name, NULL);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_CONN_LOAD, elapsed, "conn load");

  SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2),
                  "load/save connectivity mismatch A");
  p4est_connectivity_destroy (conn2);

  /* save, synchronize, load p4est and compare */
  wtime = sc_MPI_Wtime ();
  p4est_save (p4est_name, p4est, 1);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_P4EST_SAVE1, elapsed, "p4est save 1");

  wtime = sc_MPI_Wtime ();
  p4est2 = p4est_load (p4est_name, mpicomm, sizeof (int), 1, NULL, &conn2);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_P4EST_LOAD1a, elapsed, "p4est load 1a");

  SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2),
                  "load/save connectivity mismatch Ba");
  SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 1),
                  "load/save p4est mismatch Ba");
  p4est_destroy (p4est2);
  p4est_connectivity_destroy (conn2);

  wtime = sc_MPI_Wtime ();
  p4est2 = p4est_load (p4est_name, mpicomm, 0, 0, NULL, &conn2);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_P4EST_LOAD1b, elapsed, "p4est load 1b");

  SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2),
                  "load/save connectivity mismatch Bb");
  SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 0),
                  "load/save p4est mismatch Bb");
  test_deflate (p4est2);
  p4est_destroy (p4est2);
  p4est_connectivity_destroy (conn2);

  /* partition and balance */
  p4est_partition (p4est, 0, NULL);
  p4est_balance (p4est, P4EST_CONNECT_FULL, init_fn);
  csum = p4est_checksum (p4est);
  sc_stats_set1 (stats + STATS_P4EST_ELEMS,
                 (double) p4est->local_num_quadrants, "p4est elements");

  /* save, synchronize, load p4est and compare */
  wtime = sc_MPI_Wtime ();
  p4est_save (p4est_name, p4est, 0);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_P4EST_SAVE2, elapsed, "p4est save 2");

  wtime = sc_MPI_Wtime ();
  p4est2 = p4est_load (p4est_name, mpicomm, sizeof (int), 0, NULL, &conn2);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_P4EST_LOAD2, elapsed, "p4est load 2");

  SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2),
                  "load/save connectivity mismatch C");
  SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 0),
                  "load/save p4est mismatch C");
  p4est_destroy (p4est2);
  p4est_connectivity_destroy (conn2);

  /* save, synchronize, load p4est and compare */
  wtime = sc_MPI_Wtime ();
  p4est_save (p4est_name, p4est, 1);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_P4EST_SAVE3, elapsed, "p4est save 3");

  wtime = sc_MPI_Wtime ();
  p4est2 = p4est_load (p4est_name, mpicomm, sizeof (int), 0, NULL, &conn2);
  elapsed = sc_MPI_Wtime () - wtime;
  sc_stats_set1 (stats + STATS_P4EST_LOAD3, elapsed, "p4est load 3");

  SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2),
                  "load/save connectivity mismatch D");
  SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 0),
                  "load/save p4est mismatch D");
  p4est_destroy (p4est2);
  p4est_connectivity_destroy (conn2);

  /* Test autopartition load feature */
  wtime = sc_MPI_Wtime ();
  p4est2 = p4est_load_ext (p4est_name, mpicomm, sizeof (int), 0,
                           1, 0, NULL, &conn2);
  elapsed = sc_MPI_Wtime () - wtime;
  csum2 = p4est_checksum (p4est2);
  sc_stats_set1 (stats + STATS_P4EST_LOAD4, elapsed, "p4est load 4");

  SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2),
                  "load/save connectivity mismatch E");
  SC_CHECK_ABORT (mpirank != 0 || csum == csum2,
                  "load/save p4est mismatch E");
  p4est_destroy (p4est2);
  p4est_connectivity_destroy (conn2);

  /* destroy data structures */
  p4est_destroy (p4est);

  /* compute and print timings */
  sc_stats_compute (mpicomm, STATS_COUNT, stats);
  sc_stats_print (p4est_package_id, SC_LP_STATISTICS,
                  STATS_COUNT, stats, 0, 1);
}

int
main (int argc, char **argv)
{
  sc_MPI_Comm         mpicomm;
  int                 mpiret;
  int                 mpirank;
  int                 first_arg;
  const char         *prefix;
  p4est_connectivity_t *connectivity;
  sc_options_t       *opt;

  /* initialize MPI */
  mpiret = sc_MPI_Init (&argc, &argv);
  SC_CHECK_MPI (mpiret);
  mpicomm = sc_MPI_COMM_WORLD;
  mpiret = sc_MPI_Comm_rank (mpicomm, &mpirank);
  SC_CHECK_MPI (mpiret);

  /* initialize libsc and p4est */
  sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT);
  p4est_init (NULL, SC_LP_DEFAULT);

  /* handle command line options */
  opt = sc_options_new (argv[0]);
  sc_options_add_int (opt, 'l', "level", &refine_level,
                      default_refine_level, "Refinement level");
  sc_options_add_string (opt, 'o', "oprefix", &prefix,
                         P4EST_STRING, "Output prefix");
  first_arg = sc_options_parse (p4est_package_id, SC_LP_INFO,
                                opt, argc, argv);
  SC_CHECK_ABORT (first_arg >= 0, "Option error");

  /* create connectivity */
#ifndef P4_TO_P8
  connectivity = p4est_connectivity_new_star ();
#else
  connectivity = p8est_connectivity_new_rotcubes ();
#endif

  /* test with vertex information */
  test_loadsave (connectivity, prefix, mpicomm, mpirank);

  /* test without vertex information */
  connectivity->num_vertices = 0;
  P4EST_FREE (connectivity->vertices);
  connectivity->vertices = NULL;
  P4EST_FREE (connectivity->tree_to_vertex);
  connectivity->tree_to_vertex = NULL;
  p4est_connectivity_set_attr (connectivity, 1);
  memset (connectivity->tree_to_attr, 0,
          connectivity->num_trees * sizeof (int8_t));
  test_loadsave (connectivity, prefix, mpicomm, mpirank);

  /* clean up and exit */
  p4est_connectivity_destroy (connectivity);
  sc_options_destroy (opt);
  sc_finalize ();

  mpiret = sc_MPI_Finalize ();
  SC_CHECK_MPI (mpiret);

  return 0;
}