Nektar++
Loading...
Searching...
No Matches
Block.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Block.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
40
42{
43
44/**
45 * @brief A block means a group of elements of identical shape,
46 * basis and order. BlockAttributes stores the most basic
47 * information of a block.
48 */
49template <FieldState TState> class BlockAttributes
50{
51public:
52 BlockAttributes() = delete;
53 BlockAttributes(const size_t num_elements,
54 const size_t num_elements_with_padding,
55 const unsigned int num_data,
56 const unsigned int interleave_width)
57 : m_num_elements(num_elements),
58 m_num_elements_with_padding(num_elements_with_padding),
59 m_num_data(num_data),
60 m_compSize(num_elements_with_padding * num_data),
61 m_interleave_width(interleave_width)
62 {
63 }
64
65 BlockAttributes(const BlockAttributes &) = default;
66
67 template <typename TPadding>
68 void SetInterleaveWidth(const unsigned interleave_width)
69 {
70 if (interleave_width != 1)
71 {
73 m_num_elements_with_padding % interleave_width == 0,
74 "Number of elements is not divisible by interleave width.");
76 interleave_width % tinysimd::simd<TPadding>::width == 0,
77 "interleave width should be divisible by AVX vector width.");
78 }
79
80 m_interleave_width = interleave_width;
81 }
82
83 size_t GetNumElements(void) const
84 {
85 return m_num_elements;
86 }
87
88 size_t GetNumElementsWithPadding(void) const
89 {
91 }
92
93 unsigned int GetNumData(void) const
94 {
95 return m_num_data;
96 }
97
98 size_t CompSize(void) const
99 {
100 return m_compSize;
101 }
102
103 unsigned int GetInterleaveWidth(void) const
104 {
105 return m_interleave_width;
106 }
107
108 size_t GetNumPaddingElements(void) const
109 {
111 }
112
113 size_t GetNumElmtGroups(void) const
114 {
116 }
117
118 size_t GetNumElmtGroups(const unsigned int interleave_width) const
119 {
120 return m_num_elements_with_padding / interleave_width;
121 }
122
123private:
124 const size_t m_num_elements;
126 const unsigned int m_num_data;
127 const size_t m_compSize;
128 unsigned int m_interleave_width;
129};
130
131/**
132 * @brief Get the BlockAttributes for a given field state from an ExpList.
133 * This method basically captures identical elements that are contiguously
134 * stored in the ExpList and group them into blocks. Padding elements will
135 * also be set based on given vector width.
136 *
137 * @param state Field state to query.
138 * @param explist Expansion list to query.
139 * @param interleave_width Vector width to use for the field.
140 * @return std::vector<BlockAttributes>
141 */
142template <typename TData, FieldState TState>
143class BlockAccessor : public BlockAttributes<TState>
144{
145 template <typename TDataField, FieldState TStateField> friend class Field;
146
147public:
148 BlockAccessor() = delete;
150 MemoryRegion<TData> &&memory_region,
152 const unsigned int num_components,
153 const unsigned int num_homo_modes)
154 : BlockAttributes<TState>(blockAttr),
155 m_memory_region(std::move(memory_region)), m_field(field),
156 m_num_components(num_components), m_num_homo_modes(num_homo_modes)
157 {
158 }
159
160 BlockAccessor(const size_t num_elements,
161 const size_t num_elements_with_padding,
162 const unsigned int num_data, const unsigned interleave_width,
163 MemoryRegion<TData> &&memory_region,
165 const unsigned int num_components,
166 const unsigned int num_homo_modes)
167 : BlockAttributes<TState>(num_elements, num_elements_with_padding,
168 num_data, interleave_width),
169 m_memory_region(std::move(memory_region)), m_field(field),
170 m_num_components(num_components), m_num_homo_modes(num_homo_modes)
171 {
172 }
173
174 /**
175 * @brief Constructor methods - move from another BlockAccessor
176 *
177 * @param rhs - BlockAccessor to move from
178 */
180 : BlockAttributes<TState>(std::move(rhs)),
182 m_field(std::move(rhs.m_field)),
185 {
186 rhs.m_field = nullptr;
187 rhs.m_num_components = 0;
188 rhs.m_num_homo_modes = 1;
189 }
190
191 /**
192 * @brief Constructor methods - No copy methods
193 *
194 */
195 BlockAccessor(const BlockAccessor &) = delete;
197
198 /**
199 * @brief Move assignment operator.
200 *
201 * @param rhs - BlockAccessor to move from
202 *
203 * @return - BlockAccessor&
204 */
206 {
208 m_memory_region = std::move(rhs.m_memory_region);
209 m_field = std::move(rhs.m_field);
210 m_num_components = std::move(rhs.m_num_components);
211 m_num_homo_modes = std::move(rhs.m_num_homo_modes);
212
213 rhs.m_field = nullptr;
214 rhs.m_num_components = 0;
215 rhs.m_num_homo_modes = 1;
216
217 return *this;
218 }
219
221 const unsigned int &interleaveWidth, const std::string &execSpace,
222 const unsigned int streamID = 0);
223
224 /**
225 * @brief Get the pointer to the host/device memory.
226 *
227 * @return - TData*
228 */
229 template <typename MemSpace, typename MemAccess>
231 typename const_if<std::is_same_v<MemAccess, ReadOnly>, TData>::type *
232 GetPtr(const unsigned int streamID = 0);
233
234 /**
235 * @brief Gets the alignment of the memory region block.
236 *
237 * @return size_t
238 */
239 size_t GetAlignment() const
240 {
241 return m_memory_region.GetAlignment();
242 }
243
244 /**
245 * @brief Gets the number of components.
246 *
247 * @return unsigned int
248 */
249 unsigned int GetNumComponents() const
250 {
251 return m_num_components;
252 }
253
254 /**
255 * @brief Gets the number of homogeneous modes.
256 *
257 * @return unsigned int
258 */
259 unsigned int GetNumHomoModes() const
260 {
261 return m_num_homo_modes;
262 }
263
264 /**
265 * @brief initialize the storage memory.
266 *
267 * @param val - value to set
268 * @param count - number of values
269 */
270 template <typename MemSpace>
271 LIB_UTILITIES_EXPORT void Initialize(const TData val,
272 const size_t count = 0,
273 const size_t offset = 0,
274 const unsigned int streamID = 0);
275
276 /**
277 * @brief Templated copy method. This method copies the data of a
278 * BlockAccessor of the same size and takes its interleave width.
279 *
280 * @param rhs - BlockAccessor to copy from
281 * @param streamID - stream the copy is enqueued on
282 */
283 template <typename MemSpace>
285 const unsigned int streamID = 0);
286
287private:
288 // Note: m_field is a pointer to a Field object from which the current
289 // BlockAccessor object belong to, or null for a block of no Field,
290 // whose memory region then allocates its own storage.
293 unsigned int m_num_components = 0;
294 unsigned int m_num_homo_modes = 1;
295};
296
297} // namespace Nektar::LibUtilities
#define ASSERTL0(condition, msg)
#define LIB_UTILITIES_EXPORT
Get the BlockAttributes for a given field state from an ExpList. This method basically captures ident...
Definition Block.hpp:144
void Copy(BlockAccessor &rhs, const unsigned int streamID=0)
Templated copy method. This method copies the data of a BlockAccessor of the same size and takes its ...
Definition Block.cpp:70
Field< TData, TState > * m_field
Definition Block.hpp:292
unsigned int GetNumComponents() const
Gets the number of components.
Definition Block.hpp:249
BlockAccessor & operator=(const BlockAccessor &)=delete
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
Definition Block.hpp:259
void Initialize(const TData val, const size_t count=0, const size_t offset=0, const unsigned int streamID=0)
initialize the storage memory.
Definition Block.cpp:60
MemoryRegion< TData > m_memory_region
Definition Block.hpp:291
BlockAccessor & operator=(BlockAccessor &&rhs)
Move assignment operator.
Definition Block.hpp:205
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace, const unsigned int streamID=0)
BlockAccessor(const size_t num_elements, const size_t num_elements_with_padding, const unsigned int num_data, const unsigned interleave_width, MemoryRegion< TData > &&memory_region, Field< TData, TState > *field, const unsigned int num_components, const unsigned int num_homo_modes)
Definition Block.hpp:160
BlockAccessor(const BlockAccessor &)=delete
Constructor methods - No copy methods.
size_t GetAlignment() const
Gets the alignment of the memory region block.
Definition Block.hpp:239
BlockAccessor(BlockAccessor &&rhs)
Constructor methods - move from another BlockAccessor.
Definition Block.hpp:179
BlockAccessor(const BlockAttributes< TState > blockAttr, MemoryRegion< TData > &&memory_region, Field< TData, TState > *field, const unsigned int num_components, const unsigned int num_homo_modes)
Definition Block.hpp:149
const_if< std::is_same_v< MemAccess, ReadOnly >, TData >::type * GetPtr(const unsigned int streamID=0)
Get the pointer to the host/device memory.
Definition Block.cpp:43
A block means a group of elements of identical shape, basis and order. BlockAttributes stores the mos...
Definition Block.hpp:50
size_t GetNumPaddingElements(void) const
Definition Block.hpp:108
size_t GetNumElmtGroups(void) const
Definition Block.hpp:113
size_t GetNumElementsWithPadding(void) const
Definition Block.hpp:88
unsigned int GetInterleaveWidth(void) const
Definition Block.hpp:103
BlockAttributes(const size_t num_elements, const size_t num_elements_with_padding, const unsigned int num_data, const unsigned int interleave_width)
Definition Block.hpp:53
BlockAttributes(const BlockAttributes &)=default
unsigned int GetNumData(void) const
Definition Block.hpp:93
size_t GetNumElements(void) const
Definition Block.hpp:83
size_t GetNumElmtGroups(const unsigned int interleave_width) const
Definition Block.hpp:118
void SetInterleaveWidth(const unsigned interleave_width)
Definition Block.hpp:68
A Field represents expansion data to be operated on.
A MemoryRegion represents a memory region.
STL namespace.
typename abi< ScalarType, width >::type simd
Definition tinysimd.hpp:138