HILA
Loading...
Searching...
No Matches
memalloc.cpp
1#include "plumbing/memalloc.h"
2
3
4/// Memory allocator -- gives back aligned memory, if ALIGN defined
5/// This is a hacky method to insert automatic check to success of
6/// memalloc, so that we get the file name and line number where the
7/// the failure happened:
8
9
10void *memalloc(std::size_t size, const char *filename, const unsigned line) {
11
12#ifndef ALIGNED_MEMALLOC
13
14 void *p;
15
16 if ((p = std::malloc(size)) == nullptr) {
17 if (filename != nullptr) {
18 hila::out << " *** memalloc failure in file " << filename << " at line "
19 << line << '\n';
20 } else {
21 hila::out << " *** memalloc failure\n";
22 }
23 hila::out << " requested allocation size " << size << " bytes\n"
24 << " *********************************" << std::endl;
25
26 exit(1);
27 }
28 return p;
29
30#else
31
32 void *p;
33 // align to 32 bytes (parameter?)
34 // make size multiple of 32 too
35 size = ((size + 31) / 32) * 32;
36 int e = posix_memalign(&p, (std::size_t)32, size);
37 if (e != 0) {
38 if (filename != nullptr) {
39 hila::out << " *** memalloc failure in file " << filename << " at line "
40 << line << '\n';
41 } else {
42 hila::out << " *** memalloc failure\n";
43 }
44 hila::out << " requested allocation size " << size << " bytes\n"
45 << " posix_memalign() error code " << e
46 << " *********************************" << std::endl;
47 exit(1);
48 }
49 return p;
50
51#endif
52}
53
54// version without the filename
55void *memalloc(std::size_t size) { return memalloc(size, nullptr, 0); }
56
57
58/// d_malloc allocates memory from "device", either from
59
60void *d_malloc(std::size_t size) {
61
62#if !defined(CUDA) && !defined(HIP)
63
64 return memalloc(size);
65
66#else
67
68 void * p;
69 gpuMalloc(&p, size);
70 return p;
71
72#endif
73
74}
75
76void d_free(void *dptr) {
77
78#if !defined(CUDA) && !defined(HIP)
79
80 if (dptr != nullptr) free(dptr);
81
82#else
83
84 gpuFree(dptr);
85
86#endif
87
88}
std::ostream out
this is our default output file stream