/usr/include/vigra/multi_array_chunked_hdf5.hxx is in libvigraimpex-dev 1.10.0+git20160211.167be93+dfsg-2+b5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 47 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | /************************************************************************/
/* */
/* Copyright 2012-2014 by Ullrich Koethe and Thorben Kroeger */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_MULTI_ARRAY_CHUNKED_HDF5_HXX
#define VIGRA_MULTI_ARRAY_CHUNKED_HDF5_HXX
#include <queue>
#include "multi_array_chunked.hxx"
#include "hdf5impex.hxx"
// Bounds checking Macro used if VIGRA_CHECK_BOUNDS is defined.
#ifdef VIGRA_CHECK_BOUNDS
#define VIGRA_ASSERT_INSIDE(diff) \
vigra_precondition(this->isInside(diff), "Index out of bounds")
#else
#define VIGRA_ASSERT_INSIDE(diff)
#endif
namespace vigra {
/** \addtogroup ChunkedArrayClasses
*/
//@{
/** Implement ChunkedArray as a chunked dataset in an HDF5 file.
<b>\#include</b> \<vigra/multi_array_chunked_hdf5.hxx\> <br/>
Namespace: vigra
This uses the native chunking and compression functionality provided by the
HDF5 library. Note: This file must only be included when the HDF5 headers
and libraries are installed on the system.
*/
template <unsigned int N, class T, class Alloc = std::allocator<T> >
class ChunkedArrayHDF5
: public ChunkedArray<N, T>
{
/* REMARKS
Alternatives are:
* Back chunks by HDF5 chunks, possibly using on-the-fly compression. This
is in particular useful for existing HDF5 files.
* Back chunks by HDF5 datasets. This can be combined with compression
(both explicit and on-the-fly) or with memory mapping (using the
function H5Dget_offset() to get the offset from the beginning of the file).
*/
public:
class Chunk
: public ChunkBase<N, T>
{
public:
typedef typename MultiArrayShape<N>::type shape_type;
typedef T value_type;
typedef value_type * pointer;
typedef value_type & reference;
Chunk(shape_type const & shape, shape_type const & start,
ChunkedArrayHDF5 * array, Alloc const & alloc)
: ChunkBase<N, T>(detail::defaultStride(shape))
, shape_(shape)
, start_(start)
, array_(array)
, alloc_(alloc)
{}
~Chunk()
{
write();
}
std::size_t size() const
{
return prod(shape_);
}
void write(bool deallocate = true)
{
if(this->pointer_ != 0)
{
if(!array_->file_.isReadOnly())
{
herr_t status = array_->file_.writeBlock(array_->dataset_, start_,
MultiArrayView<N, T>(shape_, this->strides_, this->pointer_));
vigra_postcondition(status >= 0,
"ChunkedArrayHDF5: write to dataset failed.");
}
if(deallocate)
{
alloc_.deallocate(this->pointer_, this->size());
this->pointer_ = 0;
}
}
}
pointer read()
{
if(this->pointer_ == 0)
{
this->pointer_ = alloc_.allocate(this->size());
herr_t status = array_->file_.readBlock(array_->dataset_, start_, shape_,
MultiArrayView<N, T>(shape_, this->strides_, this->pointer_));
vigra_postcondition(status >= 0,
"ChunkedArrayHDF5: read from dataset failed.");
}
return this->pointer_;
}
shape_type shape_, start_;
ChunkedArrayHDF5 * array_;
Alloc alloc_;
private:
Chunk & operator=(Chunk const &);
};
typedef ChunkedArray<N, T> base_type;
typedef MultiArray<N, SharedChunkHandle<N, T> > ChunkStorage;
typedef typename ChunkStorage::difference_type shape_type;
typedef T value_type;
typedef value_type * pointer;
typedef value_type & reference;
/** \brief Construct with given 'shape', 'chunk_shape' and 'options',
using 'alloc' to manage the in-memory version of the data..
The data are placed in 'file' at the internal path 'dataset'. Argument
'mode' must be one of the following:
<ul>
<li>HDF5File::New: Create new dataset, possibly deleting any existing content.
It is an error to request this mode when the entire
'file' is read-only.
<li>HDF5File::Replace: Same as New.
<li>HDF5File::ReadWrite: Open the dataset for reading and writing. Create
the datset if it doesn't exist. It is an error
to request this mode when 'file' is read-only.
<li>HDF5File::ReadOnly: Open the dataset for reading. It is an error to
request this mode when the dataset doesn't exist.
<li>HDF5File::Default: Resolves to ReadOnly when the dataset exists, and
to New otherwise.
</ul>
The supported compression algorithms are:
<ul>
<li>ZLIB_FAST: Fast compression using 'zlib' (slower than LZ4, but higher compression).
<li>ZLIB_BEST: Best compression using 'zlib', slow.
<li>ZLIB_NONE: Use 'zlib' format without compression.
<li>DEFAULT_COMPRESSION: Same as ZLIB_FAST.
</ul>
*/
ChunkedArrayHDF5(HDF5File const & file, std::string const & dataset,
HDF5File::OpenMode mode,
shape_type const & shape,
shape_type const & chunk_shape=shape_type(),
ChunkedArrayOptions const & options = ChunkedArrayOptions(),
Alloc const & alloc = Alloc())
: ChunkedArray<N, T>(shape, chunk_shape, options),
file_(file),
dataset_name_(dataset),
dataset_(),
compression_(options.compression_method),
alloc_(alloc)
{
init(mode);
}
/** \brief Construct for an already existing dataset with given 'options',
using 'alloc' to manage the in-memory version of the data.
The data must be located in 'file' at the internal path 'dataset'. The
array's shape and chunk_shape are read from the file. It is an error
to use this constructor when 'dataset' doesn't exist.
Argument 'mode' must be one of the following:
<ul>
<li>HDF5File::ReadWrite: Open the dataset for reading and writing. It is an error
to request this mode when 'file' is read-only.
<li>HDF5File::ReadOnly: Open the dataset for reading (default).
<li>HDF5File::Default: Same as ReadOnly.
</ul>
The supported compression algorithms are:
<ul>
<li>ZLIB_FAST: Fast compression using 'zlib' (slower than LZ4, but higher compression).
<li>ZLIB_BEST: Best compression using 'zlib', slow.
<li>ZLIB_NONE: Use 'zlib' format without compression.
<li>DEFAULT_COMPRESSION: Same as ZLIB_FAST.
</ul>
*/
ChunkedArrayHDF5(HDF5File const & file, std::string const & dataset,
HDF5File::OpenMode mode = HDF5File::ReadOnly,
ChunkedArrayOptions const & options = ChunkedArrayOptions(),
Alloc const & alloc = Alloc())
: ChunkedArray<N, T>(shape_type(), shape_type(), options),
file_(file),
dataset_name_(dataset),
dataset_(),
compression_(options.compression_method),
alloc_(alloc)
{
init(mode);
}
void init(HDF5File::OpenMode mode)
{
bool exists = file_.existsDataset(dataset_name_);
if(mode == HDF5File::Replace)
{
mode = HDF5File::New;
}
else if(mode == HDF5File::Default)
{
if(exists)
mode = HDF5File::ReadOnly;
else
mode = HDF5File::New;
}
if(mode == HDF5File::ReadOnly)
file_.setReadOnly();
else
vigra_precondition(!file_.isReadOnly(),
"ChunkedArrayHDF5(): 'mode' is incompatible with read-only file.");
vigra_precondition(exists || !file_.isReadOnly(),
"ChunkedArrayHDF5(): dataset does not exist, but file is read-only.");
if(!exists || mode == HDF5File::New)
{
// FIXME: set rdcc_nbytes to 0 (disable cache, because we don't
// need two caches
// H5Pset_chunk_cache (dapl, rdcc_nslots, rdcc_nbytes, rdcc_w0);
// Chunk cache size (rdcc_nbytes) should be large
// enough to hold all the chunks in a selection
// • If this is not possible, it may be best to disable chunk
// caching altogether (set rdcc_nbytes to 0)
// • rdcc_slots should be a prime number that is at
// least 10 to 100 times the number of chunks that can fit
// into rdcc_nbytes
// • rdcc_w0 should be set to 1 if chunks that have been
// fully read/written will never be read/written again
//
// the above may be WRONG in general - it may only apply if the
// chunk size in the file matches the chunk size in the CachedArray.
// Otherwise, make sure that the file cache can hold at least as many
// chunks as are needed for a single array chunk.
if(compression_ == DEFAULT_COMPRESSION)
compression_ = ZLIB_FAST;
vigra_precondition(compression_ != LZ4,
"ChunkedArrayHDF5(): HDF5 does not support LZ4 compression.");
vigra_precondition(this->size() > 0,
"ChunkedArrayHDF5(): invalid shape.");
typename detail::HDF5TypeTraits<T>::value_type init(this->fill_scalar_);
dataset_ = file_.createDataset<N, T>(dataset_name_,
this->shape_,
init,
this->chunk_shape_,
compression_);
}
else
{
dataset_ = file_.getDatasetHandleShared(dataset_name_);
// check shape
ArrayVector<hsize_t> fileShape(file_.getDatasetShape(dataset_name_));
typedef detail::HDF5TypeTraits<T> TypeTraits;
if(TypeTraits::numberOfBands() > 1)
{
vigra_precondition(fileShape.size() == N+1,
"ChunkedArrayHDF5(file, dataset): dataset has wrong dimension.");
vigra_precondition(fileShape[0] == TypeTraits::numberOfBands(),
"ChunkedArrayHDF5(file, dataset): dataset has wrong number of bands.");
shape_type shape(fileShape.begin()+1);
if(this->size() > 0)
{
vigra_precondition(shape == this->shape_,
"ChunkedArrayHDF5(file, dataset, shape): shape mismatch between dataset and shape argument.");
}
else
{
this->shape_ = shape;
}
}
else
{
vigra_precondition(fileShape.size() == N,
"ChunkedArrayHDF5(file, dataset): dataset has wrong dimension.");
shape_type shape(fileShape.begin());
if(this->size() > 0)
{
vigra_precondition(shape == this->shape_,
"ChunkedArrayHDF5(file, dataset, shape): shape mismatch between dataset and shape argument.");
}
else
{
this->shape_ = shape;
ChunkStorage(detail::computeChunkArrayShape(shape, this->bits_, this->mask_)).swap(this->handle_array_);
}
}
typename ChunkStorage::iterator i = this->handle_array_.begin(),
end = this->handle_array_.end();
for(; i != end; ++i)
{
i->chunk_state_.store(base_type::chunk_asleep);
}
}
}
~ChunkedArrayHDF5()
{
closeImpl(true);
}
void close()
{
closeImpl(false);
}
void closeImpl(bool force_destroy)
{
flushToDiskImpl(true, force_destroy);
file_.close();
}
void flushToDisk()
{
flushToDiskImpl(false, false);
}
void flushToDiskImpl(bool destroy, bool force_destroy)
{
if(file_.isReadOnly())
return;
threading::lock_guard<threading::mutex> guard(*this->chunk_lock_);
typename ChunkStorage::iterator i = this->handle_array_.begin(),
end = this->handle_array_.end();
if(destroy && !force_destroy)
{
for(; i != end; ++i)
{
vigra_precondition(i->chunk_state_.load() <= 0,
"ChunkedArrayHDF5::close(): cannot close file because there are active chunks.");
}
i = this->handle_array_.begin();
}
for(; i != end; ++i)
{
Chunk * chunk = static_cast<Chunk*>(i->pointer_);
if(!chunk)
continue;
if(destroy)
{
delete chunk;
i->pointer_ = 0;
}
else
{
chunk->write(false);
}
}
file_.flushToDisk();
}
virtual bool isReadOnly() const
{
return file_.isReadOnly();
}
virtual pointer loadChunk(ChunkBase<N, T> ** p, shape_type const & index)
{
vigra_precondition(file_.isOpen(),
"ChunkedArrayHDF5::loadChunk(): file was already closed.");
if(*p == 0)
{
*p = new Chunk(this->chunkShape(index), index*this->chunk_shape_, this, alloc_);
this->overhead_bytes_ += sizeof(Chunk);
}
return static_cast<Chunk *>(*p)->read();
}
virtual bool unloadChunk(ChunkBase<N, T> * chunk, bool /* destroy */)
{
if(!file_.isOpen())
return true;
static_cast<Chunk *>(chunk)->write();
return false;
}
virtual std::string backend() const
{
return "ChunkedArrayHDF5<'" + file_.filename() + "/" + dataset_name_ + "'>";
}
virtual std::size_t dataBytes(ChunkBase<N,T> * c) const
{
return c->pointer_ == 0
? 0
: static_cast<Chunk*>(c)->size()*sizeof(T);
}
virtual std::size_t overheadBytesPerChunk() const
{
return sizeof(Chunk) + sizeof(SharedChunkHandle<N, T>);
}
std::string fileName() const
{
return file_.filename();
}
std::string datasetName() const
{
return dataset_name_;
}
HDF5File file_;
std::string dataset_name_;
HDF5HandleShared dataset_;
CompressionMethod compression_;
Alloc alloc_;
};
//@}
} // namespace vigra
#undef VIGRA_ASSERT_INSIDE
#endif /* VIGRA_MULTI_ARRAY_CHUNKED_HDF5_HXX */
|