forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkComputingResources.cxx
280 lines (238 loc) · 9.2 KB
/
vtkComputingResources.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkComputingResources.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright (c) 2008, 2009 by SCI Institute, University of Utah.
This is part of the Parallel Dataflow System originally developed by
Huy T. Vo and Claudio T. Silva. For more information, see:
"Parallel Dataflow Scheme for Streaming (Un)Structured Data" by Huy
T. Vo, Daniel K. Osmari, Brian Summa, Joao L.D. Comba, Valerio
Pascucci and Claudio T. Silva, SCI Institute, University of Utah,
Technical Report #UUSCI-2009-004, 2009.
"Multi-Threaded Streaming Pipeline For VTK" by Huy T. Vo and Claudio
T. Silva, SCI Institute, University of Utah, Technical Report
#UUSCI-2009-005, 2009.
-------------------------------------------------------------------------*/
#include "vtkComputingResources.h"
#include "vtkInformation.h"
#include "vtkMultiThreader.h"
#include "vtkObjectFactory.h"
#include "vtkThreadedImageAlgorithm.h"
#include "vtkThreadedStreamingPipeline.h"
#include <vtksys/hash_map.hxx>
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkComputingResources);
//----------------------------------------------------------------------------
class vtkCPUResource: public vtkProcessingUnitResource
{
public:
virtual int ProcessingUnit()
{
return vtkThreadedStreamingPipeline::PROCESSING_UNIT_CPU;
}
virtual bool HasResource()
{
return this->NumberOfThreads > 0;
}
virtual void Clear()
{
this->NumberOfThreads = 0;
}
virtual void ObtainMinimum()
{
this->NumberOfThreads = 1;
}
virtual void ObtainMaximum()
{
this->NumberOfThreads = vtkMultiThreader::GetGlobalDefaultNumberOfThreads();
}
virtual void IncreaseByRatio(float ratio, vtkProcessingUnitResource *refResource)
{
vtkCPUResource *other = static_cast<vtkCPUResource*>(refResource);
int thisNThread = (int)(ratio*other->NumberOfThreads+0.5);
if (thisNThread<1)
{
thisNThread = 1;
}
this->NumberOfThreads += thisNThread;
}
virtual void AllocateFor(vtkThreadedStreamingPipeline *exec)
{
if (exec->GetAlgorithm()->IsA("vtkThreadedImageAlgorithm"))
{
vtkThreadedImageAlgorithm::SafeDownCast(exec->GetAlgorithm())
->SetNumberOfThreads(this->NumberOfThreads);
}
}
virtual bool CanAccommodate(vtkProcessingUnitResource *refResource)
{
vtkCPUResource *other = static_cast<vtkCPUResource*>(refResource);
return this->NumberOfThreads>=other->NumberOfThreads;
}
virtual void Reserve(vtkProcessingUnitResource *refResource)
{
vtkCPUResource *other = static_cast<vtkCPUResource*>(refResource);
this->NumberOfThreads -= other->NumberOfThreads;
}
virtual void Collect(vtkProcessingUnitResource *refResource)
{
vtkCPUResource *other = static_cast<vtkCPUResource*>(refResource);
this->NumberOfThreads += other->NumberOfThreads;
}
private:
int NumberOfThreads;
};
//----------------------------------------------------------------------------
// This needs to reimplement
class vtkGPUResource: public vtkProcessingUnitResource
{
public:
virtual int ProcessingUnit()
{
return vtkThreadedStreamingPipeline::PROCESSING_UNIT_GPU;
}
virtual bool HasResource()
{
return false;
}
virtual void Clear() {}
virtual void ObtainMinimum() {}
virtual void ObtainMaximum() {}
virtual void IncreaseByRatio(float vtkNotUsed(ratio),
vtkProcessingUnitResource* vtkNotUsed(refResource)) {}
virtual void AllocateFor(vtkThreadedStreamingPipeline* vtkNotUsed(exec))
{
fprintf(stderr, "vtkGPUResource NEEDS TO BE IMPLEMENTED!!!!\n");
}
bool CanAccommodate(vtkProcessingUnitResource* vtkNotUsed(refResource))
{
return false;
}
void Reserve(vtkProcessingUnitResource* vtkNotUsed(refResource)) {}
void Collect(vtkProcessingUnitResource* vtkNotUsed(refResource)) {}
private:
};
//----------------------------------------------------------------------------
class vtkComputingResources::implementation
{
public:
typedef vtksys::hash_map<int, vtkProcessingUnitResource*> ProcessingUnitToResourceHashMap;
ProcessingUnitToResourceHashMap ResourceMap;
};
//----------------------------------------------------------------------------
vtkComputingResources::vtkComputingResources() :
Implementation(new implementation())
{
this->Implementation->ResourceMap[vtkThreadedStreamingPipeline::PROCESSING_UNIT_CPU] = new vtkCPUResource();
this->Implementation->ResourceMap[vtkThreadedStreamingPipeline::PROCESSING_UNIT_GPU] = new vtkGPUResource();
this->ObtainMinimumResources();
}
//----------------------------------------------------------------------------
vtkComputingResources::~vtkComputingResources()
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.begin();
for (; i != this->Implementation->ResourceMap.end(); i++)
{
delete (*i).second;
}
this->Implementation->ResourceMap.clear();
delete this->Implementation;
}
//----------------------------------------------------------------------------
void vtkComputingResources::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
vtkProcessingUnitResource *vtkComputingResources::GetResourceFor(int processingUnit)
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.find(processingUnit);
if (i != this->Implementation->ResourceMap.end())
{
return (*i).second;
}
return NULL;
}
//----------------------------------------------------------------------------
void vtkComputingResources::Clear()
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.begin();
for (; i != this->Implementation->ResourceMap.end(); i++)
{
(*i).second->Clear();
}
}
//----------------------------------------------------------------------------
void vtkComputingResources::ObtainMinimumResources()
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.begin();
for (; i != this->Implementation->ResourceMap.end(); i++) {
(*i).second->ObtainMinimum();
}
}
//----------------------------------------------------------------------------
void vtkComputingResources::ObtainMaximumResources()
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.begin();
for (; i != this->Implementation->ResourceMap.end(); i++) {
(*i).second->ObtainMaximum();
}
}
//----------------------------------------------------------------------------
void vtkComputingResources::Deploy(vtkThreadedStreamingPipeline *exec,
vtkInformation* vtkNotUsed(info))
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.begin();
for (; i != this->Implementation->ResourceMap.end(); i++)
{
int resource = vtkThreadedStreamingPipeline::PROCESSING_UNIT_CPU;
// if (exec->GetAlgorithm()->GetInformation()->
// Has(vtkThreadedStreamingPipeline::PROCESSING_UNIT()))
// resource = exec->GetAlgorithm()->GetInformation()->
// Get(vtkThreadedStreamingPipeline::PROCESSING_UNIT());
if (((*i).first & resource) &&
(*i).second->HasResource())
{
(*i).second->AllocateFor(exec);
fprintf(stderr, "UPDATE %s\n", exec->GetAlgorithm()->GetClassName());
exec->Update();
// exec->ForceUpdateData((*i).first, info);
}
}
}
//----------------------------------------------------------------------------
bool vtkComputingResources::Reserve(vtkComputingResources *res)
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.find(vtkThreadedStreamingPipeline::PROCESSING_UNIT_CPU);
implementation::ProcessingUnitToResourceHashMap::iterator j =
res->Implementation->ResourceMap.find(vtkThreadedStreamingPipeline::PROCESSING_UNIT_CPU);
bool ok = (*i).second->CanAccommodate((*j).second);
if (ok)
{
(*i).second->Reserve((*j).second);
}
return ok;
}
//----------------------------------------------------------------------------
void vtkComputingResources::Collect(vtkComputingResources *res)
{
implementation::ProcessingUnitToResourceHashMap::iterator i =
this->Implementation->ResourceMap.find(vtkThreadedStreamingPipeline::PROCESSING_UNIT_CPU);
implementation::ProcessingUnitToResourceHashMap::iterator j =
res->Implementation->ResourceMap.find(vtkThreadedStreamingPipeline::PROCESSING_UNIT_CPU);
(*i).second->Collect((*j).second);
}