Yet Another eXchange Tool 0.11.1
Loading...
Searching...
No Matches
xt_gpu.c
Go to the documentation of this file.
1
12/*
13 * Keywords:
14 * Maintainer: Jörg Behrens <behrens@dkrz.de>
15 * Moritz Hanke <hanke@dkrz.de>
16 * Thomas Jahns <jahns@dkrz.de>
17 * URL: https://dkrz-sw.gitlab-pages.dkrz.de/yaxt/
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met:
22 *
23 * Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 *
26 * Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * Neither the name of the DKRZ GmbH nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
38 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 */
46#ifdef HAVE_CONFIG_H
47#include "config.h"
48#endif
49
50#include <string.h>
51
52#include "core/core.h"
53#include "core/ppm_xfuncs.h"
54#include "xt_gpu.h"
55
56
57static const char filename[] = "xt_gpu.c";
58
59#ifdef HAVE_CUDA
60# include "xt_cuda.h"
61#endif
62#ifdef _OPENACC
63# include "openacc.h"
64#endif
65
66static void * default_malloc(size_t alloc_size, enum xt_memtype memtype);
67static void default_free(void * ptr, enum xt_memtype memtype);
68static void default_memcpy(
69 void * dst, void const * src, size_t buffer_size,
70 enum xt_memtype dst_memtype, enum xt_memtype src_memtype);
71static enum xt_memtype default_get_memtype(const void *ptr);
72static int default_instr_push(char const *);
73static int default_instr_pop(void);
74
75static struct xt_gpu_vtable vtable = {
77 .Free = default_free,
78 .Memcpy = default_memcpy,
79 .Get_memtype = default_get_memtype,
80 .Instr_push = default_instr_push,
81 .Instr_pop = default_instr_pop,
82};
83
84#ifdef HAVE_CUDA
85static const struct xt_gpu_vtable *cuda_vtable;
86#endif
87
88#ifdef _OPENACC
89 /* better move to its own file? */
90static void *xt_acc_malloc(
91 size_t alloc_size, enum xt_memtype memtype) {
92
93 switch (memtype) {
94 default:
95 Xt_abort(
96 Xt_default_comm,
97 "ERROR(xt_cuda_malloc): unsupported memory type",
98 filename, __LINE__);
99 return NULL;
100 case (XT_MEMTYPE_HOST):
101 return xmalloc(alloc_size);
102 case (XT_MEMTYPE_DEVICE):
103 return acc_malloc(alloc_size + (alloc_size == 0));
104 }
105}
106
107static void xt_acc_free(void *ptr, enum xt_memtype memtype) {
108
109 switch (memtype) {
110 default:
111 Xt_abort(
112 Xt_default_comm,
113 "ERROR(xt_cuda_free): unsupported memory type",
114 filename, __LINE__);
115 break;
116 case (XT_MEMTYPE_HOST):
117 free(ptr);
118 break;
119 case (XT_MEMTYPE_DEVICE):
120 acc_free(ptr);
121 break;
122 }
123}
124#endif
125
126void xt_gpu_init(void) {
127
128#ifdef HAVE_CUDA
130 if (cuda_vtable != NULL) vtable = *cuda_vtable;
131#endif
132#ifdef _OPENACC
133 /* better make this configurable? */
134 vtable.Malloc = xt_acc_malloc;
135 vtable.Free = xt_acc_free;
136#endif
137}
138
139#define STR(s) #s
140#define CHECK_FOR_HOST_MEM(type) \
141 do { \
142 if (type != XT_MEMTYPE_HOST) { \
143 Xt_abort( \
144 Xt_default_comm, \
145 "ERROR(" STR(__func__) "): unsupported memory type", \
146 filename, __LINE__); \
147 } \
148 } while (0)
149
150static void * default_malloc(size_t alloc_size, enum xt_memtype memtype) {
151
152 CHECK_FOR_HOST_MEM(memtype);
153
154 return xmalloc(alloc_size);
155}
156
157static void default_free(void * ptr, enum xt_memtype memtype) {
158
159 CHECK_FOR_HOST_MEM(memtype);
160
161 free(ptr);
162}
163
164static void default_memcpy(
165 void * dst, void const * src, size_t buffer_size,
166 enum xt_memtype dst_memtype, enum xt_memtype src_memtype) {
167
168 CHECK_FOR_HOST_MEM(src_memtype);
169 CHECK_FOR_HOST_MEM(dst_memtype);
170
171 memcpy(dst, src, buffer_size);
172}
173
174static enum xt_memtype default_get_memtype(const void *ptr)
175{
176 (void)ptr;
177 return XT_MEMTYPE_HOST;
178}
179
180static int default_instr_push(char const * XT_UNUSED(name)) {return 0;}
181static int default_instr_pop(void) {return 0;}
182
183void * xt_gpu_malloc(size_t alloc_size, enum xt_memtype memtype) {
184 return vtable.Malloc(alloc_size, memtype);
185}
186
187void xt_gpu_free(void * ptr, enum xt_memtype memtype) {
188 vtable.Free(ptr, memtype);
189}
190
192 void * dst, void const * src, size_t buffer_size,
193 enum xt_memtype dst_memtype, enum xt_memtype src_memtype) {
194 vtable.Memcpy(dst, src, buffer_size, dst_memtype, src_memtype);
195}
196
197enum xt_memtype xt_gpu_get_memtype(const void *ptr) {
198 return vtable.Get_memtype(ptr);
199}
200
201int xt_gpu_instr_push(char const * name) {
202 return vtable.Instr_push(name);
203}
204
206 return vtable.Instr_pop();
207}
208
209/*
210 * Local Variables:
211 * c-basic-offset: 2
212 * coding: utf-8
213 * indent-tabs-mode: nil
214 * show-trailing-whitespace: t
215 * require-trailing-newline: t
216 * End:
217 */
#define XT_UNUSED(x)
Definition core.h:84
add versions of standard API functions not returning on error
#define xmalloc(size)
Definition ppm_xfuncs.h:70
void(* Memcpy)(void *dst, void const *src, size_t buffer_size, enum xt_memtype dst_memtype, enum xt_memtype src_memtype)
Definition xt_gpu.h:77
void(* Free)(void *ptr, enum xt_memtype memtype)
Definition xt_gpu.h:76
int(* Instr_pop)(void)
Definition xt_gpu.h:82
enum xt_memtype(* Get_memtype)(const void *ptr)
Definition xt_gpu.h:80
int(* Instr_push)(char const *name)
Definition xt_gpu.h:81
void *(* Malloc)(size_t alloc_size, enum xt_memtype memtype)
Definition xt_gpu.h:75
char name[32]
Definition xt_config.c:92
const struct xt_gpu_vtable * xt_cuda_init(void)
Definition xt_cuda.c:314
static struct xt_gpu_vtable const cuda_vtable
Definition xt_cuda.c:300
routines for using CUDA in yaxt
static const char filename[]
Definition xt_gpu.c:57
int xt_gpu_instr_pop(void)
Definition xt_gpu.c:205
static enum xt_memtype default_get_memtype(const void *ptr)
Definition xt_gpu.c:174
int xt_gpu_instr_push(char const *name)
Definition xt_gpu.c:201
static void * default_malloc(size_t alloc_size, enum xt_memtype memtype)
Definition xt_gpu.c:150
static void default_memcpy(void *dst, void const *src, size_t buffer_size, enum xt_memtype dst_memtype, enum xt_memtype src_memtype)
Definition xt_gpu.c:164
static struct xt_gpu_vtable vtable
Definition xt_gpu.c:75
void * xt_gpu_malloc(size_t alloc_size, enum xt_memtype memtype)
Definition xt_gpu.c:183
enum xt_memtype xt_gpu_get_memtype(const void *ptr)
Definition xt_gpu.c:197
void xt_gpu_init(void)
Definition xt_gpu.c:126
static int default_instr_push(char const *)
void xt_gpu_memcpy(void *dst, void const *src, size_t buffer_size, enum xt_memtype dst_memtype, enum xt_memtype src_memtype)
Definition xt_gpu.c:191
#define CHECK_FOR_HOST_MEM(type)
Definition xt_gpu.c:140
static void default_free(void *ptr, enum xt_memtype memtype)
Definition xt_gpu.c:157
static int default_instr_pop(void)
Definition xt_gpu.c:181
void xt_gpu_free(void *ptr, enum xt_memtype memtype)
Definition xt_gpu.c:187
routines for using GPU devices
xt_memtype
Definition xt_gpu.h:68
@ XT_MEMTYPE_HOST
Definition xt_gpu.h:69
@ XT_MEMTYPE_DEVICE
Definition xt_gpu.h:70