VTK  9.3.20240328
vtkExodusIICache.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 #ifndef vtkExodusIICache_h
4 #define vtkExodusIICache_h
5 
6 // ============================================================================
7 // The following classes define an LRU cache for data arrays
8 // loaded by the Exodus reader. Here's how they work:
9 //
10 // The actual cache consists of two STL containers: a set of
11 // cache entries (vtkExodusIICacheEntry) and a list of
12 // cache references (vtkExodusIICacheRef). The entries in
13 // these containers are sorted for fast retrieval:
14 // 1. The cache entries are indexed by the timestep, the
15 // object type (edge block, face set, ...), and the
16 // object ID (if one exists). When you call Find() to
17 // retrieve a cache entry, you provide a key containing
18 // this information and the array is returned if it exists.
19 // 2. The list of cache references are stored in "least-recently-used"
20 // order. The least recently referenced array is the first in
21 // the list. Whenever you request an entry with Find(), it is
22 // moved to the back of the list if it exists.
23 // This makes retrieving arrays O(n log n) and popping LRU
24 // entries O(1). Each cache entry stores an iterator into
25 // the list of references so that it can be located quickly for
26 // removal.
27 
28 #include "vtkIOExodusModule.h" // For export macro
29 #include "vtkObject.h"
30 
31 #include <list> // use for LRU ordering
32 #include <map> // used for cache storage
33 
34 VTK_ABI_NAMESPACE_BEGIN
35 class VTKIOEXODUS_EXPORT vtkExodusIICacheKey
36 {
37 public:
38  int Time;
40  int ObjectId;
41  int ArrayId;
43  {
44  Time = -1;
45  ObjectType = -1;
46  ObjectId = -1;
47  ArrayId = -1;
48  }
49  vtkExodusIICacheKey(int time, int objType, int objId, int arrId)
50  {
51  Time = time;
52  ObjectType = objType;
53  ObjectId = objId;
54  ArrayId = arrId;
55  }
57  {
58  Time = src.Time;
59  ObjectType = src.ObjectType;
60  ObjectId = src.ObjectId;
61  ArrayId = src.ArrayId;
62  }
64  bool match(const vtkExodusIICacheKey& other, const vtkExodusIICacheKey& pattern) const
65  {
66  if (pattern.Time && this->Time != other.Time)
67  return false;
68  if (pattern.ObjectType && this->ObjectType != other.ObjectType)
69  return false;
70  if (pattern.ObjectId && this->ObjectId != other.ObjectId)
71  return false;
72  if (pattern.ArrayId && this->ArrayId != other.ArrayId)
73  return false;
74  return true;
75  }
76  bool operator<(const vtkExodusIICacheKey& other) const
77  {
78  if (this->Time < other.Time)
79  return true;
80  else if (this->Time > other.Time)
81  return false;
82  if (this->ObjectType < other.ObjectType)
83  return true;
84  else if (this->ObjectType > other.ObjectType)
85  return false;
86  if (this->ObjectId < other.ObjectId)
87  return true;
88  else if (this->ObjectId > other.ObjectId)
89  return false;
90  if (this->ArrayId < other.ArrayId)
91  return true;
92  return false;
93  }
94 };
95 
97 class vtkExodusIICache;
98 class vtkDataArray;
99 
100 typedef std::map<vtkExodusIICacheKey, vtkExodusIICacheEntry*> vtkExodusIICacheSet;
101 typedef std::map<vtkExodusIICacheKey, vtkExodusIICacheEntry*>::iterator vtkExodusIICacheRef;
102 typedef std::list<vtkExodusIICacheRef> vtkExodusIICacheLRU;
103 typedef std::list<vtkExodusIICacheRef>::iterator vtkExodusIICacheLRURef;
104 
105 class VTKIOEXODUS_EXPORT vtkExodusIICacheEntry
106 {
107 public:
111 
113 
114  vtkDataArray* GetValue() { return this->Value; }
115 
116 protected:
119 
120  friend class vtkExodusIICache;
121 };
122 
123 class VTKIOEXODUS_EXPORT vtkExodusIICache : public vtkObject
124 {
125 public:
127  vtkTypeMacro(vtkExodusIICache, vtkObject);
128  void PrintSelf(ostream& os, vtkIndent indent) override;
129 
131  void Clear();
132 
135  void SetCacheCapacity(double sizeInMiB);
136 
141  double GetSpaceLeft() { return this->Capacity - this->Size; }
142 
146  int ReduceToSize(double newSize);
147 
150 
155 
161 
172 
173 protected:
176 
178  ~vtkExodusIICache() override;
179 
182 
184  double Capacity;
185 
188  double Size;
189 
197 
200 
201 private:
202  vtkExodusIICache(const vtkExodusIICache&) = delete;
203  void operator=(const vtkExodusIICache&) = delete;
204 };
205 VTK_ABI_NAMESPACE_END
206 #endif // vtkExodusIICache_h
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:154
vtkExodusIICacheEntry(vtkDataArray *arr)
vtkDataArray * GetValue()
vtkExodusIICacheLRURef LRUEntry
vtkExodusIICacheEntry(const vtkExodusIICacheEntry &other)
bool operator<(const vtkExodusIICacheKey &other) const
vtkExodusIICacheKey & operator=(const vtkExodusIICacheKey &src)=default
bool match(const vtkExodusIICacheKey &other, const vtkExodusIICacheKey &pattern) const
vtkExodusIICacheKey(int time, int objType, int objId, int arrId)
vtkExodusIICacheKey(const vtkExodusIICacheKey &src)
void Clear()
Empty the cache.
void SetCacheCapacity(double sizeInMiB)
Set the maximum allowable cache size.
vtkExodusIICacheLRU LRU
The actual LRU list (indices into the cache ordered least to most recently used).
double Size
The current size of the cache (i.e., the size of the all the arrays it currently contains) in MiB.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkExodusIICache()
Default constructor.
double Capacity
The capacity of the cache (i.e., the maximum size of all arrays it contains) in MiB.
int Invalidate(const vtkExodusIICacheKey &key)
Invalidate a cache entry (drop it from the cache) if the key exists.
int ReduceToSize(double newSize)
Remove cache entries until the size of the cache is at or below the given size.
static vtkExodusIICache * New()
~vtkExodusIICache() override
Destructor.
void RecomputeSize()
Avoid (some) FP problems.
double GetSpaceLeft()
See how much cache space is left.
void Insert(vtkExodusIICacheKey &key, vtkDataArray *value)
Insert an entry into the cache (this can remove other cache entries to make space).
vtkDataArray *& Find(const vtkExodusIICacheKey &)
Determine whether a cache entry exists.
vtkExodusIICacheSet Cache
A least-recently-used (LRU) cache to hold arrays.
int Invalidate(const vtkExodusIICacheKey &key, const vtkExodusIICacheKey &pattern)
Invalidate all cache entries matching a specified pattern, dropping all matches from the cache.
a simple class to control print indentation
Definition: vtkIndent.h:108
abstract base class for most VTK objects
Definition: vtkObject.h:161
@ key
Definition: vtkX3D.h:257
@ value
Definition: vtkX3D.h:220
@ time
Definition: vtkX3D.h:497
std::list< vtkExodusIICacheRef > vtkExodusIICacheLRU
std::map< vtkExodusIICacheKey, vtkExodusIICacheEntry * >::iterator vtkExodusIICacheRef
std::map< vtkExodusIICacheKey, vtkExodusIICacheEntry * > vtkExodusIICacheSet
std::list< vtkExodusIICacheRef >::iterator vtkExodusIICacheLRURef