VTK  9.0.1
vtkPStructuredGridConnectivity.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkPStructuredGridConnectivity.h
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  =========================================================================*/
31 #ifndef vtkPStructuredGridConnectivity_h
32 #define vtkPStructuredGridConnectivity_h
33 
34 // VTK include directives
35 #include "vtkFiltersParallelGeometryModule.h" // For export macro
36 #include "vtkMPICommunicator.h" // Needed for vtkMPICommunicator::Request
38 
39 // C++ include directives
40 #include <vector> // For STL vector
41 
42 // Forward declarations
44 class vtkMPIController;
46 // class vtkMPICommunicator::Request;
47 
48 class VTKFILTERSPARALLELGEOMETRY_EXPORT vtkPStructuredGridConnectivity
50 {
51 public:
54  void PrintSelf(ostream& os, vtkIndent indent) override;
55 
57 
60  vtkSetMacro(Controller, vtkMultiProcessController*);
61  vtkGetMacro(Controller, vtkMultiProcessController*);
63 
67  void SetNumberOfGrids(const unsigned int N) override;
68 
72  void RegisterGrid(const int gridID, int extents[6], vtkUnsignedCharArray* nodesGhostArray,
73  vtkUnsignedCharArray* cellGhostArray, vtkPointData* pointData, vtkCellData* cellData,
74  vtkPoints* gridNodes) override;
75 
80  int GetNumberOfLocalGrids() { return static_cast<int>(this->GridIds.size()); }
81 
88  int GetGridRank(const int gridID);
89 
93  bool IsGridRemote(const int gridID);
94 
98  bool IsGridLocal(const int gridID);
99 
105  void Initialize();
106 
112  void ComputeNeighbors() override;
113 
118  virtual void CreateGhostLayers(const int N = 1) override;
119 
120 protected:
122  ~vtkPStructuredGridConnectivity() override;
123 
125  int Rank;
127 
128  std::vector<int> GridRanks; // Corresponding rank for each grid
129  std::vector<int> GridIds; // List of GridIds, owned by this process
130 
131  // Data structures to store the remote ghost data of each grid for each one
132  // of its neighbors. The first index is the global grid index. The second
133  // is the neighbor index.
134  std::vector<std::vector<vtkPoints*> > RemotePoints;
135  std::vector<std::vector<vtkPointData*> > RemotePointData;
136  std::vector<std::vector<vtkCellData*> > RemoteCellData;
137 
138  // Data structures to store the send/receive buffer sizes and corresponding
139  // persistent buffers. The first index is the global grid index. The second
140  // index is the neighbor index for the given grid.
141  std::vector<std::vector<unsigned int> > SendBufferSizes;
142  std::vector<std::vector<unsigned int> > RcvBufferSizes;
143  std::vector<std::vector<unsigned char*> > SendBuffers;
144  std::vector<std::vector<unsigned char*> > RcvBuffers;
145 
149 
150  // Array of MPI requests
152 
156  bool GridExtentsAreEqual(int rhs[6], int lhs[6]);
157 
161  bool HasPointData(const int gridIdx);
162 
166  bool HasCellData(const int gridIdx);
167 
171  bool HasPoints(const int gridIdx);
172 
176  void InitializeMessageCounters();
177 
182  void ClearRemoteData();
183 
187  void ClearRawBuffers();
188 
193  void RegisterRemoteGrid(const int gridID, int extents[6], int process);
194 
199  void TransferRemoteNeighborData(
200  const int gridIdx, const int nei, const vtkStructuredNeighbor& Neighbor);
201 
207  virtual void TransferGhostDataFromNeighbors(const int gridID) override;
208 
212  void PackGhostData();
213 
218  void UnpackGhostData();
219 
224  void DeserializeBufferSizesForProcess(int* buffersizes, vtkIdType N, const int processId);
225 
230  void SerializeBufferSizes(int*& sizesbuf, vtkIdType& N);
231 
236  void ExchangeBufferSizes();
237 
243  void ExchangeGhostDataInit();
244 
249  void PostReceives();
250 
255  void PostSends();
256 
262  void CommunicateGhostData();
263 
269  void ExchangeGhostDataPost();
270 
274  void ExchangeGhostData();
275 
280  void SerializeGhostPoints(const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream);
281 
285  void SerializeDataArray(vtkDataArray* dataArray, vtkMultiProcessStream& bytestream);
286 
291  void SerializeFieldData(int sourceExtent[6], int targetExtent[6], vtkFieldData* fieldData,
292  vtkMultiProcessStream& bytestream);
293 
298  void SerializeGhostPointData(const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream);
299 
304  void SerializeGhostCellData(const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream);
305 
310  void DeserializeGhostPoints(
311  const int gridIdx, const int nei, int ext[6], vtkMultiProcessStream& bytestream);
312 
316  void DeserializeDataArray(vtkDataArray*& dataArray, const int dataType, const int numberOfTuples,
317  const int numberOfComponents, vtkMultiProcessStream& bytestream);
318 
323  void DeserializeFieldData(int ext[6], vtkFieldData* fieldData, vtkMultiProcessStream& bytestream);
324 
329  void DeserializeGhostPointData(
330  const int gridIdx, const int nei, int ext[6], vtkMultiProcessStream& bytestream);
331 
336  void DeserializeGhostCellData(
337  const int gridIdx, const int nei, int ext[6], vtkMultiProcessStream& bytestream);
338 
345  void SerializeGhostData(const int sndGridID, const int rcvGrid, int sndext[6],
346  unsigned char*& buffer, unsigned int& size);
347 
352  void DeserializeGhostData(const int gridID, const int neiListID, const int neiGridIdx,
353  int rcvext[6], unsigned char* buffer, unsigned int size);
354 
359  void ExchangeGridExtents();
360 
365  void SerializeGridExtents(int*& sndbuffer, vtkIdType& N);
366 
371  void DeserializeGridExtentForProcess(int* rcvbuffer, vtkIdType& N, const int processId);
372 
373 private:
375  void operator=(const vtkPStructuredGridConnectivity&) = delete;
376 };
377 
378 //=============================================================================
379 // INLINE METHODS
380 //=============================================================================
381 
382 inline bool vtkPStructuredGridConnectivity::GridExtentsAreEqual(int rhs[6], int lhs[6])
383 {
384  for (int i = 0; i < 6; ++i)
385  {
386  if (rhs[i] != lhs[i])
387  {
388  return false;
389  }
390  }
391  return true;
392 }
393 
394 //------------------------------------------------------------------------------
395 inline bool vtkPStructuredGridConnectivity::HasPointData(const int gridIdx)
396 {
397  // Sanity check
398  assert("pre: grid index is out-of-bounds!" && (gridIdx >= 0) &&
399  (gridIdx < static_cast<int>(this->NumberOfGrids)));
400 
401  if ((this->GridPointData[gridIdx] != nullptr) &&
402  (this->GridPointData[gridIdx]->GetNumberOfArrays() > 0))
403  {
404  return true;
405  }
406  return false;
407 }
408 
409 //------------------------------------------------------------------------------
410 inline bool vtkPStructuredGridConnectivity::HasCellData(const int gridIdx)
411 {
412  // Sanity check
413  assert("pre: grid index is out-of-bounds!" && (gridIdx >= 0) &&
414  (gridIdx < static_cast<int>(this->NumberOfGrids)));
415 
416  if ((this->GridCellData[gridIdx] != nullptr) &&
417  (this->GridCellData[gridIdx]->GetNumberOfArrays() > 0))
418  {
419  return true;
420  }
421  return false;
422 }
423 
424 //------------------------------------------------------------------------------
425 inline bool vtkPStructuredGridConnectivity::HasPoints(const int gridIdx)
426 {
427  // Sanity check
428  assert("pre: grid index is out-of-bounds!" && (gridIdx >= 0) &&
429  (gridIdx < static_cast<int>(this->NumberOfGrids)));
430 
431  if (this->GridPoints[gridIdx] != nullptr)
432  {
433  return true;
434  }
435  return false;
436 }
437 
438 //------------------------------------------------------------------------------
440 {
441  this->TotalNumberOfMsgs = this->TotalNumberOfRcvs = this->TotalNumberOfSends = 0;
442 }
443 
444 //------------------------------------------------------------------------------
446 {
447  this->SendBufferSizes.clear();
448  this->RcvBufferSizes.clear();
449 
450  // STEP 0: Clear send buffers
451  for (unsigned int i = 0; i < this->SendBuffers.size(); ++i)
452  {
453  for (unsigned int j = 0; j < this->SendBuffers[i].size(); ++j)
454  {
455  delete[] this->SendBuffers[i][j];
456  } // END for all neighbors
457  this->SendBuffers[i].clear();
458  } // END for all grids
459  this->SendBuffers.clear();
460 
461  // STEP 1: Clear rcv buffers
462  for (unsigned int i = 0; i < this->RcvBuffers.size(); ++i)
463  {
464  for (unsigned int j = 0; j < this->RcvBuffers[i].size(); ++j)
465  {
466  delete[] this->RcvBuffers[i][j];
467  } // END for all neighbors
468  this->RcvBuffers[i].clear();
469  } // END for all grids
470  this->RcvBuffers.clear();
471 }
472 
473 //------------------------------------------------------------------------------
475 {
476  // STEP 0: Clear remote points
477  for (unsigned int i = 0; i < this->RemotePoints.size(); ++i)
478  {
479  for (unsigned int j = 0; j < this->RemotePoints[i].size(); ++j)
480  {
481  if (this->RemotePoints[i][j] != nullptr)
482  {
483  this->RemotePoints[i][j]->Delete();
484  }
485  } // END for all j
486  this->RemotePoints[i].clear();
487  } // END for all i
488  this->RemotePoints.clear();
489 
490  // STEP 1: Clear remote point data
491  for (unsigned int i = 0; i < this->RemotePointData.size(); ++i)
492  {
493  for (unsigned int j = 0; j < this->RemotePointData[i].size(); ++j)
494  {
495  if (this->RemotePointData[i][j] != nullptr)
496  {
497  this->RemotePointData[i][j]->Delete();
498  }
499  } // END for all j
500  this->RemotePointData[i].clear();
501  } // END for all i
502  this->RemotePointData.clear();
503 
504  // STEP 2: Clear remote cell data
505  for (unsigned int i = 0; i < this->RemoteCellData.size(); ++i)
506  {
507  for (unsigned int j = 0; j < this->RemoteCellData[i].size(); ++j)
508  {
509  if (this->RemoteCellData[i][j] != nullptr)
510  {
511  this->RemoteCellData[i][j]->Delete();
512  }
513  } // END for all j
514  this->RemoteCellData[i].clear();
515  }
516  this->RemoteCellData.clear();
517 }
518 
519 //------------------------------------------------------------------------------
520 inline bool vtkPStructuredGridConnectivity::IsGridRemote(const int gridID)
521 {
522  return (!this->IsGridLocal(gridID));
523 }
524 
525 //------------------------------------------------------------------------------
526 inline bool vtkPStructuredGridConnectivity::IsGridLocal(const int gridID)
527 {
528  assert("pre: Instance has not been initialized!" && this->Initialized);
529  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
530  (gridID < static_cast<int>(this->NumberOfGrids)));
531  assert(
532  "pre: GridRanks is not properly allocated" && this->NumberOfGrids == this->GridRanks.size());
533  return ((this->GridRanks[gridID] == this->Rank));
534 }
535 
536 //------------------------------------------------------------------------------
538 {
539  assert("pre: Instance has not been initialized!" && this->Initialized);
540  assert("pre: gridID out-of-bounds!" &&
541  (gridID >= 0 && gridID < static_cast<int>(this->NumberOfGrids)));
542  return (this->GridRanks[gridID]);
543 }
544 #endif /* vtkPStructuredGridConnectivity_h */
vtkPStructuredGridConnectivity inherits from vtkStructuredGridConnectivity and implements functionali...
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
represent and manipulate point attribute data
Definition: vtkPointData.h:31
stream used to pass data across processes using vtkMultiProcessController.
std::vector< vtkPointData * > GridPointData
int GetNumberOfLocalGrids()
Returns the number of local grids registers by the process that owns the current vtkPStructuredGridCo...
represent and manipulate cell attribute data
Definition: vtkCellData.h:32
bool HasCellData(const int gridIdx)
Returns true iff the grid corresponding to the given ID has cell data.
bool HasPointData(const int gridIdx)
Returns true iff the grid corresponding to the given ID has point data.
bool HasPoints(const int gridIdx)
Returns true iff the grid corresponding to the given ID has points.
virtual void RegisterGrid(const int gridID, int extents[6], vtkUnsignedCharArray *nodesGhostArray, vtkUnsignedCharArray *cellGhostArray, vtkPointData *pointData, vtkCellData *cellData, vtkPoints *gridNodes)
Registers the current grid corresponding to the grid ID by its global extent w.r.t.
int vtkIdType
Definition: vtkType.h:338
vtkMPICommunicator::Request * MPIRequests
void ClearRemoteData()
Clears all internal VTK data-structures that are used to store the remote ghost data.
void ComputeNeighbors() override
Computes neighboring information.
bool GridExtentsAreEqual(int rhs[6], int lhs[6])
Returns true if the two extents are equal, otherwise false.
void SetNumberOfGrids(const unsigned int N) override
Set/Get the total number of domains distributed among processors.
std::vector< std::vector< unsigned int > > SendBufferSizes
vtkStructuredGridConnectivity is a concrete instance of vtkObject that implements functionality for c...
std::vector< std::vector< unsigned char * > > RcvBuffers
a simple class to control print indentation
Definition: vtkIndent.h:33
int GetGridRank(const int gridID)
Returns the rank of the given gridID.
static vtkStructuredGridConnectivity * New()
std::vector< std::vector< vtkCellData * > > RemoteCellData
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:49
bool IsGridRemote(const int gridID)
Returns true iff the grid is remote, otherwise false.
Process communication using MPI.
void ClearRawBuffers()
Clears all raw send/rcv buffers.
void CreateGhostLayers(const int N=1) override
Creates ghost layers.
std::vector< vtkCellData * > GridCellData
dynamic, self-adjusting array of unsigned char
An internal, light-weight class used to store neighbor information.
std::vector< std::vector< vtkPoints * > > RemotePoints
virtual void TransferGhostDataFromNeighbors(const int gridID)
This method transfers the fields (point data and cell data) to the ghost extents from the neighboring...
void InitializeMessageCounters()
Sets all message counters to 0.
std::vector< std::vector< unsigned int > > RcvBufferSizes
std::vector< std::vector< unsigned char * > > SendBuffers
std::vector< std::vector< vtkPointData * > > RemotePointData
bool IsGridLocal(const int gridID)
Returns true iff the grid corresponding to the given gridID is local.
represent and manipulate 3D points
Definition: vtkPoints.h:33
std::vector< vtkPoints * > GridPoints
represent and manipulate fields of data
Definition: vtkFieldData.h:53
Multiprocessing communication superclass.