vtkMaterialInterfaceUtilities.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: $RCSfile$
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #ifndef vtkMaterialInterfaceUtilities_h
17 #define vtkMaterialInterfaceUtilities_h
18 
19 // Vtk
20 #include <vtkCommunicator.h>
21 // Vtk containers
22 #include <vtkDataArraySelection.h>
23 #include <vtkDoubleArray.h>
24 #include <vtkFloatArray.h>
25 #include <vtkIntArray.h>
26 #include <vtkUnsignedIntArray.h>
27 // STL
28 #include <fstream>
29 using std::ofstream;
30 using std::ifstream;
31 #include <sstream>
32 using std::ostringstream;
33 #include <vector>
34 using std::vector;
35 #include <string>
36 using std::string;
37 #include <algorithm>
38 // other
39 #include <assert.h>
40 
41 // some useful functionality that stradles multiple filters
42 // and has file scope.
43 namespace
44 {
45 // vector memory management helper
46 template <class T>
47 void ClearVectorOfPointers(vector<T*>& V)
48 {
49  size_t n = V.size();
50  for (size_t i = 0; i < n; ++i)
51  {
52  if (V[i] != 0)
53  {
54  delete V[i];
55  }
56  }
57  V.clear();
58 }
59 // vector memory management helper
60 template <class T>
61 void ClearVectorOfVtkPointers(vector<T*>& V)
62 {
63  size_t n = V.size();
64  for (size_t i = 0; i < n; ++i)
65  {
66  if (V[i] != 0)
67  {
68  V[i]->Delete();
69  }
70  }
71  V.clear();
72 }
73 // vector memory management helper
74 template <class T>
75 void ClearVectorOfArrayPointers(vector<T*>& V)
76 {
77  size_t n = V.size();
78  for (size_t i = 0; i < n; ++i)
79  {
80  if (V[i] != 0)
81  {
82  delete[] V[i];
83  }
84  }
85  V.clear();
86 }
87 // vector memory management helper
88 template <class T>
89 void ResizeVectorOfVtkPointers(vector<T*>& V, int n)
90 {
91  ClearVectorOfVtkPointers(V);
92 
93  V.resize(n);
94  for (int i = 0; i < n; ++i)
95  {
96  V[i] = T::New();
97  }
98 }
99 // vector memory management helper
100 template <class T>
101 void ResizeVectorOfArrayPointers(vector<T*>& V, int nV, int nA)
102 {
103  ClearVectorOfArrayPointers(V);
104 
105  V.resize(nV);
106  for (int i = 0; i < nV; ++i)
107  {
108  V[i] = new T[nA];
109  }
110 }
111 // vector memory management helper
112 template <class T>
113 void ResizeVectorOfVtkArrayPointers(vector<T*>& V, int nComps, vtkIdType nTups, string name, int nv)
114 {
115  ClearVectorOfVtkPointers(V);
116 
117  V.resize(nv);
118  for (int i = 0; i < nv; ++i)
119  {
120  V[i] = T::New();
121  V[i]->SetNumberOfComponents(nComps);
122  V[i]->SetNumberOfTuples(nTups);
123  V[i]->SetName(name.c_str());
124  }
125 }
126 // vector memory management helper
127 template <class T>
128 void ResizeVectorOfVtkArrayPointers(vector<T*>& V, int nComps, int nv)
129 {
130  ResizeVectorOfVtkArrayPointers(V, nComps, 0, "", nv);
131 }
132 // vector memory management helper
133 template <class T>
134 void ResizeVectorOfVtkArrayPointers(vector<T*>& V, int nComps, int nTups, int nv)
135 {
136  ResizeVectorOfVtkArrayPointers(V, nComps, nTups, "", nv);
137 }
138 
139 // vtk object memory management helper
140 template <class T>
141 inline void ReNewVtkPointer(T*& pv)
142 {
143  if (pv != 0)
144  {
145  pv->Delete();
146  }
147  pv = T::New();
148 }
149 // vtk object memory management helper
150 template <class T>
151 inline void NewVtkArrayPointer(T*& pv, int nComps, vtkIdType nTups, std::string name)
152 {
153  pv = T::New();
154  pv->SetNumberOfComponents(nComps);
155  pv->SetNumberOfTuples(nTups);
156  pv->SetName(name.c_str());
157 }
158 // vtk object memory management helper
159 template <class T>
160 inline void ReNewVtkArrayPointer(T*& pv, int nComps, vtkIdType nTups, std::string name)
161 {
162  if (pv != 0)
163  {
164  pv->Delete();
165  }
166  NewVtkArrayPointer(pv, nComps, nTups, name);
167 }
168 // vtk object memory management helper
169 template <class T>
170 inline void ReNewVtkArrayPointer(T*& pv, std::string name)
171 {
172  ReNewVtkArrayPointer(pv, 1, 0, name);
173 }
174 // vtk object memory management helper
175 template <class T>
176 inline void ReleaseVtkPointer(T*& pv)
177 {
178  assert("Attempted to release a 0 pointer." && pv != 0);
179  pv->Delete();
180  pv = 0;
181 }
182 // vtk object memory management helper
183 template <class T>
184 inline void CheckAndReleaseVtkPointer(T*& pv)
185 {
186  if (pv == 0)
187  {
188  return;
189  }
190  pv->Delete();
191  pv = 0;
192 }
193 // memory management helper
194 template <class T>
195 inline void CheckAndReleaseArrayPointer(T*& pv)
196 {
197  if (pv == 0)
198  {
199  return;
200  }
201  delete[] pv;
202  pv = 0;
203 }
204 // memory management helper
205 template <class T>
206 inline void CheckAndReleaseCArrayPointer(T*& pv)
207 {
208  if (pv == 0)
209  {
210  return;
211  }
212  free(pv);
213  pv = 0;
214 }
215 // zero vector
216 template <class T>
217 inline void FillVector(vector<T>& V, const T& v)
218 {
219  size_t n = V.size();
220  for (size_t i = 0; i < n; ++i)
221  {
222  V[i] = v;
223  }
224 }
225 // Copier to copy from an array where type is not known
226 // into a destination buffer.
227 // returns 0 if the type of the source array
228 // is not supported.
229 template <class T>
230 inline int CopyTuple(T* dest, // scalar/vector
231  vtkDataArray* src, //
232  int nComps, //
233  int srcCellIndex) //
234 {
235  // convert cell index to array index
236  int srcIndex = nComps * srcCellIndex;
237  // copy
238  switch (src->GetDataType())
239  {
240  case VTK_FLOAT:
241  {
242  float* thisTuple = dynamic_cast<vtkFloatArray*>(src)->GetPointer(srcIndex);
243  for (int q = 0; q < nComps; ++q)
244  {
245  dest[q] = static_cast<T>(thisTuple[q]);
246  }
247  }
248  break;
249  case VTK_DOUBLE:
250  {
251  double* thisTuple = dynamic_cast<vtkDoubleArray*>(src)->GetPointer(srcIndex);
252  for (int q = 0; q < nComps; ++q)
253  {
254  dest[q] = static_cast<T>(thisTuple[q]);
255  }
256  }
257  break;
258  case VTK_INT:
259  {
260  int* thisTuple = dynamic_cast<vtkIntArray*>(src)->GetPointer(srcIndex);
261  for (int q = 0; q < nComps; ++q)
262  {
263  dest[q] = static_cast<T>(thisTuple[q]);
264  }
265  }
266  break;
267  case VTK_UNSIGNED_INT:
268  {
269  unsigned int* thisTuple = dynamic_cast<vtkUnsignedIntArray*>(src)->GetPointer(srcIndex);
270  for (int q = 0; q < nComps; ++q)
271  {
272  dest[q] = static_cast<T>(thisTuple[q]);
273  }
274  }
275  break;
276  default:
277  assert("This data type is unsupported." && 0);
278  return 0;
279  break;
280  }
281  return 1;
282 }
283 
284 #ifdef vtkMaterialInterfaceFilterDEBUG
285 //
286 int WritePidFile(vtkCommunicator* comm, string pidFileName)
287 {
288  // build an identifier string
289  // "host : rank : pid"
290  int nProcs = comm->GetNumberOfProcesses();
291  int myProcId = comm->GetLocalProcessId();
292  const int hostNameSize = 256;
293  char hostname[hostNameSize] = { '\0' };
294  gethostname(hostname, hostNameSize);
295  int pid = getpid();
296  const int hrpSize = 512;
297  char hrp[hrpSize] = { '\0' };
298  sprintf(hrp, "%s : %d : %d", hostname, myProcId, pid);
299  // move all identifiers to controller
300  char* hrpBuffer = 0;
301  if (myProcId == 0)
302  {
303  hrpBuffer = new char[nProcs * hrpSize];
304  }
305  comm->Gather(hrp, hrpBuffer, hrpSize, 0);
306  // put identifiers into a file
307  if (myProcId == 0)
308  {
309  // open a file in the current working directory
310  ofstream hrpFile;
311  hrpFile.open(pidFileName.c_str());
312  char* thisHrp = hrpBuffer;
313  if (hrpFile.is_open())
314  {
315  for (int procId = 0; procId < nProcs; ++procId)
316  {
317  hrpFile << thisHrp << endl;
318  thisHrp += hrpSize;
319  };
320  hrpFile.close();
321  }
322  // if we can't open a file send to stderr
323  else
324  {
325  for (int procId = 0; procId < nProcs; ++procId)
326  {
327  cerr << thisHrp << endl;
328  thisHrp += hrpSize;
329  }
330  }
331  delete[] hrpBuffer;
332  }
333 
334  return pid;
335 }
336 //
337 string GetMemoryUsage(int pid, int line, int procId)
338 {
339  ostringstream memoryUsage;
340 
341  ostringstream statusFileName;
342  statusFileName << "/proc/" << pid << "/status";
343  ifstream statusFile;
344  statusFile.open(statusFileName.str().c_str());
345  if (statusFile.is_open())
346  {
347  const int cbufSize = 1024;
348  char cbuf[cbufSize] = { '\0' };
349  while (statusFile.good())
350  {
351  statusFile.getline(cbuf, cbufSize);
352  string content(cbuf);
353  if (content.find("VmSize") != string::npos || content.find("VmRSS") != string::npos ||
354  content.find("VmData") != string::npos)
355  {
356  int tabStart = content.find_first_of("\t ");
357  int tabSpan = 1;
358  while (content[tabStart + tabSpan] == '\t' || content[tabStart + tabSpan] == ' ')
359  {
360  ++tabSpan;
361  }
362  string formattedContent =
363  content.substr(0, tabStart - 1) + " " + content.substr(tabStart + tabSpan);
364  memoryUsage << "[" << line << "] " << procId << " " << formattedContent << endl;
365  }
366  }
367  statusFile.close();
368  }
369  else
370  {
371  cerr << "[" << line << "] " << procId << " could not open " << statusFileName << "." << endl;
372  }
373 
374  return memoryUsage.str();
375 }
376 
377 template <class T>
378 void writeTuple(ostream& sout, T* tup, int nComp)
379 {
380  if (nComp == 1)
381  {
382  sout << tup[0];
383  }
384  else
385  {
386  sout << "(" << tup[0];
387  for (int q = 1; q < nComp; ++q)
388  {
389  sout << ", " << tup[q];
390  }
391  sout << ")";
392  }
393 }
394 //
395 ostream& operator<<(ostream& sout, vtkDoubleArray& da)
396 {
397  sout << "Name: " << da.GetName() << endl;
398 
399  int nTup = da.GetNumberOfTuples();
400  int nComp = da.GetNumberOfComponents();
401 
402  sout << "NumberOfComps: " << nComp << endl;
403  sout << "NumberOfTuples:" << nTup << endl;
404  if (nTup == 0)
405  {
406  sout << "{}" << endl;
407  }
408  else
409  {
410  sout << "{";
411  double* thisTup = da.GetTuple(0);
412  writeTuple(sout, thisTup, nComp);
413  for (int i = 1; i < nTup; ++i)
414  {
415  thisTup = da.GetTuple(i);
416  sout << ", ";
417  writeTuple(sout, thisTup, nComp);
418  }
419  sout << "}" << endl;
420  }
421  return sout;
422 }
423 //
424 ostream& operator<<(ostream& sout, vector<vtkDoubleArray*>& vda)
425 {
426  size_t nda = vda.size();
427  for (size_t i = 0; i < nda; ++i)
428  {
429  sout << "[" << i << "]\n" << *vda[i] << endl;
430  }
431  return sout;
432 }
433 //
434 ostream& operator<<(ostream& sout, vector<vector<int> >& vvi)
435 {
436  size_t nv = vvi.size();
437  for (size_t i = 0; i < nv; ++i)
438  {
439  sout << "[" << i << "]{";
440  size_t ni = vvi[i].size();
441  if (ni < 1)
442  {
443  sout << "}" << endl;
444  continue;
445  }
446  sout << vvi[i][0];
447  for (size_t j = 1; j < ni; ++j)
448  {
449  sout << "," << vvi[i][j];
450  }
451  sout << "}" << endl;
452  }
453  return sout;
454 }
455 //
456 ostream& operator<<(ostream& sout, vector<int>& vi)
457 {
458  sout << "{";
459  size_t ni = vi.size();
460  if (ni < 1)
461  {
462  sout << "}";
463  return sout;
464  }
465  sout << vi[0];
466  for (size_t j = 1; j < ni; ++j)
467  {
468  sout << "," << vi[j];
469  }
470  sout << "}";
471 
472  return sout;
473 }
474 // //
475 // ostream &operator<<(ostream &sout, vtkDoubleArray &da)
476 // {
477 // sout << "Name: " << da.GetName() << endl;
478 //
479 // vtkIdType nTup = da.GetNumberOfTuples();
480 // int nComp = da.GetNumberOfComponents();
481 //
482 // sout << "NumberOfComps: " << nComp << endl;
483 // sout << "NumberOfTuples:" << nTup << endl;
484 // sout << "{\n";
485 // for (int i=0; i<nTup; ++i)
486 // {
487 // double *thisTup=da.GetTuple(i);
488 // for (int q=0; q<nComp; ++q)
489 // {
490 // sout << thisTup[q] << ",";
491 // }
492 // sout << (char)0x08 << "\n";
493 // }
494 // sout << "}\n";
495 //
496 // return sout;
497 // }
498 // write a set of loading arrays
499 // ostream &operator<<(ostream &sout,
500 // vector<vector<vtkIdType> > &pla)
501 // {
502 // int nProcs=pla.size();
503 // for (int procId=0; procId<nProcs; ++procId)
504 // {
505 // cerr << "Fragment loading on process " << procId << ":" << endl;
506 // int nLocalFragments=pla[procId].size();
507 // for (int fragmentIdx=0; fragmentIdx<nLocalFragments; ++fragmentIdx)
508 // {
509 // if (pla[procId][fragmentIdx]>0)
510 // {
511 // sout << "("
512 // << fragmentIdx
513 // << ","
514 // << pla[procId][fragmentIdx]
515 // << "), ";
516 // }
517 // }
518 // sout << endl;
519 // }
520 // return sout;
521 // }
522 #endif
523 //
524 template <typename TCnt, typename TLabel>
525 void PrintHistogram(vector<TCnt>& bins, vector<TLabel>& binIds)
526 {
527  const int maxWidth = 40;
528  const size_t n = bins.size();
529  if (n == 0)
530  {
531  return;
532  }
533  int maxBin = *max_element(bins.begin(), bins.end());
534  for (size_t i = 0; i < n; ++i)
535  {
536  if (bins[i] == 0)
537  {
538  continue;
539  }
540  // clip at width of 40.
541  int wid = maxBin < maxWidth ? bins[i] : bins[i] * maxWidth / maxBin;
542  cerr << "{" << setw(12) << std::left << binIds[i] << "}*";
543  for (int j = 1; j < wid; ++j)
544  {
545  cerr << "*";
546  }
547  cerr << "(" << bins[i] << ")" << endl;
548  }
549  return;
550 }
551 //
552 template <typename TCnt>
553 void PrintHistogram(vector<TCnt>& bins)
554 {
555  // generate default labels, 0...n
556  const int n = static_cast<int>(bins.size());
557  vector<int> binIds(n);
558  for (int i = 0; i < n; ++i)
559  {
560  binIds[i] = i;
561  }
562  //
563  PrintHistogram(bins, binIds);
564  return;
565 }
566 };
567 #endif
568 // VTK-HeaderTest-Exclude: vtkMaterialInterfaceUtilities.h
int Gather(const int *sendBuffer, int *recvBuffer, vtkIdType length, int destProcessId)
content
#define VTK_UNSIGNED_INT
vtkIdType GetNumberOfTuples()
virtual int GetNumberOfProcesses()
virtual double * GetTuple(vtkIdType tupleIdx)=0
virtual int GetDataType()=0
int vtkIdType
int GetNumberOfComponents()
#define VTK_DOUBLE
#define VTK_FLOAT
virtual int GetLocalProcessId()
virtual char * GetName()
VTKWRAPPINGJAVA_EXPORT jlong q(JNIEnv *env, jobject obj)
QDebug & operator<<(QDebug debug, const std::string &stdstring)
Definition: pqDebug.h:65
#define VTK_INT