write_setview.c 1.98 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
  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.
*/

10
#include <stdlib.h>
gsell's avatar
gsell committed
11
#include "H5hut.h"
12

13 14 15 16 17
#define DEFAULT_VERBOSITY       H5_VERBOSE_DEFAULT

#define FNAME                   "example_setview.h5"
#define DATASIZE                32
#define ITERS                   4
18

gsell's avatar
gsell committed
19 20
int
main (
21 22
        int argc, char** argv
        ) {
23
        h5_int64_t verbosity = DEFAULT_VERBOSITY;
24

gsell's avatar
gsell committed
25 26 27
        // initialize MPI & H5hut
        MPI_Init (&argc, &argv);
        MPI_Comm comm = MPI_COMM_WORLD;
28 29
        int comm_rank = 0;
        MPI_Comm_rank (comm, &comm_rank);
30

gsell's avatar
gsell committed
31
        H5AbortOnError ();
32
        H5SetVerbosityLevel (verbosity);
33 34

        // create fake data
gsell's avatar
gsell committed
35
        h5_int64_t npoints = ITERS*DATASIZE;
gsell's avatar
gsell committed
36 37
        h5_int32_t data[ITERS*DATASIZE];
        for (int i = 0; i < npoints; i++) {
38
                data[i] = i + comm_rank*npoints;
39 40
        }

gsell's avatar
gsell committed
41 42 43
        // open file and create step #0
        h5_file_t file = H5OpenFile (FNAME, H5_O_WRONLY, H5_PROP_DEFAULT);
        H5SetStep(file, 0);
44

gsell's avatar
gsell committed
45 46 47 48 49
        // before we can start writing, we have to define the number of
        // items this processor will write
        H5PartSetNumParticles(file, npoints);

        // write ITER consecutive blocks of size DATASIZE
50
        h5_int64_t offset = comm_rank * npoints;
gsell's avatar
gsell committed
51
        for (int i = 0; i < ITERS; i++) {
52
                // set the "view" to select a subset of the dataset
gsell's avatar
gsell committed
53 54 55 56
                H5PartSetView (
                        file,
                        offset + i * DATASIZE,
                        offset + (i+1) * DATASIZE - 1);
57
                // write the data
gsell's avatar
gsell committed
58
                H5PartWriteDataInt32 (file, "data", data + i*DATASIZE);
59
        }
gsell's avatar
gsell committed
60 61

        // done
62 63 64 65 66
        H5CloseFile(file);
        MPI_Finalize();
        return H5_SUCCESS;
}