Commit | Line | Data |
---|---|---|
10988083 MW |
1 | // -------------------------------------------------------------------------- |
2 | // | |
3 | // Copyright | |
4 | // Markus Wittmann, 2016-2017 | |
5 | // RRZE, University of Erlangen-Nuremberg, Germany | |
6 | // markus.wittmann -at- fau.de or hpc -at- rrze.fau.de | |
7 | // | |
8 | // Viktor Haag, 2016 | |
9 | // LSS, University of Erlangen-Nuremberg, Germany | |
10 | // | |
8cafd9ea MW |
11 | // Michael Hussnaetter, 2017-2018 |
12 | // University of Erlangen-Nuremberg, Germany | |
13 | // michael.hussnaetter -at- fau.de | |
14 | // | |
10988083 MW |
15 | // This file is part of the Lattice Boltzmann Benchmark Kernels (LbmBenchKernels). |
16 | // | |
17 | // LbmBenchKernels is free software: you can redistribute it and/or modify | |
18 | // it under the terms of the GNU General Public License as published by | |
19 | // the Free Software Foundation, either version 3 of the License, or | |
20 | // (at your option) any later version. | |
21 | // | |
22 | // LbmBenchKernels is distributed in the hope that it will be useful, | |
23 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | // GNU General Public License for more details. | |
26 | // | |
27 | // You should have received a copy of the GNU General Public License | |
28 | // along with LbmBenchKernels. If not, see <http://www.gnu.org/licenses/>. | |
29 | // | |
30 | // -------------------------------------------------------------------------- | |
e3f82424 MW |
31 | // TODO: make configurable |
32 | #define HAVE_HUGE_PAGES | |
33 | ||
34 | ||
35 | #ifdef HAVE_HUGE_PAGES | |
36 | #define _BSD_SOURCE | |
37 | #endif | |
38 | ||
10988083 MW |
39 | #include <stdio.h> |
40 | #include <stdlib.h> | |
41 | #include <string.h> // strerror | |
42 | #include <errno.h> | |
43 | ||
e3f82424 | 44 | |
8cafd9ea MW |
45 | #ifdef HAVE_MEMKIND |
46 | #include <hbwmalloc.h> | |
47 | #endif | |
48 | ||
e3f82424 MW |
49 | #ifdef HAVE_HUGE_PAGES |
50 | #include <sys/mman.h> // madvise | |
51 | #endif | |
52 | ||
10988083 MW |
53 | #include "Base.h" |
54 | #include "Memory.h" | |
55 | ||
56 | ||
57 | int MemAlloc(void ** ptr, size_t bytesToAlloc) | |
58 | { | |
59 | void * tmpPtr; | |
60 | ||
61 | tmpPtr = malloc(bytesToAlloc); | |
62 | ||
63 | if (tmpPtr == NULL) { // && bytesToAlloc != 0) { | |
64 | Error("allocation of %lu bytes failed: %d - %s\n", bytesToAlloc, errno, strerror(errno)); | |
65 | exit(1); | |
66 | } | |
67 | ||
68 | *ptr = tmpPtr; | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | int MemAllocAligned(void ** ptr, size_t bytesToAlloc, size_t alignmentBytes) | |
74 | { | |
75 | int ret; | |
76 | ||
77 | ret = posix_memalign(ptr, alignmentBytes, bytesToAlloc); | |
78 | ||
79 | if (ret) { | |
80 | Error("allocation of %lu bytes aligned to %lu bytes failed: %d - %s\n", bytesToAlloc, alignmentBytes, errno, strerror(errno)); | |
81 | exit(1); | |
82 | } | |
83 | ||
e3f82424 MW |
84 | #ifdef HAVE_HUGE_PAGES |
85 | ||
86 | if (alignmentBytes % 4096 == 0) { | |
87 | ret = madvise(*ptr, bytesToAlloc, MADV_HUGEPAGE); | |
88 | ||
89 | if (ret != 0) { | |
90 | DEBUG_BREAK_POINT(); | |
91 | Error("madvise(%p, %lu, MADV_HUGEPAGE) failed: %d - %s.\n", *ptr, bytesToAlloc, errno, strerror(errno)); | |
92 | exit(1); | |
93 | } | |
94 | } | |
95 | #endif | |
96 | ||
10988083 MW |
97 | return 0; |
98 | } | |
99 | ||
100 | ||
101 | int MemFree(void ** ptr) | |
102 | { | |
103 | Assert(*ptr != NULL); | |
104 | ||
105 | free(*ptr); | |
106 | ||
107 | *ptr = NULL; | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
112 | int MemZero(void * ptr, size_t bytesToZero) | |
113 | { | |
114 | Assert(ptr != NULL); | |
115 | Assert(bytesToZero > 0); | |
116 | ||
117 | memset(ptr, 0, bytesToZero); | |
118 | ||
119 | return 0; | |
120 | } | |
8cafd9ea MW |
121 | |
122 | #ifdef HAVE_MEMKIND | |
123 | int HbwAlloc(void ** ptr, size_t bytesToAlloc) | |
124 | { | |
125 | void * tmpPtr; | |
126 | ||
127 | tmpPtr = hbw_malloc(bytesToAlloc); | |
128 | ||
129 | if (tmpPtr == NULL) { // && bytesToAlloc != 0) { | |
130 | Error("allocation of %lu bytes in HBM failed: %d - %s\n", bytesToAlloc, errno, strerror(errno)); | |
131 | exit(1); | |
132 | } | |
133 | ||
134 | *ptr = tmpPtr; | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | int HbwAllocAligned(void ** ptr, size_t bytesToAlloc, size_t alignmentBytes) | |
140 | { | |
141 | int ret; | |
142 | ||
143 | ret = hbw_posix_memalign(ptr, alignmentBytes, bytesToAlloc); | |
144 | ||
145 | if (ret) { | |
146 | Error("allocation of %lu bytes in HBM aligned to %lu bytes failed: %d - %s\n", bytesToAlloc, alignmentBytes, errno, strerror(errno)); | |
147 | exit(1); | |
148 | } | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
153 | ||
154 | int HbwFree(void ** ptr) | |
155 | { | |
156 | Assert(*ptr != NULL); | |
157 | ||
158 | hbw_free(*ptr); | |
159 | ||
160 | *ptr = NULL; | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
165 | #endif // HAVE_MEMKIND |