h5u_model.c 21.7 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 155 156 157 158 159 160 161 162 163 164
	}
#endif

	TRY (h5u_reset_view(fh));

	TRY (hdf5_close_dataspace (u->shape));
	u->shape = H5S_ALL;

	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)
	 */
165 166
	if (stride > 1) {
		h5_debug ("Striding by %lld elements.", (long long)stride);
167
		start = 0;
168
                hsize_t hstride = (hsize_t)stride;
169 170 171 172 173 174 175 176
		count = u->nparticles;
		TRY (hdf5_select_hyperslab_of_dataspace (
                             u->memshape,
                             H5S_SELECT_SET,
                             &start, &hstride, &count,
                             NULL));
	}

177
#ifndef H5_HAVE_PARALLEL
178 179 180 181
	count = u->nparticles;
	TRY( u->shape = hdf5_create_dataspace (1, &count, NULL));
	u->viewstart = 0;
	u->viewend   = nparticles - 1; // view range is *inclusive*
182
#else /* H5_HAVE_PARALLEL */
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	/*
	 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
205
	h5_debug("Start index on this processor: %lld.", (long long)start);
206 207 208 209 210 211 212 213 214 215 216 217 218

	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 */
	TRY( u->diskshape = hdf5_create_dataspace(1, &count, NULL) );

	count = nparticles;
	if (count > 0) {
219
                hsize_t hstride = 1;
220 221 222 223 224 225 226 227 228
		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
229
	H5_RETURN (H5_SUCCESS);
230 231 232 233
}

h5_err_t
h5u_has_view (
234
	const h5_file_t fh
235 236 237
	) {
        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
238
	check_iteration_handle_is_valid (f);
gsell's avatar
gsell committed
239
	H5_RETURN (f->u->viewindexed || f->u->viewstart >= 0);
240 241 242 243 244 245 246 247
}

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
248
	check_iteration_handle_is_valid (f);
249 250 251 252 253 254 255 256 257 258
	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
259
	H5_RETURN (H5_SUCCESS);
260 261
}

262 263
/*
  if start == -1 && end == -1 -> reset view
gsell's avatar
gsell committed
264
  elif end == -1 -> select to end
265 266

 */
267 268 269 270 271 272 273 274 275 276
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
277
	check_iteration_handle_is_valid (f);
278 279 280 281
	struct h5u_fdata *u = f->u;

	TRY (h5u_reset_view (fh));

282
	if (start == -1 && end == -1)   // we are already done
gsell's avatar
gsell committed
283
		H5_LEAVE (H5_SUCCESS);
284

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

	h5_debug (
339
	        "This view includes %lld particles.",
340 341
	        (long long)u->nparticles );

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

	hsize_t hstart = (hsize_t)start;
347 348
	hsize_t hstride = 1;
	hsize_t hcount = (hsize_t)u->nparticles;
349 350 351 352

	TRY (hdf5_select_hyperslab_of_dataspace (
	             u->diskshape,
	             H5S_SELECT_SET,
353
	             &hstart, &hstride, &hcount,
354 355 356
	             NULL));

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

h5_err_t
363
h5u_set_view_length (
364 365 366 367 368 369 370 371
	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
372
	check_iteration_handle_is_valid (f);
373 374 375 376 377
	struct h5u_fdata *u = f->u;

	TRY (h5u_reset_view (fh));

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

380
	hsize_t total = 0;
381 382 383 384 385 386 387 388 389 390 391 392
	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
393
		H5_LEAVE (H5_SUCCESS);
394 395 396
	}

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

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

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

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

	hsize_t hstart = (hsize_t)start;
416 417
	hsize_t hstride = 1;
	hsize_t hcount = (hsize_t)u->nparticles;
418 419 420 421

	TRY (hdf5_select_hyperslab_of_dataspace (
	             u->diskshape,
	             H5S_SELECT_SET,
422
	             &hstart, &hstride, &hcount,
423 424 425
	             NULL));

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

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
442 443
        if (f->iteration_gid < 0) {
                TRY (h5_set_iteration (fh, 0));
444 445 446 447 448 449 450 451 452
        }
	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
453
		H5_LEAVE (H5_SUCCESS);
454 455 456 457 458 459 460 461 462 463 464 465 466
	}
	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
467
		H5_LEAVE (H5_SUCCESS);
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	}

	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
488
	H5_RETURN (H5_SUCCESS);
489 490 491 492 493 494 495 496 497 498 499 500
}

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
501
	check_iteration_handle_is_valid (f);
502 503 504
	struct h5u_fdata *u = f->u;

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

	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
522
		TRY (viewend = h5u_get_num_items (fh));
523 524 525 526 527
	}

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

gsell's avatar
gsell committed
528
	H5_RETURN (viewend - viewstart + 1); // view range is *inclusive*
529 530
}

531
h5_err_t
532 533 534 535 536
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
537
	check_iteration_handle_is_valid (f);
538 539 540 541 542 543
	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
544
	TRY (total = h5u_get_num_items (fh));
545

gsell's avatar
gsell committed
546 547
	u->nparticles = total / f->nprocs;

548
#ifdef H5_HAVE_PARALLEL
549 550 551 552 553 554 555 556
	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
557 558 559 560
	if ( f->myproc < remainder )
		start += f->myproc;
	else
		start += remainder;
561
#endif // H5_HAVE_PARALLEL
562 563

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

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;
573
	H5_CORE_API_ENTER (h5_ssize_t, "f=%p", f);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
574 575
	check_iteration_handle_is_valid (f);
	TRY (ret_value = hdf5_get_num_datasets (f->iteration_gid));
gsell's avatar
gsell committed
576
	H5_RETURN (ret_value);
577 578 579 580 581 582 583 584 585 586 587
}

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
588 589
	check_iteration_handle_is_valid (f);
        TRY (ret_value = hdf5_link_exists (f->iteration_gid, name));
gsell's avatar
gsell committed
590
	H5_RETURN (ret_value);
591 592 593 594 595 596 597 598 599 600 601
}

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
602
		TRY (type_ = h5priv_get_normalized_dataset_type (dataset_id));
603 604 605 606 607 608 609
		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
610
	H5_RETURN (H5_SUCCESS);
611 612 613 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
}

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
643 644
	TRY (hdf5_close_dataset (dataset_id));
	H5_RETURN (H5_SUCCESS);
645 646 647 648 649 650
}

/*!
   Get information about dataset in current index given by its index
 */
h5_err_t
651 652 653 654 655 656 657
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. */
658 659 660 661 662 663
	) {
        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, "
664
			   "dataset_type=%p, dataset_nelem=%p",
665 666 667 668
			   f,
			   (long long)idx,
			   dataset_name,
			   (long long unsigned)len_dataset_name,
669
			   dataset_type, dataset_nelem);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
670
	check_iteration_handle_is_valid (f);
671
	TRY (h5priv_get_dataset_info_by_idx (
Gsell Achim's avatar
* C-API  
Gsell Achim committed
672
	             f->iteration_gid,
673
	             idx,
674 675
	             dataset_name, len_dataset_name,
		     dataset_type, dataset_nelem));
gsell's avatar
gsell committed
676
	H5_RETURN (H5_SUCCESS);
677 678
}

679
h5_err_t
680 681 682 683 684
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. */
685
	) {
686 687 688 689 690 691 692 693 694 695
	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
696 697
	TRY (hdf5_close_dataset (dataset_id));
	H5_RETURN (H5_SUCCESS);
698
}
699

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

h5_err_t
h5u_set_chunk (
        const h5_file_t fh,
        const h5_size_t size
        ) {
        h5_file_p f = (h5_file_p)fh;
732 733 734 735
	H5_CORE_API_ENTER (
		h5_int64_t,
		"f=%p, size=%llu",
		f, (long long unsigned)size);
gsell's avatar
gsell committed
736
	CHECK_FILEHANDLE (f);
737
	if (size == 0) {
738 739 740
		h5_info ("Disabling chunking" );
		TRY (hdf5_set_layout_property (
		             f->u->dcreate_prop, H5D_CONTIGUOUS));
741
	} else {
742 743 744 745
		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
746
	H5_RETURN (H5_SUCCESS);
747 748 749 750 751 752 753 754 755 756
}

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
757
	check_iteration_handle_is_valid (f);
758 759 760 761
	hid_t dataset_id;
	hid_t plist_id;
	hsize_t hsize;

Gsell Achim's avatar
* C-API  
Gsell Achim committed
762
	TRY (dataset_id = hdf5_open_dataset_by_name (f->iteration_gid, name) );
763 764 765 766 767 768 769 770
	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
771
	H5_RETURN (H5_SUCCESS);
772 773
}