VTK-m  2.0
ArrayPortalBasic.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_internal_ArrayPortalBasic_h
11 #define vtk_m_internal_ArrayPortalBasic_h
12 
13 #include <vtkm/Assert.h>
14 #include <vtkm/Types.h>
15 
16 #ifdef VTKM_CUDA
17 // CUDA devices have special instructions for faster data loading
19 #endif // VTKM_CUDA
20 
21 namespace vtkm
22 {
23 namespace internal
24 {
25 
26 namespace detail
27 {
28 
29 // These templated methods can be overloaded for special access to data.
30 
31 template <typename T>
32 VTKM_EXEC_CONT static inline T ArrayPortalBasicReadGet(const T* const data)
33 {
34  return *data;
35 }
36 
37 template <typename T>
38 VTKM_EXEC_CONT static inline T ArrayPortalBasicWriteGet(const T* const data)
39 {
40  return *data;
41 }
42 
43 template <typename T>
44 VTKM_EXEC_CONT static inline void ArrayPortalBasicWriteSet(T* data, const T& value)
45 {
46  *data = value;
47 }
48 
49 } // namespace detail
50 
51 template <typename T>
52 class ArrayPortalBasicRead
53 {
54  const T* Array = nullptr;
55  vtkm::Id NumberOfValues = 0;
56 
57 public:
58  using ValueType = T;
59 
60  VTKM_EXEC_CONT vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
61 
62  VTKM_EXEC_CONT ValueType Get(vtkm::Id index) const
63  {
64  VTKM_ASSERT(index >= 0);
65  VTKM_ASSERT(index < this->NumberOfValues);
66 
67  return detail::ArrayPortalBasicReadGet(this->Array + index);
68  }
69 
70  VTKM_EXEC_CONT const ValueType* GetIteratorBegin() const { return this->Array; }
71  VTKM_EXEC_CONT const ValueType* GetIteratorEnd() const
72  {
73  return this->Array + this->NumberOfValues;
74  }
75 
76  VTKM_EXEC_CONT const ValueType* GetArray() const { return this->Array; }
77 
78  ArrayPortalBasicRead() = default;
79  ArrayPortalBasicRead(ArrayPortalBasicRead&&) = default;
80  ArrayPortalBasicRead(const ArrayPortalBasicRead&) = default;
81  ArrayPortalBasicRead& operator=(ArrayPortalBasicRead&&) = default;
82  ArrayPortalBasicRead& operator=(const ArrayPortalBasicRead&) = default;
83 
84  VTKM_EXEC_CONT ArrayPortalBasicRead(const T* array, vtkm::Id numberOfValues)
85  : Array(array)
86  , NumberOfValues(numberOfValues)
87  {
88  }
89 };
90 
91 template <typename T>
92 class ArrayPortalBasicWrite
93 {
94  T* Array = nullptr;
95  vtkm::Id NumberOfValues = 0;
96 
97 public:
98  using ValueType = T;
99 
100  VTKM_EXEC_CONT vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
101 
102  VTKM_EXEC_CONT ValueType Get(vtkm::Id index) const
103  {
104  VTKM_ASSERT(index >= 0);
105  VTKM_ASSERT(index < this->NumberOfValues);
106 
107  return detail::ArrayPortalBasicWriteGet(this->Array + index);
108  }
109 
110  VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
111  {
112  VTKM_ASSERT(index >= 0);
113  VTKM_ASSERT(index < this->NumberOfValues);
114 
115  detail::ArrayPortalBasicWriteSet(this->Array + index, value);
116  }
117 
118  VTKM_EXEC_CONT ValueType* GetIteratorBegin() const { return this->Array; }
119  VTKM_EXEC_CONT ValueType* GetIteratorEnd() const { return this->Array + this->NumberOfValues; }
120 
121  VTKM_EXEC_CONT ValueType* GetArray() const { return this->Array; }
122 
123  ArrayPortalBasicWrite() = default;
124  ArrayPortalBasicWrite(ArrayPortalBasicWrite&&) = default;
125  ArrayPortalBasicWrite(const ArrayPortalBasicWrite&) = default;
126  ArrayPortalBasicWrite& operator=(ArrayPortalBasicWrite&&) = default;
127  ArrayPortalBasicWrite& operator=(const ArrayPortalBasicWrite&) = default;
128 
129  VTKM_EXEC_CONT ArrayPortalBasicWrite(T* array, vtkm::Id numberOfValues)
130  : Array(array)
131  , NumberOfValues(numberOfValues)
132  {
133  }
134 };
135 }
136 } // namespace vtkm::internal
137 
138 #endif //vtk_m_internal_ArrayPortalBasic_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT auto Get(const vtkm::Tuple< Ts... > &tuple) -> decltype(tuple.template Get< Index >())
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:83
Assert.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
ArrayPortalBasicCuda.h