parallel_io.c 50.5 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk),
4
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk).
5
 *
6
7
8
9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10
 *
11
12
13
14
 * 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 General Public License for more details.
15
 *
16
17
 * 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/>.
18
 *
19
20
21
22
23
 ******************************************************************************/

/* Config parameters. */
#include "../config.h"

24
#if defined(HAVE_HDF5) && defined(WITH_MPI) && defined(HAVE_PARALLEL_HDF5)
25
26
27
28
29

/* Some standard headers. */
#include <hdf5.h>
#include <math.h>
#include <mpi.h>
30
31
32
33
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
34
#include <time.h>
35

36
37
38
39
/* This object's header. */
#include "parallel_io.h"

/* Local includes. */
40
#include "chemistry_io.h"
41
#include "common_io.h"
42
#include "cooling_io.h"
43
#include "dimension.h"
44
#include "engine.h"
45
#include "error.h"
46
#include "gravity_io.h"
47
#include "gravity_properties.h"
48
#include "hydro_io.h"
49
#include "hydro_properties.h"
50
#include "io_properties.h"
51
52
#include "kernel_hydro.h"
#include "part.h"
lhausamm's avatar
lhausamm committed
53
#include "part_type.h"
54
#include "stars_io.h"
55
#include "units.h"
56
#include "xmf.h"
57

58
/* The current limit of ROMIO (the underlying MPI-IO layer) is 2GB */
59
#define HDF5_PARALLEL_IO_MAX_BYTES 2147000000LL
60

61
/* Are we timing the i/o? */
62
//#define IO_SPEED_MEASUREMENT
63

64
/**
65
 * @brief Reads a chunk of data from an open HDF5 dataset
66
 *
67
68
69
70
71
 * @param h_data The HDF5 dataset to write to.
 * @param h_plist_id the parallel HDF5 properties.
 * @param props The #io_props of the field to read.
 * @param N The number of particles to write.
 * @param offset Offset in the array where this mpi task starts writing.
72
 * @param internal_units The #unit_system used internally.
73
 * @param ic_units The #unit_system used in the snapshots.
74
 * @param cleanup_h Are we removing h-factors from the ICs?
75
76
 * @param cleanup_sqrt_a Are we cleaning-up the sqrt(a) factors in the Gadget
 * IC velocities?
77
 * @param h The value of the reduced Hubble constant to use for cleaning.
78
 * @param a The current value of the scale-factor.
79
 */
80
void readArray_chunk(hid_t h_data, hid_t h_plist_id,
81
82
                     const struct io_props props, size_t N, long long offset,
                     const struct unit_system* internal_units,
83
                     const struct unit_system* ic_units, int cleanup_h,
84
                     int cleanup_sqrt_a, double h, double a) {
85

86
87
88
  const size_t typeSize = io_sizeof_type(props.type);
  const size_t copySize = typeSize * props.dimension;
  const size_t num_elements = N * props.dimension;
89

90
91
  /* Can't handle writes of more than 2GB */
  if (N * props.dimension * typeSize > HDF5_PARALLEL_IO_MAX_BYTES)
92
    error("Dataset too large to be read in one pass!");
93

94
  /* Allocate temporary buffer */
95
  void* temp = malloc(num_elements * typeSize);
96
  if (temp == NULL) error("Unable to allocate memory for temporary buffer");
97

98
  /* Prepare information for hyper-slab */
99
100
  hsize_t shape[2], offsets[2];
  int rank;
101
  if (props.dimension > 1) {
102
103
    rank = 2;
    shape[0] = N;
104
    shape[1] = props.dimension;
105
106
107
    offsets[0] = offset;
    offsets[1] = 0;
  } else {
108
    rank = 2;
109
    shape[0] = N;
110
    shape[1] = 1;
111
112
113
    offsets[0] = offset;
    offsets[1] = 0;
  }
114
115

  /* Create data space in memory */
116
  const hid_t h_memspace = H5Screate_simple(rank, shape, NULL);
117

118
  /* Select hyper-slab in file */
119
  const hid_t h_filespace = H5Dget_space(h_data);
120
121
  H5Sselect_hyperslab(h_filespace, H5S_SELECT_SET, offsets, NULL, shape, NULL);

122
123
124
  /* Read HDF5 dataspace in temporary buffer */
  /* Dirty version that happens to work for vectors but should be improved */
  /* Using HDF5 dataspaces would be better */
125
  const hid_t h_err = H5Dread(h_data, io_hdf5_type(props.type), h_memspace,
126
                              h_filespace, h_plist_id, temp);
127
  if (h_err < 0) error("Error while reading data array '%s'.", props.name);
128
129
130

  /* Unit conversion if necessary */
  const double factor =
131
132
      units_conversion_factor(ic_units, internal_units, props.units);
  if (factor != 1.) {
133

Matthieu Schaller's avatar
Matthieu Schaller committed
134
    /* message("Converting ! factor=%e", factor); */
135

136
    if (io_is_double_precision(props.type)) {
Matthieu Schaller's avatar
Matthieu Schaller committed
137
      double* temp_d = (double*)temp;
138
139
      for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= factor;
    } else {
Matthieu Schaller's avatar
Matthieu Schaller committed
140
      float* temp_f = (float*)temp;
141
142
      for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= factor;
    }
143
  }
144

145
146
  /* Clean-up h if necessary */
  const float h_factor_exp = units_h_factor(internal_units, props.units);
147
  if (cleanup_h && h_factor_exp != 0.f) {
148
149
150
151
152
153

    /* message("Multipltying '%s' by h^%f=%f", props.name, h_factor_exp,
     * h_factor); */

    if (io_is_double_precision(props.type)) {
      double* temp_d = (double*)temp;
154
      const double h_factor = pow(h, h_factor_exp);
155
156
157
      for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= h_factor;
    } else {
      float* temp_f = (float*)temp;
158
      const float h_factor = pow(h, h_factor_exp);
159
160
161
162
      for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= h_factor;
    }
  }

163
164
165
166
167
168
169
170
171
172
173
174
175
176
  /* Clean-up a if necessary */
  if (cleanup_sqrt_a && a != 1. && (strcmp(props.name, "Velocities") == 0)) {

    if (io_is_double_precision(props.type)) {
      double* temp_d = (double*)temp;
      const double vel_factor = sqrt(a);
      for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= vel_factor;
    } else {
      float* temp_f = (float*)temp;
      const float vel_factor = sqrt(a);
      for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= vel_factor;
    }
  }

177
  /* Copy temporary buffer to particle data */
Matthieu Schaller's avatar
Matthieu Schaller committed
178
  char* temp_c = (char*)temp;
179
  for (size_t i = 0; i < N; ++i)
180
    memcpy(props.field + i * props.partSize, &temp_c[i * copySize], copySize);
181

182
183
  /* Free and close everything */
  free(temp);
184
185
  H5Sclose(h_filespace);
  H5Sclose(h_memspace);
186
187
188
189
190
191
}

/**
 * @brief Reads a data array from a given HDF5 group.
 *
 * @param grp The group from which to read.
Matthieu Schaller's avatar
Matthieu Schaller committed
192
 * @param props The #io_props of the field to read.
193
194
195
196
197
198
 * @param N The number of particles on that rank.
 * @param N_total The total number of particles.
 * @param mpi_rank The MPI rank of this node.
 * @param offset The offset in the array on disk for this rank.
 * @param internal_units The #unit_system used internally.
 * @param ic_units The #unit_system used in the ICs.
199
 * @param cleanup_h Are we removing h-factors from the ICs?
200
201
 * @param cleanup_sqrt_a Are we cleaning-up the sqrt(a) factors in the Gadget
 * IC velocities?
202
 * @param h The value of the reduced Hubble constant to use for cleaning.
203
 * @param a The current value of the scale-factor.
204
 */
205
206
void readArray(hid_t grp, struct io_props props, size_t N, long long N_total,
               int mpi_rank, long long offset,
207
               const struct unit_system* internal_units,
208
209
               const struct unit_system* ic_units, int cleanup_h,
               int cleanup_sqrt_a, double h, double a) {
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

  const size_t typeSize = io_sizeof_type(props.type);
  const size_t copySize = typeSize * props.dimension;

  /* Check whether the dataspace exists or not */
  const htri_t exist = H5Lexists(grp, props.name, 0);
  if (exist < 0) {
    error("Error while checking the existence of data set '%s'.", props.name);
  } else if (exist == 0) {
    if (props.importance == COMPULSORY) {
      error("Compulsory data set '%s' not present in the file.", props.name);
    } else {
      for (size_t i = 0; i < N; ++i)
        memset(props.field + i * props.partSize, 0, copySize);
      return;
    }
  }

  /* Open data space in file */
  const hid_t h_data = H5Dopen2(grp, props.name, H5P_DEFAULT);
  if (h_data < 0) error("Error while opening data space '%s'.", props.name);

232
233
234
235
236
237
238
239
240
241
242
243
/* Parallel-HDF5 1.10.2 incorrectly reads data that was compressed */
/* We detect this here and crash with an error message instead of  */
/* continuing with garbage data.                                   */
#if H5_VERSION_LE(1, 10, 2) && H5_VERSION_GE(1, 10, 2)
  if (mpi_rank == 0) {

    /* Recover the list of filters that were applied to the data */
    const hid_t h_plist = H5Dget_create_plist(h_data);
    if (h_plist < 0)
      error("Error getting property list for data set '%s'", props.name);

    /* Recover the number of filters in the list */
244
    const int n_filters = H5Pget_nfilters(h_plist);
245
246
247
248

    for (int n = 0; n < n_filters; ++n) {

      unsigned int flag;
249
      size_t cd_nelmts = 32;
250
251
      unsigned int* cd_values = malloc(cd_nelmts * sizeof(unsigned int));
      size_t namelen = 256;
252
      char* name = calloc(namelen, sizeof(char));
253
254
255
256
      unsigned int filter_config;

      /* Recover the n^th filter in the list */
      const H5Z_filter_t filter =
257
          H5Pget_filter(h_plist, n, &flag, &cd_nelmts, cd_values, namelen, name,
258
259
260
                        &filter_config);
      if (filter < 0)
        error("Error retrieving %d^th (%d) filter for data set '%s'", n,
261
              n_filters, props.name);
262
263
264
265
266
267
268

      /* Now check whether the deflate filter had been applied */
      if (filter == H5Z_FILTER_DEFLATE)
        error(
            "HDF5 1.10.2 cannot correctly read data that was compressed with "
            "the 'deflate' filter.\nThe field '%s' has had this filter applied "
            "and the code would silently read garbage into the particle arrays "
269
            "so we'd rather stop here. You can:\n - Recompile the code with an "
270
            "earlier or older version of HDF5.\n - Use the 'h5repack' tool to "
271
272
            "remove the filter from the ICs (e.g. h5repack -f NONE -i in_file "
            "-o out_file).\n",
273
274
275
276
277
            props.name);

      free(name);
      free(cd_values);
    }
278
279

    H5Pclose(h_plist);
280
281
282
  }
#endif

283
284
285
286
287
288
289
290
291
292
293
  /* Create property list for collective dataset read. */
  const hid_t h_plist_id = H5Pcreate(H5P_DATASET_XFER);
  H5Pset_dxpl_mpio(h_plist_id, H5FD_MPIO_COLLECTIVE);

  /* Given the limitations of ROM-IO we will need to read the data in chunk of
     HDF5_PARALLEL_IO_MAX_BYTES bytes per node until all the nodes are done. */
  char redo = 1;
  while (redo) {

    /* Maximal number of elements */
    const size_t max_chunk_size =
294
        HDF5_PARALLEL_IO_MAX_BYTES / (props.dimension * typeSize);
295
296
297
298

    /* Write the first chunk */
    const size_t this_chunk = (N > max_chunk_size) ? max_chunk_size : N;
    readArray_chunk(h_data, h_plist_id, props, this_chunk, offset,
299
                    internal_units, ic_units, cleanup_h, cleanup_sqrt_a, h, a);
300
301
302
303

    /* Compute how many items are left */
    if (N > max_chunk_size) {
      N -= max_chunk_size;
304
305
      props.field += max_chunk_size * props.partSize; /* char* on the field */
      props.parts += max_chunk_size;                  /* part* on the part */
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
      offset += max_chunk_size;
      redo = 1;
    } else {
      N = 0;
      offset += 0;
      redo = 0;
    }

    /* Do we need to run again ? */
    MPI_Allreduce(MPI_IN_PLACE, &redo, 1, MPI_SIGNED_CHAR, MPI_MAX,
                  MPI_COMM_WORLD);

    if (redo && mpi_rank == 0)
      message("Need to redo one iteration for array '%s'", props.name);
  }

  /* Close everything */
  H5Pclose(h_plist_id);
324
325
326
327
  H5Dclose(h_data);
}

/**
328
 * @brief Prepares an array in the snapshot.
329
 *
330
 * @param e The #engine we are writing from.
331
332
333
334
335
336
337
 * @param grp The HDF5 grp to write to.
 * @param fileName The name of the file we are writing to.
 * @param xmfFile The (opened) XMF file we are appending to.
 * @param partTypeGroupName The name of the group we are writing to.
 * @param props The #io_props of the field to write.
 * @param N_total The total number of particles to write in this array.
 * @param snapshot_units The units used for the data in this snapshot.
338
 */
339
void prepareArray(struct engine* e, hid_t grp, char* fileName, FILE* xmfFile,
Matthieu Schaller's avatar
Matthieu Schaller committed
340
341
                  char* partTypeGroupName, struct io_props props,
                  long long N_total, const struct unit_system* snapshot_units) {
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

  /* Create data space */
  const hid_t h_space = H5Screate(H5S_SIMPLE);
  if (h_space < 0)
    error("Error while creating data space for field '%s'.", props.name);

  int rank = 0;
  hsize_t shape[2];
  hsize_t chunk_shape[2];
  if (props.dimension > 1) {
    rank = 2;
    shape[0] = N_total;
    shape[1] = props.dimension;
    chunk_shape[0] = 1 << 16; /* Just a guess...*/
    chunk_shape[1] = props.dimension;
  } else {
    rank = 1;
    shape[0] = N_total;
    shape[1] = 0;
    chunk_shape[0] = 1 << 16; /* Just a guess...*/
    chunk_shape[1] = 0;
  }

  /* Make sure the chunks are not larger than the dataset */
Matthieu Schaller's avatar
Matthieu Schaller committed
366
  if ((long long)chunk_shape[0] > N_total) chunk_shape[0] = N_total;
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

  /* Change shape of data space */
  hid_t h_err = H5Sset_extent_simple(h_space, rank, shape, NULL);
  if (h_err < 0)
    error("Error while changing data space shape for field '%s'.", props.name);

  /* Create property list for collective dataset write.    */
  const hid_t h_plist_id = H5Pcreate(H5P_DATASET_XFER);
  H5Pset_dxpl_mpio(h_plist_id, H5FD_MPIO_COLLECTIVE);

  /* Set chunk size */
  /* h_err = H5Pset_chunk(h_prop, rank, chunk_shape); */
  /* if (h_err < 0) { */
  /*   error("Error while setting chunk size (%llu, %llu) for field '%s'.", */
  /*         chunk_shape[0], chunk_shape[1], props.name); */
  /* } */

  /* Create dataset */
Matthieu Schaller's avatar
Matthieu Schaller committed
385
386
387
388
  const hid_t h_data =
      H5Dcreate(grp, props.name, io_hdf5_type(props.type), h_space, H5P_DEFAULT,
                H5P_DEFAULT, H5P_DEFAULT);
  if (h_data < 0) error("Error while creating dataspace '%s'.", props.name);
389
390
391
392
393
394
395

  /* Write unit conversion factors for this data set */
  char buffer[FIELD_BUFFER_SIZE];
  units_cgs_conversion_string(buffer, snapshot_units, props.units);
  io_write_attribute_d(
      h_data, "CGS conversion factor",
      units_cgs_conversion_factor(snapshot_units, props.units));
396
  io_write_attribute_f(h_data, "h-scale exponent", 0);
397
398
399
400
401
402
  io_write_attribute_f(h_data, "a-scale exponent",
                       units_a_factor(snapshot_units, props.units));
  io_write_attribute_s(h_data, "Conversion factor", buffer);

  /* Add a line to the XMF */
  xmf_write_line(xmfFile, fileName, partTypeGroupName, props.name, N_total,
Matthieu Schaller's avatar
Matthieu Schaller committed
403
                 props.dimension, props.type);
404
405
406
407
408
409
410

  /* Close everything */
  H5Pclose(h_plist_id);
  H5Dclose(h_data);
  H5Sclose(h_space);
}

411
412
413
414
415
416
417
418
419
420
421
/**
 * @brief Writes a chunk of data in an open HDF5 dataset
 *
 * @param e The #engine we are writing from.
 * @param h_data The HDF5 dataset to write to.
 * @param props The #io_props of the field to write.
 * @param N The number of particles to write.
 * @param offset Offset in the array where this mpi task starts writing.
 * @param internal_units The #unit_system used internally.
 * @param snapshot_units The #unit_system used in the snapshots.
 */
422
void writeArray_chunk(struct engine* e, hid_t h_data,
423
424
425
426
                      const struct io_props props, size_t N, long long offset,
                      const struct unit_system* internal_units,
                      const struct unit_system* snapshot_units) {

427
  const size_t typeSize = io_sizeof_type(props.type);
428
429
  const size_t num_elements = N * props.dimension;

430
  /* Can't handle writes of more than 2GB */
431
  if (N * props.dimension * typeSize > HDF5_PARALLEL_IO_MAX_BYTES)
432
    error("Dataset too large to be written in one pass!");
433

Matthieu Schaller's avatar
Matthieu Schaller committed
434
  /* message("Writing '%s' array...", props.name); */
435
436

  /* Allocate temporary buffer */
437
  void* temp = NULL;
438
  if (posix_memalign((void**)&temp, IO_BUFFER_ALIGNMENT,
439
440
                     num_elements * typeSize) != 0)
    error("Unable to allocate temporary i/o buffer");
441

442
443
444
445
#ifdef IO_SPEED_MEASUREMENT
  MPI_Barrier(MPI_COMM_WORLD);
  ticks tic = getticks();
#endif
446

447
448
  /* Copy the particle data to the temporary buffer */
  io_copy_temp_buffer(temp, e, props, N, internal_units, snapshot_units);
449

450
451
#ifdef IO_SPEED_MEASUREMENT
  MPI_Barrier(MPI_COMM_WORLD);
452
453
454
  if (engine_rank == 0)
    message("Copying for '%s' took %.3f %s.", props.name,
            clocks_from_ticks(getticks() - tic), clocks_getunit());
455
#endif
456

457
  /* Create data space */
458
  const hid_t h_memspace = H5Screate(H5S_SIMPLE);
459
  if (h_memspace < 0)
460
461
    error("Error while creating data space (memory) for field '%s'.",
          props.name);
462

463
464
465
466
  int rank;
  hsize_t shape[2];
  hsize_t offsets[2];
  if (props.dimension > 1) {
467
468
    rank = 2;
    shape[0] = N;
469
    shape[1] = props.dimension;
470
471
472
473
474
475
476
477
478
479
    offsets[0] = offset;
    offsets[1] = 0;
  } else {
    rank = 1;
    shape[0] = N;
    shape[1] = 0;
    offsets[0] = offset;
    offsets[1] = 0;
  }

480
  /* Change shape of memory data space */
481
  hid_t h_err = H5Sset_extent_simple(h_memspace, rank, shape, NULL);
482
  if (h_err < 0)
483
    error("Error while changing data space (memory) shape for field '%s'.",
484
          props.name);
485

486
487
  /* Select the hyper-salb corresponding to this rank */
  hid_t h_filespace = H5Dget_space(h_data);
488
  if (N > 0)
Matthieu Schaller's avatar
Matthieu Schaller committed
489
490
    H5Sselect_hyperslab(h_filespace, H5S_SELECT_SET, offsets, NULL, shape,
                        NULL);
491
  else
492
    H5Sselect_none(h_filespace);
493

494
495
496
497
    /* message("Writing %lld '%s', %zd elements = %zd bytes (int=%d) at offset
     * %zd", N, props.name, N * props.dimension, N * props.dimension * typeSize,
     */
    /* 	  (int)(N * props.dimension * typeSize), offset); */
498

499
500
501
502
#ifdef IO_SPEED_MEASUREMENT
  MPI_Barrier(MPI_COMM_WORLD);
  tic = getticks();
#endif
503

504
505
  /* Write temporary buffer to HDF5 dataspace */
  h_err = H5Dwrite(h_data, io_hdf5_type(props.type), h_memspace, h_filespace,
506
                   H5P_DEFAULT, temp);
507
  if (h_err < 0) error("Error while writing data array '%s'.", props.name);
508

509
510
511
512
513
514
515
516
517
518
519
#ifdef IO_SPEED_MEASUREMENT
  MPI_Barrier(MPI_COMM_WORLD);
  ticks toc = getticks();
  float ms = clocks_from_ticks(toc - tic);
  int megaBytes = N * props.dimension * typeSize / (1024 * 1024);
  int total = 0;
  MPI_Reduce(&megaBytes, &total, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  if (engine_rank == 0)
    message("H5Dwrite for '%s' (%d MB) took %.3f %s (speed = %f MB/s).",
            props.name, total, ms, clocks_getunit(), total / (ms / 1000.));
#endif
520

521
522
523
524
525
  /* Free and close everything */
  free(temp);
  H5Sclose(h_memspace);
  H5Sclose(h_filespace);
}
526

527
528
529
530
531
/**
 * @brief Writes a data array in given HDF5 group.
 *
 * @param e The #engine we are writing from.
 * @param grp The group in which to write.
532
 * @param fileName The name of the file in which the data is written.
533
534
535
536
 * @param partTypeGroupName The name of the group containing the particles in
 * the HDF5 file.
 * @param props The #io_props of the field to read
 * @param N The number of particles to write.
537
538
539
540
541
 * @param N_total Total number of particles across all cores.
 * @param mpi_rank The rank of this node.
 * @param offset Offset in the array where this mpi task starts writing.
 * @param internal_units The #unit_system used internally.
 * @param snapshot_units The #unit_system used in the snapshots.
542
 */
543
void writeArray(struct engine* e, hid_t grp, char* fileName,
544
                char* partTypeGroupName, struct io_props props, size_t N,
545
546
547
548
549
                long long N_total, int mpi_rank, long long offset,
                const struct unit_system* internal_units,
                const struct unit_system* snapshot_units) {

  const size_t typeSize = io_sizeof_type(props.type);
550
551
552
553

#ifdef IO_SPEED_MEASUREMENT
  const ticks tic = getticks();
#endif
554

555
556
  /* Open dataset */
  const hid_t h_data = H5Dopen(grp, props.name, H5P_DEFAULT);
Matthieu Schaller's avatar
Matthieu Schaller committed
557
  if (h_data < 0) error("Error while opening dataset '%s'.", props.name);
558

559
560
561
  /* Given the limitations of ROM-IO we will need to write the data in chunk of
     HDF5_PARALLEL_IO_MAX_BYTES bytes per node until all the nodes are done. */
  char redo = 1;
562
563
564
565
566
567
  while (redo) {

    /* Maximal number of elements */
    const size_t max_chunk_size =
        HDF5_PARALLEL_IO_MAX_BYTES / (props.dimension * typeSize);

568
    /* Write the first chunk */
569
    const size_t this_chunk = (N > max_chunk_size) ? max_chunk_size : N;
570
571
    writeArray_chunk(e, h_data, props, this_chunk, offset, internal_units,
                     snapshot_units);
572
573

    /* Compute how many items are left */
574
    if (N > max_chunk_size) {
575
      N -= max_chunk_size;
576
577
      props.field += max_chunk_size * props.partSize; /* char* on the field */
      props.parts += max_chunk_size;                  /* part* on the part */
578
579
      offset += max_chunk_size;
      redo = 1;
580
    } else {
581
582
583
584
585
586
      N = 0;
      offset += 0;
      redo = 0;
    }

    /* Do we need to run again ? */
587
588
    MPI_Allreduce(MPI_IN_PLACE, &redo, 1, MPI_SIGNED_CHAR, MPI_MAX,
                  MPI_COMM_WORLD);
589

590
    if (redo && e->verbose && mpi_rank == 0)
591
      message("Need to redo one iteration for array '%s'", props.name);
592
  }
593

594
  /* Close everything */
595
  H5Dclose(h_data);
596

597
598
#ifdef IO_SPEED_MEASUREMENT
  MPI_Barrier(MPI_COMM_WORLD);
599
600
601
  if (engine_rank == 0)
    message("'%s' took %.3f %s.", props.name,
            clocks_from_ticks(getticks() - tic), clocks_getunit());
602
#endif
603
604
}

605
606
607
608
/**
 * @brief Reads an HDF5 initial condition file (GADGET-3 type) in parallel
 *
 * @param fileName The file to read.
609
 * @param internal_units The system units used internally
610
611
 * @param dim (output) The dimension of the volume read from the file.
 * @param parts (output) The array of #part read from the file.
612
613
614
615
616
 * @param gparts (output) The array of #gpart read from the file.
 * @param sparts (output) The array of #spart read from the file.
 * @param Ngas (output) The number of particles read from the file.
 * @param Ngparts (output) The number of particles read from the file.
 * @param Nstars (output) The number of particles read from the file.
617
 * @param periodic (output) 1 if the volume is periodic, 0 if not.
618
 * @param flag_entropy (output) 1 if the ICs contained Entropy in the
619
 * InternalEnergy field
620
621
622
 * @param with_hydro Are we running with hydro ?
 * @param with_gravity Are we running with gravity ?
 * @param with_stars Are we running with stars ?
623
 * @param cleanup_h Are we cleaning-up h-factors from the quantities we read?
624
625
 * @param cleanup_sqrt_a Are we cleaning-up the sqrt(a) factors in the Gadget
 * IC velocities?
626
 * @param h The value of the reduced Hubble constant to use for correction.
627
 * @param a The current value of the scale-factor.
628
629
630
631
 * @param mpi_rank The MPI rank of this node
 * @param mpi_size The number of MPI ranks
 * @param comm The MPI communicator
 * @param info The MPI information object
632
 * @param n_threads The number of threads to use for local operations.
633
 * @param dry_run If 1, don't read the particle. Only allocates the arrays.
634
635
 *
 */
636
void read_ic_parallel(char* fileName, const struct unit_system* internal_units,
637
                      double dim[3], struct part** parts, struct gpart** gparts,
Matthieu Schaller's avatar
Matthieu Schaller committed
638
639
640
                      struct spart** sparts, size_t* Ngas, size_t* Ngparts,
                      size_t* Nstars, int* periodic, int* flag_entropy,
                      int with_hydro, int with_gravity, int with_stars,
641
642
643
                      int cleanup_h, int cleanup_sqrt_a, double h, double a,
                      int mpi_rank, int mpi_size, MPI_Comm comm, MPI_Info info,
                      int n_threads, int dry_run) {
644

645
  hid_t h_file = 0, h_grp = 0;
646
  /* GADGET has only cubic boxes (in cosmological mode) */
Matthieu Schaller's avatar
Matthieu Schaller committed
647
  double boxSize[3] = {0.0, -1.0, -1.0};
648
649
650
651
652
  long long numParticles[swift_type_count] = {0};
  long long numParticles_highWord[swift_type_count] = {0};
  size_t N[swift_type_count] = {0};
  long long N_total[swift_type_count] = {0};
  long long offset[swift_type_count] = {0};
653
  int dimension = 3; /* Assume 3D if nothing is specified */
654
  size_t Ndm = 0;
655
656
657
658
659
660

  /* Open file */
  /* message("Opening file '%s' as IC.", fileName); */
  hid_t h_plist_id = H5Pcreate(H5P_FILE_ACCESS);
  H5Pset_fapl_mpio(h_plist_id, comm, info);
  h_file = H5Fopen(fileName, H5F_ACC_RDONLY, h_plist_id);
661
  if (h_file < 0) error("Error while opening file '%s'.", fileName);
662
663
664

  /* Open header to read simulation properties */
  /* message("Reading runtime parameters..."); */
665
  h_grp = H5Gopen(h_file, "/RuntimePars", H5P_DEFAULT);
666
667
668
  if (h_grp < 0) error("Error while opening runtime parameters\n");

  /* Read the relevant information */
669
  io_read_attribute(h_grp, "PeriodicBoundariesOn", INT, periodic);
670
671
672
673
674
675

  /* Close runtime parameters */
  H5Gclose(h_grp);

  /* Open header to read simulation properties */
  /* message("Reading file header..."); */
676
  h_grp = H5Gopen(h_file, "/Header", H5P_DEFAULT);
677
678
  if (h_grp < 0) error("Error while opening file header\n");

679
680
681
682
  /* Check the dimensionality of the ICs (if the info exists) */
  const hid_t hid_dim = H5Aexists(h_grp, "Dimension");
  if (hid_dim < 0)
    error("Error while testing existance of 'Dimension' attribute");
683
  if (hid_dim > 0) io_read_attribute(h_grp, "Dimension", INT, &dimension);
684
685
686
687
  if (dimension != hydro_dimension)
    error("ICs dimensionality (%dD) does not match code dimensionality (%dD)",
          dimension, (int)hydro_dimension);

688
  /* Read the relevant information and print status */
689
  int flag_entropy_temp[6];
690
  io_read_attribute(h_grp, "Flag_Entropy_ICs", INT, flag_entropy_temp);
691
  *flag_entropy = flag_entropy_temp[0];
692
693
694
695
  io_read_attribute(h_grp, "BoxSize", DOUBLE, boxSize);
  io_read_attribute(h_grp, "NumPart_Total", LONGLONG, numParticles);
  io_read_attribute(h_grp, "NumPart_Total_HighWord", LONGLONG,
                    numParticles_highWord);
696

697
  for (int ptype = 0; ptype < swift_type_count; ++ptype)
Matthieu Schaller's avatar
Matthieu Schaller committed
698
699
    N_total[ptype] =
        (numParticles[ptype]) + (numParticles_highWord[ptype] << 32);
700

701
  /* Get the box size if not cubic */
702
703
704
705
  dim[0] = boxSize[0];
  dim[1] = (boxSize[1] < 0) ? boxSize[0] : boxSize[1];
  dim[2] = (boxSize[2] < 0) ? boxSize[0] : boxSize[2];

706
707
708
709
710
711
  /* Change box size in the 1D and 2D case */
  if (hydro_dimension == 2)
    dim[2] = min(dim[0], dim[1]);
  else if (hydro_dimension == 1)
    dim[2] = dim[1] = dim[0];

712
713
714
715
716
717
718
  /* Convert the box size if we want to clean-up h-factors */
  if (cleanup_h) {
    dim[0] /= h;
    dim[1] /= h;
    dim[2] /= h;
  }

719
  /* message("Found %lld particles in a %speriodic box of size [%f %f %f].", */
720
  /* 	  N_total[0], (periodic ? "": "non-"), dim[0], dim[1], dim[2]); */
721
722

  /* Divide the particles among the tasks. */
723
  for (int ptype = 0; ptype < swift_type_count; ++ptype) {
724
725
726
    offset[ptype] = mpi_rank * N_total[ptype] / mpi_size;
    N[ptype] = (mpi_rank + 1) * N_total[ptype] / mpi_size - offset[ptype];
  }
727
728
729
730

  /* Close header */
  H5Gclose(h_grp);

731
  /* Read the unit system used in the ICs */
Matthieu Schaller's avatar
Matthieu Schaller committed
732
733
  struct unit_system* ic_units =
      (struct unit_system*)malloc(sizeof(struct unit_system));
734
  if (ic_units == NULL) error("Unable to allocate memory for IC unit system");
735
  io_read_unit_system(h_file, ic_units, internal_units, mpi_rank);
736
737
738
739
740

  /* Tell the user if a conversion will be needed */
  if (mpi_rank == 0) {
    if (units_are_equal(ic_units, internal_units)) {

Matthieu Schaller's avatar
Matthieu Schaller committed
741
      message("IC and internal units match. No conversion needed.");
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

    } else {

      message("Conversion needed from:");
      message("(ICs) Unit system: U_M =      %e g.", ic_units->UnitMass_in_cgs);
      message("(ICs) Unit system: U_L =      %e cm.",
              ic_units->UnitLength_in_cgs);
      message("(ICs) Unit system: U_t =      %e s.", ic_units->UnitTime_in_cgs);
      message("(ICs) Unit system: U_I =      %e A.",
              ic_units->UnitCurrent_in_cgs);
      message("(ICs) Unit system: U_T =      %e K.",
              ic_units->UnitTemperature_in_cgs);
      message("to:");
      message("(internal) Unit system: U_M = %e g.",
              internal_units->UnitMass_in_cgs);
      message("(internal) Unit system: U_L = %e cm.",
              internal_units->UnitLength_in_cgs);
      message("(internal) Unit system: U_t = %e s.",
              internal_units->UnitTime_in_cgs);
      message("(internal) Unit system: U_I = %e A.",
              internal_units->UnitCurrent_in_cgs);
      message("(internal) Unit system: U_T = %e K.",
              internal_units->UnitTemperature_in_cgs);
    }
  }

768
  /* Convert the dimensions of the box */
Matthieu Schaller's avatar
Matthieu Schaller committed
769
770
771
  for (int j = 0; j < 3; j++)
    dim[j] *=
        units_conversion_factor(ic_units, internal_units, UNIT_CONV_LENGTH);
772

773
  /* Allocate memory to store SPH particles */
774
775
  if (with_hydro) {
    *Ngas = N[0];
776
    if (posix_memalign((void**)parts, part_align,
Matthieu Schaller's avatar
Matthieu Schaller committed
777
                       (*Ngas) * sizeof(struct part)) != 0)
778
779
780
781
      error("Error while allocating memory for particles");
    bzero(*parts, *Ngas * sizeof(struct part));
  }

782
  /* Allocate memory to store stars particles */
783
  if (with_stars) {
784
    *Nstars = N[swift_type_stars];
785
    if (posix_memalign((void**)sparts, spart_align,
786
                       *Nstars * sizeof(struct spart)) != 0)
787
      error("Error while allocating memory for stars particles");
788
789
790
791
792
793
    bzero(*sparts, *Nstars * sizeof(struct spart));
  }

  /* Allocate memory to store gravity particles */
  if (with_gravity) {
    Ndm = N[1];
794
795
    *Ngparts = (with_hydro ? N[swift_type_gas] : 0) +
               N[swift_type_dark_matter] +
796
               (with_stars ? N[swift_type_stars] : 0);
797
    if (posix_memalign((void**)gparts, gpart_align,
Matthieu Schaller's avatar
Matthieu Schaller committed
798
                       *Ngparts * sizeof(struct gpart)) != 0)
799
800
801
802
803
      error("Error while allocating memory for gravity particles");
    bzero(*gparts, *Ngparts * sizeof(struct gpart));
  }

  /* message("Allocated %8.2f MB for particles.", *N * sizeof(struct part) /
804
805
   * (1024.*1024.)); */

806
  /* message("BoxSize = %lf", dim[0]); */
807
  /* message("NumPart = [%zd, %zd] Total = %zd", *Ngas, Ndm, *Ngparts); */
808
809

  /* Loop over all particle types */
810
  for (int ptype = 0; ptype < swift_type_count; ptype++) {
Matthieu Schaller's avatar
Matthieu Schaller committed
811

812
813
    /* Don't do anything if no particle of this kind */
    if (N_total[ptype] == 0) continue;
Matthieu Schaller's avatar
Matthieu Schaller committed
814

815
816
817
    /* Open the particle group in the file */
    char partTypeGroupName[PARTICLE_GROUP_BUFFER_SIZE];
    snprintf(partTypeGroupName, PARTICLE_GROUP_BUFFER_SIZE, "/PartType%d",
Matthieu Schaller's avatar
Matthieu Schaller committed
818
             ptype);
819
    h_grp = H5Gopen(h_file, partTypeGroupName, H5P_DEFAULT);
820
    if (h_grp < 0)
821
      error("Error while opening particle group %s.", partTypeGroupName);
Matthieu Schaller's avatar
Matthieu Schaller committed
822

823
824
    int num_fields = 0;
    struct io_props list[100];
825
    size_t Nparticles = 0;
826

827
828
    /* Read particle fields into the particle structure */
    switch (ptype) {
Matthieu Schaller's avatar
Matthieu Schaller committed
829

830
      case swift_type_gas:
Matthieu Schaller's avatar
Matthieu Schaller committed
831
832
833
        if (with_hydro) {
          Nparticles = *Ngas;
          hydro_read_particles(*parts, list, &num_fields);
834
          num_fields += chemistry_read_particles(*parts, list + num_fields);
Matthieu Schaller's avatar
Matthieu Schaller committed
835
        }
Matthieu Schaller's avatar
Matthieu Schaller committed
836
837
        break;

838
      case swift_type_dark_matter:
Matthieu Schaller's avatar
Matthieu Schaller committed
839
840
841
842
        if (with_gravity) {
          Nparticles = Ndm;
          darkmatter_read_particles(*gparts, list, &num_fields);
        }
Matthieu Schaller's avatar
Matthieu Schaller committed
843
844
        break;

845
      case swift_type_stars:
846
        if (with_stars) {
Matthieu Schaller's avatar
Matthieu Schaller committed
847
          Nparticles = *Nstars;
848
          stars_read_particles(*sparts, list, &num_fields);
Matthieu Schaller's avatar
Matthieu Schaller committed
849
850
        }
        break;
851

Matthieu Schaller's avatar
Matthieu Schaller committed
852
      default:
853
854
855
        if (mpi_rank == 0)
          message("Particle Type %d not yet supported. Particles ignored",
                  ptype);
856
    }
Matthieu Schaller's avatar
Matthieu Schaller committed
857

858
859
860
    /* Read everything */
    if (!dry_run)
      for (int i = 0; i < num_fields; ++i)
861
        readArray(h_grp, list[i], Nparticles, N_total[ptype], mpi_rank,
862
863
                  offset[ptype], internal_units, ic_units, cleanup_h,
                  cleanup_sqrt_a, h, a);
864

865
866
867
    /* Close particle group */
    H5Gclose(h_grp);
  }
868

869
  if (!dry_run && with_gravity) {
870

871
872
    /* Let's initialise a bit of thread parallelism here */
    struct threadpool tp;
873
    threadpool_init(&tp, n_threads);
874

875
    /* Prepare the DM particles */
876
    io_prepare_dm_gparts(&tp, *gparts, Ndm);
877
878

    /* Duplicate the hydro particles into gparts */
879
    if (with_hydro) io_duplicate_hydro_gparts(&tp, *parts, *gparts, *Ngas, Ndm);
880

881
    /* Duplicate the stars particles into gparts */
882
    if (with_stars)
883
      io_duplicate_stars_gparts(&tp, *sparts, *gparts, *Nstars, Ndm + *Ngas);
884
885
886

    threadpool_clean(&tp);
  }
887
888

  /* message("Done Reading particles..."); */
889

890
891
892
  /* Clean up */
  free(ic_units);

893
894
895
896
897
898
899
  /* Close property handler */
  H5Pclose(h_plist_id);

  /* Close file */
  H5Fclose(h_file);
}

900
901
902
903
904
905
906
907
908
/**
 * @brief Prepares a file for a parallel write.
 *
 * @param e The #engine.
 * @param baseName The base name of the snapshots.
 * @param N_total The total number of particles of each type to write.
 * @param internal_units The #unit_system used internally.
 * @param snapshot_units The #unit_system used in the snapshots.
 */
909
void prepare_file(struct engine* e, const char* baseName, long long N_total[6],
Matthieu Schaller's avatar
Matthieu Schaller committed
910
911
                  const struct unit_system* internal_units,
                  const struct unit_system* snapshot_units) {
912

913
914
915
916
  const struct part* parts = e->s->parts;
  const struct xpart* xparts = e->s->xparts;
  const struct gpart* gparts = e->s->gparts;
  const struct spart* sparts = e->s->sparts;
lhausamm's avatar
lhausamm committed
917
  struct swift_params* params = e->parameter_file;
918
  FILE* xmfFile = 0;
919
920
  int periodic = e->s->periodic;
  int numFiles = 1;
921
922

  /* First time, we need to create the XMF file */
923
  if (e->snapshot_output_count == 0) xmf_create_file(baseName);
924

925
  /* Prepare the XMF file for the new entry */
926
  xmfFile = xmf_prepare_file(baseName);
927

928
929
  /* HDF5 File name */
  char fileName[FILENAME_BUFFER_SIZE];
930
931
  if (e->snapshot_label_delta == 1)
    snprintf(fileName, FILENAME_BUFFER_SIZE, "%s_%04i.hdf5", baseName,
932
             e->snapshot_output_count + e->snapshot_label_first);
933
934
  else
    snprintf(fileName, FILENAME_BUFFER_SIZE, "%s_%06i.hdf5", baseName,
935
936
             e->snapshot_output_count * e->snapshot_label_delta +
                 e->snapshot_label_first);
937

938
  /* Open HDF5 file with the chosen parameters */
939
  hid_t h_file = H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
Matthieu Schaller's avatar
Matthieu Schaller committed
940
  if (h_file < 0) error("Error while opening file '%s'.", fileName);
941

<