VTK
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  =========================================================================*/
34 #ifndef vtkPStructuredGridConnectivity_h
35 #define vtkPStructuredGridConnectivity_h
36 
37 // VTK include directives
38 #include "vtkFiltersParallelGeometryModule.h" // For export macro
40 #include "vtkMPICommunicator.h" // Needed for vtkMPICommunicator::Request
41 
42 // C++ include directives
43 #include <vector> // For STL vector
44 
45 // Forward declarations
47 class vtkMPIController;
49 //class vtkMPICommunicator::Request;
50 
51 class VTKFILTERSPARALLELGEOMETRY_EXPORT vtkPStructuredGridConnectivity :
53 {
54 public:
57  void PrintSelf(ostream& os, vtkIndent indent ) override;
58 
60 
63  vtkSetMacro( Controller,vtkMultiProcessController* );
64  vtkGetMacro( Controller,vtkMultiProcessController* );
66 
70  void SetNumberOfGrids( const unsigned int N ) override;
71 
75  void RegisterGrid( const int gridID, int extents[6],
76  vtkUnsignedCharArray* nodesGhostArray,
77  vtkUnsignedCharArray* cellGhostArray,
78  vtkPointData* pointData,
79  vtkCellData* cellData,
80  vtkPoints* gridNodes ) override;
81 
87  { return static_cast<int>(this->GridIds.size()); };
88 
95  int GetGridRank( const int gridID );
96 
100  bool IsGridRemote( const int gridID );
101 
105  bool IsGridLocal( const int gridID );
106 
112  void Initialize();
113 
119  void ComputeNeighbors() override;
120 
125  virtual void CreateGhostLayers( const int N=1 ) override;
126 
127 protected:
129  ~vtkPStructuredGridConnectivity() override;
130 
132  int Rank;
134 
135  std::vector< int > GridRanks; // Corresponding rank for each grid
136  std::vector< int > GridIds; // List of GridIds, owned by this process
137 
138  // Data structures to store the remote ghost data of each grid for each one
139  // of its neighbors. The first index is the global grid index. The second
140  // is the neighbor index.
141  std::vector< std::vector< vtkPoints* > > RemotePoints;
142  std::vector< std::vector< vtkPointData* > > RemotePointData;
143  std::vector< std::vector< vtkCellData* > > RemoteCellData;
144 
145  // Data structures to store the send/receive buffer sizes and corresponding
146  // persistent buffers. The first index is the global grid index. The second
147  // index is the neighbor index for the given grid.
148  std::vector< std::vector< unsigned int > > SendBufferSizes;
149  std::vector< std::vector< unsigned int > > RcvBufferSizes;
150  std::vector< std::vector< unsigned char* > > SendBuffers;
151  std::vector< std::vector< unsigned char* > > RcvBuffers;
152 
156 
157  // Array of MPI requests
159 
163  bool GridExtentsAreEqual( int rhs[6], int lhs[6] );
164 
168  bool HasPointData(const int gridIdx);
169 
173  bool HasCellData(const int gridIdx);
174 
178  bool HasPoints(const int gridIdx);
179 
183  void InitializeMessageCounters();
184 
189  void ClearRemoteData();
190 
194  void ClearRawBuffers();
195 
200  void RegisterRemoteGrid( const int gridID, int extents[6], int process );
201 
206  void TransferRemoteNeighborData(
207  const int gridIdx,const int nei,const vtkStructuredNeighbor& Neighbor );
208 
214  virtual void TransferGhostDataFromNeighbors(const int gridID) override;
215 
219  void PackGhostData();
220 
225  void UnpackGhostData();
226 
231  void DeserializeBufferSizesForProcess(
232  int *buffersizes, vtkIdType N, const int processId );
233 
238  void SerializeBufferSizes(int *&sizesbuf, vtkIdType &N);
239 
244  void ExchangeBufferSizes();
245 
251  void ExchangeGhostDataInit();
252 
257  void PostReceives();
258 
263  void PostSends();
264 
270  void CommunicateGhostData();
271 
277  void ExchangeGhostDataPost();
278 
282  void ExchangeGhostData();
283 
288  void SerializeGhostPoints(
289  const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream );
290 
294  void SerializeDataArray(
295  vtkDataArray *dataArray, vtkMultiProcessStream& bytestream );
296 
301  void SerializeFieldData(
302  int sourceExtent[6], int targetExtent[6], vtkFieldData *fieldData,
303  vtkMultiProcessStream& bytestream );
304 
309  void SerializeGhostPointData(
310  const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream );
311 
316  void SerializeGhostCellData(
317  const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream );
318 
323  void DeserializeGhostPoints(
324  const int gridIdx, const int nei,
325  int ext[6], vtkMultiProcessStream& bytestream );
326 
330  void DeserializeDataArray(
331  vtkDataArray *&dataArray,const int dataType,
332  const int numberOfTuples, const int numberOfComponents,
333  vtkMultiProcessStream& bytestream );
334 
339  void DeserializeFieldData(
340  int ext[6], vtkFieldData *fieldData,
341  vtkMultiProcessStream &bytestream );
342 
347  void DeserializeGhostPointData(
348  const int gridIdx, const int nei,
349  int ext[6], vtkMultiProcessStream& bytestream );
350 
355  void DeserializeGhostCellData(
356  const int gridIdx, const int nei,
357  int ext[6], vtkMultiProcessStream& bytestream );
358 
365  void SerializeGhostData(
366  const int sndGridID, const int rcvGrid, int sndext[6],
367  unsigned char*& buffer, unsigned int &size);
368 
373  void DeserializeGhostData(
374  const int gridID, const int neiListID,
375  const int neiGridIdx, int rcvext[6],
376  unsigned char *buffer, unsigned int size );
377 
382  void ExchangeGridExtents();
383 
388  void SerializeGridExtents( int *&sndbuffer, vtkIdType &N );
389 
394  void DeserializeGridExtentForProcess(
395  int *rcvbuffer, vtkIdType &N, const int processId );
396 
397 private:
399  void operator=(const vtkPStructuredGridConnectivity& ) = delete;
400 };
401 
402 //=============================================================================
403 // INLINE METHODS
404 //=============================================================================
405 
406 
408  int rhs[6], int lhs[6] )
409 {
410  for( int i=0; i < 6; ++i )
411  {
412  if( rhs[i] != lhs[i] )
413  {
414  return false;
415  }
416  }
417  return true;
418 }
419 
420 //------------------------------------------------------------------------------
421 inline bool vtkPStructuredGridConnectivity::HasPointData(const int gridIdx)
422 {
423  // Sanity check
424  assert("pre: grid index is out-of-bounds!" &&
425  (gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
426 
427  if( (this->GridPointData[gridIdx] != nullptr) &&
428  (this->GridPointData[gridIdx]->GetNumberOfArrays() > 0) )
429  {
430  return true;
431  }
432  return false;
433 }
434 
435 //------------------------------------------------------------------------------
436 inline bool vtkPStructuredGridConnectivity::HasCellData(const int gridIdx)
437 {
438  // Sanity check
439  assert("pre: grid index is out-of-bounds!" &&
440  (gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
441 
442  if( (this->GridCellData[gridIdx] != nullptr) &&
443  (this->GridCellData[gridIdx]->GetNumberOfArrays( ) > 0) )
444  {
445  return true;
446  }
447  return false;
448 }
449 
450 //------------------------------------------------------------------------------
451 inline bool vtkPStructuredGridConnectivity::HasPoints(const int gridIdx)
452 {
453  // Sanity check
454  assert("pre: grid index is out-of-bounds!" &&
455  (gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
456 
457  if( this->GridPoints[gridIdx] != nullptr )
458  {
459  return true;
460  }
461  return false;
462 }
463 
464 //------------------------------------------------------------------------------
466 {
467  this->TotalNumberOfMsgs=this->TotalNumberOfRcvs=this->TotalNumberOfSends=0;
468 }
469 
470 //------------------------------------------------------------------------------
472 {
473  this->SendBufferSizes.clear();
474  this->RcvBufferSizes.clear();
475 
476  // STEP 0: Clear send buffers
477  for( unsigned int i=0; i < this->SendBuffers.size(); ++i )
478  {
479  for( unsigned int j=0; j < this->SendBuffers[i].size(); ++j )
480  {
481  delete [] this->SendBuffers[i][j];
482  } // END for all neighbors
483  this->SendBuffers[i].clear();
484  } // END for all grids
485  this->SendBuffers.clear();
486 
487  // STEP 1: Clear rcv buffers
488  for( unsigned int i=0; i < this->RcvBuffers.size(); ++i )
489  {
490  for( unsigned int j=0; j < this->RcvBuffers[i].size(); ++j )
491  {
492  delete [] this->RcvBuffers[i][j];
493  } // END for all neighbors
494  this->RcvBuffers[i].clear();
495  } // END for all grids
496  this->RcvBuffers.clear();
497 }
498 
499 //------------------------------------------------------------------------------
501 {
502  // STEP 0: Clear remote points
503  for( unsigned int i=0; i < this->RemotePoints.size(); ++i )
504  {
505  for( unsigned int j=0; j < this->RemotePoints[i].size(); ++j )
506  {
507  if( this->RemotePoints[ i ][ j ] != nullptr )
508  {
509  this->RemotePoints[ i ][ j ]->Delete();
510  }
511  } // END for all j
512  this->RemotePoints[ i ].clear();
513  } // END for all i
514  this->RemotePoints.clear();
515 
516  // STEP 1: Clear remote point data
517  for( unsigned int i=0; i < this->RemotePointData.size(); ++i )
518  {
519  for( unsigned int j=0; j < this->RemotePointData[i].size(); ++j )
520  {
521  if( this->RemotePointData[ i ][ j ] != nullptr )
522  {
523  this->RemotePointData[ i ][ j ]->Delete();
524  }
525  } // END for all j
526  this->RemotePointData[ i ].clear();
527  } // END for all i
528  this->RemotePointData.clear();
529 
530  // STEP 2: Clear remote cell data
531  for( unsigned int i=0; i < this->RemoteCellData.size(); ++i )
532  {
533  for( unsigned int j=0; j < this->RemoteCellData[i].size(); ++j )
534  {
535  if( this->RemoteCellData[ i ][ j ] != nullptr )
536  {
537  this->RemoteCellData[ i ][ j ]->Delete();
538  }
539  } // END for all j
540  this->RemoteCellData[ i ].clear();
541  }
542  this->RemoteCellData.clear();
543 }
544 
545 //------------------------------------------------------------------------------
546 inline bool vtkPStructuredGridConnectivity::IsGridRemote(const int gridID )
547 {
548  return( !this->IsGridLocal(gridID) );
549 }
550 
551 //------------------------------------------------------------------------------
552 inline bool vtkPStructuredGridConnectivity::IsGridLocal(const int gridID)
553 {
554  assert( "pre: Instance has not been initialized!" && this->Initialized );
555  assert( "pre: gridID is out-of-bounds" &&
556  (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids) ) );
557  assert( "pre: GridRanks is not properly allocated" &&
558  this->NumberOfGrids == this->GridRanks.size() );
559  return( (this->GridRanks[ gridID ] == this->Rank) );
560 }
561 
562 //------------------------------------------------------------------------------
563 inline int vtkPStructuredGridConnectivity::GetGridRank( const int gridID )
564 {
565  assert( "pre: Instance has not been initialized!" && this->Initialized );
566  assert( "pre: gridID out-of-bounds!" &&
567  (gridID >= 0 && gridID < static_cast<int>(this->NumberOfGrids)));
568  return( this->GridRanks[ gridID ] );
569 }
570 #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:37
stream used to pass data across processes using vtkMultiProcessController.
std::vector< std::vector< unsigned int > > RcvBufferSizes
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:38
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.
std::vector< std::vector< vtkPoints * > > RemotePoints
int vtkIdType
Definition: vtkType.h:347
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.
vtkStructuredGridConnectivity is a concrete instance of vtkObject that implements functionality for c...
std::vector< std::vector< vtkPointData * > > RemotePointData
std::vector< std::vector< vtkCellData * > > RemoteCellData
a simple class to control print indentation
Definition: vtkIndent.h:39
int GetGridRank(const int gridID)
Returns the rank of the given gridID.
static vtkStructuredGridConnectivity * New()
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
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.
dynamic, self-adjusting array of unsigned char
An internal, light-weight class used to store neighbor information.
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 char * > > RcvBuffers
std::vector< std::vector< unsigned char * > > SendBuffers
std::vector< std::vector< unsigned int > > SendBufferSizes
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:39
represent and manipulate fields of data
Definition: vtkFieldData.h:56
Multiprocessing communication superclass.