forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPBGLGraphSQLReader.cxx
372 lines (311 loc) · 10.7 KB
/
vtkPBGLGraphSQLReader.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPBGLGraphSQLReader.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) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkPBGLGraphSQLReader.h"
#include "vtkDataSetAttributes.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkMutableUndirectedGraph.h"
#include "vtkObjectFactory.h"
#include "vtkPBGLDistributedGraphHelper.h"
#include "vtkSmartPointer.h"
#include "vtkSQLDatabase.h"
#include "vtkSQLQuery.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkTimerLog.h"
#include "vtkVariantArray.h"
#include <vtksys/ios/sstream>
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
vtkStandardNewMacro(vtkPBGLGraphSQLReader);
vtkIdType IdentityDistribution(const vtkVariant& id, void* user_data)
{
vtkIdType* data = (vtkIdType*)user_data;
vtkIdType min, size;
int num_procs = static_cast<int>(data[0]);
int val = id.ToInt() - 1;
for (vtkIdType i = 0; i < num_procs; ++i)
{
vtkPBGLGraphSQLReader::GetRange(i, num_procs, data[1], min, size);
if (val >= min && val < min + size)
{
return i;
}
}
//return static_cast<vtkIdType>(*((int*)user_data));
//return id.ToInt() % 2;
return 0;
}
vtkPBGLGraphSQLReader::vtkPBGLGraphSQLReader()
{
this->Directed = true;
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
this->VertexTable = 0;
this->EdgeTable = 0;
this->SourceField = 0;
this->TargetField = 0;
this->VertexIdField = 0;
this->Database = 0;
this->DistributionUserData[0] = 0;
this->DistributionUserData[1] = 0;
}
vtkPBGLGraphSQLReader::~vtkPBGLGraphSQLReader()
{
this->SetDatabase(0);
this->SetVertexTable(0);
this->SetEdgeTable(0);
this->SetSourceField(0);
this->SetTargetField(0);
this->SetVertexIdField(0);
}
void vtkPBGLGraphSQLReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Directed: " << this->Directed << endl;
os << indent << "VertexIdField: " << (this->VertexIdField ? this->VertexIdField : "(null)") << endl;
os << indent << "SourceField: " << (this->SourceField ? this->SourceField : "(null)") << endl;
os << indent << "TargetField: " << (this->TargetField ? this->TargetField : "(null)") << endl;
os << indent << "EdgeTable: " << (this->EdgeTable ? this->EdgeTable : "(null)") << endl;
os << indent << "VertexTable: " << (this->VertexTable ? this->VertexTable : "(null)") << endl;
os << indent << "Database: " << (this->Database ? "" : "(null)") << endl;
if (this->Database)
{
this->Database->PrintSelf(os, indent.GetNextIndent());
}
}
vtkCxxSetObjectMacro(vtkPBGLGraphSQLReader, Database, vtkSQLDatabase);
template<class MutableGraph>
int vtkPBGLGraphSQLReaderRequestData(
vtkPBGLGraphSQLReader* self,
vtkInformation*,
vtkInformationVector**,
vtkInformationVector* output_vec)
{
VTK_CREATE(vtkTimerLog, timer);
timer->StartTimer();
// Check for valid inputs
if (!self->GetDatabase())
{
vtkGenericWarningMacro("The Database must be defined");
return 0;
}
if (!self->GetEdgeTable())
{
vtkGenericWarningMacro("The EdgeTable must be defined");
return 0;
}
if (!self->GetSourceField())
{
vtkGenericWarningMacro("The SourceField must be defined");
return 0;
}
if (!self->GetTargetField())
{
vtkGenericWarningMacro("The TargetField must be defined");
return 0;
}
if (!self->GetVertexTable())
{
vtkGenericWarningMacro("The VertexTable must be defined");
return 0;
}
if (!self->GetVertexIdField())
{
vtkGenericWarningMacro("The VertexIdField must be defined");
return 0;
}
vtkGraph* output = vtkGraph::GetData(output_vec);
// Create directed or undirected graph
MutableGraph* builder = MutableGraph::New();
// Set up vertex and edge queries
vtksys_ios::ostringstream oss;
oss << "select count(*) from " << self->GetVertexTable();
vtkSmartPointer<vtkSQLQuery> vertex_count;
vertex_count.TakeReference(self->GetDatabase()->GetQueryInstance());
vertex_count->SetQuery(oss.str().c_str());
vertex_count->Execute();
vertex_count->NextRow();
int num_verts = vertex_count->DataValue(0).ToInt();
oss.str("");
oss << "select count(*) from " << self->GetEdgeTable();
vtkSmartPointer<vtkSQLQuery> edge_count;
edge_count.TakeReference(self->GetDatabase()->GetQueryInstance());
edge_count->SetQuery(oss.str().c_str());
edge_count->Execute();
edge_count->NextRow();
int num_edges = edge_count->DataValue(0).ToInt();
vtkInformation* out_info = output_vec->GetInformationObject(0);
int rank = out_info->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
int total = out_info->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
vtkIdType vert_limit, vert_offset;
vtkIdType edge_limit, edge_offset;
self->GetRange(rank, total, num_verts, vert_offset, vert_limit);
self->GetRange(rank, total, num_edges, edge_offset, edge_limit);
// Setup the graph as a distributed graph
VTK_CREATE(vtkPBGLDistributedGraphHelper, helper);
builder->SetDistributedGraphHelper(helper);
// -----[ Verts ]-------------------------
// Read my vertices from vertex query, adding attribute values
oss.str("");
oss << "select * from " << self->GetVertexTable()
<< " limit " << vert_limit
<< " offset " << vert_offset;
vtkSmartPointer<vtkSQLQuery> vertex_query;
vertex_query.TakeReference(self->GetDatabase()->GetQueryInstance());
vertex_query->SetQuery(oss.str().c_str());
vertex_query->Execute();
// Add local vertex data arrays
// Note: GetNumberOfFields() is analogous to the # of columns in Query Tbl.
for (int i = 0; i < vertex_query->GetNumberOfFields(); ++i)
{
vtkStdString field_name = vertex_query->GetFieldName(i);
vtkSmartPointer<vtkAbstractArray> arr;
arr.TakeReference(vtkAbstractArray::CreateArray(
vertex_query->GetFieldType(i)));
arr->SetName(field_name);
if (field_name == self->GetVertexIdField())
{
builder->GetVertexData()->SetPedigreeIds(arr);
}
else
{
builder->GetVertexData()->AddArray(arr);
}
}
helper->Synchronize();
// Add the vertices
VTK_CREATE(vtkVariantArray, row);
while (vertex_query->NextRow(row))
{
builder->LazyAddVertex(row);
}
helper->Synchronize();
// -----[ Edges ]-------------------------
// Read edges from edge query, adding attribute values
oss.str("");
oss << "select * from " << self->GetEdgeTable()
<< " limit " << edge_limit
<< " offset " << edge_offset;
vtkSmartPointer<vtkSQLQuery> edge_query;
edge_query.TakeReference(self->GetDatabase()->GetQueryInstance());
edge_query->SetQuery(oss.str().c_str());
edge_query->Execute();
#if defined(DEBUG) // WCMCLEN
cout << "["<<rank<<"]\tSQLReader: NumberOfEdgeAttributeArrays = "
<< edge_query->GetNumberOfFields() << endl;
fflush(stdout);
#endif
// Add local edge data arrays
for (int i = 0; i < edge_query->GetNumberOfFields(); ++i)
{
vtkStdString field_name = edge_query->GetFieldName(i);
vtkSmartPointer<vtkAbstractArray> arr;
arr.TakeReference(vtkAbstractArray::CreateArray(edge_query->GetFieldType(i)));
arr->SetName(field_name);
builder->GetEdgeData()->AddArray(arr);
#if defined(DEBUG) // WCMCLEN
cout << "["<<rank<<"]\t"
<< "-\tEdge field_name["<<i<<"]='"<<field_name<<"'"
<< endl;
fflush(stdout);
#endif
}
helper->Synchronize();
// Add the edges
int source_id = edge_query->GetFieldIndex(self->GetSourceField());
int target_id = edge_query->GetFieldIndex(self->GetTargetField());
while (edge_query->NextRow(row))
{
vtkVariant source = edge_query->DataValue(source_id);
vtkVariant target = edge_query->DataValue(target_id);
#if defined(DEBUG) // WCMCLEN
cout << "["<<rank<< "]\tReader: Read edge("
<< source.ToString() << ", " << target.ToString() << ", " << &row << ")"
<< endl;
fflush(stdout);
#endif
builder->LazyAddEdge(source, target, row);
}
#if defined(DEBUG) // WCMCLEN
cout << "["<<rank<<"]\tReader: Done adding edges!" << endl;
fflush(stdout);
#endif
helper->Synchronize();
// Call a second synchronize to pick up anything that was possibly sent
// in a trigger after the previous synchronize.
helper->Synchronize();
// Copy into output graph
if (!output->CheckedShallowCopy(builder))
{
vtkGenericWarningMacro("Could not copy to output.");
return 0;
}
timer->StopTimer();
#ifdef VTKPBGL_REPORT_TIMES
// Suppress timer printing (for now)
cerr << "vtkPBGLGraphSQLReader: " << timer->GetElapsedTime() << endl;
#endif
return 1;
}
int vtkPBGLGraphSQLReader::RequestData(
vtkInformation* info,
vtkInformationVector** input_vec,
vtkInformationVector* output_vec)
{
int rval = 0;
if (this->Directed)
{
rval = vtkPBGLGraphSQLReaderRequestData<vtkMutableDirectedGraph>(
this, info, input_vec, output_vec);
}
else
{
rval = vtkPBGLGraphSQLReaderRequestData<vtkMutableUndirectedGraph>(
this, info, input_vec, output_vec);
}
return rval;
}
int vtkPBGLGraphSQLReader::RequestDataObject(
vtkInformation* info,
vtkInformationVector** input_vec,
vtkInformationVector* output_vec)
{
vtkDataObject *current = this->GetExecutive()->GetOutputData(0);
if (!current
|| (this->Directed && !vtkDirectedGraph::SafeDownCast(current))
|| (!this->Directed && vtkDirectedGraph::SafeDownCast(current)))
{
vtkGraph *output = 0;
if (this->Directed)
{
output = vtkDirectedGraph::New();
}
else
{
output = vtkUndirectedGraph::New();
}
vtkInformation* out_info = output_vec->GetInformationObject(0);
this->GetExecutive()->SetOutputData(0, output);
output->Delete();
}
return 1;
}
void vtkPBGLGraphSQLReader::GetRange(int rank, int total,
vtkIdType size, vtkIdType& offset, vtkIdType& limit)
{
offset = size*rank/total;
limit = size*(rank+1)/total - offset;
}