FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
memory_aux.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
11#include <kernel/util/cuda_util.hpp>
12
13// includes, system
14#include <memory>
15
16namespace FEAT
17{
21 namespace Memory
22 {
34 void* alloc_main(std::size_t bytes);
35
44 void free_main(void* ptr);
45
61 void memset_main(void* ptr, int value, std::size_t bytes);
62
75 void memset_random_main(void* ptr, std::size_t bytes, std::uint64_t seed = 0u);
76
95 void memcopy_main(void* dst, const void* src, std::size_t bytes);
96
98 template<typename T_>
99 inline void convert_main(T_* dst, const T_* src, const Index count)
100 {
101 memcopy_main(dst, src, std::size_t(count) * sizeof(T_));
102 }
103
120 template<typename Tdst_, typename Tsrc_>
121 inline void convert_main(Tdst_* dst, const Tsrc_* src, const Index count)
122 {
123 if(count <= Index(0))
124 return;
125
126 XASSERT(dst != nullptr);
127 XASSERT(src != nullptr);
128
129 // check for overlapping memory regions
130 {
131 const char* vdst = reinterpret_cast<const char*>(dst);
132 const char* vsrc = reinterpret_cast<const char*>(src);
133 XASSERTM((vdst < vsrc) || (vsrc + std::size_t(count)*sizeof(Tsrc_) <= vdst), "source and destination memory regions overlap");
134 XASSERTM((vsrc < vdst) || (vdst + std::size_t(count)*sizeof(Tdst_) <= vsrc), "source and destination memory regions overlap");
135 }
136
137 for(Index i = 0u; i < count; ++i)
138 dst[i] = Tdst_(src[i]);
139 }
140
153 template<typename T_>
154 inline void format_main(T_* dst, const T_& value, const Index count)
155 {
156 if(count <= Index(0))
157 return;
158
159 XASSERT(dst != nullptr);
160
161 for(Index i = 0u; i < count; ++i)
162 dst[i] = value;
163 }
164
168
169#if defined(FEAT_HAVE_CUDA) || defined(DOXYGEN)
181 void* alloc_cuda(std::size_t bytes);
182
197 void* alloc_cuda_async(std::size_t bytes, const Util::StreamWrapper& stream = Util::StreamWrapper{});
198
207 void free_cuda(void* ptr);
208
217 void free_cuda_async(void* ptr, const Util::StreamWrapper& stream = Util::StreamWrapper{});
218
234 void memset_cuda(void* ptr, int value, std::size_t bytes, const Util::StreamWrapper& stream = Util::StreamWrapper{});
235
254 void memcopy_cuda(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream = Util::StreamWrapper{});
255
256
271 void memcopy_cuda_to_main(void* dst, const void* src, std::size_t bytes);
272
287 void memcopy_main_to_cuda(void* dst, const void* src, std::size_t bytes, const Util::StreamWrapper& stream = Util::StreamWrapper{});
288
315 template<typename Tdst_, typename Tsrc_>
316 void convert_cuda(Tdst_* dst, const Tsrc_* src, const Index count);
317
319 template<typename T_>
320 inline void convert_cuda(T_* dst, const T_* src, const Index count)
321 {
322 memcopy_cuda(dst, src, std::size_t(count) * sizeof(T_));
323 }
324
337 template<typename T_>
338 void format_cuda(T_* dst, const T_& value, const Index count);
339
341#else // no CUDA
343
344 inline void* alloc_cuda(std::size_t)
345 {
346 XABORTM("Memory::alloc_cuda: CUDA is not available");
347 }
348
349 inline void free_cuda(void*)
350 {
351 XABORTM("Memory::free_cuda: CUDA is not available");
352 }
353
354 inline void memset_cuda(void*, int, std::size_t)
355 {
356 XABORTM("Memory::memset_cuda: CUDA is not available");
357 }
358
359 inline void memcopy_cuda(void*, const void*, std::size_t)
360 {
361 XABORTM("Memory::memcopy_cuda: CUDA is not available");
362 }
363
364 inline void memcopy_cuda_to_main(void*, const void*, std::size_t)
365 {
366 XABORTM("Memory::memcopy_cuda_to_main: CUDA is not available");
367 }
368
369 inline void memcopy_main_to_cuda(void*, const void*, std::size_t)
370 {
371 XABORTM("Memory::memcopy_main_to_cuda: CUDA is not available");
372 }
373
374 template<typename Tdst_, typename Tsrc_>
375 inline void convert_cuda(Tdst_*, const Tsrc_*, Index)
376 {
377 XABORTM("Memory::convert_cuda: CUDA is not available");
378 }
379
380 template<typename T_>
381 void format_cuda(T_*, const T_&, const Index)
382 {
383 XABORTM("Memory::format_cuda: CUDA is not available");
384 }
385#endif // defined(FEAT_HAVE_CUDA) || defined(DOXYGEN)
386 } // namespace Memory
387} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
void memcopy_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a main memory block into another main memory block.
void format_cuda(T_ *dst, const T_ &value, const Index count)
Sets all entries of a cuda memory array to a given value.
void memcopy_main_to_cuda(void *dst, const void *src, std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
Copies a region of a main memory block into a cuda memory block.
void convert_cuda(Tdst_ *dst, const Tsrc_ *src, const Index count)
Converts the elements of an array into element of another array of a different data type.
void memcopy_cuda_to_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a cuda memory block into a main memory block.
void memset_main(void *ptr, int value, std::size_t bytes)
Formats all bytes of a memory block in main memory to a given value.
void * alloc_cuda(std::size_t bytes)
Allocate a memory block in cuda heap memory.
void memcopy_cuda(void *dst, const void *src, std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
Copies a region of a cuda memory block into another cuda memory block.
void memset_cuda(void *ptr, int value, std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
Formats all bytes of a memory block in cuda memory to a given value.
void free_main(void *ptr)
Frees a memory block in main heap memory allocated by alloc_main.
void convert_main(T_ *dst, const T_ *src, const Index count)
overload for Tdst_ = Tsrc_; performs a memcopy internally
Definition: memory_aux.hpp:99
void free_cuda(void *ptr)
Frees a memory block in cuda heap memory allocated by alloc_cuda.
void memset_random_main(void *ptr, std::size_t bytes, std::uint64_t seed=0u)
Formats all bytes of a memory block in main memory to random values.
void free_cuda_async(void *ptr, const Util::StreamWrapper &stream=Util::StreamWrapper{})
Frees a memory block in cuda heap memory allocated by alloc_cuda.
void * alloc_main(std::size_t bytes)
Allocate a memory block in main heap memory.
void format_main(T_ *dst, const T_ &value, const Index count)
Sets all entries of a main memory array to a given value.
Definition: memory_aux.hpp:154
void * alloc_cuda_async(std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
Allocates stream ordered memory block in cuda heap memory.
FEAT namespace.
Definition: adjactor.hpp:12
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.