VTK-m  2.0
augmented/PrintVectors.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 // Copyright (c) 2018, The Regents of the University of California, through
11 // Lawrence Berkeley National Laboratory (subject to receipt of any required approvals
12 // from the U.S. Dept. of Energy). All rights reserved.
13 //
14 // Redistribution and use in source and binary forms, with or without modification,
15 // are permitted provided that the following conditions are met:
16 //
17 // (1) Redistributions of source code must retain the above copyright notice, this
18 // list of conditions and the following disclaimer.
19 //
20 // (2) Redistributions in binary form must reproduce the above copyright notice,
21 // this list of conditions and the following disclaimer in the documentation
22 // and/or other materials provided with the distribution.
23 //
24 // (3) Neither the name of the University of California, Lawrence Berkeley National
25 // Laboratory, U.S. Dept. of Energy nor the names of its contributors may be
26 // used to endorse or promote products derived from this software without
27 // specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
37 // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
38 // OF THE POSSIBILITY OF SUCH DAMAGE.
39 //
40 //=============================================================================
41 //
42 // This code is an extension of the algorithm presented in the paper:
43 // Parallel Peak Pruning for Scalable SMP Contour Tree Computation.
44 // Hamish Carr, Gunther Weber, Christopher Sewell, and James Ahrens.
45 // Proceedings of the IEEE Symposium on Large Data Analysis and Visualization
46 // (LDAV), October 2016, Baltimore, Maryland.
47 //
48 // The PPP2 algorithm and software were jointly developed by
49 // Hamish Carr (University of Leeds), Gunther H. Weber (LBNL), and
50 // Oliver Ruebel (LBNL)
51 //==============================================================================
52 
53 // include guard
54 #ifndef vtk_m_worklet_contourtree_augmented_print_vectors_h
55 #define vtk_m_worklet_contourtree_augmented_print_vectors_h
56 
57 // global libraries
58 #include <iomanip>
59 #include <iostream>
60 #include <string>
61 
62 // local includes
65 
66 
67 namespace vtkm
68 {
69 namespace worklet
70 {
71 namespace contourtree_augmented
72 {
73 
74 // local constants to allow changing the spacing as needed
75 constexpr int PRINT_WIDTH = 12;
76 constexpr int PREFIX_WIDTH = 30;
77 
78 template <typename T, typename StorageType>
79 void PrintValues(std::string label,
81  vtkm::Id nValues = -1,
82  std::ostream& outStream = std::cout);
83 template <typename T>
84 void PrintIndices(std::string label,
85  const vtkm::cont::ArrayHandle<T>& iVec,
86  vtkm::Id nIndices = -1,
87  std::ostream& outStream = std::cout);
88 template <typename T>
89 void PrintArray(std::string label,
90  const T& iVec,
91  vtkm::Id nIndices = -1,
92  std::ostream& outStream = std::cout);
93 template <typename T, typename StorageType>
94 void PrintSortedValues(std::string label,
96  IdArrayType& sortVec,
97  vtkm::Id nValues = -1,
98  std::ostream& outStream = std::cout);
99 
100 // base routines for printing label & prefix bars
101 inline void PrintLabel(std::string label, std::ostream& outStream = std::cout)
102 { // PrintLabel()
103  // print out the front end
104  outStream << std::setw(PREFIX_WIDTH) << std::left << label;
105  // print out the vertical line
106  outStream << std::right << "|";
107 } // PrintLabel()
108 
109 
110 inline void PrintSeparatingBar(vtkm::Id howMany, std::ostream& outStream = std::cout)
111 { // PrintSeparatingBar()
112  // print out the front end
113  outStream << std::setw(PREFIX_WIDTH) << std::setfill('-') << "";
114  // now the + at the vertical line
115  outStream << "+";
116  // now print out the tail end - fixed number of spaces per entry
117  for (vtkm::Id block = 0; block < howMany; block++)
118  {
119  outStream << std::setw(PRINT_WIDTH) << std::setfill('-') << "";
120  }
121  // now the std::endl, resetting the fill character
122  outStream << std::setfill(' ') << std::endl;
123 } // PrintSeparatingBar()
124 
125 
126 // routine to print out a single index
127 inline void PrintIndexType(vtkm::Id index, std::ostream& outStream = std::cout)
128 { // PrintIndexType
129  outStream << std::setw(PRINT_WIDTH - 6) << MaskedIndex(index) << " " << FlagString(index);
130 } // PrintIndexType
131 
132 
133 // routine to print out a single value
134 template <typename T>
135 inline void PrintDataType(T value, std::ostream& outStream = std::cout)
136 { // PrintDataType
137  outStream << std::setw(PRINT_WIDTH) << value;
138 } // PrintDataType
139 
140 
141 // Specialization of PrintDataType for vtkm::Id to use PrintIndexType instead so we can properly
142 // print Id arrays using the PrintArrayHandle function, e.g,. to pint permutted Id arrays.
143 template <>
144 inline void PrintDataType<vtkm::Id>(vtkm::Id value, std::ostream& outStream)
145 {
146  PrintIndexType(value, outStream);
147 }
148 
149 
150 // header line
151 inline void PrintHeader(vtkm::Id howMany, std::ostream& outStream = std::cout)
152 { // PrintHeader()
153  // print out a separating bar
154  PrintSeparatingBar(howMany, outStream);
155  // print out a label
156  PrintLabel("ID", outStream);
157  // print out the ID numbers
158  for (vtkm::Id entry = 0; entry < howMany; entry++)
159  {
160  PrintIndexType(entry, outStream);
161  }
162  // and an std::endl
163  outStream << std::endl;
164  // print out another separating bar
165  PrintSeparatingBar(howMany, outStream);
166 } // PrintHeader()
167 
168 
169 // base routines for reading & writing host vectors
170 template <typename ARRAYTYPE>
171 inline void PrintArrayHandle(std::string label,
172  const ARRAYTYPE& dVec,
173  vtkm::Id nValues,
174  std::ostream& outStream)
175 { // PrintArrayHandle()
176  // -1 means full size
177  if (nValues == -1)
178  {
179  nValues = dVec.GetNumberOfValues();
180  }
181 
182  // print the label
183  PrintLabel(label, outStream);
184 
185  // now print the data
186  auto portal = dVec.ReadPortal();
187  for (vtkm::Id entry = 0; entry < nValues; entry++)
188  {
189  PrintDataType(portal.Get(entry), outStream);
190  }
191  // and an std::endl
192  outStream << std::endl;
193 } // PrintArrayHandle()
194 
195 // base routines for reading & writing host vectors
196 template <typename T, typename StorageType>
197 inline void PrintValues(std::string label,
199  vtkm::Id nValues,
200  std::ostream& outStream)
201 { // PrintValues()
202  // -1 means full size
203  if (nValues == -1)
204  {
205  nValues = dVec.GetNumberOfValues();
206  }
207 
208  // print the label
209  PrintLabel(label, outStream);
210 
211  // now print the data
212  auto portal = dVec.ReadPortal();
213  for (vtkm::Id entry = 0; entry < nValues; entry++)
214  {
215  PrintDataType(portal.Get(entry), outStream);
216  }
217  // and an std::endl
218  outStream << std::endl;
219 } // PrintValues()
220 
221 
222 // base routines for reading & writing host vectors
223 template <typename T, typename StorageType>
224 inline void PrintSortedValues(std::string label,
226  IdArrayType& sortVec,
227  vtkm::Id nValues,
228  std::ostream& outStream)
229 { // PrintSortedValues()
230  // -1 means full size
231  if (nValues == -1)
232  {
233  nValues = sortVec.GetNumberOfValues();
234  }
235 
236  // print the label
237  PrintLabel(label, outStream);
238 
239  // now print the data
240  auto dportal = dVec.ReadPortal();
241  auto sortPortal = sortVec.ReadPortal();
242  for (vtkm::Id entry = 0; entry < nValues; entry++)
243  {
244  PrintDataType(dportal.Get(sortPortal.Get(entry)), outStream);
245  }
246  // and an std::endl
247  outStream << std::endl;
248 } // PrintSortedValues()
249 
250 
251 // routine for printing index arrays
252 template <typename T>
253 inline void PrintIndices(std::string label,
254  const vtkm::cont::ArrayHandle<T>& iVec,
255  vtkm::Id nIndices,
256  std::ostream& outStream)
257 { // PrintIndices()
258  // -1 means full size
259  if (nIndices == -1)
260  {
261  nIndices = iVec.GetNumberOfValues();
262  }
263 
264  // print the label
265  PrintLabel(label, outStream);
266 
267  auto portal = iVec.ReadPortal();
268  for (vtkm::Id entry = 0; entry < nIndices; entry++)
269  PrintIndexType(portal.Get(entry), outStream);
270 
271  // and the std::endl
272  outStream << std::endl;
273 } // PrintIndices()
274 
275 // routine for printing index arrays
276 template <typename T>
277 inline void PrintArray(std::string label, const T& iVec, vtkm::Id nArray, std::ostream& outStream)
278 { // PrintArray()
279  // -1 means full size
280  if (nArray == -1)
281  {
282  nArray = iVec.GetNumberOfValues();
283  }
284 
285  // print the label
286  PrintLabel(label, outStream);
287 
288  auto portal = iVec.ReadPortal();
289  for (vtkm::Id entry = 0; entry < nArray; entry++)
290  PrintIndexType(portal.Get(entry), outStream);
291 
292  // and the std::endl
293  outStream << std::endl;
294 } // PrintArray()
295 
296 template <typename T, typename StorageType>
297 inline void PrintLabelledDataBlock(std::string label,
299  vtkm::Id nColumns,
300  std::ostream& outStream = std::cout)
301 { // PrintLabelledDataBlock()
302  // start with a header
303  PrintHeader(nColumns, outStream);
304  // loop control variable
305  vtkm::Id entry = 0;
306  // per row
307  auto portal = dVec.ReadPortal();
308  for (vtkm::Id row = 0; entry < portal.GetNumberOfValues(); row++)
309  { // per row
310  PrintLabel(label + "[" + std::to_string(row) + "]", outStream);
311  // now print the data
312  for (vtkm::Id col = 0; col < nColumns; col++, entry++)
313  {
314  PrintDataType(portal.Get(entry), outStream);
315  }
316  outStream << std::endl;
317  } // per row
318  // and a final std::endl
319  outStream << std::endl;
320 } // PrintLabelledDataBlock()
321 
322 
323 // routine for printing list of edge pairs in row format, i.e., First and Second of the Edge
324 // are printed separated. Used, e.g.,in standard debug.
325 inline void PrintEdgePairArray(std::string label,
326  const EdgePairArray& edgePairArray,
327  vtkm::Id nIndices,
328  std::ostream& outStream = std::cout)
329 { // PrintEdgePairArray()
330  // -1 means full size
331  if (nIndices == -1)
332  {
333  nIndices = edgePairArray.GetNumberOfValues();
334  }
335  // now print them out
336  auto edgePairArrayConstPortal = edgePairArray.ReadPortal();
337 
338  // print the low end
339  PrintLabel(label + " High", outStream);
340  for (vtkm::Id superarc = 0; superarc < nIndices; superarc++)
341  { // per superarc
342  PrintIndexType(edgePairArrayConstPortal.Get(superarc).second, outStream);
343  } // per superarc
344  outStream << std::endl;
345 
346  // print the high end
347  PrintLabel(label + " Low", outStream);
348  for (vtkm::Id superarc = 0; superarc < nIndices; superarc++)
349  { // per edge
350  PrintIndexType(edgePairArrayConstPortal.Get(superarc).first, outStream);
351  }
352  outStream << std::endl;
353 } // PrintEdgePairArray()
354 
355 
356 // routine for printing list of edge pairs in column format (first, second).
357 // Used, e.g., to print the sorted list of saddle peaks from the ContourTree
358 inline void PrintEdgePairArrayColumnLayout(const EdgePairArray& edgePairArray,
359  std::ostream& outStream = std::cout)
360 { // PrintEdgePairArray()
361  // now print them out
362  auto edgePairArrayConstPortal = edgePairArray.ReadPortal();
363  for (vtkm::Id superarc = 0; superarc < edgePairArray.GetNumberOfValues(); superarc++)
364  { // per superarc
365  outStream << std::right << std::setw(PRINT_WIDTH)
366  << edgePairArrayConstPortal.Get(superarc).first << " ";
367  outStream << std::right << std::setw(PRINT_WIDTH)
368  << edgePairArrayConstPortal.Get(superarc).second << std::endl;
369  } // per superarc
370 } // PrintEdgePairArray()
371 
372 
373 } // namespace contourtree_augmented
374 } // worklet
375 } // vtkm
376 
377 // tail of include guard
378 #endif
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::cont::ArrayHandle< T, StorageType >
vtkm::worklet::contourtree_augmented::PrintLabel
void PrintLabel(std::string label, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:101
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::worklet::contourtree_augmented::FlagString
std::string FlagString(vtkm::Id flaggedIndex)
Definition: filter/scalar_topology/worklet/contourtree_augmented/Types.h:217
vtkm::worklet::contourtree_augmented::MaskedIndex
VTKM_EXEC_CONT vtkm::Id MaskedIndex(vtkm::Id flaggedIndex)
Definition: filter/scalar_topology/worklet/contourtree_augmented/Types.h:127
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::worklet::contourtree_augmented::PrintDataType
void PrintDataType(T value, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:135
vtkm::worklet::contourtree_augmented::PrintEdgePairArrayColumnLayout
void PrintEdgePairArrayColumnLayout(const EdgePairArray &edgePairArray, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:358
vtkm::worklet::contourtree_augmented::PrintSortedValues
void PrintSortedValues(std::string label, const vtkm::cont::ArrayHandle< T, StorageType > &dVec, IdArrayType &sortVec, vtkm::Id nValues=-1, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:224
Types.h
vtkm::worklet::contourtree_augmented::PrintValues
void PrintValues(std::string label, const vtkm::cont::ArrayHandle< T, StorageType > &dVec, vtkm::Id nValues=-1, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:197
vtkm::worklet::contourtree_augmented::PrintArray
void PrintArray(std::string label, const T &iVec, vtkm::Id nIndices=-1, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:277
vtkm::worklet::contourtree_augmented::PREFIX_WIDTH
constexpr int PREFIX_WIDTH
Definition: augmented/PrintVectors.h:76
Transport.h
vtkm::worklet::contourtree_augmented::PrintLabelledDataBlock
void PrintLabelledDataBlock(std::string label, const vtkm::cont::ArrayHandle< T, StorageType > &dVec, vtkm::Id nColumns, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:297
vtkm::worklet::contourtree_augmented::PrintSeparatingBar
void PrintSeparatingBar(vtkm::Id howMany, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:110
vtkm::worklet::contourtree_augmented::PrintIndexType
void PrintIndexType(vtkm::Id index, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:127
vtkm::cont::ArrayHandle::ReadPortal
VTKM_CONT ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:414
vtkm::worklet::contourtree_augmented::PrintHeader
void PrintHeader(vtkm::Id howMany, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:151
vtkm::worklet::contourtree_augmented::PrintIndices
void PrintIndices(std::string label, const vtkm::cont::ArrayHandle< T > &iVec, vtkm::Id nIndices=-1, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:253
vtkm::worklet::contourtree_augmented::PRINT_WIDTH
constexpr int PRINT_WIDTH
Definition: augmented/PrintVectors.h:75
vtkm::worklet::contourtree_augmented::PrintArrayHandle
void PrintArrayHandle(std::string label, const ARRAYTYPE &dVec, vtkm::Id nValues, std::ostream &outStream)
Definition: augmented/PrintVectors.h:171
vtkm::worklet::contourtree_augmented::PrintEdgePairArray
void PrintEdgePairArray(std::string label, const EdgePairArray &edgePairArray, vtkm::Id nIndices, std::ostream &outStream=std::cout)
Definition: augmented/PrintVectors.h:325