-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathgpuarray_buffer.c
More file actions
269 lines (228 loc) · 7.81 KB
/
gpuarray_buffer.c
File metadata and controls
269 lines (228 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "gpuarray/buffer.h"
#include "gpuarray/buffer_collectives.h"
#include "gpuarray/error.h"
#include "util/error.h"
#include "private.h"
extern const gpuarray_buffer_ops cuda_ops;
extern const gpuarray_buffer_ops opencl_ops;
const gpuarray_buffer_ops *gpuarray_get_ops(const char *name) {
if (strcmp("cuda", name) == 0) return &cuda_ops;
if (strcmp("opencl", name) == 0) return &opencl_ops;
return NULL;
}
#define FAIL(v, e) { if (ret) *ret = (e)->code; return v; }
int gpu_get_platform_count(const char* name, unsigned int* platcount) {
const gpuarray_buffer_ops* ops = gpuarray_get_ops(name);
if (ops == NULL) {
return error_set(global_err, GA_INVALID_ERROR, "Invalid platform");
}
return ops->get_platform_count(platcount);
}
int gpu_get_device_count(const char* name, unsigned int platform,
unsigned int* devcount) {
const gpuarray_buffer_ops* ops = gpuarray_get_ops(name);
if (ops == NULL) {
return error_set(global_err, GA_INVALID_ERROR, "Invalid platform");
}
return ops->get_device_count(platform, devcount);
}
int gpucontext_props_new(gpucontext_props **res) {
gpucontext_props *r = calloc(1, sizeof(gpucontext_props));
if (r == NULL) return error_sys(global_err, "calloc");
r->dev = -1;
r->sched = GA_CTX_SCHED_AUTO;
r->flags = 0;
r->kernel_cache_path = NULL;
r->initial_cache_size = 0;
r->max_cache_size = (size_t)-1;
*res = r;
return GA_NO_ERROR;
}
int gpucontext_props_cuda_dev(gpucontext_props *p, int devno) {
p->dev = devno;
return GA_NO_ERROR;
}
int gpucontext_props_opencl_dev(gpucontext_props *p, int platno, int devno) {
p->dev = (platno << 16) | devno;
return GA_NO_ERROR;
}
int gpucontext_props_sched(gpucontext_props *p, int sched) {
switch (sched) {
case GA_CTX_SCHED_MULTI:
case GA_CTX_SCHED_AUTO:
case GA_CTX_SCHED_SINGLE:
p->sched = sched;
break;
default:
return error_fmt(global_err, GA_INVALID_ERROR, "Invalid value for sched: %d", sched);
}
if (sched == GA_CTX_SCHED_MULTI)
FLSET(p->flags, GA_CTX_MULTI_THREAD);
else
FLCLR(p->flags, GA_CTX_MULTI_THREAD);
return GA_NO_ERROR;
}
int gpucontext_props_set_single_stream(gpucontext_props *p) {
p->flags |= GA_CTX_SINGLE_STREAM;
return GA_NO_ERROR;
}
int gpucontext_props_kernel_cache(gpucontext_props *p, const char *path) {
p->kernel_cache_path = path;
return GA_NO_ERROR;
}
int gpucontext_props_alloc_cache(gpucontext_props *p, size_t initial, size_t max) {
if (initial > max)
return error_set(global_err, GA_VALUE_ERROR, "Initial size can't be bigger than max size");
p->initial_cache_size = initial;
p->max_cache_size = max;
return GA_NO_ERROR;
}
void gpucontext_props_del(gpucontext_props *p) {
free(p);
}
int gpucontext_init(gpucontext **res, const char *name, gpucontext_props *p) {
const gpuarray_buffer_ops *ops = gpuarray_get_ops(name);
gpucontext *r;
if (ops == NULL) {
gpucontext_props_del(p);
return global_err->code;
}
if (p == NULL && gpucontext_props_new(&p) != GA_NO_ERROR)
return global_err->code;
r = ops->buffer_init(p);
gpucontext_props_del(p);
if (r == NULL) return global_err->code;
r->ops = ops;
r->extcopy_cache = NULL;
*res = r;
return GA_NO_ERROR;
}
void gpucontext_deref(gpucontext *ctx) {
if (ctx->blas_handle != NULL)
ctx->blas_ops->teardown(ctx);
if (ctx->extcopy_cache != NULL) {
cache_destroy(ctx->extcopy_cache);
ctx->extcopy_cache = NULL;
}
ctx->ops->buffer_deinit(ctx);
}
int gpucontext_property(gpucontext *ctx, int prop_id, void *res) {
return ctx->ops->property(ctx, NULL, NULL, prop_id, res);
}
const char *gpucontext_error(gpucontext *ctx, int err) {
if (ctx == NULL)
return global_err->msg;
else
return ctx->ops->ctx_error(ctx);
}
gpudata *gpudata_alloc(gpucontext *ctx, size_t sz, void *data, int flags,
int *ret) {
gpudata *res = ctx->ops->buffer_alloc(ctx, sz, data, flags);
if (res == NULL && ret) *ret = ctx->err->code;
return res;
}
void gpudata_retain(gpudata *b) {
((partial_gpudata *)b)->ctx->ops->buffer_retain(b);
}
void gpudata_release(gpudata *b) {
if (b)
((partial_gpudata *)b)->ctx->ops->buffer_release(b);
}
int gpudata_share(gpudata *a, gpudata *b, int *ret) {
int res = ((partial_gpudata *)a)->ctx->ops->buffer_share(a, b);
if (res == -1 && ret)
*ret = ((partial_gpudata *)a)->ctx->err->code;
return res;
}
int gpudata_move(gpudata *dst, size_t dstoff, gpudata *src, size_t srcoff,
size_t sz) {
return ((partial_gpudata *)src)->ctx->ops->buffer_move(dst, dstoff,
src, srcoff, sz);
}
int gpudata_transfer(gpudata *dst, size_t dstoff, gpudata *src, size_t srcoff,
size_t sz) {
gpucontext *src_ctx;
gpucontext *dst_ctx;
void *tmp;
int res;
src_ctx = ((partial_gpudata *)src)->ctx;
dst_ctx = ((partial_gpudata *)dst)->ctx;
if (src_ctx == dst_ctx)
return src_ctx->ops->buffer_move(dst, dstoff, src, srcoff, sz);
if (src_ctx->ops == dst_ctx->ops) {
res = src_ctx->ops->buffer_transfer(dst, dstoff, src, srcoff, sz);
if (res == GA_NO_ERROR)
return res;
}
/* Fallback to host copy */
tmp = malloc(sz);
if (tmp == NULL) {
error_sys(src_ctx->err, "malloc");
return error_sys(dst_ctx->err, "malloc");
}
res = src_ctx->ops->buffer_read(tmp, src, srcoff, sz);
if (res != GA_NO_ERROR) {
free(tmp);
return res;
}
res = dst_ctx->ops->buffer_write(dst, dstoff, tmp, sz);
free(tmp);
return res;
}
int gpudata_read(void *dst, gpudata *src, size_t srcoff, size_t sz) {
return ((partial_gpudata *)src)->ctx->ops->buffer_read(dst, src, srcoff, sz);
}
int gpudata_write(gpudata *dst, size_t dstoff, const void *src, size_t sz) {
return ((partial_gpudata *)dst)->ctx->ops->buffer_write(dst, dstoff,
src, sz);
}
int gpudata_memset(gpudata *dst, size_t dstoff, int data) {
return ((partial_gpudata *)dst)->ctx->ops->buffer_memset(dst, dstoff, data);
}
int gpudata_sync(gpudata *b) {
return ((partial_gpudata *)b)->ctx->ops->buffer_sync(b);
}
int gpudata_property(gpudata *b, int prop_id, void *res) {
return ((partial_gpudata *)b)->ctx->ops->property(NULL, b, NULL, prop_id,
res);
}
gpukernel *gpukernel_init(gpucontext *ctx, unsigned int count,
const char **strings, const size_t *lengths,
const char *fname, unsigned int numargs,
const int *typecodes, int flags, int *ret,
char **err_str) {
gpukernel *res = NULL;
int err;
err = ctx->ops->kernel_alloc(&res, ctx, count, strings, lengths, fname,
numargs, typecodes, flags, err_str);
if (err != GA_NO_ERROR && ret != NULL)
*ret = ctx->err->code;
return res;
}
void gpukernel_retain(gpukernel *k) {
((partial_gpukernel *)k)->ctx->ops->kernel_retain(k);
}
void gpukernel_release(gpukernel *k) {
((partial_gpukernel *)k)->ctx->ops->kernel_release(k);
}
int gpukernel_setarg(gpukernel *k, unsigned int i, void *a) {
return ((partial_gpukernel *)k)->ctx->ops->kernel_setarg(k, i, a);
}
int gpukernel_call(gpukernel *k, unsigned int n, const size_t *gs,
const size_t *ls, size_t shared, void **args) {
return ((partial_gpukernel *)k)->ctx->ops->kernel_call(k, n, gs, ls,
shared, args);
}
int gpukernel_property(gpukernel *k, int prop_id, void *res) {
return ((partial_gpukernel *)k)->ctx->ops->property(NULL, NULL, k, prop_id,
res);
}
gpucontext *gpudata_context(gpudata *b) {
return ((partial_gpudata *)b)->ctx;
}
gpucontext *gpukernel_context(gpukernel *k) {
return ((partial_gpukernel *)k)->ctx;
}