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
|
/*=========================================================================
Program: ParaView
Module: vtkPVCacheKeeper.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPVCacheKeeper.h"
#include "vtkCacheSizeKeeper.h"
#include "vtkDataObject.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkProcessModule.h"
#include "vtkPVCacheKeeperPipeline.h"
#include "vtkSmartPointer.h"
#include <map>
//----------------------------------------------------------------------------
class vtkPVCacheKeeper::vtkCacheMap :
public std::map<double, vtkSmartPointer<vtkDataObject> >
{
public:
unsigned long GetActualMemorySize()
{
unsigned long actual_size = 0;
vtkCacheMap::iterator iter;
for (iter = this->begin(); iter != this->end(); ++iter)
{
actual_size += iter->second.GetPointer()->GetActualMemorySize();
}
return actual_size;
}
};
vtkStandardNewMacro(vtkPVCacheKeeper);
vtkCxxSetObjectMacro(vtkPVCacheKeeper, CacheSizeKeeper, vtkCacheSizeKeeper);
//----------------------------------------------------------------------------
int vtkPVCacheKeeper::CacheHit = 0;
int vtkPVCacheKeeper::CacheMiss = 0;
int vtkPVCacheKeeper::CacheSkips = 0;
//----------------------------------------------------------------------------
vtkPVCacheKeeper::vtkPVCacheKeeper()
{
this->Cache = new vtkPVCacheKeeper::vtkCacheMap();
this->CacheTime = 0.0;
this->CachingEnabled = true;
this->CacheSizeKeeper = 0;
this->SetCacheSizeKeeper(vtkCacheSizeKeeper::GetInstance());
}
//----------------------------------------------------------------------------
vtkPVCacheKeeper::~vtkPVCacheKeeper()
{
this->RemoveAllCaches();
// Unset cache keeper only after having cleared the cache.
this->SetCacheSizeKeeper(0);
delete this->Cache;
this->Cache = 0;
}
//----------------------------------------------------------------------------
void vtkPVCacheKeeper::RemoveAllCaches()
{
// cout << this << " RemoveAllCaches" << endl;
unsigned long freed_size = this->Cache->GetActualMemorySize();
this->Cache->clear();
if (freed_size > 0 && this->CacheSizeKeeper)
{
// Tell the cache size keeper about the newly freed memory size.
this->CacheSizeKeeper->FreeCacheSize(freed_size);
}
// this method should never mark the filter modified !!!
}
//----------------------------------------------------------------------------
bool vtkPVCacheKeeper::IsCached(double cacheTime)
{
vtkPVCacheKeeper::vtkCacheMap::iterator iter = this->Cache->find(cacheTime);
return (iter != this->Cache->end());
}
//----------------------------------------------------------------------------
bool vtkPVCacheKeeper::SaveData(vtkDataObject* output)
{
if (!this->CacheSizeKeeper || !this->CacheSizeKeeper->GetCacheFull())
{
vtkSmartPointer<vtkDataObject> cache;
cache.TakeReference(output->NewInstance());
cache->ShallowCopy(output);
(*this->Cache)[this->CacheTime] = cache;
if (this->CacheSizeKeeper)
{
// Register used cache size.
this->CacheSizeKeeper->AddCacheSize(cache->GetActualMemorySize());
}
return true;
}
return false;
}
//----------------------------------------------------------------------------
vtkExecutive* vtkPVCacheKeeper::CreateDefaultExecutive()
{
vtkPVCacheKeeperPipeline* executive = vtkPVCacheKeeperPipeline::New();
return executive;
}
//----------------------------------------------------------------------------
int vtkPVCacheKeeper::RequestDataObject(
vtkInformation* vtkNotUsed(reqInfo),
vtkInformationVector** inputVector ,
vtkInformationVector* outputVector)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
if (!inInfo)
{
return 0;
}
vtkDataObject *input = vtkDataObject::GetData(inInfo);
if (input)
{
// for each output
for(int i=0; i < this->GetNumberOfOutputPorts(); ++i)
{
vtkDataObject *output = vtkDataObject::GetData(outputVector, 0);
if (!output || !output->IsA(input->GetClassName()))
{
vtkDataObject* newOutput = input->NewInstance();
outputVector->GetInformationObject(0)->Set(vtkDataObject::DATA_OBJECT(), newOutput);
newOutput->Delete();
this->GetOutputPortInformation(i)->Set(
vtkDataObject::DATA_EXTENT_TYPE(), newOutput->GetExtentType());
}
}
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkPVCacheKeeper::RequestData(vtkInformation* vtkNotUsed(reqInfo),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataObject *output = outInfo->Get(vtkDataObject::DATA_OBJECT());
if (this->CachingEnabled)
{
if (this->IsCached(this->CacheTime))
{
output->ShallowCopy((*this->Cache)[this->CacheTime]);
//cout << this << " using Cache: " << this->CacheTime << endl;
vtkPVCacheKeeper::CacheHit++;
}
else
{
output->ShallowCopy(input);
this->SaveData(output);
//cout << this << " Saving cache: " << this->CacheTime << endl;
vtkPVCacheKeeper::CacheMiss++;
}
}
else
{
output->ShallowCopy(input);
vtkPVCacheKeeper::CacheSkips++;
//cout << this << " Not using cache" << endl;
}
return 1;
}
//----------------------------------------------------------------------------
void vtkPVCacheKeeper::ClearCacheStateFlags()
{
vtkPVCacheKeeper::CacheHit = 0;
vtkPVCacheKeeper::CacheMiss = 0;
vtkPVCacheKeeper::CacheSkips = 0;
}
//----------------------------------------------------------------------------
int vtkPVCacheKeeper::GetCacheHits()
{
return vtkPVCacheKeeper::CacheHit;
}
//----------------------------------------------------------------------------
int vtkPVCacheKeeper::GetCacheMisses()
{
return vtkPVCacheKeeper::CacheMiss;
}
//----------------------------------------------------------------------------
int vtkPVCacheKeeper::GetCacheSkips()
{
return vtkPVCacheKeeper::CacheSkips;
}
//----------------------------------------------------------------------------
void vtkPVCacheKeeper::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
|