VTK  9.3.1
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  }
63  vtkExodusIICacheKey& operator=(const vtkExodusIICacheKey& src) = default;
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;
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:
126  static vtkExodusIICache* New();
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 
149  void Insert(vtkExodusIICacheKey& key, vtkDataArray* value);
150 
154  vtkDataArray*& Find(const vtkExodusIICacheKey&);
155 
160  int Invalidate(const vtkExodusIICacheKey& key);
161 
171  int Invalidate(const vtkExodusIICacheKey& key, const vtkExodusIICacheKey& pattern);
172 
173 protected:
176 
178  ~vtkExodusIICache() override;
179 
181  void RecomputeSize();
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
std::list< vtkExodusIICacheRef >::iterator vtkExodusIICacheLRURef
bool match(const vtkExodusIICacheKey &other, const vtkExodusIICacheKey &pattern) const
vtkExodusIICacheSet Cache
A least-recently-used (LRU) cache to hold arrays.
abstract base class for most VTK objects
Definition: vtkObject.h:51
double Size
The current size of the cache (i.e., the size of the all the arrays it currently contains) in MiB...
std::list< vtkExodusIICacheRef > vtkExodusIICacheLRU
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
double GetSpaceLeft()
See how much cache space is left.
vtkExodusIICacheLRURef LRUEntry
double Capacity
The capacity of the cache (i.e., the maximum size of all arrays it contains) in MiB.
vtkExodusIICacheKey(int time, int objType, int objId, int arrId)
vtkExodusIICacheLRU LRU
The actual LRU list (indices into the cache ordered least to most recently used). ...
a simple class to control print indentation
Definition: vtkIndent.h:28
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
std::map< vtkExodusIICacheKey, vtkExodusIICacheEntry * >::iterator vtkExodusIICacheRef
vtkDataArray * GetValue()
vtkExodusIICacheKey(const vtkExodusIICacheKey &src)
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
bool operator<(const vtkExodusIICacheKey &other) const
std::map< vtkExodusIICacheKey, vtkExodusIICacheEntry * > vtkExodusIICacheSet