File: TestPartitionBalancer.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,992 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 185; javascript: 165; objc: 153; tcl: 59
file content (419 lines) | stat: -rw-r--r-- 12,100 bytes parent folder | download | duplicates (4)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkPartitionBalancer.h"

#include "vtkFieldData.h"
#include "vtkImageData.h"
#include "vtkLogger.h"
#include "vtkMPIController.h"
#include "vtkMultiProcessController.h"
#include "vtkNew.h"
#include "vtkPartitionedDataSet.h"
#include "vtkPartitionedDataSetCollection.h"
#include "vtkStringArray.h"

namespace
{
constexpr const char* names[2][4] = { { "r0_PD0_DS0", "r0_PD0_DS1", "r0_PD0_DS2", "r0_PD1_DS0" },
  { "r1_PD0_DS0", "r2_PD0_DS1", nullptr, nullptr } };

//----------------------------------------------------------------------------
vtkNew<vtkImageData> GenerateDataSet(int rank, int id)
{
  vtkNew<vtkStringArray> array;
  array->SetName(names[rank][id]);
  vtkNew<vtkImageData> ds;
  ds->GetFieldData()->AddArray(array);
  return ds;
}

//----------------------------------------------------------------------------
bool TestExpandPDS0(vtkPartitionedDataSet* outPDS0, int rank)
{
  bool retVal = true;

  if (outPDS0->GetNumberOfPartitions() != 5)
  {
    vtkLog(ERROR,
      "Wrong number of generated partitions in PD0 in rank "
        << rank << ". There are " << outPDS0->GetNumberOfPartitions() << " instead of 5.");
    retVal = false;
  }

  vtkDataSet* outDS0 = outPDS0->GetPartition(0);
  vtkDataSet* outDS1 = outPDS0->GetPartition(1);
  vtkDataSet* outDS2 = outPDS0->GetPartition(2);
  vtkDataSet* outDS3 = outPDS0->GetPartition(3);
  vtkDataSet* outDS4 = outPDS0->GetPartition(4);

  if (rank == 0)
  {
    if (!outDS0 || !outDS1 || !outDS2)
    {
      vtkLog(ERROR,
        "Output partitioned data set r0 - PD0 has nullptr at wrong places."
          << " All those pointers should be non nullptr: DS0 == " << outDS0 << ", DS1 == " << outDS1
          << ", DS2 == " << outDS2);
      retVal = false;
    }
    if (retVal != EXIT_FAILURE &&
      (!outDS0->GetFieldData()->GetAbstractArray(names[0][0]) ||
        !outDS1->GetFieldData()->GetAbstractArray(names[0][1]) ||
        !outDS2->GetFieldData()->GetAbstractArray(names[0][2])))
    {
      vtkLog(ERROR, "Output partitioned data set r0 - PD0 have been wrongly copied.");
      retVal = false;
    }
    if (outDS3 || outDS4)
    {
      vtkLog(ERROR,
        "Output partitioned data set r0 - PD0"
          << " should have nullptr at partition 3 and 4");
      retVal = false;
    }
  }
  if (rank == 1)
  {
    if (!outDS3 || !outDS4)
    {
      vtkLog(ERROR,
        "Output partitioned data set r1 - PD0 has nullptr at wrong places."
          << " All those pointers should be non nullptr: DS3 == " << outDS3
          << ", DS4 == " << outDS4);
      retVal = false;
    }
    if (retVal != EXIT_FAILURE &&
      (!outDS3->GetFieldData()->GetAbstractArray(names[1][0]) ||
        !outDS4->GetFieldData()->GetAbstractArray(names[1][1])))
    {
      vtkLog(ERROR, "Output partitioned data set r1 - PD0 have been wrongly copied.");
      retVal = false;
    }
    if (outDS0 || outDS1 || outDS2)
    {
      vtkLog(ERROR,
        "Output partitioned data set r1 - PD0"
          << " should have nullptr at partition 3 and 4");
      retVal = false;
    }
  }

  return retVal;
}

//----------------------------------------------------------------------------
bool TestExpandPDS1(vtkPartitionedDataSet* outPDS1, int rank)
{
  bool retVal = true;

  if (outPDS1->GetNumberOfPartitions() != 1)
  {
    vtkLog(ERROR,
      "Wrong number of generated partitions in PD0 in rank "
        << rank << ". There are " << outPDS1->GetNumberOfPartitions() << " instead of 1.");
    retVal = false;
  }

  vtkDataSet* outDS100 = outPDS1->GetPartition(0);

  if (rank == 0)
  {
    if (!outDS100)
    {
      vtkLog(ERROR, "Output partitioned data set r0 - PD1 has a nullptr partition.");
      retVal = false;
    }
    else if (!outDS100->GetFieldData()->GetAbstractArray(names[0][3]))
    {
      vtkLog(ERROR, "DS100 has been wrongly copied in rank 0.");
    }
  }
  if (rank == 1)
  {
    if (outDS100)
    {
      vtkLog(ERROR, "Output partitioned data set r1 - PD1 should have a nullptr partition.");
      retVal = false;
    }
  }

  return retVal;
}

//----------------------------------------------------------------------------
bool TestSquashPDS0(vtkPartitionedDataSet* outPDS0, int rank)
{
  bool retVal = true;

  if (outPDS0->GetNumberOfPartitions() != 3)
  {
    vtkLog(ERROR,
      "Wrong number of generated partitions in PD0 in rank "
        << rank << ". There are " << outPDS0->GetNumberOfPartitions() << " instead of 3.");
    retVal = false;
  }

  if (rank == 0)
  {
    vtkDataSet* outDS0 = outPDS0->GetPartition(0);
    vtkDataSet* outDS1 = outPDS0->GetPartition(1);
    vtkDataSet* outDS2 = outPDS0->GetPartition(2);

    if (!outDS0 || !outDS1 || !outDS2)
    {
      vtkLog(ERROR,
        "Output partitioned data set r0 - PD0 has nullptr at wrong places."
          << " All those pointers should be non nullptr: DS0 == " << outDS0 << ", DS1 == " << outDS1
          << ", DS2 == " << outDS2);
      retVal = false;
    }
    if (retVal &&
      (!outDS0->GetFieldData()->GetAbstractArray(names[0][0]) ||
        !outDS1->GetFieldData()->GetAbstractArray(names[0][1]) ||
        !outDS2->GetFieldData()->GetAbstractArray(names[0][2])))
    {
      vtkLog(ERROR, "Output partitioned data set r0 - PD0 have been wrongly copied.");
      retVal = false;
    }
  }
  if (rank == 1)
  {
    vtkDataSet* outDS3 = outPDS0->GetPartition(0);
    vtkDataSet* outDS4 = outPDS0->GetPartition(1);
    vtkDataSet* outDS5 = outPDS0->GetPartition(2);

    if (!outDS3 || !outDS4)
    {
      vtkLog(ERROR,
        "Output partitioned data set r1 - PD0 has nullptr at wrong places."
          << " All those pointers should be non nullptr: DS3 == " << outDS3
          << ", DS4 == " << outDS4);
      retVal = false;
    }
    if (retVal &&
      (!outDS3->GetFieldData()->GetAbstractArray(names[1][0]) ||
        !outDS4->GetFieldData()->GetAbstractArray(names[1][1])))
    {
      vtkLog(ERROR, "Output partitioned data set r1 - PD0 have been wrongly copied.");
      retVal = false;
    }
    if (outDS5)
    {
      vtkLog(ERROR,
        "Output partitioned data set r1 - PD0"
          << " should have nullptr at partition 2");
      retVal = false;
    }
  }

  return retVal;
}

//----------------------------------------------------------------------------
bool TestSquashPDS1(vtkPartitionedDataSet* outPDS1, int rank)
{
  bool retVal = true;

  if (outPDS1->GetNumberOfPartitions() != 1)
  {
    vtkLog(ERROR,
      "Wrong number of generated partitions in PD1 in rank "
        << rank << ". There are " << outPDS1->GetNumberOfPartitions() << " instead of 1.");
    retVal = false;
  }

  vtkDataSet* outDS100 = outPDS1->GetPartition(0);

  if (rank == 0)
  {
    if (!outDS100)
    {
      vtkLog(ERROR, "Output partitioned data set r0 - PD1 has a nullptr partition.");
      retVal = false;
    }
    else if (!outDS100->GetFieldData()->GetAbstractArray(names[0][3]))
    {
      vtkLog(ERROR, "DS100 has been wrongly copied in rank 0.");
    }
  }
  if (rank == 1)
  {
    if (outDS100)
    {
      vtkLog(ERROR, "Output partitioned data set r1 - PD1 should have a nullptr partition.");
      retVal = false;
    }
  }

  return retVal;
}
} // anonmymous namespace

//----------------------------------------------------------------------------
int TestPartitionBalancer(int argc, char* argv[])
{
  int retVal = EXIT_SUCCESS;

  vtkNew<vtkMPIController> controller;

  controller->Initialize(&argc, &argv);
  vtkMultiProcessController::SetGlobalController(controller);

  int rank = controller->GetLocalProcessId();

  vtkNew<vtkPartitionedDataSetCollection> pdsc;
  pdsc->SetNumberOfPartitionedDataSets(2);

  // rank 0: PDC [ PD (DS0, DS1,     DS2) ] [PD (nullptr, DS100) ]
  // rank 1: PDC [ PD (DS3, nullptr, DS4) ] [PD () ]

  if (rank == 0)
  {
    vtkNew<vtkPartitionedDataSet> pds0;
    pds0->SetNumberOfPartitions(3);
    pds0->SetPartition(0, GenerateDataSet(0, 0));
    pds0->SetPartition(1, GenerateDataSet(0, 1));
    pds0->SetPartition(2, GenerateDataSet(0, 2));
    pdsc->SetPartitionedDataSet(0, pds0);

    vtkNew<vtkPartitionedDataSet> pds1;
    pds1->SetNumberOfPartitions(2);
    pds1->SetPartition(0, nullptr);
    pds1->SetPartition(0, GenerateDataSet(0, 3));
    pdsc->SetPartitionedDataSet(1, pds1);
  }
  else if (rank == 1)
  {
    vtkNew<vtkPartitionedDataSet> pds0;
    pds0->SetNumberOfPartitions(3);
    pds0->SetPartition(0, GenerateDataSet(1, 0));
    pds0->SetPartition(1, nullptr);
    pds0->SetPartition(2, GenerateDataSet(1, 1));
    pdsc->SetPartitionedDataSet(0, pds0);

    pdsc->SetPartitionedDataSet(1, vtkNew<vtkPartitionedDataSet>());
  }

  vtkNew<vtkPartitionBalancer> balancer;
  balancer->SetInputDataObject(pdsc);
  balancer->SetController(controller);

  if (rank == 0)
  {
    vtkLog(INFO, "Testing vtkPartitionBalancer for vtkPartitionedDataSetCollection input");
  }

  if (rank == 0)
  {
    vtkLog(INFO, "*** Expand mode");
  }

  balancer->SetModeToExpand();
  balancer->Update();

  {
    // Looking if output looks like that:
    // rank 0: PDC [ PD (DS0,     DS1,     DS2,     nullptr, nullptr) ] [PD (DS100)   ]
    // rank 1: PDC [ PD (nullptr, nullptr, nullptr, DS3,     DS4)     ] [PD (nullptr) ]
    auto outPDSC = vtkPartitionedDataSetCollection::SafeDownCast(balancer->GetOutputDataObject(0));

    if (outPDSC->GetNumberOfPartitionedDataSets() != 2)
    {
      vtkLog(ERROR,
        "Wrong number of generated partitioned data sets in rank "
          << rank << ". There are " << outPDSC->GetNumberOfPartitionedDataSets()
          << " instead of 2");
      retVal = EXIT_FAILURE;
    }

    retVal = !TestExpandPDS0(outPDSC->GetPartitionedDataSet(0), rank) ||
        !TestExpandPDS1(outPDSC->GetPartitionedDataSet(1), rank)
      ? EXIT_FAILURE
      : retVal;
  }

  if (rank == 0)
  {
    vtkLog(INFO, "*** Squash mode");
  }

  balancer->SetModeToSquash();
  balancer->Update();

  {
    // Looking if output looks like that:
    //  rank 0: PDC [ PD (DS0, DS1, DS2)     ] [PD (DS100)   ]
    //  rank 1: PDC [ PD (DS3, DS4, nullptr) ] [PD (nullptr) ]
    auto outPDSC = vtkPartitionedDataSetCollection::SafeDownCast(balancer->GetOutputDataObject(0));

    if (outPDSC->GetNumberOfPartitionedDataSets() != 2)
    {
      vtkLog(ERROR,
        "Wrong number of generated partitioned data sets in rank "
          << rank << ". There are " << outPDSC->GetNumberOfPartitionedDataSets()
          << " instead of 2");
      retVal = EXIT_FAILURE;
    }

    retVal = !TestSquashPDS0(outPDSC->GetPartitionedDataSet(0), rank) ||
        !TestSquashPDS1(outPDSC->GetPartitionedDataSet(1), rank)
      ? EXIT_FAILURE
      : retVal;
  }

  if (rank == 0)
  {
    vtkLog(INFO, "Testing vtkPartitionBalancer for vtkPartitionedDataSet input");
  }

  // Here, we will test the same input as before, but directly feed in partitioned data sets.

  if (rank == 0)
  {
    vtkLog(INFO, "*** Expand mode");
  }

  balancer->SetModeToExpand();
  balancer->SetInputDataObject(pdsc->GetPartitionedDataSet(0));
  balancer->Update();

  retVal =
    !TestExpandPDS0(vtkPartitionedDataSet::SafeDownCast(balancer->GetOutputDataObject(0)), rank)
    ? EXIT_FAILURE
    : retVal;

  balancer->SetInputDataObject(pdsc->GetPartitionedDataSet(1));
  balancer->Update();

  retVal =
    !TestExpandPDS1(vtkPartitionedDataSet::SafeDownCast(balancer->GetOutputDataObject(0)), rank)
    ? EXIT_FAILURE
    : retVal;

  if (rank == 0)
  {
    vtkLog(INFO, "*** Squash mode");
  }

  balancer->SetModeToSquash();
  balancer->SetInputDataObject(pdsc->GetPartitionedDataSet(0));
  balancer->Update();

  retVal =
    !TestSquashPDS0(vtkPartitionedDataSet::SafeDownCast(balancer->GetOutputDataObject(0)), rank)
    ? EXIT_FAILURE
    : retVal;

  balancer->SetInputDataObject(pdsc->GetPartitionedDataSet(1));
  balancer->Update();

  retVal =
    !TestSquashPDS1(vtkPartitionedDataSet::SafeDownCast(balancer->GetOutputDataObject(0)), rank)
    ? EXIT_FAILURE
    : retVal;

  controller->Finalize();

  return retVal;
}