VTK
dox/Filters/Parallel/vtkPKdTree.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkPKdTree.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 =========================================================================*/
00015 /*----------------------------------------------------------------------------
00016  Copyright (c) Sandia Corporation
00017  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
00018 ----------------------------------------------------------------------------*/
00019 
00042 #ifndef __vtkPKdTree_h
00043 #define __vtkPKdTree_h
00044 
00045 #include "vtkFiltersParallelModule.h" // For export macro
00046 #include "vtkKdTree.h"
00047 
00048 class vtkMultiProcessController;
00049 class vtkCommunicator;
00050 class vtkSubGroup;
00051 class vtkIntArray;
00052 class vtkKdNode;
00053 
00054 class VTKFILTERSPARALLEL_EXPORT vtkPKdTree : public vtkKdTree
00055 {
00056 public:
00057   vtkTypeMacro(vtkPKdTree, vtkKdTree);
00058 
00059 
00060   void PrintSelf(ostream& os, vtkIndent indent);
00061   void PrintTiming(ostream& os, vtkIndent indent);
00062   void PrintTables(ostream& os, vtkIndent indent);
00063 
00064   static vtkPKdTree *New();
00065 
00069   void BuildLocator();
00070 
00074   vtkIdType GetTotalNumberOfCells(){return this->TotalNumCells;}
00075 
00081   int CreateProcessCellCountData();
00082 
00088   int CreateGlobalDataArrayBounds();
00089 
00091 
00092   void SetController(vtkMultiProcessController *c);
00093   vtkGetObjectMacro(Controller, vtkMultiProcessController);
00095 
00097 
00104   vtkGetMacro(RegionAssignment, int);
00106 
00107   static const int NoRegionAssignment;
00108   static const int ContiguousAssignment;
00109   static const int UserDefinedAssignment;
00110   static const int RoundRobinAssignment;
00111 
00115   int AssignRegions(int *map, int numRegions);
00116 
00120   int AssignRegionsRoundRobin();
00121 
00127   int AssignRegionsContiguous();
00128 
00130 
00132   const int* GetRegionAssignmentMap()
00133     { return this->RegionAssignmentMap; }
00135 
00137 
00138   vtkGetMacro(RegionAssignmentMapLength, int);
00140 
00144   int GetRegionAssignmentList(int procId, vtkIntArray *list);
00145 
00147 
00152   void GetAllProcessesBorderingOnPoint(float x, float y, float z,
00153                                        vtkIntArray *list);
00155 
00157   int GetProcessAssignedToRegion(int regionId);
00158 
00160   int HasData(int processId, int regionId);
00161 
00164   int GetProcessCellCountForRegion(int processId, int regionId);
00165 
00168   int GetTotalProcessesInRegion(int regionId);
00169 
00172   int GetProcessListForRegion(int regionId, vtkIntArray *processes);
00173 
00178   int GetProcessesCellCountForRegion(int regionId, int *count, int len);
00179 
00182   int GetTotalRegionsForProcess(int processId);
00183 
00186   int GetRegionListForProcess(int processId, vtkIntArray *regions);
00187 
00192   int GetRegionsCellCountForProcess(int ProcessId, int *count, int len);
00193 
00195 
00212   vtkIdType GetCellListsForProcessRegions(int ProcessId, int set,
00213             vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00214   vtkIdType GetCellListsForProcessRegions(int ProcessId, vtkDataSet *set,
00215             vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00216   vtkIdType GetCellListsForProcessRegions(int ProcessId,
00217                                           vtkIdList *inRegionCells,
00218                                           vtkIdList *onBoundaryCells);
00220 
00222 
00227   int ViewOrderAllProcessesInDirection(const double directionOfProjection[3],
00228                                        vtkIntArray *orderedList);
00230 
00232 
00237   int ViewOrderAllProcessesFromPosition(const double cameraPosition[3],
00238                                         vtkIntArray *orderedList);
00240 
00246   int GetCellArrayGlobalRange(const char *name, float range[2]);
00247   int GetPointArrayGlobalRange(const char *name, float range[2]);
00248   int GetCellArrayGlobalRange(const char *name, double range[2]);
00249   int GetPointArrayGlobalRange(const char *name, double range[2]);
00250 
00251   int GetCellArrayGlobalRange(int arrayIndex, double range[2]);
00252   int GetPointArrayGlobalRange(int arrayIndex, double range[2]);
00253   int GetCellArrayGlobalRange(int arrayIndex, float range[2]);
00254   int GetPointArrayGlobalRange(int arrayIndex, float range[2]);
00255 
00256 protected:
00257 
00258   vtkPKdTree();
00259   ~vtkPKdTree();
00260 
00261   void SingleProcessBuildLocator();
00262   int MultiProcessBuildLocator(double *bounds);
00263 
00264 private:
00265 
00266   int RegionAssignment;
00267 
00268   vtkMultiProcessController *Controller;
00269 
00270   vtkSubGroup *SubGroup;
00271 
00272   static char *StrDupWithNew(const char *s);
00273 
00274   int NumProcesses;
00275   int MyId;
00276 
00277   // basic tables - each region is the responsibility of one process, but
00278   //                one process may be assigned many regions
00279 
00280   int *RegionAssignmentMap;        // indexed by region ID
00281   int RegionAssignmentMapLength;
00282   int **ProcessAssignmentMap;      // indexed by process ID
00283   int *NumRegionsAssigned;         // indexed by process ID
00284 
00285   int UpdateRegionAssignment();
00286 
00287   // basic tables reflecting the data that was read from disk
00288   // by each process
00289 
00290   char *DataLocationMap;              // by process, by region
00291 
00292   int *NumProcessesInRegion;          // indexed by region ID
00293   int **ProcessList;                  // indexed by region ID
00294 
00295   int *NumRegionsInProcess;           // indexed by process ID
00296   int **RegionList;                   // indexed by process ID
00297 
00298   vtkIdType **CellCountList;                // indexed by region ID
00299 
00300   double *CellDataMin;           // global range for data arrays
00301   double *CellDataMax;
00302   double *PointDataMin;
00303   double *PointDataMax;
00304   char **CellDataName;
00305   char **PointDataName;
00306   int NumCellArrays;
00307   int NumPointArrays;
00308 
00309   // distribution of indices for select operation
00310 
00311   int BuildGlobalIndexLists(vtkIdType ncells);
00312 
00313   vtkIdType *StartVal;
00314   vtkIdType *EndVal;
00315   vtkIdType *NumCells;
00316   vtkIdType TotalNumCells;
00317 
00318   // local share of points to be partitioned, and local cache
00319 
00320   int WhoHas(int pos);
00321   int _whoHas(int L, int R, int pos);
00322   float *GetLocalVal(int pos);
00323   float *GetLocalValNext(int pos);
00324   void SetLocalVal(int pos, float *val);
00325   void ExchangeVals(int pos1, int pos2);
00326   void ExchangeLocalVals(int pos1, int pos2);
00327 
00328   float *PtArray;
00329   float *PtArray2;
00330   float *CurrentPtArray;
00331   float *NextPtArray;
00332   int PtArraySize;
00333 
00334   int *SelectBuffer;
00335 
00336   // Parallel build of k-d tree
00337 
00338   int AllCheckForFailure(int rc, const char *where, const char *how);
00339   void AllCheckParameters();
00340 
00342 
00344   bool VolumeBounds(double*);
00345   int DivideRegion(vtkKdNode *kd, int L, int level, int tag);
00346   int BreadthFirstDivide(double *bounds);
00347   void enQueueNode(vtkKdNode *kd, int L, int level, int tag);
00349 
00350   int Select(int dim, int L, int R);
00351   void _select(int L, int R, int K, int dim);
00352   void DoTransfer(int from, int to, int fromIndex, int toIndex, int count);
00353 
00354   int *PartitionAboutMyValue(int L, int R, int K, int dim);
00355   int *PartitionAboutOtherValue(int L, int R, float T, int dim);
00356   int *PartitionSubArray(int L, int R, int K, int dim, int p1, int p2);
00357 
00358   int CompleteTree();
00359 #ifdef YIELDS_INCONSISTENT_REGION_BOUNDARIES
00360   void RetrieveData(vtkKdNode *kd, int *buf);
00361 #else
00362   void ReduceData(vtkKdNode *kd, int *sources);
00363   void BroadcastData(vtkKdNode *kd);
00364 #endif
00365 
00366   float *DataBounds(int L, int K, int R);
00367   void GetLocalMinMax(int L, int R, int me, float *min, float *max);
00368 
00369   static int FillOutTree(vtkKdNode *kd, int level);
00370   static int ComputeDepth(vtkKdNode *kd);
00371   static void PackData(vtkKdNode *kd, double *data);
00372   static void UnpackData(vtkKdNode *kd, double *data);
00373   static void CheckFixRegionBoundaries(vtkKdNode *tree);
00374 
00375   // list management
00376 
00377   int AllocateDoubleBuffer();
00378   void FreeDoubleBuffer();
00379   void SwitchDoubleBuffer();
00380   int AllocateSelectBuffer();
00381   void FreeSelectBuffer();
00382 
00383   void InitializeGlobalIndexLists();
00384   int AllocateAndZeroGlobalIndexLists();
00385   void FreeGlobalIndexLists();
00386   void InitializeRegionAssignmentLists();
00387   int AllocateAndZeroRegionAssignmentLists();
00388   void FreeRegionAssignmentLists();
00389   void InitializeProcessDataLists();
00390   int AllocateAndZeroProcessDataLists();
00391   void FreeProcessDataLists();
00392   void InitializeFieldArrayMinMax();
00393   int AllocateAndZeroFieldArrayMinMax();
00394   void FreeFieldArrayMinMax();
00395 
00396   void ReleaseTables();
00397 
00398   // Assigning regions to processors
00399 
00400   void AddProcessRegions(int procId, vtkKdNode *kd);
00401   void BuildRegionListsForProcesses();
00402 
00403   // Gather process/region data totals
00404 
00405   int *CollectLocalRegionProcessData();
00406   int BuildRegionProcessTables();
00407   int BuildFieldArrayMinMax();
00408   void AddEntry(int *list, int len, int id);
00409 #ifdef VTK_USE_64BIT_IDS
00410   void AddEntry(vtkIdType *list, int len, vtkIdType id);
00411 #endif
00412   static int BinarySearch(vtkIdType *list, int len, vtkIdType which);
00413 
00414   static int FindNextLocalArrayIndex(const char *n, const char **names, int len, int start=0);
00415 
00416   vtkPKdTree(const vtkPKdTree&); // Not implemented
00417   void operator=(const vtkPKdTree&); // Not implemented
00418 };
00419 
00420 #endif