VTK
dox/Filters/ParallelGeometry/vtkPStructuredGridConnectivity.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003  Program:   Visualization Toolkit
00004  Module:    vtkPStructuredGridConnectivity.h
00005 
00006  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007  All rights reserved.
00008  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010  This software is distributed WITHOUT ANY WARRANTY; without even
00011  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012  PURPOSE.  See the above copyright notice for more information.
00013 
00014  =========================================================================*/
00033 #ifndef VTKPSTRUCTUREDGRIDCONNECTIVITY_H_
00034 #define VTKPSTRUCTUREDGRIDCONNECTIVITY_H_
00035 
00036 // VTK include directives
00037 #include "vtkFiltersParallelGeometryModule.h" // For export macro
00038 #include "vtkStructuredGridConnectivity.h"
00039 #include "vtkMPICommunicator.h" // Needed for vtkMPICommunicator::Request
00040 
00041 // C++ include directives
00042 #include <vector> // For STL vector
00043 
00044 // Forward declarations
00045 class vtkMultiProcessController;
00046 class vtkMPIController;
00047 class vtkMultiProcessStream;
00048 //class vtkMPICommunicator::Request;
00049 
00050 class VTKFILTERSPARALLELGEOMETRY_EXPORT vtkPStructuredGridConnectivity :
00051   public vtkStructuredGridConnectivity
00052 {
00053 public:
00054   static vtkPStructuredGridConnectivity* New();
00055   vtkTypeMacro(vtkPStructuredGridConnectivity,vtkStructuredGridConnectivity);
00056   void PrintSelf(ostream& os, vtkIndent indent );
00057 
00059 
00060   vtkSetMacro( Controller,vtkMultiProcessController* );
00061   vtkGetMacro( Controller,vtkMultiProcessController* );
00063 
00065   void SetNumberOfGrids( const unsigned int N );
00066 
00068 
00069   void RegisterGrid( const int gridID, int extents[6],
00070       vtkUnsignedCharArray* nodesGhostArray,
00071       vtkUnsignedCharArray* cellGhostArray,
00072       vtkPointData* pointData,
00073       vtkCellData* cellData,
00074       vtkPoints* gridNodes );
00076 
00078 
00080   int GetNumberOfLocalGrids()
00081     { return static_cast<int>(this->GridIds.size()); };
00083 
00088   int GetGridRank( const int gridID );
00089 
00091   bool IsGridRemote( const int gridID );
00092 
00094   bool IsGridLocal( const int gridID );
00095 
00100   void Initialize();
00101 
00104   void ComputeNeighbors();
00105 
00108   virtual void CreateGhostLayers( const int N=1 );
00109 
00110 protected:
00111   vtkPStructuredGridConnectivity();
00112   ~vtkPStructuredGridConnectivity();
00113 
00114   vtkMultiProcessController *Controller;
00115   int Rank;
00116   bool Initialized;
00117 
00118   // BTX
00119   std::vector< int > GridRanks; // Corresponding rank for each grid
00120   std::vector< int > GridIds;   // List of GridIds, owned by this process
00121 
00122   // Data structures to store the remote ghost data of each grid for each one
00123   // of its neighbors. The first index is the global grid index. The second
00124   // is the neighbor index.
00125   std::vector< std::vector< vtkPoints* > >    RemotePoints;
00126   std::vector< std::vector< vtkPointData* > > RemotePointData;
00127   std::vector< std::vector< vtkCellData* > >  RemoteCellData;
00128 
00129   // Data structures to store the send/receive buffer sizes and corresponding
00130   // persistent buffers. The first index is the global grid index. The second
00131   // index is the neighbor index for the given grid.
00132   std::vector< std::vector< unsigned int > > SendBufferSizes;
00133   std::vector< std::vector< unsigned int > > RcvBufferSizes;
00134   std::vector< std::vector< unsigned char* > > SendBuffers;
00135   std::vector< std::vector< unsigned char* > > RcvBuffers;
00136 
00137   int TotalNumberOfSends;
00138   int TotalNumberOfRcvs;
00139   int TotalNumberOfMsgs;
00140 
00141   // Array of MPI requests
00142   vtkMPICommunicator::Request *MPIRequests;
00143   // ETX
00144 
00146   bool GridExtentsAreEqual( int rhs[6], int lhs[6] );
00147 
00150   bool HasPointData(const int gridIdx);
00151 
00154   bool HasCellData(const int gridIdx);
00155 
00157   bool HasPoints(const int gridIdx);
00158 
00160   void InitializeMessageCounters();
00161 
00164   void ClearRemoteData();
00165 
00167   void ClearRawBuffers();
00168 
00171   void RegisterRemoteGrid( const int gridID, int extents[6], int process );
00172 
00174 
00176   void TransferRemoteNeighborData(
00177       const int gridIdx,const int nei,const vtkStructuredNeighbor& Neighbor );
00179 
00183   virtual void TransferGhostDataFromNeighbors(const int gridID);
00184 
00186   void PackGhostData();
00187 
00190   void UnpackGhostData();
00191 
00193 
00195   void DeserializeBufferSizesForProcess(
00196       int *buffersizes, vtkIdType N, const int processId );
00198 
00201   void SerializeBufferSizes(int *&sizesbuf, vtkIdType &N);
00202 
00205   void ExchangeBufferSizes();
00206 
00210   void ExchangeGhostDataInit();
00211 
00214   void PostReceives();
00215 
00218   void PostSends();
00219 
00223   void CommunicateGhostData();
00224 
00228   void ExchangeGhostDataPost();
00229 
00231   void ExchangeGhostData();
00232 
00234 
00236   void SerializeGhostPoints(
00237       const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream );
00239 
00241 
00242   void SerializeDataArray(
00243       vtkDataArray *dataArray, vtkMultiProcessStream& bytestream );
00245 
00247 
00249   void SerializeFieldData(
00250       int sourceExtent[6], int targetExtent[6], vtkFieldData *fieldData,
00251       vtkMultiProcessStream& bytestream );
00253 
00255 
00257   void SerializeGhostPointData(
00258       const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream );
00260 
00262 
00264   void SerializeGhostCellData(
00265       const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream );
00267 
00269 
00271   void DeserializeGhostPoints(
00272       const int gridIdx, const int nei,
00273       int ext[6], vtkMultiProcessStream& bytestream );
00275 
00277 
00278   void DeserializeDataArray(
00279       vtkDataArray *&dataArray,const int dataType,
00280       const int numberOfTuples, const int numberOfComponents,
00281       vtkMultiProcessStream& bytestream );
00283 
00285 
00287   void DeserializeFieldData(
00288       int ext[6], vtkFieldData *fieldData,
00289       vtkMultiProcessStream &bytestream );
00291 
00293 
00295   void DeserializeGhostPointData(
00296       const int gridIdx, const int nei,
00297       int ext[6], vtkMultiProcessStream& bytestream );
00299 
00301 
00303   void DeserializeGhostCellData(
00304       const int gridIdx, const int nei,
00305       int ext[6], vtkMultiProcessStream& bytestream );
00307 
00309 
00313   void SerializeGhostData(
00314       const int sndGridID, const int rcvGrid, int sndext[6],
00315       unsigned char*& buffer, unsigned int &size);
00317 
00319 
00322   void DeserializeGhostData(
00323       const int gridID, const int neiListID,
00324       const int neiGridIdx, int rcvext[6],
00325       unsigned char *buffer, unsigned int size );
00327 
00330   void ExchangeGridExtents();
00331 
00335   void SerializeGridExtents( int *&sndbuffer, vtkIdType &N );
00336 
00338 
00340   void DeserializeGridExtentForProcess(
00341       int *rcvbuffer, vtkIdType &N, const int processId );
00343 
00344 private:
00345   vtkPStructuredGridConnectivity(const vtkPStructuredGridConnectivity& ); // Not implemented
00346   void operator=(const vtkPStructuredGridConnectivity& ); // Not implemented
00347 };
00348 
00349 //=============================================================================
00350 //  INLINE METHODS
00351 //=============================================================================
00352 
00353 
00354 inline bool vtkPStructuredGridConnectivity::GridExtentsAreEqual(
00355     int rhs[6], int lhs[6] )
00356 {
00357   for( int i=0; i < 6; ++i )
00358     {
00359     if( rhs[i] != lhs[i] )
00360       {
00361       return false;
00362       }
00363     }
00364   return true;
00365 }
00366 
00367 //------------------------------------------------------------------------------
00368 inline bool vtkPStructuredGridConnectivity::HasPointData(const int gridIdx)
00369 {
00370   // Sanity check
00371   assert("pre: grid index is out-of-bounds!" &&
00372          (gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
00373 
00374   if( (this->GridPointData[gridIdx] != NULL) &&
00375       (this->GridPointData[gridIdx]->GetNumberOfArrays() > 0) )
00376     {
00377     return true;
00378     }
00379   return false;
00380 }
00381 
00382 //------------------------------------------------------------------------------
00383 inline bool vtkPStructuredGridConnectivity::HasCellData(const int gridIdx)
00384 {
00385   // Sanity check
00386   assert("pre: grid index is out-of-bounds!" &&
00387          (gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
00388 
00389   if( (this->GridCellData[gridIdx] != NULL) &&
00390       (this->GridCellData[gridIdx]->GetNumberOfArrays( ) > 0) )
00391     {
00392     return true;
00393     }
00394   return false;
00395 }
00396 
00397 //------------------------------------------------------------------------------
00398 inline bool vtkPStructuredGridConnectivity::HasPoints(const int gridIdx)
00399 {
00400   // Sanity check
00401   assert("pre: grid index is out-of-bounds!" &&
00402          (gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
00403 
00404   if( this->GridPoints[gridIdx] != NULL )
00405     {
00406     return true;
00407     }
00408   return false;
00409 }
00410 
00411 //------------------------------------------------------------------------------
00412 inline void vtkPStructuredGridConnectivity::InitializeMessageCounters()
00413 {
00414   this->TotalNumberOfMsgs=this->TotalNumberOfRcvs=this->TotalNumberOfSends=0;
00415 }
00416 
00417 //------------------------------------------------------------------------------
00418 inline void vtkPStructuredGridConnectivity::ClearRawBuffers()
00419 {
00420   this->SendBufferSizes.clear();
00421   this->RcvBufferSizes.clear();
00422 
00423   // STEP 0: Clear send buffers
00424   for( unsigned int i=0; i < this->SendBuffers.size(); ++i )
00425     {
00426     for( unsigned int j=0; j < this->SendBuffers[i].size(); ++j )
00427       {
00428       if( this->SendBuffers[i][j] != NULL )
00429         {
00430         delete [] this->SendBuffers[i][j];
00431         }
00432       } // END for all neighbors
00433     this->SendBuffers[i].clear();
00434     } // END for all grids
00435   this->SendBuffers.clear();
00436 
00437   // STEP 1: Clear rcv buffers
00438   for( unsigned int i=0; i < this->RcvBuffers.size(); ++i )
00439     {
00440     for( unsigned int j=0; j < this->RcvBuffers[i].size(); ++j )
00441       {
00442       if( this->RcvBuffers[i][j] != NULL )
00443         {
00444         delete [] this->RcvBuffers[i][j];
00445         }
00446       } // END for all neighbors
00447     this->RcvBuffers[i].clear();
00448     } // END for all grids
00449   this->RcvBuffers.clear();
00450 }
00451 
00452 //------------------------------------------------------------------------------
00453 inline void vtkPStructuredGridConnectivity::ClearRemoteData()
00454 {
00455   // STEP 0: Clear remote points
00456   for( unsigned int i=0; i < this->RemotePoints.size(); ++i )
00457     {
00458     for( unsigned int j=0; j < this->RemotePoints[i].size(); ++j )
00459       {
00460       if( this->RemotePoints[ i ][ j ] != NULL )
00461         {
00462         this->RemotePoints[ i ][ j ]->Delete();
00463         }
00464       } // END for all j
00465     this->RemotePoints[ i ].clear();
00466     } // END for all i
00467   this->RemotePoints.clear();
00468 
00469   // STEP 1: Clear remote point data
00470   for( unsigned int i=0; i < this->RemotePointData.size(); ++i )
00471     {
00472     for( unsigned int j=0; j < this->RemotePointData[i].size(); ++j )
00473       {
00474       if( this->RemotePointData[ i ][ j ] != NULL )
00475         {
00476         this->RemotePointData[ i ][ j ]->Delete();
00477         }
00478       } // END for all j
00479     this->RemotePointData[ i ].clear();
00480     } // END for all i
00481   this->RemotePointData.clear();
00482 
00483   // STEP 2: Clear remote cell data
00484   for( unsigned int i=0; i < this->RemoteCellData.size(); ++i )
00485     {
00486     for( unsigned int j=0; j < this->RemoteCellData[i].size(); ++j )
00487       {
00488       if( this->RemoteCellData[ i ][ j ] != NULL )
00489         {
00490         this->RemoteCellData[ i ][ j ]->Delete();
00491         }
00492       } // END for all j
00493     this->RemoteCellData[ i ].clear();
00494     }
00495   this->RemoteCellData.clear();
00496 }
00497 
00498 //------------------------------------------------------------------------------
00499 inline bool vtkPStructuredGridConnectivity::IsGridRemote(const int gridID )
00500 {
00501   return( !this->IsGridLocal(gridID) );
00502 }
00503 
00504 //------------------------------------------------------------------------------
00505 inline bool vtkPStructuredGridConnectivity::IsGridLocal(const int gridID)
00506 {
00507   assert( "pre: Instance has not been intialized!" && this->Initialized );
00508   assert( "pre: gridID is out-of-bounds" &&
00509           (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids) ) );
00510   assert( "pre: GridRanks is not properly allocated" &&
00511           this->NumberOfGrids == this->GridRanks.size() );
00512   return( (this->GridRanks[ gridID ] == this->Rank) );
00513 }
00514 
00515 //------------------------------------------------------------------------------
00516 inline int vtkPStructuredGridConnectivity::GetGridRank( const int gridID )
00517 {
00518   assert( "pre: Instance has not been intialized!" && this->Initialized );
00519   assert( "pre: gridID out-of-bounds!" &&
00520           (gridID >= 0  && gridID < static_cast<int>(this->NumberOfGrids)));
00521   return( this->GridRanks[ gridID ] );
00522 }
00523 #endif /* VTKPSTRUCTUREDGRIDCONNECTIVITY_H_ */