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