forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkThreadedCompositeDataPipeline.cxx
289 lines (250 loc) · 9.13 KB
/
vtkThreadedCompositeDataPipeline.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
281
282
283
284
285
286
287
288
289
/*=========================================================================
Program: Visualization Toolkit
Module: vtkThreadedCompositeDataPipeline.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.
=========================================================================*/
#include "vtkThreadedCompositeDataPipeline.h"
#include "vtkAlgorithm.h"
#include "vtkAlgorithmOutput.h"
#include "vtkExecutive.h"
#include "vtkInformation.h"
#include "vtkInformationExecutivePortKey.h"
#include "vtkInformationExecutivePortVectorKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationObjectBaseKey.h"
#include "vtkInformationRequestKey.h"
#include "vtkInformationVector.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCompositeDataSet.h"
#include "vtkDebugLeaks.h"
#include "vtkImageData.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkTimerLog.h"
#include "vtkSMPProgressObserver.h"
#include "vtkSMPThreadLocal.h"
#include "vtkSMPThreadLocalObject.h"
#include "vtkSMPTools.h"
#include <cassert>
#include <vector>
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkThreadedCompositeDataPipeline);
//------------------------------------------------------------------------------
namespace
{
vtkInformationVector** Clone(vtkInformationVector** src, int n)
{
vtkInformationVector** dst = new vtkInformationVector*[n];
for (int i = 0; i < n; ++i)
{
dst[i] = vtkInformationVector::New();
dst[i]->Copy(src[i], 1);
}
return dst;
}
void DeleteAll(vtkInformationVector** dst, int n)
{
for (int i = 0; i < n; ++i)
{
dst[i]->Delete();
}
delete[] dst;
}
};
//------------------------------------------------------------------------------
class ProcessBlockData : public vtkObjectBase
{
public:
vtkBaseTypeMacro(ProcessBlockData, vtkObjectBase);
vtkInformationVector** In;
vtkInformationVector* Out;
int InSize;
static ProcessBlockData* New()
{
// Can't use object factory macros, this is not a vtkObject.
ProcessBlockData* ret = new ProcessBlockData;
ret->InitializeObjectBase();
return ret;
}
void Construct(
vtkInformationVector** inInfoVec, int inInfoVecSize, vtkInformationVector* outInfoVec)
{
this->InSize = inInfoVecSize;
this->In = Clone(inInfoVec, inInfoVecSize);
this->Out = vtkInformationVector::New();
this->Out->Copy(outInfoVec, 1);
}
~ProcessBlockData() override
{
DeleteAll(this->In, this->InSize);
this->Out->Delete();
}
protected:
ProcessBlockData()
: In(nullptr)
, Out(nullptr)
{
}
};
//------------------------------------------------------------------------------
class ProcessBlock
{
public:
ProcessBlock(vtkThreadedCompositeDataPipeline* exec, vtkInformationVector** inInfoVec,
vtkInformationVector* outInfoVec, int compositePort, int connection, vtkInformation* request,
const std::vector<vtkDataObject*>& inObjs, std::vector<vtkDataObject*>& outObjs)
: Exec(exec)
, InInfoVec(inInfoVec)
, OutInfoVec(outInfoVec)
, CompositePort(compositePort)
, Connection(connection)
, Request(request)
, InObjs(inObjs)
{
int numInputPorts = this->Exec->GetNumberOfInputPorts();
this->OutObjs = &outObjs[0];
this->InfoPrototype = vtkSmartPointer<ProcessBlockData>::New();
this->InfoPrototype->Construct(this->InInfoVec, numInputPorts, this->OutInfoVec);
}
~ProcessBlock()
{
vtkSMPThreadLocal<vtkInformationVector**>::iterator itr1 = this->InInfoVecs.begin();
vtkSMPThreadLocal<vtkInformationVector**>::iterator end1 = this->InInfoVecs.end();
while (itr1 != end1)
{
DeleteAll(*itr1, this->InfoPrototype->InSize);
++itr1;
}
vtkSMPThreadLocal<vtkInformationVector*>::iterator itr2 = this->OutInfoVecs.begin();
vtkSMPThreadLocal<vtkInformationVector*>::iterator end2 = this->OutInfoVecs.end();
while (itr2 != end2)
{
(*itr2)->Delete();
++itr2;
}
}
void Initialize()
{
vtkInformationVector**& inInfoVec = this->InInfoVecs.Local();
vtkInformationVector*& outInfoVec = this->OutInfoVecs.Local();
inInfoVec = Clone(this->InfoPrototype->In, this->InfoPrototype->InSize);
outInfoVec = vtkInformationVector::New();
outInfoVec->Copy(this->InfoPrototype->Out, 1);
vtkInformation*& request = this->Requests.Local();
request->Copy(this->Request, 1);
}
void operator()(vtkIdType begin, vtkIdType end)
{
vtkInformationVector** inInfoVec = this->InInfoVecs.Local();
vtkInformationVector* outInfoVec = this->OutInfoVecs.Local();
vtkInformation* request = this->Requests.Local();
vtkInformation* inInfo = inInfoVec[this->CompositePort]->GetInformationObject(this->Connection);
for (vtkIdType i = begin; i < end; ++i)
{
std::vector<vtkDataObject*> outObjList = this->Exec->ExecuteSimpleAlgorithmForBlock(
&inInfoVec[0], outInfoVec, inInfo, request, this->InObjs[i]);
for (int j = 0; j < outInfoVec->GetNumberOfInformationObjects(); ++j)
{
this->OutObjs[i * outInfoVec->GetNumberOfInformationObjects() + j] = outObjList[j];
}
}
}
void Reduce() {}
protected:
vtkThreadedCompositeDataPipeline* Exec;
vtkInformationVector** InInfoVec;
vtkInformationVector* OutInfoVec;
vtkSmartPointer<ProcessBlockData> InfoPrototype;
int CompositePort;
int Connection;
vtkInformation* Request;
const std::vector<vtkDataObject*>& InObjs;
vtkDataObject** OutObjs;
vtkSMPThreadLocal<vtkInformationVector**> InInfoVecs;
vtkSMPThreadLocal<vtkInformationVector*> OutInfoVecs;
vtkSMPThreadLocalObject<vtkInformation> Requests;
};
//------------------------------------------------------------------------------
vtkThreadedCompositeDataPipeline::vtkThreadedCompositeDataPipeline() = default;
//------------------------------------------------------------------------------
vtkThreadedCompositeDataPipeline::~vtkThreadedCompositeDataPipeline() = default;
//------------------------------------------------------------------------------
void vtkThreadedCompositeDataPipeline::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
void vtkThreadedCompositeDataPipeline::ExecuteEach(vtkCompositeDataIterator* iter,
vtkInformationVector** inInfoVec, vtkInformationVector* outInfoVec, int compositePort,
int connection, vtkInformation* request,
std::vector<vtkSmartPointer<vtkCompositeDataSet>>& compositeOutput)
{
// from input data objects itr -> (inObjs, indices)
// inObjs are the non-null objects that we will loop over.
// indices map the input objects to inObjs
std::vector<vtkDataObject*> inObjs;
std::vector<int> indices;
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
vtkDataObject* dobj = iter->GetCurrentDataObject();
if (dobj)
{
inObjs.push_back(dobj);
indices.push_back(static_cast<int>(inObjs.size()) - 1);
}
else
{
indices.push_back(-1);
}
}
// instantiate outObjs, the output objects that will be created from inObjs
std::vector<vtkDataObject*> outObjs;
outObjs.resize(indices.size() * outInfoVec->GetNumberOfInformationObjects(), nullptr);
// create the parallel task processBlock
ProcessBlock processBlock(
this, inInfoVec, outInfoVec, compositePort, connection, request, inObjs, outObjs);
vtkSmartPointer<vtkProgressObserver> origPo(this->Algorithm->GetProgressObserver());
vtkNew<vtkSMPProgressObserver> po;
this->Algorithm->SetProgressObserver(po);
vtkSMPTools::For(0, static_cast<vtkIdType>(inObjs.size()), processBlock);
this->Algorithm->SetProgressObserver(origPo);
int i = 0;
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem(), i++)
{
int j = indices[i];
if (j >= 0)
{
for (int k = 0; k < outInfoVec->GetNumberOfInformationObjects(); ++k)
{
vtkDataObject* outObj = outObjs[j * outInfoVec->GetNumberOfInformationObjects() + k];
compositeOutput[k]->SetDataSet(iter, outObj);
if (outObj)
{
outObj->FastDelete();
}
}
}
}
}
//------------------------------------------------------------------------------
int vtkThreadedCompositeDataPipeline::CallAlgorithm(vtkInformation* request, int direction,
vtkInformationVector** inInfo, vtkInformationVector* outInfo)
{
// Copy default information in the direction of information flow.
this->CopyDefaultInformation(request, direction, inInfo, outInfo);
// Invoke the request on the algorithm.
int result = this->Algorithm->ProcessRequest(request, inInfo, outInfo);
// If the algorithm failed report it now.
if (!result)
{
vtkErrorMacro("Algorithm " << this->Algorithm->GetClassName() << "(" << this->Algorithm
<< ") returned failure for request: " << *request);
}
return result;
}