Peano
Loading...
Searching...
No Matches
accelerator.cpp
Go to the documentation of this file.
1#include "accelerator.h"
2
3#include "tarch/Assertions.h"
4
5#include <cstring>
6
7std::string tarch::toString(MemoryLocation value) {
8 switch (value) {
9 case MemoryLocation::Heap:
10 return "heap";
11 case MemoryLocation::ManagedSharedAcceleratorDeviceMemory:
12 return "managed-shared-accelerator-device";
13 }
14 return "<undef>";
15}
16
17#if defined(GPUOffloadingOff)
18
19void* tarch::internal::allocateRawData(std::size_t size, [[maybe_unused]] MemoryLocation location, int) {
20 assertion2(size > 0, size, toString(location));
21 // Aligned alloc requires size to be a multiple of the alignment
22 void* data = std::aligned_alloc(
25 );
26 assertion(data);
27 return data;
28}
29
30void tarch::freeMemory(void* data, MemoryLocation, int) {
31 if (data!=nullptr) {
32 std::free(data);
33 }
34}
35
36void tarch::gpuAbort() { std::abort(); }
37
38#endif
39
40std::size_t tarch::padSizeToAlignment(std::size_t size, std::size_t alignment) {
41 assertion(size > 0);
42 assertion(alignment > 0);
43 // Aligned alloc requires size to be a multiple of alignment, therefore we
44 // need to check it. Also see:
45 // https://en.cppreference.com/w/c/memory/aligned_alloc
46 // Pad to nearest multiple of alignment.
47 int padding = size % alignment == 0 ? 0 : alignment - (size % alignment);
48 return size + padding;
49}
50
51#if defined(GPUOffloadingOMP)
52#pragma omp declare target
53#endif
54double* tarch::memset(double* dest, double ch, size_t byteCount){
55 #if defined(GPUOffloadingOff)
56 assertion2( byteCount%sizeof(double)==0, ch, byteCount );
57 #endif
58 #if defined(OpenMPManuallyOffloadMemset)
59 for (size_t i=0; i<byteCount/sizeof(double); i++){
60 dest[i] = ch;
61 }
62 return dest;
63 #else
64 return static_cast<double*>(
65 std::memset(
66 static_cast<void*>(dest),
67 //static_cast<int>(ch),
68 ch,
69 byteCount
70 )
71 );
72 #endif
73}
74#if defined(GPUOffloadingOMP)
75#pragma omp end declare target
76#endif
#define assertion2(expr, param0, param1)
#define assertion(expr)
#define AlignmentOnHeap
Definition LinuxAMD.h:23
std::string toString(Filter filter)
Definition convert.cpp:170
void * allocateRawData(std::size_t size, MemoryLocation location, int device)
double * memset(double *dest, double ch, size_t byteCount)
Alternative GPU-ready version of memset.
void freeMemory(void *data, MemoryLocation location, int device=accelerator::Device::HostDevice)
Free memory.
void gpuAbort()
Delegates to std::abort() if no GPU offloading is active.
std::size_t padSizeToAlignment(std::size_t size, std::size_t alignment)
std::string toString(MemoryLocation value)
MemoryLocation
Definition accelerator.h:58