VTK
vtkPKdTree.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkPKdTree.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 =========================================================================*/
15 /*----------------------------------------------------------------------------
16  Copyright (c) Sandia Corporation
17  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
18 ----------------------------------------------------------------------------*/
19 
43 #ifndef vtkPKdTree_h
44 #define vtkPKdTree_h
45 
46 #include "vtkFiltersParallelModule.h" // For export macro
47 #include "vtkKdTree.h"
48 #include <string> // Instead of using char*
49 #include <vector> // For automatic array memory management
50 
52 class vtkCommunicator;
53 class vtkSubGroup;
54 class vtkIntArray;
55 class vtkKdNode;
56 
57 class VTKFILTERSPARALLEL_EXPORT vtkPKdTree : public vtkKdTree
58 {
59 public:
60  vtkTypeMacro(vtkPKdTree, vtkKdTree);
61 
62 
63  void PrintSelf(ostream& os, vtkIndent indent) override;
64  void PrintTiming(ostream& os, vtkIndent indent) override;
65  void PrintTables(ostream& os, vtkIndent indent);
66 
67  static vtkPKdTree *New();
68 
75  void BuildLocator() override;
76 
82  vtkIdType GetTotalNumberOfCells(){return this->TotalNumCells;}
83 
92  int CreateProcessCellCountData();
93 
102  int CreateGlobalDataArrayBounds();
103 
105 
108  void SetController(vtkMultiProcessController *c);
109  vtkGetObjectMacro(Controller, vtkMultiProcessController);
111 
113 
122  vtkGetMacro(RegionAssignment, int);
124 
125  static const int NoRegionAssignment;
126  static const int ContiguousAssignment;
127  static const int UserDefinedAssignment;
128  static const int RoundRobinAssignment;
129 
135  int AssignRegions(int *map, int numRegions);
136 
142  int AssignRegionsRoundRobin();
143 
152  int AssignRegionsContiguous();
153 
159  { return &this->RegionAssignmentMap[0]; }
160 
162 
166  { return static_cast<int>(this->RegionAssignmentMap.size()); }
168 
174  int GetRegionAssignmentList(int procId, vtkIntArray *list);
175 
184  void GetAllProcessesBorderingOnPoint(float x, float y, float z,
185  vtkIntArray *list);
186 
190  int GetProcessAssignedToRegion(int regionId);
191 
196  int HasData(int processId, int regionId);
197 
202  int GetProcessCellCountForRegion(int processId, int regionId);
203 
208  int GetTotalProcessesInRegion(int regionId);
209 
215  int GetProcessListForRegion(int regionId, vtkIntArray *processes);
216 
224  int GetProcessesCellCountForRegion(int regionId, int *count, int len);
225 
230  int GetTotalRegionsForProcess(int processId);
231 
236  int GetRegionListForProcess(int processId, vtkIntArray *regions);
237 
245  int GetRegionsCellCountForProcess(int ProcessId, int *count, int len);
246 
248 
273  vtkIdType GetCellListsForProcessRegions(int ProcessId, int set,
274  vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
275  vtkIdType GetCellListsForProcessRegions(int ProcessId, vtkDataSet *set,
276  vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
277  vtkIdType GetCellListsForProcessRegions(int ProcessId,
278  vtkIdList *inRegionCells,
279  vtkIdList *onBoundaryCells);
281 
289  int ViewOrderAllProcessesInDirection(const double directionOfProjection[3],
290  vtkIntArray *orderedList);
291 
299  int ViewOrderAllProcessesFromPosition(const double cameraPosition[3],
300  vtkIntArray *orderedList);
301 
310  int GetCellArrayGlobalRange(const char *name, float range[2]);
311  int GetPointArrayGlobalRange(const char *name, float range[2]);
312  int GetCellArrayGlobalRange(const char *name, double range[2]);
313  int GetPointArrayGlobalRange(const char *name, double range[2]);
314 
315  int GetCellArrayGlobalRange(int arrayIndex, double range[2]);
316  int GetPointArrayGlobalRange(int arrayIndex, double range[2]);
317  int GetCellArrayGlobalRange(int arrayIndex, float range[2]);
318  int GetPointArrayGlobalRange(int arrayIndex, float range[2]);
319 
320 protected:
321 
322  vtkPKdTree();
323  ~vtkPKdTree() override;
324 
325  void SingleProcessBuildLocator();
326  int MultiProcessBuildLocator(double *bounds);
327 
328 private:
329 
330  int RegionAssignment;
331 
332  vtkMultiProcessController *Controller;
333 
334  vtkSubGroup *SubGroup;
335 
336  void StrDupWithNew(const char *s, std::string& output);
337 
338  int NumProcesses;
339  int MyId;
340 
341  // basic tables - each region is the responsibility of one process, but
342  // one process may be assigned many regions
343 
344  std::vector<int> RegionAssignmentMap; // indexed by region ID
345  std::vector<std::vector<int> > ProcessAssignmentMap; // indexed by process ID
346  std::vector<int> NumRegionsAssigned; // indexed by process ID
347 
348  int UpdateRegionAssignment();
349 
350  // basic tables reflecting the data that was read from disk
351  // by each process
352 
353  std::vector<char> DataLocationMap; // by process, by region
354 
355  std::vector<int> NumProcessesInRegion; // indexed by region ID
356  std::vector<std::vector<int> > ProcessList; // indexed by region ID
357 
358  std::vector<int> NumRegionsInProcess; // indexed by process ID
359  std::vector<std::vector<int> > ParallelRegionList; // indexed by process ID
360 
361  std::vector<std::vector<vtkIdType> > CellCountList; // indexed by region ID
362 
363  std::vector<double> CellDataMin; // global range for data arrays
364  std::vector<double> CellDataMax;
365  std::vector<double> PointDataMin;
366  std::vector<double> PointDataMax;
367  std::vector<std::string> CellDataName;
368  std::vector<std::string> PointDataName;
369  int NumCellArrays;
370  int NumPointArrays;
371 
372  // distribution of indices for select operation
373 
374  int BuildGlobalIndexLists(vtkIdType ncells);
375 
376  std::vector<vtkIdType> StartVal;
377  std::vector<vtkIdType> EndVal;
378  std::vector<vtkIdType> NumCells;
379  vtkIdType TotalNumCells;
380 
381  // local share of points to be partitioned, and local cache
382 
383  int WhoHas(int pos);
384  int _whoHas(int L, int R, int pos);
385  float *GetLocalVal(int pos);
386  float *GetLocalValNext(int pos);
387  void SetLocalVal(int pos, float *val);
388  void ExchangeVals(int pos1, int pos2);
389  void ExchangeLocalVals(int pos1, int pos2);
390 
391  // keep PtArray and PtArray2 as dynamically allocated since it's set as a return
392  // value from parent class method
393  float *PtArray;
394  float *PtArray2;
395  float *CurrentPtArray; // just pointer to other memory but never allocated
396  float *NextPtArray; // just pointer to other memory but never allocated
397  int PtArraySize;
398 
399  std::vector<int> SelectBuffer;
400 
401  // Parallel build of k-d tree
402 
403  int AllCheckForFailure(int rc, const char *where, const char *how);
404  void AllCheckParameters();
405 
407 
411  bool VolumeBounds(double*);
412  int DivideRegion(vtkKdNode *kd, int L, int level, int tag);
413  int BreadthFirstDivide(double *bounds);
414  void enQueueNode(vtkKdNode *kd, int L, int level, int tag);
416 
417  int Select(int dim, int L, int R);
418  void _select(int L, int R, int K, int dim);
419  void DoTransfer(int from, int to, int fromIndex, int toIndex, int count);
420 
421  int *PartitionAboutMyValue(int L, int R, int K, int dim);
422  int *PartitionAboutOtherValue(int L, int R, float T, int dim);
423  int *PartitionSubArray(int L, int R, int K, int dim, int p1, int p2);
424 
425  int CompleteTree();
426 #ifdef YIELDS_INCONSISTENT_REGION_BOUNDARIES
427  void RetrieveData(vtkKdNode *kd, int *buf);
428 #else
429  void ReduceData(vtkKdNode *kd, int *sources);
430  void BroadcastData(vtkKdNode *kd);
431 #endif
432 
433  void GetDataBounds(int L, int K, int R, float dataBounds[12]);
434  void GetLocalMinMax(int L, int R, int me, float *min, float *max);
435 
436  static int FillOutTree(vtkKdNode *kd, int level);
437  static int ComputeDepth(vtkKdNode *kd);
438  static void PackData(vtkKdNode *kd, double *data);
439  static void UnpackData(vtkKdNode *kd, double *data);
440  static void CheckFixRegionBoundaries(vtkKdNode *tree);
441 
442  // list management
443 
444  int AllocateDoubleBuffer();
445  void FreeDoubleBuffer();
446  void SwitchDoubleBuffer();
447  void AllocateSelectBuffer();
448  void FreeSelectBuffer();
449 
450  void InitializeGlobalIndexLists();
451  void AllocateAndZeroGlobalIndexLists();
452  void FreeGlobalIndexLists();
453  void InitializeRegionAssignmentLists();
454  void AllocateAndZeroRegionAssignmentLists();
455  void FreeRegionAssignmentLists();
456  void InitializeProcessDataLists();
457  void AllocateAndZeroProcessDataLists();
458  void FreeProcessDataLists();
459  void InitializeFieldArrayMinMax();
460  void AllocateAndZeroFieldArrayMinMax();
461  void FreeFieldArrayMinMax();
462 
463  void ReleaseTables();
464 
465  // Assigning regions to processors
466 
467  void AddProcessRegions(int procId, vtkKdNode *kd);
468  void BuildRegionListsForProcesses();
469 
470  // Gather process/region data totals
471 
472  bool CollectLocalRegionProcessData(std::vector<int>&); // returns false for failure
473  int BuildRegionProcessTables();
474  int BuildFieldArrayMinMax();
475  void AddEntry(int *list, int len, int id);
476 #ifdef VTK_USE_64BIT_IDS
477  void AddEntry(vtkIdType *list, int len, vtkIdType id);
478 #endif
479  static int BinarySearch(vtkIdType *list, int len, vtkIdType which);
480 
481  static int FindNextLocalArrayIndex(const char *n, const std::vector<std::string>& names,
482  int len, int start=0);
483 
484  vtkPKdTree(const vtkPKdTree&) = delete;
485  void operator=(const vtkPKdTree&) = delete;
486 };
487 
488 #endif
int DivideRegion(vtkKdNode *kd, float *c1, int *ids, int nlevels)
void operator=(const vtkKdTree &)=delete
static const int RoundRobinAssignment
Definition: vtkPKdTree.h:128
This class represents a single spatial region in an 3D axis aligned binary spatial partitioning...
Definition: vtkKdNode.h:45
const int * GetRegionAssignmentMap()
Returns the region assignment map where index is the region and value is the processes id for that re...
Definition: vtkPKdTree.h:158
Build a k-d tree decomposition of a list of points.
Definition: vtkPKdTree.h:57
int GetRegionAssignmentMapLength()
/ Returns the number of regions in the region assignment map.
Definition: vtkPKdTree.h:165
abstract class to specify dataset behavior
Definition: vtkDataSet.h:62
vtkIdType GetTotalNumberOfCells()
Get the total number of cells distributed across the data files read by all processes.
Definition: vtkPKdTree.h:82
void BuildLocator() override
Create the k-d tree decomposition of the cells of the data set or data sets.
int vtkIdType
Definition: vtkType.h:347
dynamic, self-adjusting array of int
Definition: vtkIntArray.h:45
a simple class to control print indentation
Definition: vtkIndent.h:39
list of point or cell ids
Definition: vtkIdList.h:36
static const int ContiguousAssignment
Definition: vtkPKdTree.h:126
static int Select(int dim, float *c1, int *ids, int nvals, double &coord)
virtual void PrintTiming(ostream &os, vtkIndent indent)
Print timing of k-d tree build.
static vtkKdTree * New()
scalable collective communication for a subset of members of a parallel VTK application ...
Definition: vtkSubGroup.h:51
static const int NoRegionAssignment
Definition: vtkPKdTree.h:122
a Kd-tree spatial decomposition of a set of points
Definition: vtkKdTree.h:78
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static const int UserDefinedAssignment
Definition: vtkPKdTree.h:127
Used to send/receive messages in a multiprocess environment.
#define max(a, b)
Multiprocessing communication superclass.