write_strided.c 2.26 KB
Newer Older
gsell's avatar
gsell committed
1
/*
gsell's avatar
gsell committed
2
  Copyright (c) 2006-2015, The Regents of the University of California,
gsell's avatar
gsell committed
3 4 5 6 7 8 9 10 11
  through Lawrence Berkeley National Laboratory (subject to receipt of any
  required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
  Institut (Switzerland).  All rights reserved.

  License: see file COPYING in top level of source distribution.
*/

#include "H5hut.h"

12 13 14 15
#define DEFAULT_VERBOSITY       H5_VERBOSE_DEFAULT

#define FNAME                   "example_strided.h5"
#define NPOINTS                 99
gsell's avatar
gsell committed
16 17 18 19 20 21

int
main (
	int argc,
	char** argv
	) {
22 23
        h5_int64_t verbosity = DEFAULT_VERBOSITY;

gsell's avatar
gsell committed
24 25 26 27 28
        // MPI & H5hut init
	MPI_Init (&argc, &argv);
        int rank;
        MPI_Comm comm = MPI_COMM_WORLD;
        MPI_Comm_rank (comm, &rank);
29

gsell's avatar
gsell committed
30
        H5AbortOnError ();
31
        H5SetVerbosityLevel (verbosity);
gsell's avatar
gsell committed
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

        // create fake data
        h5_float64_t particles[6*NPOINTS];
        h5_int64_t id[NPOINTS];
        for (int i = 0; i < NPOINTS; i++) {
                particles [6*i + 0] = 0.0 + i + NPOINTS * rank;
                particles [6*i + 1] = 0.1 + i + NPOINTS * rank;
                particles [6*i + 2] = 0.2 + i + NPOINTS * rank;
                particles [6*i + 3] = 0.3 + i + NPOINTS * rank;
                particles [6*i + 4] = 0.4 + i + NPOINTS * rank;
                particles [6*i + 5] = 0.5 + i + NPOINTS * rank;
                id [i] = i + NPOINTS * rank;
        }

        // open file with MPI_COMM_WORLD and create step #0
        h5_file_t file = H5OpenFile (FNAME, H5_O_WRONLY, H5_PROP_DEFAULT);
        H5SetStep (file, 0);

        // define number of items this processor will write and set the
        // in-memory striding
        H5PartSetNumParticlesStrided (file, NPOINTS, 6);

        // write strided data
        H5PartWriteDataFloat64 (file, "x",  particles+0);
        H5PartWriteDataFloat64 (file, "y",  particles+1);
        H5PartWriteDataFloat64 (file, "z",  particles+2);
        H5PartWriteDataFloat64 (file, "px", particles+3);
        H5PartWriteDataFloat64 (file, "py", particles+4);
        H5PartWriteDataFloat64 (file, "pz", particles+5);

        // disable striding to write the ID's
        H5PartSetNumParticles (file, NPOINTS);
        H5PartWriteDataInt64 (file, "id", id);

        // cleanup
	H5CloseFile (file);
	MPI_Finalize ();
	return 0;
}