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:
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 
200  const int gridIdx, const int nei, const vtkStructuredNeighbor& Neighbor);
201 
207  virtual void TransferGhostDataFromNeighbors(const int gridID) override;
208 
213 
219 
224  void DeserializeBufferSizesForProcess(int* buffersizes, vtkIdType N, const int processId);
225 
230  void SerializeBufferSizes(int*& sizesbuf, vtkIdType& N);
231 
237 
244 
249  void PostReceives();
250 
255  void PostSends();
256 
263 
270 
275 
280  void SerializeGhostPoints(const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream);
281 
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 
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 
330  const int gridIdx, const int nei, int ext[6], vtkMultiProcessStream& bytestream);
331 
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 
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 */
std::vector< vtkCellData * > GridCellData
std::vector< vtkPointData * > GridPointData
std::vector< vtkPoints * > GridPoints
represent and manipulate cell attribute data
Definition: vtkCellData.h:33
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:50
represent and manipulate fields of data
Definition: vtkFieldData.h:54
a simple class to control print indentation
Definition: vtkIndent.h:34
Process communication using MPI.
Multiprocessing communication superclass.
stream used to pass data across processes using vtkMultiProcessController.
vtkPStructuredGridConnectivity inherits from vtkStructuredGridConnectivity and implements functionali...
int GetNumberOfLocalGrids()
Returns the number of local grids registers by the process that owns the current vtkPStructuredGridCo...
~vtkPStructuredGridConnectivity() override
std::vector< std::vector< unsigned int > > RcvBufferSizes
virtual void CreateGhostLayers(const int N=1) override
Creates ghost layers on the grids owned by this process using data from both local and remote block n...
bool GridExtentsAreEqual(int rhs[6], int lhs[6])
Returns true if the two extents are equal, otherwise false.
void DeserializeGhostPoints(const int gridIdx, const int nei, int ext[6], vtkMultiProcessStream &bytestream)
Helper method to de-serialize the ghost points received from a remote process.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void SerializeDataArray(vtkDataArray *dataArray, vtkMultiProcessStream &bytestream)
Serializes the data array into a bytestream.
virtual void TransferGhostDataFromNeighbors(const int gridID) override
This method transfers the fields (point data and cell data) to the ghost extents from the neighboring...
void DeserializeFieldData(int ext[6], vtkFieldData *fieldData, vtkMultiProcessStream &bytestream)
Helper method to de-serialize field data.
void DeserializeGhostPointData(const int gridIdx, const int nei, int ext[6], vtkMultiProcessStream &bytestream)
Helper method to de-serialize the ghost point data received from a remote process.
void ClearRawBuffers()
Clears all raw send/rcv buffers.
bool IsGridLocal(const int gridID)
Returns true iff the grid corresponding to the given gridID is local.
void DeserializeGridExtentForProcess(int *rcvbuffer, vtkIdType &N, const int processId)
Deserializes the received grid extent information to the GridExtents internal data-structures.
void ExchangeGhostDataPost()
Helper method for exchanging ghost data.
void DeserializeGhostData(const int gridID, const int neiListID, const int neiGridIdx, int rcvext[6], unsigned char *buffer, unsigned int size)
Given the raw buffer consisting of ghost data, this method deserializes the object and returns the gr...
int GetGridRank(const int gridID)
Returns the rank of the given gridID.
std::vector< std::vector< unsigned char * > > SendBuffers
std::vector< std::vector< vtkPoints * > > RemotePoints
vtkMPICommunicator::Request * MPIRequests
void ExchangeGhostData()
Exchanges ghost data of the grids owned by this process.
static vtkPStructuredGridConnectivity * New()
bool IsGridRemote(const int gridID)
Returns true iff the grid is remote, otherwise false.
void RegisterGrid(const int gridID, int extents[6], vtkUnsignedCharArray *nodesGhostArray, vtkUnsignedCharArray *cellGhostArray, vtkPointData *pointData, vtkCellData *cellData, vtkPoints *gridNodes) override
See vtkStructuredGridConnectivity::RegisterGrid.
void PostSends()
Helper method to communicate ghost data.
void CommunicateGhostData()
Helper method for exchanging ghost data.
void SerializeGhostPointData(const int gridIdx, int ext[6], vtkMultiProcessStream &bytestream)
Helper method to serialize ghost point data.
void SetNumberOfGrids(const unsigned int N) override
Sets the total number of domains distributed among processors.
void ExchangeGridExtents()
Exchanges the grid extents among all processes and fully populates the GridExtents vector.
void ClearRemoteData()
Clears all internal VTK data-structures that are used to store the remote ghost data.
void PackGhostData()
Helper method to pack all the ghost data into send buffers.
void ExchangeBufferSizes()
Helper method to exchange buffer sizes.Each process sends the send buffer size of each grid to each o...
void DeserializeGhostCellData(const int gridIdx, const int nei, int ext[6], vtkMultiProcessStream &bytestream)
Helper method to de-serialize the ghost cell data received from a remote process.
void DeserializeBufferSizesForProcess(int *buffersizes, vtkIdType N, const int processId)
Helper method to deserialize the buffer sizes coming from the given process.
bool HasCellData(const int gridIdx)
Returns true iff the grid corresponding to the given ID has cell data.
void RegisterRemoteGrid(const int gridID, int extents[6], int process)
Registers a remote grid with the given grid Id, structured extents and process.
void UnpackGhostData()
Helper method to unpack the raw ghost data from the receive buffers in to the VTK remote point data-s...
void SerializeGridExtents(int *&sndbuffer, vtkIdType &N)
Serializes the grid extents and information in a buffer to send over MPI The data is serialized as fo...
void DeserializeDataArray(vtkDataArray *&dataArray, const int dataType, const int numberOfTuples, const int numberOfComponents, vtkMultiProcessStream &bytestream)
Helper method to deserialize the data array from a bytestream.
void SerializeBufferSizes(int *&sizesbuf, vtkIdType &N)
Helper method to serialize the buffer sizes for the grids of this process to neighboring grids.
std::vector< std::vector< unsigned int > > SendBufferSizes
void ExchangeGhostDataInit()
Helper method for exchanging ghost data.
void SerializeGhostData(const int sndGridID, const int rcvGrid, int sndext[6], unsigned char *&buffer, unsigned int &size)
Given a grid ID and the corresponding send extent, this method serializes the grid and data within th...
void TransferRemoteNeighborData(const int gridIdx, const int nei, const vtkStructuredNeighbor &Neighbor)
This method transfers all the remote neighbor data to the ghosted grid instance of the grid correspon...
std::vector< std::vector< vtkCellData * > > RemoteCellData
void SerializeFieldData(int sourceExtent[6], int targetExtent[6], vtkFieldData *fieldData, vtkMultiProcessStream &bytestream)
Helper method to serialize field data.
void ComputeNeighbors() override
Computes the neighboring topology of a distributed structured grid data-set.
void SerializeGhostPoints(const int gridIdx, int ext[6], vtkMultiProcessStream &bytestream)
Helper method to serialize the ghost points to send to a remote process.
void PostReceives()
Helper method to communicate ghost data.
void Initialize()
Initializes this instance of vtkPStructuredGridConnectivity, essentially, the acquires the local proc...
bool HasPointData(const int gridIdx)
Returns true iff the grid corresponding to the given ID has point data.
void SerializeGhostCellData(const int gridIdx, int ext[6], vtkMultiProcessStream &bytestream)
Helper method to serialize ghost cell data.
void InitializeMessageCounters()
Sets all message counters to 0.
std::vector< std::vector< vtkPointData * > > RemotePointData
bool HasPoints(const int gridIdx)
Returns true iff the grid corresponding to the given ID has points.
std::vector< std::vector< unsigned char * > > RcvBuffers
represent and manipulate point attribute data
Definition: vtkPointData.h:32
represent and manipulate 3D points
Definition: vtkPoints.h:34
vtkStructuredGridConnectivity is a concrete instance of vtkObject that implements functionality for c...
An internal, light-weight class used to store neighbor information.
dynamic, self-adjusting array of unsigned char
@ size
Definition: vtkX3D.h:259
int vtkIdType
Definition: vtkType.h:338