VTK
Allocators.h
Go to the documentation of this file.
1 //=============================================================================
2 //
3 // Copyright (c) Kitware, Inc.
4 // All rights reserved.
5 // See LICENSE.txt for details.
6 //
7 // This software is distributed WITHOUT ANY WARRANTY; without even
8 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 // PURPOSE. See the above copyright notice for more information.
10 //
11 // Copyright 2012 Sandia Corporation.
12 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
13 // the U.S. Government retains certain rights in this software.
14 //
15 //=============================================================================
16 
17 #ifndef vtkToDax_Allocators_h
18 #define vtkToDax_Allocators_h
19 
20 #include <dax/VectorTraits.h>
21 #include <cstddef>
22 
23 class vtkPoints;
24 class vtkCellArray;
25 
26 /*
27  * The one rule of allocators is that they can allocate
28  * memory, but they can't set any values in the allocated memory.
29  * We can't write to the memory because that causes affinity
30  * between the memory location and the current thread, which is a very
31  * bad thing as we want that memory location affinity to be assigned to the dax
32  * thread that will be using section, not the master thread
33  */
34 
35 namespace vtkToDax
36 {
37 template< typename _T,
38  int NUM_COMPONENTS>
39 struct vtkAlloc
40 {
41  typedef _T T;
43 
44  typedef std::size_t size_type;
45  typedef ptrdiff_t difference_type;
46  typedef T* pointer;
47  typedef const T* const_pointer;
48  typedef T& reference;
49  typedef const T& const_reference;
50  typedef T value_type;
51 
52 
53  pointer allocate(size_type n, self::const_pointer hint = 0)
54  {
55  pointer p = value_type::New();
56  p->SetNumberOfComponents(NUM_COMPONENTS);
57  p->SetNumberOfTuples(n);
58  return p;
59  }
60 
62  {
63  p->Delete();
64  }
65 };
66 
67 template<int NUM_COMPONENTS>
68 struct vtkAlloc<vtkPoints, NUM_COMPONENTS>
69 {
70  typedef vtkPoints T;
72 
73  typedef std::size_t size_type;
74  typedef ptrdiff_t difference_type;
75  typedef T* pointer;
76  typedef const T* const_pointer;
77  typedef T& reference;
78  typedef const T& const_reference;
79  typedef T value_type;
80 
81 
82  pointer allocate(size_type n, const_pointer hint = 0)
83  {
84 #ifdef DAX_USE_DOUBLE_PRECISION
85  pointer p = value_type::New(VTK_DOUBLE);
86 #else
87  pointer p = value_type::New(VTK_FLOAT);
88 #endif
89  p->SetNumberOfPoints(n);
90  return p;
91  }
92 
93  void deallocate(pointer p, size_type)
94  {
95  p->Delete();
96  }
97 };
98 
99 
100 template<int NUM_COMPONENTS>
101 struct vtkAlloc<vtkCellArray, NUM_COMPONENTS>
102 {
103  typedef vtkCellArray T;
105 
106  typedef std::size_t size_type;
107  typedef ptrdiff_t difference_type;
108  typedef T* pointer;
109  typedef const T* const_pointer;
110  typedef T& reference;
111  typedef const T& const_reference;
112  typedef T value_type;
113 
114 
115  //for cell arrays dax requests an allocation that is num_cells * num_components
116  pointer allocate(size_type n, const_pointer hint = 0)
117  {
118  pointer p = value_type::New();
119  const size_type numCells = n/NUM_COMPONENTS;
120  p->SetNumberOfCells(numCells);
121  p->GetData()->SetNumberOfTuples(n+numCells);
122  return p;
123  }
124 
125  void deallocate(pointer p, size_type)
126  {
127  p->Delete();
128  }
129 };
130 }
131 
132 #endif //vtkToDax_Allocators_h
void deallocate(self::pointer p, self::size_type)
Definition: Allocators.h:61
pointer allocate(size_type n, self::const_pointer hint=0)
Definition: Allocators.h:53
std::size_t size_type
Definition: Allocators.h:44
const T & const_reference
Definition: Allocators.h:49
#define VTK_DOUBLE
Definition: vtkType.h:59
#define VTK_FLOAT
Definition: vtkType.h:58
pointer allocate(size_type n, const_pointer hint=0)
Definition: Allocators.h:116
const T * const_pointer
Definition: Allocators.h:47
void SetNumberOfPoints(vtkIdType numPoints)
Specify the number of points for this object to hold.
Definition: vtkPoints.h:252
virtual void SetNumberOfCells(vtkIdType)
Set the number of cells in the array.
object to represent cell connectivity
Definition: vtkCellArray.h:50
vtkIdTypeArray * GetData()
Return the underlying data as a data array.
Definition: vtkCellArray.h:256
pointer allocate(size_type n, const_pointer hint=0)
Definition: Allocators.h:82
virtual void SetNumberOfTuples(vtkIdType numTuples)=0
Set the number of tuples (a component group) in the array.
represent and manipulate 3D points
Definition: vtkPoints.h:39
virtual void Delete()
Delete a VTK object.
ptrdiff_t difference_type
Definition: Allocators.h:45