VTK-m  2.0
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) 2016, Los Alamos National Security, LLC
11 // All rights reserved.
12 //
13 // Copyright 2016. Los Alamos National Security, LLC.
14 // This software was produced under U.S. Government contract DE-AC52-06NA25396
15 // for Los Alamos National Laboratory (LANL), which is operated by
16 // Los Alamos National Security, LLC for the U.S. Department of Energy.
17 // The U.S. Government has rights to use, reproduce, and distribute this
18 // software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC
19 // MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE
20 // USE OF THIS SOFTWARE. If software is modified to produce derivative works,
21 // such modified software should be clearly marked, so as not to confuse it
22 // with the version available from LANL.
23 //
24 // Additionally, redistribution and use in source and binary forms, with or
25 // without modification, are permitted provided that the following conditions
26 // are met:
27 //
28 // 1. Redistributions of source code must retain the above copyright notice,
29 // this list of conditions and the following disclaimer.
30 // 2. Redistributions in binary form must reproduce the above copyright notice,
31 // this list of conditions and the following disclaimer in the documentation
32 // and/or other materials provided with the distribution.
33 // 3. Neither the name of Los Alamos National Security, LLC, Los Alamos
34 // National Laboratory, LANL, the U.S. Government, nor the names of its
35 // contributors may be used to endorse or promote products derived from
36 // this software without specific prior written permission.
37 //
38 // THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND
39 // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
40 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS
42 // NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 //============================================================================
50 
51 // This code is based on the algorithm presented in the paper:
52 // “Parallel Peak Pruning for Scalable SMP Contour Tree Computation.”
53 // Hamish Carr, Gunther Weber, Christopher Sewell, and James Ahrens.
54 // Proceedings of the IEEE Symposium on Large Data Analysis and Visualization
55 // (LDAV), October 2016, Baltimore, Maryland.
56 
57 #ifndef vtkm_worklet_contourtree_print_vector_h
58 #define vtkm_worklet_contourtree_print_vector_h
59 
60 #include <fstream>
61 #include <iomanip>
62 #include <iostream>
63 #include <string>
64 
65 #include <vtkm/Types.h>
66 #include <vtkm/cont/ArrayHandle.h>
67 
68 namespace vtkm
69 {
70 namespace worklet
71 {
72 namespace contourtree
73 {
74 
75 // debug value for number of columns to print
76 #define PRINT_COLS 10
77 #define PRINT_WIDTH 12
78 #define PREFIX_WIDTH 20
79 
80 // utility routine to convert number to a string
81 std::string NumString(vtkm::Id number);
82 
83 // base routines for printing label & prefix bars
84 void PrintLabel(std::string label);
85 void PrintSeparatingBar(vtkm::Id howMany);
86 
87 // routines to print out a single value
88 template <typename T>
89 void PrintDataType(T value);
90 void PrintIndexType(vtkm::Id value);
91 
92 // header line
93 void PrintHeader(vtkm::Id howMany);
94 
95 // base routines for reading & writing host vectors
96 template <typename T, typename StorageType>
97 void PrintValues(std::string label,
99  vtkm::Id nValues = -1);
100 void PrintIndices(std::string label,
102  vtkm::Id nIndices = -1);
103 
104 // routines for printing indices & data in blocks
105 template <typename T, typename StorageType>
106 void PrintLabelledBlock(std::string label,
108  vtkm::Id nRows,
109  vtkm::Id nColumns);
110 
111 // utility routine to convert number to a string
112 inline std::string NumString(vtkm::Id number)
113 { // NumString()
114  char strBuf[20];
115  snprintf(strBuf, 19, "%1d", static_cast<int>(number));
116  return std::string(strBuf);
117 } // NumString()
118 
119 // base routines for printing label & prefix bars
120 inline void PrintLabel(std::string label)
121 { // PrintLabel()
122  // print out the front end
123  std::cout << std::setw(PREFIX_WIDTH) << std::left << label;
124  // print out the vertical line
125  std::cout << std::right << "|";
126 } // PrintLabel()
127 
128 inline void PrintSeparatingBar(vtkm::Id howMany)
129 { // PrintSeparatingBar()
130  // print out the front end
131  std::cout << std::setw(PREFIX_WIDTH) << std::setfill('-') << "";
132  // now the + at the vertical line
133  std::cout << "+";
134  // now print out the tail end - fixed number of spaces per entry
135  for (vtkm::Id block = 0; block < howMany; block++)
136  std::cout << std::setw(PRINT_WIDTH) << std::setfill('-') << "";
137  // now the endl, resetting the fill character
138  std::cout << std::setfill(' ') << std::endl;
139 } // PrintSeparatingBar()
140 
141 // routine to print out a single value
142 template <typename T>
143 void PrintDataType(T value)
144 { // PrintDataType
145  std::cout << std::setw(PRINT_WIDTH) << value;
146 } // PrintDataType
147 
148 // routine to print out a single value
149 inline void PrintIndexType(vtkm::Id value)
150 { // PrintIndexType
151  std::cout << std::setw(PRINT_WIDTH) << value;
152 } // PrintIndexType
153 
154 // header line
155 inline void PrintHeader(vtkm::Id howMany)
156 { // PrintHeader()
157  if (howMany > PRINT_COLS)
158  howMany = PRINT_COLS;
159  // print out a separating bar
160  PrintSeparatingBar(howMany);
161  // print out a label
162  PrintLabel("ID");
163  // print out the ID numbers
164  for (vtkm::Id entry = 0; entry < howMany; entry++)
165  PrintIndexType(entry);
166  // and an endl
167  std::cout << std::endl;
168  // print out another separating bar
169  PrintSeparatingBar(howMany);
170 } // PrintHeader()
171 
172 // base routines for reading & writing host vectors
173 template <typename T, typename StorageType>
174 void PrintValues(std::string label, vtkm::cont::ArrayHandle<T, StorageType>& dVec, vtkm::Id nValues)
175 {
176  // -1 means full size
177  if (nValues == -1)
178  {
179  nValues = dVec.GetNumberOfValues();
180  }
181 
182  if (nValues > PRINT_COLS)
183  {
184  nValues = PRINT_COLS;
185  }
186 
187  // print the label
188  PrintLabel(label);
189 
190  // now print the data
191  const auto dVecPortal = dVec.ReadPortal();
192  for (vtkm::Id entry = 0; entry < nValues; entry++)
193  {
194  PrintDataType(dVecPortal.Get(entry));
195  }
196 
197  // and an endl
198  std::cout << std::endl;
199 } // PrintValues()
200 
201 // base routines for reading & writing host vectors
202 inline void PrintIndices(std::string label,
204  vtkm::Id nIndices)
205 {
206  // -1 means full size
207  if (nIndices == -1)
208  {
209  nIndices = iVec.GetNumberOfValues();
210  }
211 
212  if (nIndices > PRINT_COLS)
213  {
214  nIndices = PRINT_COLS;
215  }
216 
217  // print the label
218  PrintLabel(label);
219 
220  // now print the data
221  const auto iVecPortal = iVec.ReadPortal();
222  for (vtkm::Id entry = 0; entry < nIndices; entry++)
223  {
224  PrintIndexType(iVecPortal.Get(entry));
225  }
226 
227  // and an endl
228  std::cout << std::endl;
229 } // PrintIndices()
230 
231 template <typename T, typename StorageType>
232 void PrintLabelledBlock(std::string label,
234  vtkm::Id nRows,
235  vtkm::Id nColumns)
236 {
237  if (nRows > PRINT_COLS)
238  {
239  nRows = PRINT_COLS;
240  }
241 
242  if (nColumns > PRINT_COLS)
243  {
244  nColumns = PRINT_COLS;
245  }
246 
247  // start with a header
248  PrintHeader(nColumns);
249  // loop control variable
250  vtkm::Id entry = 0;
251  // per row
252  const auto dVecPortal = dVec.ReadPortal();
253  for (vtkm::Id row = 0; row < nRows; row++)
254  { // per row
255  PrintLabel(label + "[" + NumString(row) + "]");
256  // now print the data
257  for (vtkm::Id col = 0; col < nColumns; col++, entry++)
258  {
259  PrintDataType(dVecPortal.Get(entry));
260  }
261  std::cout << std::endl;
262  } // per row
263  std::cout << std::endl;
264 } // PrintLabelledBlock()
265 }
266 }
267 }
268 
269 #endif
vtkm::worklet::contourtree::PrintHeader
void PrintHeader(vtkm::Id howMany)
Definition: PrintVectors.h:155
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::worklet::contourtree::PrintSeparatingBar
void PrintSeparatingBar(vtkm::Id howMany)
Definition: PrintVectors.h:128
vtkm::worklet::contourtree::PrintIndexType
void PrintIndexType(vtkm::Id value)
Definition: PrintVectors.h:149
vtkm::cont::ArrayHandle< T, StorageType >
ArrayHandle.h
vtkm::worklet::contourtree::NumString
std::string NumString(vtkm::Id number)
Definition: PrintVectors.h:112
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
PREFIX_WIDTH
#define PREFIX_WIDTH
Definition: PrintVectors.h:78
PRINT_WIDTH
#define PRINT_WIDTH
Definition: PrintVectors.h:77
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
PRINT_COLS
#define PRINT_COLS
Definition: PrintVectors.h:76
vtkm::worklet::contourtree::PrintValues
void PrintValues(std::string label, vtkm::cont::ArrayHandle< T, StorageType > &dVec, vtkm::Id nValues=-1)
Definition: PrintVectors.h:174
vtkm::worklet::contourtree::PrintLabelledBlock
void PrintLabelledBlock(std::string label, const vtkm::cont::ArrayHandle< T, StorageType > &dVec, vtkm::Id nRows, vtkm::Id nColumns)
Definition: PrintVectors.h:232
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::PrintDataType
void PrintDataType(T value)
Definition: PrintVectors.h:143
vtkm::worklet::contourtree::PrintIndices
void PrintIndices(std::string label, vtkm::cont::ArrayHandle< vtkm::Id > &iVec, vtkm::Id nIndices=-1)
Definition: PrintVectors.h:202
vtkm::worklet::contourtree::PrintLabel
void PrintLabel(std::string label)
Definition: PrintVectors.h:120