read_strided.c 2.37 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"

gsell's avatar
gsell committed
12 13
#include <stdlib.h>

gsell's avatar
gsell committed
14 15
// name of input file
const char* fname = "example_strided.h5";
16

gsell's avatar
gsell committed
17
// H5hut verbosity level
gsell's avatar
gsell committed
18 19
const h5_int64_t h5_verbosity = H5_VERBOSE_DEFAULT;
//const h5_int64_t h5_verbosity = H5_DEBUG_ALL;
gsell's avatar
gsell committed
20 21 22 23 24

int
main (
        int argc, char* argv[]
        ){
gsell's avatar
gsell committed
25
	
26
        // initialize MPI & H5hut
gsell's avatar
gsell committed
27
        MPI_Init (&argc, &argv);
28
        MPI_Comm comm = MPI_COMM_WORLD;
gsell's avatar
gsell committed
29 30 31 32
        int comm_size = 1;
        MPI_Comm_size (comm, &comm_size);
        int comm_rank = 0;
        MPI_Comm_rank (comm, &comm_rank);
33
        H5AbortOnError ();
gsell's avatar
gsell committed
34
        H5SetVerbosityLevel (h5_verbosity);
gsell's avatar
gsell committed
35

gsell's avatar
gsell committed
36 37
        // open file and go to first step
        h5_file_t file = H5OpenFile (fname, H5_O_RDONLY, H5_PROP_DEFAULT);
gsell's avatar
gsell committed
38 39
        H5SetStep (file, 0);

gsell's avatar
gsell committed
40 41 42 43 44
        // compute number of particles this process has to read
        h5_ssize_t num_particles_total = H5PartGetNumParticles (file);
        h5_ssize_t num_particles = num_particles_total / comm_size;
        if (comm_rank+1 == comm_size)
                num_particles += num_particles_total % comm_size;
gsell's avatar
gsell committed
45

gsell's avatar
gsell committed
46 47 48 49
	printf ("[proc %d]: particles in view: %lld\n", comm_rank, (long long)num_particles);
	printf ("[proc %d]: total number of particles: %lld\n",
		comm_rank, (long long unsigned)num_particles_total);
	
gsell's avatar
gsell committed
50
        // set number of particles and memory stride
gsell's avatar
gsell committed
51
        H5PartSetNumParticlesStrided (file, num_particles, 6);
gsell's avatar
gsell committed
52 53

        // read data
gsell's avatar
gsell committed
54
        h5_float64_t* data = calloc (6*num_particles, sizeof (*data));
gsell's avatar
gsell committed
55 56 57 58 59 60 61
        H5PartReadDataFloat64 (file, "x",  data+0);
        H5PartReadDataFloat64 (file, "y",  data+1);
        H5PartReadDataFloat64 (file, "z",  data+2);
        H5PartReadDataFloat64 (file, "px", data+3);
        H5PartReadDataFloat64 (file, "py", data+4);
        H5PartReadDataFloat64 (file, "pz", data+5);

gsell's avatar
gsell committed
62
	// print dataset "x"
gsell's avatar
gsell committed
63
        for (int i = 0; i < num_particles*6; i+=6) {
gsell's avatar
gsell committed
64 65 66 67 68 69
                printf ("[proc %d]: local index = %d, value = %6.3f\n",
                        comm_rank, i, data[i]);
        }

	// cleanup
	free (data);
gsell's avatar
gsell committed
70
        H5CloseFile (file);
gsell's avatar
gsell committed
71 72
	MPI_Finalize ();
        return 0;
gsell's avatar
gsell committed
73
}