44#ifndef _INCLUDED_Field3D_SparseFile_H_
45#define _INCLUDED_Field3D_SparseFile_H_
49#include <boost/scoped_ptr.hpp>
64#define F3D_SHORT_MUTEX_ARRAY 1
65#define F3D_MUTEX_ARRAY_SIZE 1000
66#define F3D_NO_BLOCKS_ARRAY 1
78 template <
typename Data_T>
83template <
typename Data_T>
86template <
typename Data_T>
104template <
class Data_T>
114 typedef boost::shared_ptr<Reference>
Ptr;
119#if F3D_NO_BLOCKS_ARRAY
122 typedef std::vector<Sparse::SparseBlock<Data_T>*>
BlockPtrs;
157#if F3D_SHORT_MUTEX_ARRAY
171 static Ptr create(
const std::string a_filename,
172 const std::string a_layerPath);
286 template <
class Data_T>
291 template <
class Data_T>
295 template <
class Data_T>
402 template <
class Data_T>
416 void setLimitMemUse(
bool enabled);
420 bool doLimitMemUse()
const;
423 void setMaxMemUse(
float maxMemUse);
430 long long totalLoads();
433 long long numLoadedBlocks();
436 long long totalLoadedBlocks();
440 float cacheFractionLoaded();
443 float cacheLoadsPerBlock();
447 float cacheEfficiency();
450 void resetCacheStatistics();
453 long long int memSize()
const;
462 template <
class Data_T>
463 void incBlockRef(
int fileId,
int blockIdx);
469 template <
class Data_T>
470 void decBlockRef(
int fileId,
int blockIdx);
475 template <
class Data_T>
476 void activateBlock(
int fileId,
int blockIdx);
481 template <
class Data_T>
486 template <
class Data_T>
487 int getNextId(
const std::string filename,
const std::string layerPath);
489 template <
class Data_T>
490 void removeFieldFromCache(
int refIdx);
501 void addBlockToCache(
DataTypeEnum blockType,
int fileId,
int blockIdx);
505 void deallocateBlocks(int64_t bytesNeeded);
509 template <
class Data_T>
513 template <
class Data_T>
514 void deallocateBlock(CacheList::iterator &it);
558template <
class Data_T>
560 const std::string a_layerPath)
561 : filename(a_filename), layerPath(a_layerPath),
562 valuesPerBlock(-1), numVoxels(-1), numBlocks(-1), occupiedBlocks(-1),
563 blockMutex(NULL), m_fileHandle(-1), m_reader(NULL), m_ogReader(NULL),
571template <
class Data_T>
581 delete [] blockMutex;
586template <
class Data_T>
589 m_ogReaderPtr.reset();
598template <
class Data_T>
620#if F3D_SHORT_MUTEX_ARRAY
621# if F3D_NO_BLOCKS_ARRAY
624 blockMutexSize = std::min(
static_cast<int>(blocks.size()),
627 blockMutex =
new boost::mutex[blockMutexSize];
629 blockMutex =
new boost::mutex[blocks.size()];
638 if (m_fileHandle >= 0) {
639 m_layerGroup.open(m_fileHandle, layerPath.c_str());
650 m_ogReaderPtr.reset();
658template <
class Data_T>
661 const std::string a_layerPath)
668template <
class Data_T>
671 return m_fileHandle >= 0;
676template <
class Data_T>
679 boost::mutex::scoped_lock lock(m_mutex);
682 numBlocks = a_numBlocks;
684 fileBlockIndices.resize(numBlocks);
685 blockLoaded.resize(numBlocks, 0);
686#if !F3D_NO_BLOCKS_ARRAY
687 blocks.resize(numBlocks, 0);
689 blockUsed.resize(numBlocks,
false);
690 loadCounts.resize(numBlocks, 0);
691 refCounts.resize(numBlocks, 0);
694#if F3D_SHORT_MUTEX_ARRAY
695# if F3D_NO_BLOCKS_ARRAY
698 blockMutexSize = std::min(blocks.size(),
701 blockMutex =
new boost::mutex[blockMutexSize];
703 blockMutex =
new boost::mutex[numBlocks];
709template <
class Data_T>
712 if (m_fileHandle >= 0) {
713 if (H5Fclose(m_fileHandle) < 0) {
714 Msg::print(
"In ~Reference(): Error closing file " + filename);
721template <
class Data_T>
725#if F3D_NO_BLOCKS_ARRAY
726 blocks[blockIdx].clear();
728 blocks[blockIdx]->clear();
731 blockLoaded[blockIdx] = 0;
736 if (m_numActiveBlocks == 0) {
744template <
class Data_T>
747#if F3D_SHORT_MUTEX_ARRAY
748 boost::mutex::scoped_lock lock(blockMutex[blockIdx % blockMutexSize]);
750 boost::mutex::scoped_lock lock(blockMutex[blockIdx]);
752 ++refCounts[blockIdx];
757template <
class Data_T>
760#if F3D_SHORT_MUTEX_ARRAY
761 boost::mutex::scoped_lock lock(blockMutex[blockIdx % blockMutexSize]);
763 boost::mutex::scoped_lock lock(blockMutex[blockIdx]);
765 --refCounts[blockIdx];
770template <
class Data_T>
773 return numVoxels *
sizeof(Data_T);
778template <
class Data_T>
781 std::vector<int>::const_iterator i = loadCounts.begin();
782 std::vector<int>::const_iterator end = loadCounts.end();
784 for (; i != end; ++i)
792template <
class Data_T>
795 std::vector<int>::const_iterator i = blockLoaded.begin();
796 std::vector<int>::const_iterator end = blockLoaded.end();
797 int numBlockCounter = 0;
798 for (; i != end; ++i)
802 return numBlockCounter;
807template <
class Data_T>
810 std::vector<int>::const_iterator i = loadCounts.begin();
811 std::vector<int>::const_iterator li = blockLoaded.begin();
812 std::vector<int>::const_iterator end = loadCounts.end();
813 int numBlockCounter = 0;
815 if (blockLoaded.size() == 0) {
816 for (; i != end; ++i)
820 assert(loadCounts.size() == blockLoaded.size());
822 for (; i != end; ++i, ++li)
827 return numBlockCounter;
832template <
class Data_T>
835 std::vector<int>::const_iterator i = loadCounts.begin();
836 std::vector<int>::const_iterator end = loadCounts.end();
837 int numLoads = 0, numBlockCounter = 0;
838 for (; i != end; ++i) {
845 return (
float)numLoads / std::max(1, numBlockCounter);
850template <
class Data_T>
853 std::vector<int>::iterator li = loadCounts.begin();
854 std::vector<int>::iterator lend = loadCounts.end();
855 for (; li != lend; ++li)
861template <
class Data_T>
865 boost::mutex::scoped_lock lock(m_mutex);
867 return sizeof(*this) +
868 fileBlockIndices.capacity() *
sizeof(int) +
869 blockLoaded.capacity() *
sizeof(int) +
873 blockUsed.capacity() *
sizeof(bool) +
874 loadCounts.capacity() *
sizeof(int) +
875 refCounts.capacity() *
sizeof(int) +
877 blockMutexSize *
sizeof(boost::mutex) +
879 numBlocks *
sizeof(boost::mutex) +
899 for (
size_t i = 0, end =
m_hRefs.size(); i < end; ++i) {
902 for (
size_t i = 0, end =
m_fRefs.size(); i < end; ++i) {
905 for (
size_t i = 0, end =
m_dRefs.size(); i < end; ++i) {
908 for (
size_t i = 0, end =
m_vhRefs.size(); i < end; ++i) {
911 for (
size_t i = 0, end =
m_vfRefs.size(); i < end; ++i) {
914 for (
size_t i = 0, end =
m_vdRefs.size(); i < end; ++i) {
1003 Mutex::scoped_lock lock(
m_mutex);
1014 Mutex::scoped_lock lock(
m_mutex);
1025 Mutex::scoped_lock lock(
m_mutex);
1036 Mutex::scoped_lock lock(
m_mutex);
1047 Mutex::scoped_lock lock(
m_mutex);
1058 Mutex::scoped_lock lock(
m_mutex);
1069 Mutex::scoped_lock lock(
m_mutex);
1079 Mutex::scoped_lock lock(
m_mutex);
1089 Mutex::scoped_lock lock(
m_mutex);
1099 Mutex::scoped_lock lock(
m_mutex);
1109 Mutex::scoped_lock lock(
m_mutex);
1119 Mutex::scoped_lock lock(
m_mutex);
1130template <
class Data_T>
1133 assert(
false &&
"Do not use memory limiting on sparse fields that aren't "
1134 "simple scalars or vectors!");
1136 "FileReferences::ref(): Do not use memory limiting on sparse "
1137 "fields that aren't simple scalars or vectors!");
1144template <
class Data_T>
1147 assert(
false &&
"Do not use memory limiting on sparse fields that aren't "
1148 "simple scalars or vectors!");
1150 "FileReferences::append(): Do not use memory limiting on sparse "
1151 "fields that aren't simple scalars or vectors!");
1157template <
class Data_T>
1160 assert(
false &&
"Do not use memory limiting on sparse fields that aren't "
1161 "simple scalars or vectors!");
1163 "FileReferences::numRefs(): "
1164 "Do not use memory limiting on sparse "
1165 "fields that aren't "
1166 "simple scalars or vectors!");
1180template <
class Data_T>
1183 const std::string layerPath)
1188 boost::mutex::scoped_lock lock(
m_mutex);
1197template <
class Data_T>
1201 boost::mutex::scoped_lock lock(
m_mutex);
1208 CacheList::iterator next;
1210 int64_t bytesFreed = 0;
1213 if (it->blockType == blockType && it->refIdx == refIdx) {
1219 bytesFreed +=
reference->blockSize(it->blockIdx);
1228 std::vector<int>().swap(
reference->fileBlockIndices);
1229#if F3D_NO_BLOCKS_ARRAY
1236 std::vector<int>().swap(
reference->blockLoaded);
1237 std::vector<bool>().swap(
reference->blockUsed);
1238 std::vector<int>().swap(
reference->loadCounts);
1239 std::vector<int>().swap(
reference->refCounts);
1246template <
class Data_T>
1255template <
class Data_T>
1261 if (
reference->fileBlockIndices[blockIdx] >= 0) {
1262 if (!
reference->blockLoaded[blockIdx]) {
1263 int blockSize =
reference->blockSize(blockIdx);
1274 boost::mutex::scoped_lock lock_A(
m_mutex);
1275#if F3D_SHORT_MUTEX_ARRAY
1276 boost::mutex::scoped_lock
1279 boost::mutex::scoped_lock lock_B(
reference->blockMutex[blockIdx]);
1283 if (!
reference->blockLoaded[blockIdx]) {
1296template <
class Data_T>
1302 if (
reference->fileBlockIndices[blockIdx] >= 0) {
1309template <
class Data_T>
1315 if (
reference->fileBlockIndices[blockIdx] >= 0) {
Contains Exception base class.
Contains various utility functions for Hdf5.
Contains forward declarations for Ogawa classes.
boost::shared_ptr< Alembic::Ogawa::IArchive > IArchivePtr
boost::shared_ptr< OgIGroup > OgIGroupPtr
Contains functions controlling the loading of sparse fields.
#define F3D_SHORT_MUTEX_ARRAY
#define F3D_NO_BLOCKS_ARRAY
#define F3D_MUTEX_ARRAY_SIZE
Scoped object - opens a group on creation and closes it on destruction.
This class gets used by SparseFieldIO and SparseFileManager to read the block data....
This Field subclass stores voxel data in block-allocated arrays.
bool m_limitMemUse
Whether to limit memory use of sparse fields from disk. Enables the cache and dynamic loading when tr...
CacheList m_blockCacheList
List of dynamically loaded blocks to be considered for unloading when the cache is full....
int getNextId(const std::string filename, const std::string layerPath)
Returns the id of the next cache item. This is stored in the SparseField in order to reference its fi...
int64_t m_memUse
Current amount of memory in use in bytes.
void activateBlock(int fileId, int blockIdx)
Called by SparseField when it's about to read from a block. This should not be called by the user,...
CacheList::iterator m_nextBlock
Pointer to the next block to test for unloading in the cache, the "hand" of the clock.
std::list< SparseFile::CacheBlock > CacheList
void removeFieldFromCache(int refIdx)
void decBlockRef(int fileId, int blockIdx)
Decrements the usage reference count on the specified block, after its value is no longer being used ...
boost::mutex m_mutex
Mutex to prevent multiple threads from deallocating blocks at the same time.
SparseFile::FileReferences m_fileData
Vector containing information for each of the managed fields. The order matches the index stored in e...
void addBlockToCache(DataTypeEnum blockType, int fileId, int blockIdx)
Adds the newly loaded block to the cache, managed by the paging algorithm.
SparseFile::Reference< Data_T > * reference(int index)
Returns a reference to the Reference object with the given index.
static boost::scoped_ptr< SparseFileManager > ms_singleton
Pointer to singleton.
float m_maxMemUse
Max amount om memory to use in megabytes.
void deallocateBlocks(int64_t bytesNeeded)
Utility function to reclaim the specified number of bytes by deallocating unneeded blocks.
void incBlockRef(int fileId, int blockIdx)
Increments the usage reference count on the specified block, to prevent it from getting unloaded whil...
int64_t m_maxMemUseInBytes
Max amount om memory to use in bytes.
CacheBlock(DataTypeEnum blockTypeIn, int refIdxIn, int blockIdxIn)
Reference< Data_T > * ref(size_t idx)
Returns a reference to the index. This is specialized so that the correct data member is accessed.
long long int memSize() const
Returns the memory use for the refs.
std::deque< Reference< double >::Ptr > m_dRefs
std::deque< Reference< float >::Ptr > m_fRefs
std::deque< Reference< V3d >::Ptr > m_vdRefs
size_t numRefs() const
Returns the number of file references of the corresponding collection.
std::deque< Reference< V3f >::Ptr > m_vfRefs
size_t append(typename Reference< Data_T >::Ptr ref)
Appends a reference to the collection. This is specialized so that the correct data member is accesse...
std::deque< Reference< half >::Ptr > m_hRefs
std::deque< Reference< V3h >::Ptr > m_vhRefs
void openFile()
Opens the file. This is done just before the first request to loadBlock. This is delayed so that the ...
std::vector< bool > blockUsed
Flags of whether the blocks have been accessed since they were last considered for deallocation by th...
BlockPtrs blocks
Pointers to each block. This is so we can go in and manipulate them as we please.
std::vector< int > fileBlockIndices
Index in file for each block.
static Ptr create(const std::string a_filename, const std::string a_layerPath)
Returns a shared pointer to a reference. Preferred way of creating References.
void incBlockRef(int blockIdx)
Increment reference count on a block, indicates the block is currently in use, so prevents it from be...
OgIGroupPtr m_ogRoot
Ogawa archive root.
long long int memSize() const
Memory use for the Reference.
Sparse::SparseBlock< Data_T > * BlockPtrs
Reference(const std::string filename, const std::string layerPath)
Constructor. Requires the filename and layer path of the field to be known.
Mutex m_mutex
Mutex to prevent two threads from modifying conflicting data.
Hdf5Util::H5ScopedGopen m_layerGroup
Hold the group containing the data open for the duration of the Reference's existence.
IArchivePtr m_ogArchive
Ogawa archive.
void closeFile()
Closes the file. Will be re-opened as needed.
OgIGroupPtr m_ogLayerGroup
Ogawa layer group.
int blockMutexSize
Size of the mutex array. Used as modulus base.
std::vector< int > blockLoaded
Whether each block is loaded. We don't use bools since vector<bool> is weird.
int totalLoadedBlocks() const
Returns the total number of blocks that were ever loaded (max 1 per block, not the number of blocks),...
float averageLoads() const
Returns the average number of loads per accessed block in this file, for cache statistics.
OgSparseDataReader< Data_T > * m_ogReader
Pointer to the ogawa reader. NULL at construction time. Created in openFile().
boost::shared_ptr< OgSparseDataReader< Data_T > > m_ogReaderPtr
Shared pointer to the ogawa reader.
SparseDataReader< Data_T > * m_reader
Pointer to the reader object. NULL at construction time. Created in openFile().
int blockSize(int blockIdx) const
Returns the number of bytes used by the data in the block.
std::vector< int > refCounts
Per-block counts of the number of current references to the blocks. If a block's ref count is non-zer...
void loadBlock(int blockIdx)
Loads the block with the given index into memory. We don't pass in a reference to where the data shou...
hid_t m_fileHandle
Holds the Hdf5 handle to the file.
int totalLoads() const
Returns the total number of loads of the blocks of this file, for cache statistics.
boost::mutex * blockMutex
Allocated array of mutexes, one per block, to lock each block individually, for guaranteeing thread-s...
void decBlockRef(int blockIdx)
Decrement reference count on a block.
size_t m_numActiveBlocks
Number of currently active blocks.
int numLoadedBlocks() const
Returns the total number of blocks that are currently loaded, for statistics.
Reference & operator=(const Reference &o)
Assignment operator. Clears ref counts and rebuilds mutex array.
void unloadBlock(int blockIdx)
Unloads the block with the given index from memory.
void setNumBlocks(int numBlocks)
Sets the number of blocks used by the SparseField we're supporting.
std::vector< int > loadCounts
Per-block counts of the number of times each block has been loaded, for cache statistics.
void resetCacheStatistics()
Resets counts of total block loads.
bool fileIsOpen()
Checks if the file used by this reference is open already.
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity.
Namespace for sparse field specifics.
#define FIELD3D_NAMESPACE_HEADER_CLOSE
static DataTypeEnum typeEnum()
Storage for one individual block of a SparseField.