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
|
#include "vtkExodusIICache.h"
#include "vtkDataArray.h"
#include "vtkObjectFactory.h"
// Define VTK_EXO_DBG_CACHE to print cache adds, drops, and replacements.
//#undef VTK_EXO_DBG_CACHE
#define VTK_EXO_PRT_KEY( ckey ) \
"(" << ckey.Time << ", " << ckey.ObjectType << ", " << ckey.ObjectId << ", " << ckey.ArrayId << ")"
#define VTK_EXO_PRT_ARR( cval ) \
" [" << cval << "," << (cval ? cval->GetActualMemorySize() / 1024. : 0.) << "/" << this->Size << "/" << this->Capacity << "]"
#define VTK_EXO_PRT_ARR2( cval ) \
" [" << cval << ", " << (cval ? cval->GetActualMemorySize() / 1024. : 0.) << "]"
#if 0
static void printCache( vtkExodusIICacheSet& cache, vtkExodusIICacheLRU& lru )
{
cout << "Cache\n";
vtkExodusIICacheRef cit;
for ( cit = cache.begin(); cit != cache.end(); ++cit )
{
cout << VTK_EXO_PRT_KEY( cit->first ) << VTK_EXO_PRT_ARR2( cit->second->GetValue() ) << "\n";
}
cout << "LRU\n";
vtkExodusIICacheLRURef lit;
for ( lit = lru.begin(); lit != lru.end(); ++lit )
{
cout << VTK_EXO_PRT_KEY( (*lit)->first ) << "\n";
}
}
#endif // 0
// ============================================================================
vtkExodusIICacheEntry::vtkExodusIICacheEntry()
{
this->Value = 0;
}
vtkExodusIICacheEntry::vtkExodusIICacheEntry( vtkDataArray* arr )
{
this->Value = arr;
if ( arr )
this->Value->Register( 0 );
}
vtkExodusIICacheEntry::~vtkExodusIICacheEntry()
{
if ( this->Value )
this->Value->Delete();
}
vtkExodusIICacheEntry::vtkExodusIICacheEntry( const vtkExodusIICacheEntry& other )
{
this->Value = other.Value;
if ( this->Value )
this->Value->Register( 0 );
}
#if 0
void printLRUBack( vtkExodusIICacheRef& cit )
{
cout << "Key is " << VTK_EXO_PRT_KEY( cit->first ) << "\n";
}
#endif // 0
// ============================================================================
vtkStandardNewMacro(vtkExodusIICache);
vtkExodusIICache::vtkExodusIICache()
{
this->Size = 0.;
this->Capacity = 2.;
}
vtkExodusIICache::~vtkExodusIICache()
{
this->ReduceToSize( 0. );
}
void vtkExodusIICache::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "Capacity: " << this->Capacity << " MiB\n";
os << indent << "Size: " << this->Size << " MiB\n";
os << indent << "Cache: " << &this->Cache << " (" << this->Cache.size() << ")\n";
os << indent << "LRU: " << &this->LRU << "\n";
}
void vtkExodusIICache::Clear()
{
//printCache( this->Cache, this->LRU );
this->ReduceToSize( 0. );
}
void vtkExodusIICache::SetCacheCapacity( double sizeInMiB )
{
if ( sizeInMiB == this->Capacity )
return;
if ( this->Size > sizeInMiB )
{
this->ReduceToSize( sizeInMiB );
}
this->Capacity = sizeInMiB < 0 ? 0 : sizeInMiB;
}
int vtkExodusIICache::ReduceToSize( double newSize )
{
int deletedSomething = 0;
while ( this->Size > newSize && ! this->LRU.empty() )
{
vtkExodusIICacheRef cit( this->LRU.back() );
vtkDataArray* arr = cit->second->Value;
if ( arr )
{
deletedSomething = 1;
double arrSz = (double) arr->GetActualMemorySize() / 1024.;
this->Size -= arrSz;
#ifdef VTK_EXO_DBG_CACHE
cout << "Dropping " << VTK_EXO_PRT_KEY( cit->first ) << VTK_EXO_PRT_ARR( arr ) << "\n";
#endif // VTK_EXO_DBG_CACHE
if ( this->Size <= 0 )
{
if ( this->Cache.size() == 0 )
this->Size = 0.;
else
this->RecomputeSize(); // oops, FP roundoff
}
}
else
{
#ifdef VTK_EXO_DBG_CACHE
cout << "Dropping " << VTK_EXO_PRT_KEY( cit->first ) << VTK_EXO_PRT_ARR( arr ) << "\n";
#endif // VTK_EXO_DBG_CACHE
}
delete cit->second;
this->Cache.erase( cit );
this->LRU.pop_back();
}
if ( this->Cache.size() == 0 )
{
this->Size = 0;
}
return deletedSomething;
}
void vtkExodusIICache::Insert( vtkExodusIICacheKey& key, vtkDataArray* value )
{
double vsize = value ? value->GetActualMemorySize() / 1024. : 0.;
vtkExodusIICacheRef it = this->Cache.find( key );
if ( it != this->Cache.end() )
{
if ( it->second->Value == value )
return;
// Remove existing array and put in our new one.
this->Size -= vsize;
if ( this->Size <= 0 )
{
this->RecomputeSize();
}
this->ReduceToSize( this->Capacity - vsize );
it->second->Value->Delete();
it->second->Value = value;
it->second->Value->Register( 0 ); // Since we re-use the cache entry, the constructor's Register won't get called.
this->Size += vsize;
#ifdef VTK_EXO_DBG_CACHE
cout << "Replacing " << VTK_EXO_PRT_KEY( it->first ) << VTK_EXO_PRT_ARR( value ) << "\n";
#endif // VTK_EXO_DBG_CACHE
this->LRU.erase( it->second->LRUEntry );
it->second->LRUEntry = this->LRU.insert( this->LRU.begin(), it );
}
else
{
this->ReduceToSize( this->Capacity - vsize );
std::pair<const vtkExodusIICacheKey,vtkExodusIICacheEntry*> entry( key, new vtkExodusIICacheEntry(value) );
std::pair<vtkExodusIICacheSet::iterator, bool> iret = this->Cache.insert( entry );
this->Size += vsize;
#ifdef VTK_EXO_DBG_CACHE
cout << "Adding " << VTK_EXO_PRT_KEY( key ) << VTK_EXO_PRT_ARR( value ) << "\n";
#endif // VTK_EXO_DBG_CACHE
iret.first->second->LRUEntry = this->LRU.insert( this->LRU.begin(), iret.first );
}
//printCache( this->Cache, this->LRU );
}
vtkDataArray*& vtkExodusIICache::Find( vtkExodusIICacheKey key )
{
static vtkDataArray* dummy = 0;
vtkExodusIICacheRef it = this->Cache.find( key );
if ( it != this->Cache.end() )
{
this->LRU.erase( it->second->LRUEntry );
it->second->LRUEntry = this->LRU.insert( this->LRU.begin(), it );
return it->second->Value;
}
dummy = 0;
return dummy;
}
int vtkExodusIICache::Invalidate( vtkExodusIICacheKey key )
{
vtkExodusIICacheRef it = this->Cache.find( key );
if ( it != this->Cache.end() )
{
#ifdef VTK_EXO_DBG_CACHE
cout << "Dropping " << VTK_EXO_PRT_KEY( it->first ) << VTK_EXO_PRT_ARR( it->second->Value ) << "\n";
#endif // VTK_EXO_DBG_CACHE
this->LRU.erase( it->second->LRUEntry );
if ( it->second->Value )
{
this->Size -= it->second->Value->GetActualMemorySize() / 1024.;
}
delete it->second;
this->Cache.erase( it );
if ( this->Size <= 0 )
{
if ( this->Cache.size() == 0 )
this->Size = 0.;
else
this->RecomputeSize(); // oops, FP roundoff
}
return 1;
}
return 0;
}
int vtkExodusIICache::Invalidate( vtkExodusIICacheKey key, vtkExodusIICacheKey pattern )
{
vtkExodusIICacheRef it;
int nDropped = 0;
it = this->Cache.begin();
while ( it != this->Cache.end() )
{
if ( ! it->first.match( key, pattern ) )
{
++it;
continue;
}
#ifdef VTK_EXO_DBG_CACHE
cout << "Dropping " << VTK_EXO_PRT_KEY( it->first ) << VTK_EXO_PRT_ARR( it->second->Value ) << "\n";
#endif // VTK_EXO_DBG_CACHE
this->LRU.erase( it->second->LRUEntry );
if ( it->second->Value )
{
this->Size -= it->second->Value->GetActualMemorySize() / 1024.;
}
vtkExodusIICacheRef tmpIt = it++;
delete tmpIt->second;
this->Cache.erase( tmpIt );
if ( this->Size <= 0 )
{
if ( this->Cache.size() == 0 )
this->Size = 0.;
else
this->RecomputeSize(); // oops, FP roundoff
}
++nDropped;
}
return nDropped;
}
void vtkExodusIICache::RecomputeSize()
{
this->Size = 0.;
vtkExodusIICacheRef it;
for ( it = this->Cache.begin(); it != this->Cache.end(); ++it )
{
if ( it->second->Value )
{
this->Size += (double)it->second->Value->GetActualMemorySize() / 1024.;
}
}
}
|