File: testParallelMultiBlockWriter.py

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (110 lines) | stat: -rw-r--r-- 3,203 bytes parent folder | download | duplicates (3)
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
from __future__ import print_function
import shutil, os

import vtk

from vtk.test import Testing
from vtk.util.misc import vtkGetTempDir
VTK_TEMP_DIR = vtkGetTempDir()

contr = vtk.vtkMultiProcessController.GetGlobalController()
if not contr:
    nranks = 1
    rank = 0
else:
    nranks = contr.GetNumberOfProcesses()
    rank = contr.GetLocalProcessId()

# Let's create a simple multiblock with non-empty blocks as indicated below.
# <MB> is a multiblock dataset.
# [<num>] is a leaf node where the number indicates the ranks on which it is
# non-null
# (<num>) is a leaf node where the number indicates the ranks on which it is
# non-empty

# <MB>
#   |
#   |-- <MB>    # level where a block is non-null on only 1 rank
#   |   |
#   |   |-- [0](0)
#   |   |-- [1](1)
#   |   |-- [2](2)
#   |   ... (up to total number of ranks)
#   |
#   |-- <MB>    # level where block is non-null on all ranks, but non-empty only on 1
#   |   |
#   |   |-- [0,1,..nranks](0)
#   |   |-- [0,1,..nranks](1)
#   |   |-- [0,1,..nranks](2)
#   |   ... (up to total number of ranks)
#   |
#   |-- <MB>    # level where block is non-null, non-empty on all ranks.
#   |   |
#   |   |-- [0,1,..nranks](0,1,...nranks)
#   |   |-- [0,1,..nranks](0,1,...nranks)
#   |   |-- [0,1,..nranks](0,1,...nranks)
#   |   ... (up to total number of ranks)


def createDataSet(empty):
    if not empty:
        s = vtk.vtkSphereSource()
        s.Update()
        clone = s.GetOutputDataObject(0).NewInstance()
        clone.ShallowCopy(s.GetOutputDataObject(0))
        return clone
    else:
        return vtk.vtkPolyData()

def createData(non_null_ranks, non_empty_ranks, num_ranks):
    mb = vtk.vtkMultiBlockDataSet()
    mb.SetNumberOfBlocks(num_ranks)
    for i in non_null_ranks:
        mb.SetBlock(i, createDataSet(i not in non_empty_ranks))
    return mb

def createMB(piece, num_pieces):
    output = vtk.vtkMultiBlockDataSet()
    output.SetNumberOfBlocks(3)
    output.SetBlock(0, createData([piece], [piece], num_pieces))
    output.SetBlock(1, createData(range(num_pieces), [piece], num_pieces))
    output.SetBlock(2, createData(range(num_pieces), range(num_pieces), num_pieces))
    return output

writer = vtk.vtkXMLPMultiBlockDataWriter()
prefix =VTK_TEMP_DIR + "/testParallelXMLWriters"
fname = prefix + ".vtm"

if rank == 0:
    try:
        os.remove(fname)
    except OSError: pass
    try:
        shutil.rmtree(prefix)
    except OSError: pass
    print("Output: %s" % fname)

# put a barrier here to make sure that the files are deleted by process 0
# before going further with the test
if contr:
    contr.Barrier()

writer.SetFileName(fname)
writer.SetInputDataObject(createMB(rank, nranks))
writer.Write()

if contr:
    contr.Barrier()

if rank == 0:
    reader = vtk.vtkXMLMultiBlockDataReader()
    reader.SetFileName(fname)
    reader.Update()

    # since verifying the md structure won't reveal if we have written the write
    # set of files, let's just look at the files we wrote out.
    files = os.listdir(prefix)

    expected_file_count = nranks + nranks + (nranks*nranks)
    print ("Expecting %d files for the leaf nodes" % expected_file_count)
    assert (len(files) == expected_file_count)