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