read_setview.c 2.24 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
#define DEFAULT_VERBOSITY       H5_VERBOSE_DEFAULT

#define FNAME                   "example_setview.h5"
gsell's avatar
gsell committed
15 16 17 18 19

int
main (
        int argc, char* argv[]
        ){
20 21
        h5_int64_t verbosity = DEFAULT_VERBOSITY;

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

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

gsell's avatar
gsell committed
33
        // open file and go to step#0
gsell's avatar
gsell committed
34 35 36
        h5_file_t file = H5OpenFile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT);
        H5SetStep (file, 0);

gsell's avatar
gsell committed
37 38 39
        // compute a "canonical" view: all cores get almost the same number of
        // particles
        h5_int64_t total_particles = H5PartGetNumParticles (file);
40 41 42
        h5_int64_t nparticles = total_particles / comm_size;
        h5_int64_t remainder = total_particles % comm_size;
        h5_int64_t start = comm_rank * nparticles;
gsell's avatar
gsell committed
43 44

        // adjust number of local particles
45
        if (comm_rank < remainder)
gsell's avatar
gsell committed
46 47 48
                nparticles++;

        // adjust start
49 50
        if (comm_rank < remainder) 
                start += comm_rank;
gsell's avatar
gsell committed
51 52 53
        else
                start += remainder;
        
54 55
        // Note: setting end = start - 1 forces the 
        // selection of zero particles!
gsell's avatar
gsell committed
56 57
        h5_int64_t end = start + nparticles - 1;
        
58
        printf ("[proc %d]: set view to [%lld..%lld]\n", comm_rank, start, end);
gsell's avatar
gsell committed
59 60 61 62 63 64
        H5PartSetView (file, start, end);
        h5_int32_t* data = calloc (nparticles, sizeof (*data));

        H5PartReadDataInt32 (file, "data", data);
        for (int i = 0; i < nparticles; i++) {
                printf ("[proc %d]: global index = %lld; local index = %d, value = %d\n",
65
                        comm_rank, start+i, i, data[i]);
gsell's avatar
gsell committed
66
        }
gsell's avatar
gsell committed
67 68 69
        H5CloseFile (file);
        return MPI_Finalize ();
}