h5u_model.c 21.8 KB
Newer Older
1
/*
gsell's avatar
gsell committed
2
  Copyright (c) 2006-2016, The Regents of the University of California,
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 "h5core/h5_log.h"
11
#include "h5core/h5u_model.h"
gsell's avatar
gsell committed
12 13
#include "h5core/h5u_io.h"

14
#include "private/h5_file.h"
gsell's avatar
gsell committed
15 16 17 18 19 20 21
#include "private/h5_hdf5.h"

#include "private/h5_model.h"
#include "private/h5_mpi.h"
#include "private/h5_io.h"
#include "private/h5u_types.h"

22
#include <string.h>
23 24

h5_ssize_t
Gsell Achim's avatar
* C-API  
Gsell Achim committed
25
h5u_get_num_items (
26 27 28 29
	const h5_file_t fh      /*!< [in]  Handle to open file */
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_ssize_t, "f=%p", f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
30
	check_iteration_handle_is_valid (f);
31 32
	h5_ssize_t nparticles;

gsell's avatar
gsell committed
33
	if (h5u_has_view ((h5_file_t)f)) {
34
                /* if a view exists, use its size as the number of particles */
Gsell Achim's avatar
* C-API  
Gsell Achim committed
35
		TRY (nparticles = h5u_get_num_items_in_view (fh));
36 37
	} else {
		/* otherwise, report all particles on disk in the first dataset
Gsell Achim's avatar
* C-API  
Gsell Achim committed
38
                   for this iteration */
39 40 41
                TRY (nparticles = h5u_get_totalnum_particles_by_idx (fh, 0));
	}

gsell's avatar
gsell committed
42
	H5_RETURN (nparticles);
43 44 45
}

h5_ssize_t
Gsell Achim's avatar
* C-API  
Gsell Achim committed
46
h5u_get_num_items_in_view (
47 48 49 50
	const h5_file_t fh      /*!< [in]  Handle to open file */
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_ssize_t, "f=%p", f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
51
	check_iteration_handle_is_valid (f);
52 53
	h5_ssize_t nparticles;

gsell's avatar
gsell committed
54
	if (!h5u_has_view (fh)) {
55 56 57 58
                H5_RETURN_ERROR (
			H5_ERR_H5PART,
			"%s",
			"No view has been set.");
59 60 61
        }
        TRY (nparticles = hdf5_get_selected_npoints_of_dataspace(f->u->diskshape));
        h5_debug ("Found %lld particles in view.", (long long)nparticles );
gsell's avatar
gsell committed
62
	H5_RETURN (nparticles);
63 64 65 66
}

h5_ssize_t
h5u_get_totalnum_particles_by_name (
67 68
	const h5_file_t fh,		///< [in] Handle to open file
        const char* const dataset_name	///< [in] dataset to query
69 70
	) {
        h5_file_p f = (h5_file_p)fh;
71 72 73
	H5_CORE_API_ENTER (h5_ssize_t,
			   "f=%p, dataset_name=%s",
			   f, dataset_name);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
74
	check_iteration_handle_is_valid (f);
75
	h5_ssize_t nparticles;
76
        TRY (nparticles = hdf5_get_npoints_of_dataset_by_name (
Gsell Achim's avatar
* C-API  
Gsell Achim committed
77
		     f->iteration_gid, dataset_name));
78 79
        h5_debug ("Found %lld particles in dataset %s.",
		  (long long)nparticles, dataset_name);
gsell's avatar
gsell committed
80
	H5_RETURN (nparticles);
81 82
}

gsell's avatar
gsell committed
83 84 85 86 87 88 89 90 91 92 93
/*
  Query number of items in dataset in current timestep. Dataset is
  given by index.

  returns:
    H5_NOK if dataset does not exist
    H5_ERROR on error
    else number of items

*/

94 95 96 97 98 99 100
h5_ssize_t
h5u_get_totalnum_particles_by_idx (
	const h5_file_t fh,     ///< [in] Handle to open file
        h5_id_t idx             ///< [in] Index of dataset to query
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_ssize_t, "f=%p, idx=%lld", f, (long long)idx);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
101
	check_iteration_handle_is_valid (f);
102 103
        char dataset_name[H5_DATANAME_LEN];
        dataset_name[0] = '\0';
gsell's avatar
gsell committed
104 105
	h5_err_t h5err;
        TRY (h5err = hdf5_get_name_of_dataset_by_idx (
Gsell Achim's avatar
* C-API  
Gsell Achim committed
106
                    f->iteration_gid,
107 108 109
                    idx,
                    dataset_name,
                    H5_DATANAME_LEN));
gsell's avatar
gsell committed
110
	if (h5err == H5_NOK)
gsell's avatar
gsell committed
111
		H5_LEAVE (H5_NOK);
112 113
	h5_ssize_t nparticles;
        TRY (nparticles = hdf5_get_npoints_of_dataset_by_name (
Gsell Achim's avatar
* C-API  
Gsell Achim committed
114
		     f->iteration_gid, dataset_name));
115 116
        h5_debug ("Found %lld particles in dataset %s.",
		  (long long)nparticles, dataset_name);
gsell's avatar
gsell committed
117
	H5_RETURN (nparticles);
118 119 120
}

h5_err_t
Gsell Achim's avatar
* C-API  
Gsell Achim committed
121
h5u_set_num_items (
122 123 124 125 126 127 128 129 130
	const h5_file_t fh,		/*!< [in] Handle to open file */
	const h5_size_t nparticles,	/*!< [in] Number of particles */
	const h5_size_t stride		/*!< [in] Stride of particles in memory */
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_ssize_t,
	                   "f=%p, nparticles=%llu, stride=%llu",
	                   f, (long long unsigned)nparticles,
	                   (long long unsigned)stride);
gsell's avatar
gsell committed
131
	CHECK_FILEHANDLE (f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
132 133
	if (f->iteration_gid < 0) {
                TRY (h5_set_iteration (fh, 0));
gsell's avatar
gsell committed
134
        }
135 136 137 138
	struct h5u_fdata *u = f->u;
	hsize_t start;
	hsize_t dmax = H5S_UNLIMITED;

139
 #ifndef H5_HAVE_PARALLEL
140 141 142 143 144 145 146
	/*
	   if we are not using parallel-IO, there is enough information
	   to know that we can short circuit this routine.  However,
	   for parallel IO, this is going to cause problems because
	   we don't know if things have changed globally
	 */
	if ( u->nparticles == nparticles && stride == 1 ) {
gsell's avatar
gsell committed
147
		H5_LEAVE (H5_SUCCESS);
148 149 150 151 152 153 154
	}
#endif

	TRY (h5u_reset_view(fh));

	TRY (hdf5_close_dataspace (u->shape));
	u->shape = H5S_ALL;
155 156
        TRY (hdf5_close_dataspace (u->memshape));
        u->memshape = H5S_ALL;
157 158 159 160 161 162 163 164 165 166

	u->nparticles = (hsize_t)nparticles;

	/* declare local memory datasize with striding */
	hsize_t count = u->nparticles * stride;
	TRY (u->memshape = hdf5_create_dataspace (1, &count, &dmax));

	/* we need a hyperslab selection if there is striding
	 * (otherwise, the default H5S_ALL selection is ok)
	 */
167 168
	if (stride > 1) {
		h5_debug ("Striding by %lld elements.", (long long)stride);
169
		start = 0;
170
                hsize_t hstride = (hsize_t)stride;
171 172 173 174 175 176 177 178
		count = u->nparticles;
		TRY (hdf5_select_hyperslab_of_dataspace (
                             u->memshape,
                             H5S_SELECT_SET,
                             &start, &hstride, &count,
                             NULL));
	}

179
#ifndef H5_HAVE_PARALLEL
180 181 182 183
	count = u->nparticles;
	TRY( u->shape = hdf5_create_dataspace (1, &count, NULL));
	u->viewstart = 0;
	u->viewend   = nparticles - 1; // view range is *inclusive*
184
#else /* H5_HAVE_PARALLEL */
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	/*
	 The Gameplan here is to declare the overall size of the on-disk
	 data structure the same way we do for the serial case.  But
	 then we must have additional "DataSpace" structures to define
	 our in-memory layout of our domain-decomposed portion of the particle
	 list as well as a "selection" of a subset of the on-disk
	 data layout that will be written in parallel to mutually exclusive
	 regions by all of the processors during a parallel I/O operation.
	 These are f->shape, f->memshape and f->diskshape respectively.
	 */

	/*
	   acquire the number of particles to be written from each MPI process
	 */
	hsize_t total;
	TRY( h5priv_mpi_sum(
	             &(u->nparticles), &total, 1, MPI_LONG_LONG, f->props->comm ) );
	TRY( h5priv_mpi_prefix_sum(
	             &(u->nparticles), &start, 1, MPI_LONG_LONG, f->props->comm ) );
	start -= u->nparticles;

	h5_debug("Total particles across all processors: %lld.", (long long)total);
gsell's avatar
gsell committed
207
	h5_debug("Start index on this processor: %lld.", (long long)start);
208 209 210 211 212 213 214 215 216

	u->viewstart = start;
	u->viewend   = start + u->nparticles - 1; // view range is *inclusive*

	/* declare overall datasize */
	count = total;
	TRY( u->shape = hdf5_create_dataspace(1, &count, NULL) );

	/* declare overall data size  but then will select a subset */
217 218
        TRY (hdf5_close_dataspace (u->diskshape));
        TRY (u->diskshape = hdf5_create_dataspace(1, &count, NULL));
219 220 221

	count = nparticles;
	if (count > 0) {
222
                hsize_t hstride = 1;
223 224 225 226 227 228 229 230 231
		TRY( hdf5_select_hyperslab_of_dataspace(
		             u->diskshape,
		             H5S_SELECT_SET,
		             &start, &hstride, &count,
		             NULL) );
	} else {
		TRY (hdf5_select_none (u->diskshape));
	}
#endif
gsell's avatar
gsell committed
232
	H5_RETURN (H5_SUCCESS);
233 234 235 236
}

h5_err_t
h5u_has_view (
237
	const h5_file_t fh
238 239 240
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_ssize_t, "f=%p", f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
241
	check_iteration_handle_is_valid (f);
gsell's avatar
gsell committed
242
	H5_RETURN (f->u->viewindexed || f->u->viewstart >= 0);
243 244 245 246 247 248 249 250
}

h5_err_t
h5u_reset_view (
	const h5_file_t fh
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_ssize_t, "f=%p", f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
251
	check_iteration_handle_is_valid (f);
252 253 254 255 256 257 258 259 260 261
	struct h5u_fdata *u = f->u;

	u->viewstart = -1;
	u->viewend = -1;
	u->viewindexed = 0;
	TRY (hdf5_close_dataspace (u->diskshape));
	u->diskshape = H5S_ALL;
	TRY (hdf5_close_dataspace (u->memshape));
	u->memshape = H5S_ALL;

gsell's avatar
gsell committed
262
	H5_RETURN (H5_SUCCESS);
263 264
}

265 266
/*
  if start == -1 && end == -1 -> reset view
gsell's avatar
gsell committed
267
  elif end == -1 -> select to end
268 269

 */
270 271 272 273 274 275 276 277 278 279
h5_err_t
h5u_set_view (
	const h5_file_t fh,		///!< [in]  Handle to open file
	h5_int64_t start,		///!< [in]  Start particle
	h5_int64_t end	        	///!< [in]  End particle
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_err_t,
	                   "f=%p, start=%lld, end=%lld",
	                   f, (long long)start, (long long)end);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
280
	check_iteration_handle_is_valid (f);
281 282 283 284
	struct h5u_fdata *u = f->u;

	TRY (h5u_reset_view (fh));

285
	if (start == -1 && end == -1)   // we are already done
gsell's avatar
gsell committed
286
		H5_LEAVE (H5_SUCCESS);
287

Gsell Achim's avatar
* C-API  
Gsell Achim committed
288
	hssize_t total = 0;
289 290 291
	if (f->u->shape > 0) {
		TRY (total = hdf5_get_npoints_of_dataspace (f->u->shape) );
        } else {
292 293
		// if iteration does not contain a dataset, next function
		// returns -1
Gsell Achim's avatar
* C-API  
Gsell Achim committed
294
                TRY (total = h5u_get_totalnum_particles_by_idx (fh, 0));
295
        }
gsell's avatar
gsell committed
296
	h5_debug ("Total = %lld", (long long) total);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
297
	if (total <= 0) {
gsell's avatar
gsell committed
298
		/*
Gsell Achim's avatar
* C-API  
Gsell Achim committed
299
		  iteration does not contain a dataset yet! 
gsell's avatar
gsell committed
300
		*/
gsell's avatar
gsell committed
301 302
		/*
		  :FIXME: Should 'total == 0' be considered valid or not?
gsell's avatar
gsell committed
303 304 305 306 307 308 309
		*/
#if H5_HAVE_PARALLEL
		TRY (
			h5priv_mpi_allreduce_max (
			     &end, &total, 1, MPI_LONG_LONG, f->props->comm)
			);
#else
Gsell Achim's avatar
* C-API  
Gsell Achim committed
310
		total = end;
gsell's avatar
gsell committed
311 312 313
#endif
		total++;
		TRY (hdf5_close_dataspace (u->shape));
Gsell Achim's avatar
* C-API  
Gsell Achim committed
314 315
		hsize_t htotal = (hsize_t)total;
		TRY (u->shape = hdf5_create_dataspace(1, &htotal, NULL) );
gsell's avatar
gsell committed
316 317
	} else {
		if (end < 0) {
Gsell Achim's avatar
* C-API  
Gsell Achim committed
318
			end = total + end;
gsell's avatar
gsell committed
319
		}
Gsell Achim's avatar
* C-API  
Gsell Achim committed
320 321 322 323 324 325 326 327 328 329 330
	}	
	if ((start < 0)      ||
	    (start >= total) ||
	    (end >= total)   ||
	    (end+1 < start)) {
		H5_RETURN_ERROR (
			H5_ERR_INVAL,
			"Invalid view: start=%lld, end=%lld, total=%lld!",
			(long long)start,
			(long long)end,
			(long long)total);
gsell's avatar
gsell committed
331 332
	}
	
333 334 335
	/* setting up the new view */
	u->viewstart =  start;
	u->viewend =    end;
336 337 338 339
        if (end == -1)
                u->nparticles = 0;
        else
                u->nparticles = end - start + 1;
340 341

	h5_debug (
342
	        "This view includes %lld particles.",
343 344
	        (long long)u->nparticles );

Gsell Achim's avatar
* C-API  
Gsell Achim committed
345
	hsize_t htotal = (hsize_t)total;
346
	/* declare overall data size  but then will select a subset */
Gsell Achim's avatar
* C-API  
Gsell Achim committed
347
	TRY (u->diskshape = hdf5_create_dataspace (1, &htotal, NULL));
348 349

	hsize_t hstart = (hsize_t)start;
350 351
	hsize_t hstride = 1;
	hsize_t hcount = (hsize_t)u->nparticles;
352 353 354 355

	TRY (hdf5_select_hyperslab_of_dataspace (
	             u->diskshape,
	             H5S_SELECT_SET,
356
	             &hstart, &hstride, &hcount,
357 358 359
	             NULL));

	/* declare local memory datasize */
gsell's avatar
gsell committed
360
	hsize_t dmax = H5S_UNLIMITED;
361
	TRY (u->memshape = hdf5_create_dataspace (1, &hcount, &dmax));
gsell's avatar
gsell committed
362
	H5_RETURN (H5_SUCCESS);
363 364 365
}

h5_err_t
366
h5u_set_view_length (
367 368 369 370 371 372 373 374
	const h5_file_t fh,		///!< [in]  Handle to open file
	h5_int64_t start,		///!< [in]  Start particle
	h5_int64_t length	       	///!< [in]  number of particle in view
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_err_t,
	                   "f=%p, start=%lld, length=%lld",
	                   f, (long long)start, (long long)length);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
375
	check_iteration_handle_is_valid (f);
376 377 378 379 380
	struct h5u_fdata *u = f->u;

	TRY (h5u_reset_view (fh));

	if (start == -1 && length == -1)
gsell's avatar
gsell committed
381
		H5_LEAVE (H5_SUCCESS);
382

383
	hsize_t total = 0;
384 385 386 387 388 389 390 391 392 393 394 395
	if (u->shape > 0) {
		TRY (total = hdf5_get_npoints_of_dataspace (u->shape) );
		h5_debug(
			"Found %lld particles from previous H5PartSetNumParticles call.",
			(long long)total);
        } else {
                TRY (total = (hsize_t)h5u_get_totalnum_particles_by_idx (fh,0));
        }
	if (total == 0) {
		/* No datasets have been created yet and no views are set.
		 * We have to leave the view empty because we don't know how
		 * many particles there should be! */
gsell's avatar
gsell committed
396
		H5_LEAVE (H5_SUCCESS);
397 398 399
	}

	if (start < 0 || length < 0 || start+length > total) 
400 401 402 403 404
                H5_RETURN_ERROR (
			H5_ERR_INVAL,
			"Invalid view: start=%lld, length=%lld, total=%lld",
			(long long)start, (long long)length,
			(long long)total);
405 406 407 408 409 410 411

	/* setting up the new view */
	u->viewstart =  start;
	u->viewend =    start + length - 1;
	u->nparticles = length;

	h5_debug (
gsell's avatar
gsell committed
412
	        "This view includes %lld particles.",
413 414 415
	        (long long)u->nparticles );

	/* declare overall data size  but then will select a subset */
416
	TRY (u->diskshape = hdf5_create_dataspace (1, &total, NULL));
417 418

	hsize_t hstart = (hsize_t)start;
419 420
	hsize_t hstride = 1;
	hsize_t hcount = (hsize_t)u->nparticles;
421 422 423 424

	TRY (hdf5_select_hyperslab_of_dataspace (
	             u->diskshape,
	             H5S_SELECT_SET,
425
	             &hstart, &hstride, &hcount,
426 427 428
	             NULL));

	/* declare local memory datasize */
429 430
	hsize_t dmax = H5S_UNLIMITED;
	TRY (u->memshape = hdf5_create_dataspace (1, &hcount, &dmax));
gsell's avatar
gsell committed
431
	H5_RETURN (H5_SUCCESS);
432 433 434 435 436 437 438 439 440 441 442 443 444
}

h5_err_t
h5u_set_view_indices (
	const h5_file_t fh,	        	/*!< [in]  Handle to open file */
	const h5_size_t *const indices,		/*!< [in]  List of indices */
	h5_size_t nelems		        /*!< [in]  Size of list */
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_err_t,
	                   "f=%p, indices=%p, nelems=%llu",
	                   f, indices, (long long unsigned)nelems);
        CHECK_FILEHANDLE (f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
445 446
        if (f->iteration_gid < 0) {
                TRY (h5_set_iteration (fh, 0));
447 448 449 450 451 452 453 454 455
        }
	hsize_t total = 0;
	hsize_t dmax = H5S_UNLIMITED;
	struct h5u_fdata *u = f->u;

	TRY (h5u_reset_view (fh));

	if ( indices == NULL ) {
		h5_warn ("View indices array is null: reseting view.");
gsell's avatar
gsell committed
456
		H5_LEAVE (H5_SUCCESS);
457 458 459 460 461 462 463 464 465 466 467 468 469
	}
	if (f->u->shape > 0) {
		TRY (total = hdf5_get_npoints_of_dataspace (f->u->shape) );
		h5_debug(
			"Found %lld particles from previous H5PartSetNumParticles call.",
			(long long)total);
        } else {
                TRY (total = h5u_get_totalnum_particles_by_idx (fh,0));
        }
	if (total == 0) {
		/* No datasets have been created yet and no views are set.
		 * We have to leave the view empty because we don't know how
		 * many particles there should be! */
gsell's avatar
gsell committed
470
		H5_LEAVE (H5_SUCCESS);
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	}

	u->nparticles = nelems;

	/* declare overall data size  but then we select a subset */
	TRY (u->diskshape = hdf5_create_dataspace (1, &total, NULL));

	/* declare local memory datasize */
	total = u->nparticles;
	TRY (u->memshape = hdf5_create_dataspace (1, &total, &dmax));
	if (nelems > 0) {
		TRY (hdf5_select_elements_of_dataspace (
		             u->diskshape,
		             H5S_SELECT_SET,
		             (hsize_t)nelems, (hsize_t*)indices ) );
	} else {
		TRY (hdf5_select_none (u->diskshape));
	}
	u->viewindexed = 1;

gsell's avatar
gsell committed
491
	H5_RETURN (H5_SUCCESS);
492 493 494 495 496 497 498 499 500 501 502 503
}

h5_int64_t
h5u_get_view (
	const h5_file_t fh,
	h5_int64_t *start,
	h5_int64_t *end
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_err_t,
	                   "f=%p, start=%p, end=%p",
	                   f, start, end);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
504
	check_iteration_handle_is_valid (f);
505 506 507
	struct h5u_fdata *u = f->u;

	if ( u->viewindexed ) {
508 509 510 511 512
		H5_RETURN_ERROR (
			H5_ERR_INVAL,
			"%s",
			"The current view has an index selection, but "
			"this function only works for ranged views." );
513 514 515 516 517 518 519 520 521 522 523 524
	}

	h5_int64_t viewstart = 0;
	h5_int64_t viewend = 0;

	if ( u->viewstart >= 0 )
		viewstart = u->viewstart;

	if ( u->viewend >= 0 ) {
		viewend = u->viewend;
	}
	else {
Gsell Achim's avatar
* C-API  
Gsell Achim committed
525
		TRY (viewend = h5u_get_num_items (fh));
526 527 528 529 530
	}

	if ( start ) *start = viewstart;
	if ( end ) *end = viewend;

gsell's avatar
gsell committed
531
	H5_RETURN (viewend - viewstart + 1); // view range is *inclusive*
532 533
}

534
h5_err_t
535 536 537 538 539
h5u_set_canonical_view (
	const h5_file_t fh
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_int64_t, "f=%p", f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
540
	check_iteration_handle_is_valid (f);
541 542 543 544 545 546
	h5u_fdata_t* u = f->u;
	TRY( h5u_reset_view (fh) );

	h5_int64_t start = 0;
	h5_int64_t total = 0;

Gsell Achim's avatar
* C-API  
Gsell Achim committed
547
	TRY (total = h5u_get_num_items (fh));
548

gsell's avatar
gsell committed
549 550
	u->nparticles = total / f->nprocs;

551
#ifdef H5_HAVE_PARALLEL
552 553 554 555 556 557 558 559
	h5_int64_t remainder = 0;
	remainder = total % f->nprocs;
	start = f->myproc * u->nparticles;

	/* distribute the remainder */
	if ( f->myproc < remainder ) u->nparticles++;

	/* adjust the offset */
gsell's avatar
gsell committed
560 561 562 563
	if ( f->myproc < remainder )
		start += f->myproc;
	else
		start += remainder;
564
#endif // H5_HAVE_PARALLEL
565 566

	h5_int64_t length = u->nparticles;
567
	TRY (h5u_set_view_length (fh, start, length));
gsell's avatar
gsell committed
568
	H5_RETURN (H5_SUCCESS);
569 570 571 572 573 574 575
}

h5_ssize_t
h5u_get_num_datasets (
	const h5_file_t fh		/*!< [in]  Handle to open file */
	) {
        h5_file_p f = (h5_file_p)fh;
576
	H5_CORE_API_ENTER (h5_ssize_t, "f=%p", f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
577 578
	check_iteration_handle_is_valid (f);
	TRY (ret_value = hdf5_get_num_datasets (f->iteration_gid));
gsell's avatar
gsell committed
579
	H5_RETURN (ret_value);
580 581 582 583 584 585 586 587 588 589 590
}

h5_err_t
h5u_has_dataset (
	const h5_file_t fh,
	const char* const name
	) {
	h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_err_t, 
			   "f=%p, name='%s'",
			   f, name);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
591 592
	check_iteration_handle_is_valid (f);
        TRY (ret_value = hdf5_link_exists (f->iteration_gid, name));
gsell's avatar
gsell committed
593
	H5_RETURN (ret_value);
594 595 596 597 598 599 600 601 602 603 604
}

static inline h5_err_t
get_dataset_info (
	hid_t dataset_id,
	h5_int64_t* dataset_type,
	h5_size_t* dataset_nelem
	) {
        H5_INLINE_FUNC_ENTER (h5_err_t);
	if (dataset_type) {
		h5_int64_t type_;
gsell's avatar
gsell committed
605
		TRY (type_ = h5priv_get_normalized_dataset_type (dataset_id));
606 607 608 609 610 611 612
		TRY (*dataset_type = h5priv_map_hdf5_type_to_enum (type_));
	}
	if (dataset_nelem) {
		h5_ssize_t nelem_;
		TRY (nelem_ = hdf5_get_npoints_of_dataset (dataset_id));
		*dataset_nelem = nelem_;
	}
gsell's avatar
gsell committed
613
	H5_RETURN (H5_SUCCESS);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
}

h5_err_t
h5priv_get_dataset_info_by_idx (
	const hid_t id,			/*!< [in] group ID */
	const h5_id_t dataset_idx,	/*!< [in] Index of the dataset */
	char* dataset_name,		/*!< [out] Name of dataset */
	const h5_size_t len_dataset_name,/*!<[in] Size of buffer */
 	h5_int64_t* dataset_type,	/*!< [out] Type of data in dataset */
	h5_size_t* dataset_nelem	/*!< [out] Number of elements. */
	) {
	H5_PRIV_API_ENTER (h5_err_t, 
			   "id=%lld, "
			   "dataset_idx=%lld, "
			   "dataset_name='%s', len_dataset_name=%llu, "
			   "dataset_type=%p, dataset_nelem=%p",
			   (long long)id,
			   (long long)dataset_idx,
			   dataset_name,
			   (long long unsigned)len_dataset_name,
			   dataset_type, dataset_nelem);
	char dataset_name_[H5_DATANAME_LEN];
	TRY (hdf5_get_name_of_dataset_by_idx (
	             id,
	             dataset_idx,
	             dataset_name_, sizeof(dataset_name_)));
	hid_t dataset_id;
	TRY (dataset_id = hdf5_open_dataset_by_name (id, dataset_name_));
	if (dataset_name) {
		strncpy (dataset_name, dataset_name_, len_dataset_name);
	}
	TRY (get_dataset_info (dataset_id, dataset_type, dataset_nelem));
gsell's avatar
gsell committed
646 647
	TRY (hdf5_close_dataset (dataset_id));
	H5_RETURN (H5_SUCCESS);
648 649 650 651 652 653
}

/*!
   Get information about dataset in current index given by its index
 */
h5_err_t
654 655 656 657 658 659 660
h5u_get_dataset_info_by_idx (
	const h5_file_t fh,		/*!< [in] Handle to open file */
	const h5_id_t idx,		/*!< [in] Index of the dataset */
	char *dataset_name,		/*!< [out] Name of dataset */
	const h5_size_t len_dataset_name,/*!< [in] Size of buffer */
 	h5_int64_t *dataset_type,	/*!< [out] Type of data in dataset */
	h5_size_t *dataset_nelem	/*!< [out] Number of elements. */
661 662 663 664 665 666
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_err_t, 
			   "f=%p, "
			   "idx=%lld, "
			   "dataset_name='%s', len_dataset_name=%llu, "
667
			   "dataset_type=%p, dataset_nelem=%p",
668 669 670 671
			   f,
			   (long long)idx,
			   dataset_name,
			   (long long unsigned)len_dataset_name,
672
			   dataset_type, dataset_nelem);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
673
	check_iteration_handle_is_valid (f);
674
	TRY (h5priv_get_dataset_info_by_idx (
Gsell Achim's avatar
* C-API  
Gsell Achim committed
675
	             f->iteration_gid,
676
	             idx,
677 678
	             dataset_name, len_dataset_name,
		     dataset_type, dataset_nelem));
gsell's avatar
gsell committed
679
	H5_RETURN (H5_SUCCESS);
680 681
}

682
h5_err_t
683 684 685 686 687
h5priv_get_dataset_info_by_name (
	const hid_t id,			/*!< [in] group ID */
	const char* const dataset_name,	/*!< [out] Name of dataset */
 	h5_int64_t* dataset_type,	/*!< [out] Type of data in dataset */
	h5_size_t* dataset_nelem	/*!< [out] Number of elements. */
688
	) {
689 690 691 692 693 694 695 696 697 698
	H5_PRIV_API_ENTER (h5_err_t, 
			   "id=%lld, "
			   "dataset_name='%s' "
			   "dataset_type=%p, dataset_nelem=%p",
			   (long long)id,
			   dataset_name,
			   dataset_type, dataset_nelem);
	hid_t dataset_id;
	TRY (dataset_id = hdf5_open_dataset_by_name (id, dataset_name));
	TRY (get_dataset_info (dataset_id, dataset_type, dataset_nelem));
gsell's avatar
gsell committed
699 700
	TRY (hdf5_close_dataset (dataset_id));
	H5_RETURN (H5_SUCCESS);
701
}
702

703 704 705 706 707
/*!
   Get information about dataset in current index given by its index
 */
h5_err_t
h5u_get_dataset_info_by_name (
708
        const h5_file_t fh,             /*!< [in] Handle to open file */
709
        const char* const dataset_name, /*!< [in] Name of dataset */
710 711
        h5_int64_t* const dataset_type, /*!< [out] Type of data in dataset */
        h5_size_t* const dataset_nelem  /*!< [out] Number of elements. */
712
        ) {
713
	h5_file_p f = (h5_file_p)fh;
714 715 716
	H5_CORE_API_ENTER (h5_err_t,
	                   "f=%p, "
	                   "dataset_name='%s', "
717
	                   "dataset_type=%p, dataset_nelem=%p",
718 719
	                   f,
	                   dataset_name,
720
	                   dataset_type, dataset_nelem);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
721
	check_iteration_handle_is_valid (f);
722
	TRY (h5priv_get_dataset_info_by_name (
Gsell Achim's avatar
* C-API  
Gsell Achim committed
723
	             f->iteration_gid,
724 725
		     dataset_name,
		     dataset_type, dataset_nelem));
gsell's avatar
gsell committed
726
	H5_RETURN (H5_SUCCESS);
727 728 729 730 731 732 733 734
}

h5_err_t
h5u_set_chunk (
        const h5_file_t fh,
        const h5_size_t size
        ) {
        h5_file_p f = (h5_file_p)fh;
735 736 737 738
	H5_CORE_API_ENTER (
		h5_int64_t,
		"f=%p, size=%llu",
		f, (long long unsigned)size);
gsell's avatar
gsell committed
739
	CHECK_FILEHANDLE (f);
740
	if (size == 0) {
741 742 743
		h5_info ("Disabling chunking" );
		TRY (hdf5_set_layout_property (
		             f->u->dcreate_prop, H5D_CONTIGUOUS));
744
	} else {
745 746 747 748
		h5_info ("Setting chunk size to %lld particles", (long long)size);
		TRY (hdf5_set_chunk_property(
		             f->u->dcreate_prop, 1, (hsize_t*)&size));
	}
gsell's avatar
gsell committed
749
	H5_RETURN (H5_SUCCESS);
750 751 752 753 754 755 756 757 758 759
}

h5_err_t
h5u_get_chunk (
	const h5_file_t fh,		/*!< IN: File handle */
	const char *name, 		/*!< IN: name of dataset */
	h5_size_t *size			/*!< OUT: chunk size in particles */
	) {
        h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_int64_t, "f=%p, name='%s', size=%p", f,name,size);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
760
	check_iteration_handle_is_valid (f);
761 762 763 764
	hid_t dataset_id;
	hid_t plist_id;
	hsize_t hsize;

Gsell Achim's avatar
* C-API  
Gsell Achim committed
765
	TRY (dataset_id = hdf5_open_dataset_by_name (f->iteration_gid, name) );
766 767 768 769 770 771 772 773
	TRY (plist_id = hdf5_get_dataset_create_plist (dataset_id) );
	TRY (hdf5_get_chunk_property (plist_id, 1, &hsize) );
	TRY (hdf5_close_property ( plist_id) );
	TRY (hdf5_close_dataset (dataset_id) );

	*size = (h5_size_t)hsize;

	h5_info ("Found chunk size of %lld particles", (long long)*size);
gsell's avatar
gsell committed
774
	H5_RETURN (H5_SUCCESS);
775 776
}