h5u_io.c 6.93 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.
*/

gsell's avatar
gsell committed
10 11 12 13 14
#include "private/h5_types.h"
#include "private/h5_hdf5.h"
#include "private/h5_model.h"
#include "private/h5_io.h"
#include "private/h5u_types.h"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#include "h5core/h5_model.h"
#include "h5core/h5_syscall.h"

/*!
  \ingroup h5_private

  \internal

  Initialize unstructured data internal data structure.

  TODO: Move to file "h5u_openclose.c"

  \return	H5_SUCCESS or error code
*/
h5_err_t
h5upriv_open_file (
	const h5_file_p f		/*!< IN: file handle */
	) {
	H5_PRIV_API_ENTER (h5_err_t, "f=%p", f);
	TRY (f->u = (h5u_fdata_t*)h5_calloc (1, sizeof (*f->u)));
	h5u_fdata_t *u = f->u;

        u->shape = -1;
	u->diskshape = H5S_ALL;
	u->memshape = H5S_ALL;
	u->viewstart = -1;
	u->viewend = -1;
	u->viewindexed = 0;

	TRY (u->dcreate_prop = hdf5_create_property (H5P_DATASET_CREATE));

gsell's avatar
gsell committed
47
	H5_RETURN (H5_SUCCESS);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

/*!
  \ingroup h5_private

  \internal

  De-initialize H5Block internal structure.  Open HDF5 objects are 
  closed and allocated memory freed.

  \return	H5_SUCCESS or error code
*/
h5_err_t
h5upriv_close_file (
	const h5_file_p f	/*!< file handle */
	) {
	H5_PRIV_API_ENTER (h5_err_t, "f=%p", f);
	struct h5u_fdata* u = f->u;

	h5_errno = H5_SUCCESS;
	TRY (hdf5_close_dataspace (u->shape));
	TRY (hdf5_close_dataspace (u->diskshape));
	TRY (hdf5_close_dataspace (u->memshape));
	TRY (hdf5_close_property (u->dcreate_prop));
	TRY (h5_free (f->u));
	f->u = NULL;

gsell's avatar
gsell committed
75
	H5_RETURN (H5_SUCCESS);
76 77 78
}

h5_err_t
Gsell Achim's avatar
* C-API  
Gsell Achim committed
79
h5u_read_dataset (
80
	const h5_file_t fh,	/*!< [in] Handle to open file */
Gsell Achim's avatar
* C-API  
Gsell Achim committed
81
	char* const name,	/*!< [in] Name to associate dataset with */
82
	void* data,		/*!< [out] Array of data */
gsell's avatar
gsell committed
83
	const h5_types_t type
84 85
	) {
        h5_file_p f = (h5_file_p)fh;
gsell's avatar
gsell committed
86 87 88
	H5_CORE_API_ENTER (h5_err_t,
			   "f=%p, name='%s', data=%p, type=%lld",
	                   f, name, data, (long long int)type);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
89
	check_iteration_is_readable (f);
90

Gsell Achim's avatar
* C-API  
Gsell Achim committed
91
	TRY (h5priv_normalize_dataset_name (name));
gsell's avatar
gsell committed
92 93
	hid_t hdf5_type;
	TRY (hdf5_type = h5priv_map_enum_to_normalized_type (type));
Gsell Achim's avatar
* C-API  
Gsell Achim committed
94 95
	if ( f->iteration_gid < 0 ) {
		TRY (h5_set_iteration ((h5_file_t)f, f->iteration_idx));
96 97
	}

Gsell Achim's avatar
* C-API  
Gsell Achim committed
98 99
	hid_t dataset_id;
	TRY (dataset_id = hdf5_open_dataset_by_name (f->iteration_gid, name));
100

Gsell Achim's avatar
* C-API  
Gsell Achim committed
101
	
102
	/* default spaces, if not using a view selection */
Gsell Achim's avatar
* C-API  
Gsell Achim committed
103 104
	hid_t memspace_id = H5S_ALL;
	hid_t space_id;
105 106 107
	TRY (space_id = hdf5_get_dataset_space (dataset_id));

	/* get the number of elements on disk for the datset */
Gsell Achim's avatar
* C-API  
Gsell Achim committed
108
	hsize_t ndisk;
109
	TRY (ndisk = hdf5_get_npoints_of_dataspace (space_id));
Gsell Achim's avatar
* C-API  
Gsell Achim committed
110 111 112
	hsize_t nread;
	if (f->u->diskshape != H5S_ALL) {
		TRY (nread = hdf5_get_selected_npoints_of_dataspace (f->u->diskshape));
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

		/* make sure the disk space selected by the view doesn't
		 * exceed the size of the dataset */
		if (nread <= ndisk) {
			/* we no longer need the dataset space... */
			TRY (hdf5_close_dataspace(space_id));
			/* ...because it's safe to use the view selection */
			space_id = f->u->diskshape;
		} else {
			/* the view selection is too big?
			 * fall back to using the dataset space */
			h5_warn (
			        "Ignoring view: dataset[%s] has fewer "
			        "elements on disk (%lld) than are selected "
			        "(%lld).",
Gsell Achim's avatar
* C-API  
Gsell Achim committed
128
			        name, (long long)ndisk, (long long)nread );
129 130 131 132 133 134 135 136
			nread = ndisk;
		}
	} else {
		/* since the view selection is H5S_ALL, we will
		 * read all available elements in the dataset space */
		nread = ndisk;
	}

Gsell Achim's avatar
* C-API  
Gsell Achim committed
137 138 139
	if (f->u->memshape != H5S_ALL) {
		hid_t nmem;
		TRY (nmem = hdf5_get_npoints_of_dataspace (f->u->memshape));
140 141 142 143 144 145 146 147 148 149 150 151

		/* make sure the memory space selected by the view has
		 * enough capacity for the read */
		if (nmem >= nread) {
			memspace_id = f->u->memshape;
		} else {
			/* the view selection is too small?
			 * fall back to using H5S_ALL */
			h5_warn (
			        "Ignoring view: dataset[%s] has more "
			        "elements selected (%lld) than are available "
			        "in memory (%lld).",
Gsell Achim's avatar
* C-API  
Gsell Achim committed
152
			        name, (long long)nread, (long long)nmem );
153 154 155 156 157 158
			memspace_id = H5S_ALL;
		}
	}
	TRY (h5priv_start_throttle (f));
	TRY (hdf5_read_dataset (
	             dataset_id,
gsell's avatar
gsell committed
159
	             hdf5_type,
160 161 162 163 164 165 166 167 168 169 170
	             memspace_id,
	             space_id,
	             f->props->xfer_prop,
	             data ));
	TRY (h5priv_end_throttle (f));
	if (space_id != f->u->diskshape) {
		TRY (hdf5_close_dataspace (space_id));
	}

	TRY (hdf5_close_dataset (dataset_id));

gsell's avatar
gsell committed
171
	H5_RETURN (H5_SUCCESS);
172 173 174
}

h5_err_t
Gsell Achim's avatar
* C-API  
Gsell Achim committed
175
h5u_write (
176
	const h5_file_t fh,	/*!< IN: Handle to open file */
Gsell Achim's avatar
* C-API  
Gsell Achim committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	hid_t dset_id,
	hid_t type,
	const void* data
	) {
	h5_file_p f = (h5_file_p)fh;
	H5_CORE_API_ENTER (h5_err_t,
			   "f=%p, dset_id=%lld, type=%lld, data=%p",
	                   f, (long long)dset_id, (long long int)type, data);
	TRY (h5priv_start_throttle (f));
	hid_t hdf5_type;
	TRY (hdf5_type = h5priv_map_enum_to_normalized_type (type));
	TRY (hdf5_write_dataset (
	             dset_id,
	             hdf5_type,
	             f->u->memshape,
	             f->u->diskshape,
	             f->props->xfer_prop,
	             data));
	TRY (h5priv_end_throttle (f));
	f->empty = 0;
	if (f->props->flush) {
		TRY (hdf5_flush (f->iteration_gid, H5F_SCOPE_LOCAL));
	}
	H5_RETURN (H5_SUCCESS);
}	

hid_t
h5u_open_dataset (
	const h5_file_t fh,	/*!< IN: Handle to open file */
	char* const name,	/*!< IN: Name to associate array with */
gsell's avatar
gsell committed
207
	const h5_types_t type	/*!< IN: Type of data */
208 209
	) {
        h5_file_p f = (h5_file_p)fh;
gsell's avatar
gsell committed
210
	H5_CORE_API_ENTER (h5_err_t,
Gsell Achim's avatar
* C-API  
Gsell Achim committed
211 212 213 214 215
			   "f=%p, name='%s', type=%lld",
	                   f, name, (long long int)type);
	check_iteration_handle_is_valid (f);

	TRY (h5priv_normalize_dataset_name (name));
gsell's avatar
gsell committed
216 217
	hid_t hdf5_type;
	TRY (hdf5_type = h5priv_map_enum_to_normalized_type (type));
Gsell Achim's avatar
* C-API  
Gsell Achim committed
218 219 220
	if ( f->iteration_gid < 0 ) {
		TRY (h5_set_iteration ((h5_file_t)f, f->iteration_idx));
	}
221

Gsell Achim's avatar
* C-API  
Gsell Achim committed
222
	if (f->u->shape == H5S_ALL )
223 224
		h5_warn("The view is unset or invalid.");

Gsell Achim's avatar
* C-API  
Gsell Achim committed
225
	hid_t dset_id;
226
	H5E_BEGIN_TRY
Gsell Achim's avatar
* C-API  
Gsell Achim committed
227
	        dset_id = H5Dopen(f->iteration_gid, name, H5P_DEFAULT);
228 229 230 231
	H5E_END_TRY

	if (dset_id > 0) {
		h5_warn("Dataset %s/%s already exists",
Gsell Achim's avatar
* C-API  
Gsell Achim committed
232
		        hdf5_get_objname (f->iteration_gid), name);
233 234
	} else {
		TRY (dset_id = hdf5_create_dataset (
Gsell Achim's avatar
* C-API  
Gsell Achim committed
235 236
		             f->iteration_gid,
		             name,
gsell's avatar
gsell committed
237
		             hdf5_type,
Gsell Achim's avatar
* C-API  
Gsell Achim committed
238
		             f->u->shape,
239 240
		             H5P_DEFAULT));
	}
Gsell Achim's avatar
* C-API  
Gsell Achim committed
241 242
	H5_RETURN (dset_id);
}
243

Gsell Achim's avatar
* C-API  
Gsell Achim committed
244 245 246 247 248 249 250 251 252
h5_err_t
h5u_write_dataset (
	const h5_file_t fh,	/*!< IN: Handle to open file */
	char* const name,	/*!< IN: Name to associate array with */
	const void* data,	/*!< IN: Array to commit to disk */
	const h5_types_t type	/*!< IN: Type of data */
	) {
	H5_CORE_API_ENTER (h5_err_t,
			   "f=%p, name='%s', data=%p, type=%lld",
253
	                   (void*)fh, name, data, (long long int)type);
Gsell Achim's avatar
* C-API  
Gsell Achim committed
254 255 256
	hid_t dset_id;
	TRY (dset_id = h5u_open_dataset (fh, name, type));
	TRY (h5u_write (fh, dset_id, type, data));
257
        TRY (hdf5_close_dataset(dset_id));
gsell's avatar
gsell committed
258
	H5_RETURN (H5_SUCCESS);
259
}