29 #include "splash/sdc_defines.hpp" 31 #include "splash/core/DCDataSet.hpp" 32 #include "splash/core/DCAttribute.hpp" 33 #include "splash/core/DCHelper.hpp" 34 #include "splash/core/logging.hpp" 35 #include "splash/DCException.hpp" 36 #include "splash/basetypes/ColTypeDim.hpp" 41 std::string DCDataSet::getExceptionString(std::string msg)
43 return (std::string(
"Exception for DCDataSet [") + name + std::string(
"] ") +
47 void DCDataSet::splitPath(
const std::string fullName, std::string &path, std::string &name)
49 std::string::size_type pos = fullName.find_last_of(
'/');
51 if (pos == std::string::npos || (pos == fullName.size() - 1))
57 path.assign(fullName.c_str(), fullName.c_str() + pos);
58 name.assign(fullName.c_str() + pos + 1);
62 void DCDataSet::getFullDataPath(
const std::string fullUserName,
63 const std::string pathBase, uint32_t
id, std::string &path, std::string &name)
65 splitPath(fullUserName, path, name);
67 std::stringstream group_id_name;
68 group_id_name << pathBase <<
"/" << id;
70 group_id_name <<
"/" << path;
72 path.assign(group_id_name.str());
75 DCDataSet::DCDataSet(
const std::string name) :
87 dsetProperties = H5Pcreate(H5P_DATASET_CREATE);
88 dsetWriteProperties = H5P_DEFAULT;
89 dsetReadProperties = H5P_DEFAULT;
92 DCDataSet::~DCDataSet()
94 H5Pclose(dsetProperties);
97 Dimensions DCDataSet::getSize()
const 102 Dimensions& DCDataSet::getLogicalSize()
107 Dimensions DCDataSet::getPhysicalSize()
109 Dimensions result(logicalSize);
110 result.swapDims(ndims);
114 bool DCDataSet::open(hid_t group)
117 if (checkExistence && !H5Lexists(group, name.c_str(), H5P_LINK_ACCESS_DEFAULT))
120 dataset = H5Dopen(group, name.c_str(), H5P_DATASET_ACCESS_DEFAULT);
123 throw DCException(getExceptionString(
"open: Failed to open dataset"));
125 datatype = H5Dget_type(dataset);
129 throw DCException(getExceptionString(
"open: Failed to get type of dataset"));
132 dataspace = H5Dget_space(dataset);
136 throw DCException(getExceptionString(
"open: Failed to open dataspace"));
139 int dims_result = H5Sget_simple_extent_ndims(dataspace);
143 throw DCException(getExceptionString(
"open: Failed to get dimensions"));
148 getLogicalSize().set(1, 1, 1);
149 if (H5Sget_simple_extent_dims(dataspace, getLogicalSize().getPointer(), NULL) < 0)
152 throw DCException(getExceptionString(
"open: Failed to get sizes"));
155 getLogicalSize().swapDims(ndims);
162 void DCDataSet::setChunking(
size_t typeSize)
165 if (getPhysicalSize().getScalarSize() != 0)
168 hsize_t chunk_dims[ndims];
169 DCHelper::getOptimalChunkDims(getPhysicalSize().getPointer(), ndims,
170 typeSize, chunk_dims);
172 if (H5Pset_chunk(this->dsetProperties, ndims, chunk_dims) < 0)
174 for (
size_t i = 0; i < ndims; ++i)
176 log_msg(1,
"chunk_dims[%llu] = %llu",
177 (
long long unsigned) i, (
long long unsigned) (chunk_dims[i]));
179 throw DCException(getExceptionString(
"setChunking: Failed to set chunking"));
184 void DCDataSet::setCompression()
187 if (this->compression && getPhysicalSize().getScalarSize() != 0)
191 if (H5Pset_shuffle(this->dsetProperties) < 0 ||
192 H5Pset_deflate(this->dsetProperties, 1) < 0)
193 throw DCException(getExceptionString(
"setCompression: Failed to set compression"));
197 void DCDataSet::create(
const CollectionType& colType,
198 hid_t group,
const Dimensions size, uint32_t ndims,
199 bool compression,
bool extensible)
202 log_msg(2,
"DCDataSet::create (%s, size %s)", name.c_str(), size.toString().c_str());
205 throw DCException(getExceptionString(
"create: dataset is already open"));
211 if (!checkExistence || (checkExistence && H5Lexists(group, name.c_str(), H5P_LINK_ACCESS_DEFAULT)))
212 H5Ldelete(group, name.c_str(), H5P_LINK_ACCESS_DEFAULT);
215 this->compression = compression;
216 this->datatype = colType.getDataType();
218 getLogicalSize().set(size);
220 setChunking(colType.getSize());
223 if (getPhysicalSize().getScalarSize() != 0)
225 hsize_t *max_dims =
new hsize_t[ndims];
226 for (
size_t i = 0; i < ndims; ++i)
229 max_dims[i] = H5F_UNLIMITED;
231 max_dims[i] = getPhysicalSize()[i];
234 dataspace = H5Screate_simple(ndims, getPhysicalSize().getPointer(), max_dims);
239 dataspace = H5Screate(H5S_NULL);
244 throw DCException(getExceptionString(
"create: Failed to create dataspace"));
247 dataset = H5Dcreate(group, this->name.c_str(), this->datatype, dataspace,
248 H5P_DEFAULT, dsetProperties, H5P_DEFAULT);
251 throw DCException(getExceptionString(
"create: Failed to create dataset"));
257 void DCDataSet::createReference(hid_t refGroup,
259 DCDataSet &srcDataSet)
263 throw DCException(getExceptionString(
"createReference: dataset is already open"));
265 if (checkExistence && H5Lexists(refGroup, name.c_str(), H5P_LINK_ACCESS_DEFAULT))
266 throw DCException(getExceptionString(
"createReference: this reference already exists"));
268 getLogicalSize().set(srcDataSet.getLogicalSize());
269 this->ndims = srcDataSet.getNDims();
271 if (H5Rcreate(®ionRef, srcGroup, srcDataSet.getName().c_str(), H5R_OBJECT, -1) < 0)
272 throw DCException(getExceptionString(
"createReference: failed to create region reference"));
275 dataspace = H5Screate_simple(1, &ndims, NULL);
277 throw DCException(getExceptionString(
"createReference: failed to create dataspace for reference"));
279 dataset = H5Dcreate(refGroup, name.c_str(), H5T_STD_REF_OBJ,
280 dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT);
283 throw DCException(getExceptionString(
"createReference: failed to create dataset for reference"));
285 if (H5Dwrite(dataset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL,
286 dsetWriteProperties, ®ionRef) < 0)
287 throw DCException(getExceptionString(
"createReference: failed to write reference"));
293 void DCDataSet::createReference(hid_t refGroup,
295 DCDataSet &srcDataSet,
302 throw DCException(getExceptionString(
"createReference: dataset is already open"));
304 if (checkExistence && H5Lexists(refGroup, name.c_str(), H5P_LINK_ACCESS_DEFAULT))
305 throw DCException(getExceptionString(
"createReference: this reference already exists"));
307 getLogicalSize().set(count);
308 this->ndims = srcDataSet.getNDims();
310 count.swapDims(this->ndims);
311 offset.swapDims(this->ndims);
312 stride.swapDims(this->ndims);
315 if (H5Sselect_hyperslab(srcDataSet.getDataSpace(), H5S_SELECT_SET,
316 offset.getPointer(), stride.getPointer(),
317 count.getPointer(), NULL) < 0 ||
318 H5Sselect_valid(srcDataSet.getDataSpace()) <= 0)
319 throw DCException(getExceptionString(
"createReference: failed to select hyperslap for reference"));
321 if (H5Rcreate(®ionRef, srcGroup, srcDataSet.getName().c_str(), H5R_DATASET_REGION,
322 srcDataSet.getDataSpace()) < 0)
323 throw DCException(getExceptionString(
"createReference: failed to create region reference"));
326 dataspace = H5Screate_simple(1, &ndims, NULL);
328 throw DCException(getExceptionString(
"createReference: failed to create dataspace for reference"));
330 dataset = H5Dcreate(refGroup, name.c_str(), H5T_STD_REF_DSETREG,
331 dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT);
334 throw DCException(getExceptionString(
"createReference: failed to create dataset for reference"));
336 if (H5Dwrite(dataset, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL,
337 dsetWriteProperties, ®ionRef) < 0)
338 throw DCException(getExceptionString(
"createReference: failed to write reference"));
344 void DCDataSet::close()
350 if (H5Dclose(dataset) < 0 || H5Sclose(dataspace) < 0)
351 throw DCException(getExceptionString(
"close: Failed to close dataset"));
354 size_t DCDataSet::getNDims()
359 hid_t DCDataSet::getDataSpace()
363 throw DCException(getExceptionString(
"getDataSpace: dataset is not opened"));
368 DCDataType DCDataSet::getDCDataType() throw (DCException)
371 throw DCException(getExceptionString(
"getDCDataType: dataset is not opened"));
375 H5T_class_t type_class = H5Tget_class(datatype);
376 size_t type_size = H5Tget_size(datatype);
377 H5T_sign_t type_signed = H5Tget_sign(datatype);
379 if (type_class == H5T_INTEGER)
381 if (type_signed == H5T_SGN_NONE)
383 if (type_size ==
sizeof (uint64_t))
384 result = DCDT_UINT64;
386 result = DCDT_UINT32;
389 if (type_size ==
sizeof (int64_t))
395 if (type_class == H5T_FLOAT)
398 if (type_size ==
sizeof (
float))
399 result = DCDT_FLOAT32;
401 if (type_size ==
sizeof (
double))
402 result = DCDT_FLOAT64;
408 size_t DCDataSet::getDataTypeSize()
412 throw DCException(getExceptionString(
"getDataTypeSize: dataset is not opened"));
414 size_t size = H5Tget_size(this->datatype);
416 throw DCException(getExceptionString(
"getDataTypeSize: could not get size of datatype"));
421 std::string DCDataSet::getName()
426 void DCDataSet::read(Dimensions dstBuffer,
427 Dimensions dstOffset,
428 Dimensions &sizeRead,
433 read(dstBuffer, dstOffset, getLogicalSize(), Dimensions(0, 0, 0),
434 sizeRead, srcNDims, dst);
437 void DCDataSet::read(Dimensions dstBuffer,
438 Dimensions dstOffset,
440 Dimensions srcOffset,
441 Dimensions& sizeRead,
446 log_msg(2,
"DCDataSet::read (%s)", name.c_str());
449 throw DCException(getExceptionString(
"read: Dataset has not been opened/created"));
451 if (dstBuffer.getScalarSize() == 0)
452 dstBuffer.set(srcSize);
461 " logical_size = %s\n" 462 " physical_size = %s\n" 467 (
long long unsigned) ndims,
468 getLogicalSize().toString().c_str(),
469 getPhysicalSize().toString().c_str(),
470 dstBuffer.toString().c_str(),
471 dstOffset.toString().c_str(),
472 srcSize.toString().c_str(),
473 srcOffset.toString().c_str());
475 dstBuffer.swapDims(ndims);
476 dstOffset.swapDims(ndims);
477 srcSize.swapDims(ndims);
478 srcOffset.swapDims(ndims);
480 hid_t dst_dataspace = H5Screate_simple(ndims, dstBuffer.getPointer(), NULL);
481 if (dst_dataspace < 0)
482 throw DCException(getExceptionString(
"read: Failed to create target dataspace"));
485 H5Sselect_none(dst_dataspace);
487 if (H5Sselect_hyperslab(dst_dataspace, H5S_SELECT_SET, dstOffset.getPointer(), NULL,
488 srcSize.getPointer(), NULL) < 0 ||
489 H5Sselect_valid(dst_dataspace) <= 0)
490 throw DCException(getExceptionString(
"read: Target dataspace hyperslab selection is not valid!"));
493 if (!dst || srcSize.getScalarSize() == 0) {
494 H5Sselect_none(dataspace);
496 if (H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, srcOffset.getPointer(), NULL,
497 srcSize.getPointer(), NULL) < 0 ||
498 H5Sselect_valid(dataspace) <= 0)
499 throw DCException(getExceptionString(
"read: Source dataspace hyperslab selection is not valid!"));
502 if (H5Dread(dataset, this->datatype, dst_dataspace, dataspace, dsetReadProperties, dst) < 0)
503 throw DCException(getExceptionString(
"read: Failed to read dataset"));
505 H5Sclose(dst_dataspace);
507 srcSize.swapDims(ndims);
511 sizeRead.set(srcSize);
512 srcNDims = this->ndims;
514 log_msg(3,
" returns ndims = %llu", (
long long unsigned) ndims);
515 log_msg(3,
" returns sizeRead = %s", sizeRead.toString().c_str());
518 void DCDataSet::write(
520 Dimensions dstOffset,
524 log_msg(2,
"DCDataSet::write (%s)", name.c_str());
527 throw DCException(getExceptionString(
"write: Dataset has not been opened/created"));
531 " logical_size = %s\n" 532 " physical_size = %s\n" 534 " dst_offset = %s\n",
535 (
long long unsigned) ndims,
536 getLogicalSize().toString().c_str(),
537 getPhysicalSize().toString().c_str(),
538 srcSelect.toString().c_str(),
539 dstOffset.toString().c_str());
542 srcSelect.swapDims(ndims);
543 dstOffset.swapDims(ndims);
548 if (getLogicalSize().getScalarSize() != 0)
550 dsp_src = H5Screate_simple(ndims, srcSelect.size.getPointer(), NULL);
552 throw DCException(getExceptionString(
"write: Failed to create source dataspace"));
555 if ((srcSelect.offset.getScalarSize() != 0) || (srcSelect.count != srcSelect.size) ||
556 (srcSelect.stride.getScalarSize() != 1))
558 if (H5Sselect_hyperslab(dsp_src, H5S_SELECT_SET, srcSelect.offset.getPointer(),
559 srcSelect.stride.getPointer(), srcSelect.count.getPointer(), NULL) < 0 ||
560 H5Sselect_valid(dsp_src) <= 0)
561 throw DCException(getExceptionString(
"write: Invalid source hyperslap selection"));
564 if (srcSelect.count.getScalarSize() == 0)
565 H5Sselect_none(dsp_src);
569 if ((dstOffset.getScalarSize() != 0) || (srcSelect.count != getPhysicalSize()))
571 if (H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, dstOffset.getPointer(),
572 NULL, srcSelect.count.getPointer(), NULL) < 0 ||
573 H5Sselect_valid(dataspace) <= 0)
574 throw DCException(getExceptionString(
"write: Invalid target hyperslap selection"));
577 if (!data || (srcSelect.count.getScalarSize() == 0))
579 H5Sselect_none(dataspace);
585 if (H5Dwrite(dataset, this->datatype, dsp_src, dataspace, dsetWriteProperties, data) < 0)
586 throw DCException(getExceptionString(
"write: Failed to write dataset"));
592 void DCDataSet::append(
size_t count,
size_t offset,
size_t stride,
const void* data)
595 log_msg(2,
"DCDataSet::append");
598 throw DCException(getExceptionString(
"append: Dataset has not been opened/created."));
600 log_msg(3,
"logical_size = %s", getLogicalSize().toString().c_str());
602 Dimensions target_offset(getLogicalSize());
604 getLogicalSize()[0] += count;
606 hsize_t * max_dims =
new hsize_t[ndims];
607 for (
size_t i = 0; i < ndims; ++i)
608 max_dims[i] = H5F_UNLIMITED;
610 if (H5Sset_extent_simple(dataspace, 1, getLogicalSize().getPointer(), max_dims) < 0)
611 throw DCException(getExceptionString(
"append: Failed to set new extent"));
616 log_msg(3,
"logical_size = %s", getLogicalSize().toString().c_str());
618 if (H5Dset_extent(dataset, getLogicalSize().getPointer()) < 0)
619 throw DCException(getExceptionString(
"append: Failed to extend dataset"));
622 Dimensions dim_data(count, 1, 1);
623 if (H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, target_offset.getPointer(),
624 NULL, dim_data.getPointer(), NULL) < 0 ||
625 H5Sselect_valid(dataspace) < 0)
626 throw DCException(getExceptionString(
"append: Invalid target hyperslap selection"));
630 Dimensions dim_src(offset + count * stride, 1, 1);
631 hid_t dsp_src = H5Screate_simple(1, dim_src.getPointer(), NULL);
633 throw DCException(getExceptionString(
"append: Failed to create src dataspace while appending"));
635 if (H5Sselect_hyperslab(dsp_src, H5S_SELECT_SET, Dimensions(offset, 0, 0).getPointer(),
636 Dimensions(stride, 1, 1).getPointer(), dim_data.getPointer(), NULL) < 0 ||
637 H5Sselect_valid(dsp_src) < 0)
638 throw DCException(getExceptionString(
"append: Invalid source hyperslap selection"));
640 if (!data || (count == 0))
642 H5Sselect_none(dataspace);
646 if (H5Dwrite(dataset, this->datatype, dsp_src, dataspace, dsetWriteProperties, data) < 0)
647 throw DCException(getExceptionString(
"append: Failed to append dataset"));
EXTERN void log_msg(int level, const char *fmt,...)
void read(const CollectionType &colType, void *buf)