44 #ifndef _INCLUDED_Field3D_SparseFile_H_ 45 #define _INCLUDED_Field3D_SparseFile_H_ 63 #define F3D_SHORT_MUTEX_ARRAY 1 64 #define F3D_MUTEX_ARRAY_SIZE 1000 65 #define F3D_NO_BLOCKS_ARRAY 1 77 template <
typename Data_T>
82 template <
typename Data_T>
85 template <
typename Data_T>
103 template <
class Data_T>
113 typedef boost::shared_ptr<Reference> Ptr;
118 #if F3D_NO_BLOCKS_ARRAY 121 typedef std::vector<Sparse::SparseBlock<Data_T>*> BlockPtrs;
156 #if F3D_SHORT_MUTEX_ARRAY 170 static Ptr create(
const std::string a_filename,
171 const std::string a_layerPath);
175 void setNumBlocks(
int numBlocks);
185 void loadBlock(
int blockIdx);
187 void unloadBlock(
int blockIdx);
190 void incBlockRef(
int blockIdx);
192 void decBlockRef(
int blockIdx);
194 int blockSize(
int blockIdx)
const;
197 int totalLoads()
const;
200 int numLoadedBlocks()
const;
203 int totalLoadedBlocks()
const;
206 float averageLoads()
const;
208 void resetCacheStatistics();
210 long long int memSize()
const;
219 Reference(
const std::string filename,
const std::string layerPath);
285 template <
class Data_T>
290 template <
class Data_T>
294 template <
class Data_T>
295 size_t numRefs()
const;
298 long long int memSize()
const;
327 blockType(blockTypeIn), refIdx(refIdxIn), blockIdx(blockIdxIn)
401 template <
class Data_T>
415 void setLimitMemUse(
bool enabled);
419 bool doLimitMemUse()
const;
422 void setMaxMemUse(
float maxMemUse);
429 long long totalLoads();
432 long long numLoadedBlocks();
435 long long totalLoadedBlocks();
439 float cacheFractionLoaded();
442 float cacheLoadsPerBlock();
446 float cacheEfficiency();
449 void resetCacheStatistics();
461 template <
class Data_T>
468 template <
class Data_T>
474 template <
class Data_T>
480 template <
class Data_T>
485 template <
class Data_T>
486 int getNextId(
const std::string filename,
const std::string layerPath);
488 template <
class Data_T>
489 void removeFieldFromCache(
int refIdx);
500 void addBlockToCache(
DataTypeEnum blockType,
int fileId,
int blockIdx);
504 void deallocateBlocks(int64_t bytesNeeded);
508 template <
class Data_T>
512 template <
class Data_T>
513 void deallocateBlock(CacheList::iterator &it);
557 template <
class Data_T>
559 const std::string a_layerPath)
560 : filename(a_filename), layerPath(a_layerPath),
561 valuesPerBlock(-1), numVoxels(-1), numBlocks(-1), occupiedBlocks(-1),
562 blockMutex(NULL), m_fileHandle(-1), m_reader(NULL), m_ogReader(NULL),
570 template <
class Data_T>
585 template <
class Data_T>
597 template <
class Data_T>
619 #if F3D_SHORT_MUTEX_ARRAY 620 # if F3D_NO_BLOCKS_ARRAY 657 template <
class Data_T>
660 const std::string a_layerPath)
667 template <
class Data_T>
675 template <
class Data_T>
678 boost::mutex::scoped_lock lock(
m_mutex);
685 #if !F3D_NO_BLOCKS_ARRAY 693 #if F3D_SHORT_MUTEX_ARRAY 694 # if F3D_NO_BLOCKS_ARRAY 708 template <
class Data_T>
720 template <
class Data_T>
724 #if F3D_NO_BLOCKS_ARRAY 743 template <
class Data_T>
746 #if F3D_SHORT_MUTEX_ARRAY 749 boost::mutex::scoped_lock lock(
blockMutex[blockIdx]);
756 template <
class Data_T>
759 #if F3D_SHORT_MUTEX_ARRAY 762 boost::mutex::scoped_lock lock(
blockMutex[blockIdx]);
769 template <
class Data_T>
777 template <
class Data_T>
780 std::vector<int>::const_iterator i =
loadCounts.begin();
781 std::vector<int>::const_iterator end =
loadCounts.end();
783 for (; i != end; ++i)
791 template <
class Data_T>
794 std::vector<int>::const_iterator i =
blockLoaded.begin();
795 std::vector<int>::const_iterator end =
blockLoaded.end();
796 int numBlockCounter = 0;
797 for (; i != end; ++i)
801 return numBlockCounter;
806 template <
class Data_T>
809 std::vector<int>::const_iterator i =
loadCounts.begin();
810 std::vector<int>::const_iterator li =
blockLoaded.begin();
811 std::vector<int>::const_iterator end =
loadCounts.end();
812 int numBlockCounter = 0;
815 for (; i != end; ++i)
821 for (; i != end; ++i, ++li)
826 return numBlockCounter;
831 template <
class Data_T>
834 std::vector<int>::const_iterator i =
loadCounts.begin();
835 std::vector<int>::const_iterator end =
loadCounts.end();
836 int numLoads = 0, numBlockCounter = 0;
837 for (; i != end; ++i) {
844 return (
float)numLoads /
std::max(1, numBlockCounter);
849 template <
class Data_T>
852 std::vector<int>::iterator li =
loadCounts.begin();
853 std::vector<int>::iterator lend =
loadCounts.end();
854 for (; li != lend; ++li)
860 template <
class Data_T>
864 boost::mutex::scoped_lock lock(
m_mutex);
866 return sizeof(*this) +
898 for (
size_t i = 0, end = m_hRefs.size(); i < end; ++i) {
901 for (
size_t i = 0, end = m_fRefs.size(); i < end; ++i) {
904 for (
size_t i = 0, end = m_dRefs.size(); i < end; ++i) {
907 for (
size_t i = 0, end = m_vhRefs.size(); i < end; ++i) {
910 for (
size_t i = 0, end = m_vfRefs.size(); i < end; ++i) {
913 for (
size_t i = 0, end = m_vdRefs.size(); i < end; ++i) {
926 return m_hRefs[idx].get();
939 return m_vhRefs[idx].get();
941 return m_vhRefs[idx];
952 return m_fRefs[idx].get();
965 return m_vfRefs[idx].get();
967 return m_vfRefs[idx];
978 return m_dRefs[idx].get();
991 return m_vdRefs[idx].get();
993 return m_vdRefs[idx];
1002 Mutex::scoped_lock lock(
m_mutex);
1004 m_hRefs.push_back(ref);
1005 return m_hRefs.size() - 1;
1013 Mutex::scoped_lock lock(
m_mutex);
1015 m_vhRefs.push_back(ref);
1016 return m_vhRefs.size() - 1;
1024 Mutex::scoped_lock lock(
m_mutex);
1026 m_fRefs.push_back(ref);
1027 return m_fRefs.size() - 1;
1035 Mutex::scoped_lock lock(
m_mutex);
1037 m_vfRefs.push_back(ref);
1038 return m_vfRefs.size() - 1;
1046 Mutex::scoped_lock lock(
m_mutex);
1048 m_dRefs.push_back(ref);
1049 return m_dRefs.size() - 1;
1057 Mutex::scoped_lock lock(
m_mutex);
1059 m_vdRefs.push_back(ref);
1060 return m_vdRefs.size() - 1;
1066 inline size_t FileReferences::numRefs<half>()
const 1068 Mutex::scoped_lock lock(
m_mutex);
1070 return m_hRefs.size();
1076 inline size_t FileReferences::numRefs<V3h>()
const 1078 Mutex::scoped_lock lock(
m_mutex);
1080 return m_vhRefs.size();
1086 inline size_t FileReferences::numRefs<float>()
const 1088 Mutex::scoped_lock lock(
m_mutex);
1090 return m_fRefs.size();
1096 inline size_t FileReferences::numRefs<V3f>()
const 1098 Mutex::scoped_lock lock(
m_mutex);
1100 return m_vfRefs.size();
1106 inline size_t FileReferences::numRefs<double>()
const 1108 Mutex::scoped_lock lock(
m_mutex);
1110 return m_dRefs.size();
1116 inline size_t FileReferences::numRefs<V3d>()
const 1118 Mutex::scoped_lock lock(
m_mutex);
1120 return m_vdRefs.size();
1129 template <
class Data_T>
1132 assert(
false &&
"Do not use memory limiting on sparse fields that aren't " 1133 "simple scalars or vectors!");
1135 "FileReferences::ref(): Do not use memory limiting on sparse " 1136 "fields that aren't simple scalars or vectors!");
1143 template <
class Data_T>
1146 assert(
false &&
"Do not use memory limiting on sparse fields that aren't " 1147 "simple scalars or vectors!");
1149 "FileReferences::append(): Do not use memory limiting on sparse " 1150 "fields that aren't simple scalars or vectors!");
1156 template <
class Data_T>
1159 assert(
false &&
"Do not use memory limiting on sparse fields that aren't " 1160 "simple scalars or vectors!");
1162 "FileReferences::numRefs(): " 1163 "Do not use memory limiting on sparse " 1164 "fields that aren't " 1165 "simple scalars or vectors!");
1179 template <
class Data_T>
1187 boost::mutex::scoped_lock lock(
m_mutex);
1196 template <
class Data_T>
1200 boost::mutex::scoped_lock lock(
m_mutex);
1205 CacheList::iterator it = m_blockCacheList.begin();
1206 CacheList::iterator end = m_blockCacheList.end();
1207 CacheList::iterator next;
1209 int64_t bytesFreed = 0;
1212 if (it->blockType == blockType && it->refIdx == refIdx) {
1213 if (it == m_nextBlock) {
1218 bytesFreed += reference->
blockSize(it->blockIdx);
1219 m_blockCacheList.erase(it);
1225 m_memUse -= bytesFreed;
1228 #if F3D_NO_BLOCKS_ARRAY 1236 std::vector<bool>().swap(reference->
blockUsed);
1237 std::vector<int>().swap(reference->
loadCounts);
1238 std::vector<int>().swap(reference->
refCounts);
1245 template <
class Data_T>
1249 return m_fileData.ref<Data_T>(index);
1254 template <
class Data_T>
1263 if (m_limitMemUse) {
1266 deallocateBlocks(blockSize);
1273 boost::mutex::scoped_lock lock_A(
m_mutex);
1274 #if F3D_SHORT_MUTEX_ARRAY 1275 boost::mutex::scoped_lock
1278 boost::mutex::scoped_lock lock_B(reference->
blockMutex[blockIdx]);
1295 template <
class Data_T>
1308 template <
class Data_T>
int numLoadedBlocks() const
Returns the total number of blocks that are currently loaded, for statistics.
void removeFieldFromCache(int refIdx)
#define FIELD3D_NAMESPACE_HEADER_CLOSE
size_t m_numActiveBlocks
Number of currently active blocks.
boost::shared_ptr< Alembic::Ogawa::IArchive > IArchivePtr
hid_t m_fileHandle
Holds the Hdf5 handle to the file.
IArchivePtr m_ogArchive
Ogawa archive.
CacheList m_blockCacheList
List of dynamically loaded blocks to be considered for unloading when the cache is full...
boost::shared_ptr< OgSparseDataReader< Data_T > > m_ogReaderPtr
Shared pointer to the ogawa reader.
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...
std::vector< int > blockLoaded
Whether each block is loaded. We don't use bools since vector<bool> is weird.
#define F3D_SHORT_MUTEX_ARRAY
Contains forward declarations for Ogawa classes.
void incBlockRef(int fileId, int blockIdx)
Increments the usage reference count on the specified block, to prevent it from getting unloaded whil...
void incBlockRef(int blockIdx)
Increment reference count on a block, indicates the block is currently in use, so prevents it from be...
std::vector< bool > blockUsed
Flags of whether the blocks have been accessed since they were last considered for deallocation by th...
CacheBlock(DataTypeEnum blockTypeIn, int refIdxIn, int blockIdxIn)
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.
boost::shared_ptr< OgIGroup > OgIGroupPtr
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
std::vector< int > fileBlockIndices
Index in file for each block.
std::deque< Reference< V3f >::Ptr > m_vfRefs
void setNumBlocks(int numBlocks)
Sets the number of blocks used by the SparseField we're supporting.
static DataTypeEnum typeEnum()
void decBlockRef(int blockIdx)
Decrement reference count on a 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...
Contains various utility functions for Hdf5.
Mutex m_mutex
Mutex to prevent two threads from modifying conflicting data.
void openFile()
Opens the file. This is done just before the first request to loadBlock. This is delayed so that the ...
std::deque< Reference< V3d >::Ptr > m_vdRefs
float m_maxMemUse
Max amount om memory to use in megabytes.
OgSparseDataReader< Data_T > * m_ogReader
Pointer to the ogawa reader. NULL at construction time. Created in openFile().
SparseDataReader< Data_T > * m_reader
Pointer to the reader object. NULL at construction time. Created in openFile().
virtual long long int memSize() const
Returns the memory usage (in bytes)
int64_t m_maxMemUseInBytes
Max amount om memory to use in bytes.
boost::mutex m_mutex
Mutex to prevent multiple threads from deallocating blocks at the same time.
int blockSize(int blockIdx) const
Returns the number of bytes used by the data in the block.
OgIGroupPtr m_ogLayerGroup
Ogawa layer group.
bool m_limitMemUse
Whether to limit memory use of sparse fields from disk. Enables the cache and dynamic loading when tr...
static SparseFileManager * ms_singleton
Pointer to singleton.
This class gets used by SparseFieldIO and SparseFileManager to read the block data. On creation it will open the data set and not close it until the object is destroyed.
T max(const T a, const T2 b)
Max operation on mixed types.
int blockMutexSize
Size of the mutex array. Used as modulus base.
BlockPtrs blocks
Pointers to each block. This is so we can go in and manipulate them as we please. ...
CacheList::iterator m_nextBlock
Pointer to the next block to test for unloading in the cache, the "hand" of the clock.
void unloadBlock(int blockIdx)
Unloads the block with the given index from memory.
SparseFile::FileReferences m_fileData
Vector containing information for each of the managed fields. The order matches the index stored in e...
OgIGroupPtr m_ogRoot
Ogawa archive root.
Namespace for sparse field specifics.
std::list< SparseFile::CacheBlock > CacheList
void resize(int n)
Alloc data.
std::deque< Reference< float >::Ptr > m_fRefs
Sparse::SparseBlock< Data_T > * BlockPtrs
Contains functions controlling the loading of sparse fields.
void activateBlock(const int blockId) const
Activates a given block.
int64_t m_memUse
Current amount of memory in use in bytes.
#define F3D_MUTEX_ARRAY_SIZE
void resetCacheStatistics()
Resets counts of total block loads.
Hdf5Util::H5ScopedGopen m_layerGroup
Hold the group containing the data open for the duration of the Reference's existence.
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...
Reference< Data_T > * ref(size_t idx)
Returns a reference to the index. This is specialized so that the correct data member is accessed...
void decBlockRef(int fileId, int blockIdx)
Decrements the usage reference count on the specified block, after its value is no longer being used ...
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...
Storage for one individual block of a SparseField.
Reference & operator=(const Reference &o)
Assignment operator. Clears ref counts and rebuilds mutex array.
size_t numRefs() const
Returns the number of file references of the corresponding collection.
#define F3D_NO_BLOCKS_ARRAY
std::deque< Reference< double >::Ptr > m_dRefs
float averageLoads() const
Returns the average number of loads per accessed block in this file, for cache statistics.
void decBlockRef(const int blockId) const
Decrements the block ref count for the given block.
int totalLoadedBlocks() const
Returns the total number of blocks that were ever loaded (max 1 per block, not the number of blocks)...
void incBlockRef(const int blockId) const
Increments the block ref count for the given block.
Reference(const std::string filename, const std::string layerPath)
Constructor. Requires the filename and layer path of the field to be known.
bool fileIsOpen()
Checks if the file used by this reference is open already.
std::vector< int > loadCounts
Per-block counts of the number of times each block has been loaded, for cache statistics.
This Field subclass stores voxel data in block-allocated arrays.
SparseFile::Reference< Data_T > * reference(int index)
Returns a reference to the Reference object with the given index.
std::deque< Reference< V3h >::Ptr > m_vhRefs
T min(const T a, const T2 b)
Min operation on mixed types.
void closeFile()
Closes the file. Will be re-opened as needed.
Scoped object - opens a group on creation and closes it on destruction.
Contains Exception base class.
boost::mutex * blockMutex
Allocated array of mutexes, one per block, to lock each block individually, for guaranteeing thread-s...
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...
void open(hid_t parentLocation, const std::string &name)
int totalLoads() const
Returns the total number of loads of the blocks of this file, for cache statistics.
long long int memSize() const
Memory use for the Reference.
std::deque< Reference< half >::Ptr > m_hRefs