Skip to content

Commit b045f72

Browse files
committed
Define a struct for the contexts and use it everywhere.
1 parent a07cc4a commit b045f72

16 files changed

Lines changed: 152 additions & 148 deletions

src/gpuarray/array.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ static inline int GpuArray_CHKFLAGS(const GpuArray *a, int flags) {
219219
* left uninitialized.
220220
*/
221221
GPUARRAY_PUBLIC int GpuArray_empty(GpuArray *a, const gpuarray_buffer_ops *ops,
222-
void *ctx, int typecode, unsigned int nd,
223-
const size_t *dims, ga_order ord);
222+
gpucontext *ctx, int typecode, unsigned int nd,
223+
const size_t *dims, ga_order ord);
224224

225225
/**
226226
* Initialize and allocate a new zero-initialized array.
@@ -241,8 +241,8 @@ GPUARRAY_PUBLIC int GpuArray_empty(GpuArray *a, const gpuarray_buffer_ops *ops,
241241
* left uninitialized.
242242
*/
243243
GPUARRAY_PUBLIC int GpuArray_zeros(GpuArray *a, const gpuarray_buffer_ops *ops,
244-
void *ctx, int typecode, unsigned int nd,
245-
const size_t *dims, ga_order ord);
244+
gpucontext *ctx, int typecode, unsigned int nd,
245+
const size_t *dims, ga_order ord);
246246

247247
/**
248248
* Initialize and allocate a new array structure from a pre-existing buffer.
@@ -268,17 +268,17 @@ GPUARRAY_PUBLIC int GpuArray_zeros(GpuArray *a, const gpuarray_buffer_ops *ops,
268268
* is left uninitialized and the provided buffer is deallocated.
269269
*/
270270
GPUARRAY_PUBLIC int GpuArray_fromdata(GpuArray *a,
271-
const gpuarray_buffer_ops *ops,
272-
gpudata *data, size_t offset,
273-
int typecode, unsigned int nd,
274-
const size_t *dims,
275-
const ssize_t *strides, int writeable);
271+
const gpuarray_buffer_ops *ops,
272+
gpudata *data, size_t offset,
273+
int typecode, unsigned int nd,
274+
const size_t *dims,
275+
const ssize_t *strides, int writeable);
276276

277277
GPUARRAY_PUBLIC int GpuArray_copy_from_host(GpuArray *a,
278-
const gpuarray_buffer_ops *ops,
279-
void *ctx, void *buf, int typecode,
280-
unsigned int nd, const size_t *dims,
281-
const ssize_t *strides);
278+
const gpuarray_buffer_ops *ops,
279+
gpucontext *ctx, void *buf, int typecode,
280+
unsigned int nd, const size_t *dims,
281+
const ssize_t *strides);
282282

283283
/**
284284
* Initialize an array structure to provide a view of another.
@@ -464,7 +464,7 @@ GPUARRAY_PUBLIC int GpuArray_share(const GpuArray *a, const GpuArray *b);
464464
*
465465
* \returns the context in which `a` was allocated.
466466
*/
467-
GPUARRAY_PUBLIC void *GpuArray_context(const GpuArray *a);
467+
GPUARRAY_PUBLIC gpucontext *GpuArray_context(const GpuArray *a);
468468

469469
/**
470470
* Copies all the elements of and array to another.
@@ -491,7 +491,7 @@ GPUARRAY_PUBLIC int GpuArray_move(GpuArray *dst, const GpuArray *src);
491491
* \return an error code otherwise
492492
*/
493493
GPUARRAY_PUBLIC int GpuArray_write(GpuArray *dst, const void *src,
494-
size_t src_sz);
494+
size_t src_sz);
495495

496496
/**
497497
* Copy data from the device memory to the host memory.
@@ -548,9 +548,9 @@ GPUARRAY_PUBLIC int GpuArray_copy(GpuArray *res, const GpuArray *a,
548548
* \return an error code otherwise
549549
*/
550550
GPUARRAY_PUBLIC int GpuArray_transfer(GpuArray *res, const GpuArray *a,
551-
void *new_ctx,
552-
const gpuarray_buffer_ops *new_ops,
553-
int may_share);
551+
gpucontext *new_ctx,
552+
const gpuarray_buffer_ops *new_ops,
553+
int may_share);
554554

555555
/**
556556
* Split an array into multiple views.

src/gpuarray/buffer.h

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,26 @@ extern "C" {
2222
#ifdef CONFUSE_EMACS
2323
}
2424
#endif
25-
2625
struct _gpudata;
2726

27+
#define GPUCONTEXT_HEAD \
28+
void *blas_handle; \
29+
unsigned int refcnt; \
30+
int flags; \
31+
struct _gpudata *errbuf; \
32+
char bin_id[64]; \
33+
char tag[8]
34+
35+
/**
36+
* Partially opaque struct for context data.
37+
*/
38+
typedef struct _gpucontext {
39+
GPUCONTEXT_HEAD;
40+
void *ctx_ptr;
41+
void *private[7];
42+
} gpucontext;
43+
44+
2845
/**
2946
* Opaque struct for buffer data.
3047
*/
@@ -37,11 +54,14 @@ struct _gpukernel;
3754
*/
3855
typedef struct _gpukernel gpukernel;
3956

57+
struct _gpuarray_buffer_ops;
4058
/**
4159
* Function table that a backend must provide.
4260
* \headerfile gpuarray/buffer.h
4361
*/
44-
typedef struct _gpuarray_buffer_ops {
62+
typedef struct _gpuarray_buffer_ops gpuarray_buffer_ops;
63+
64+
struct _gpuarray_buffer_ops {
4565
/**
4666
* Create a context on the specified device.
4767
*
@@ -55,7 +75,7 @@ typedef struct _gpuarray_buffer_ops {
5575
* \returns An opaque pointer to the created context or NULL if an
5676
* error occured.
5777
*/
58-
void *(*buffer_init)(int dev, int flags, int *ret);
78+
gpucontext *(*buffer_init)(int dev, int flags, int *ret);
5979

6080
/**
6181
* \defgroup context_flags Context flags
@@ -108,7 +128,7 @@ typedef struct _gpuarray_buffer_ops {
108128
*
109129
* \param ctx a valid context pointer.
110130
*/
111-
void (*buffer_deinit)(void *ctx);
131+
void (*buffer_deinit)(gpucontext *ctx);
112132

113133
/**
114134
* Allocates a buffer of size `sz` in context `ctx`.
@@ -126,7 +146,7 @@ typedef struct _gpuarray_buffer_ops {
126146
* structure is intentionally opaque as its content may change
127147
* according to the backend used.
128148
*/
129-
gpudata *(*buffer_alloc)(void *ctx, size_t sz, void *data, int flags,
149+
gpudata *(*buffer_alloc)(gpucontext *ctx, size_t sz, void *data, int flags,
130150
int *ret);
131151

132152
/**
@@ -308,7 +328,7 @@ typedef struct _gpuarray_buffer_ops {
308328
* \returns Allocated kernel structure or NULL if an error occured.
309329
* `ret` will be updated with the error code if not NULL.
310330
*/
311-
gpukernel *(*kernel_alloc)(void *ctx, unsigned int count,
331+
gpukernel *(*kernel_alloc)(gpucontext *ctx, unsigned int count,
312332
const char **strings, const size_t *lengths,
313333
const char *fname, unsigned int numargs,
314334
const int *typecodes, int flags, int *ret, char **err_str);
@@ -453,7 +473,7 @@ typedef struct _gpuarray_buffer_ops {
453473
* transfer could be found.
454474
*/
455475
gpudata *(*buffer_transfer)(gpudata *src, size_t offset, size_t sz,
456-
void *dst_ctx, int may_share);
476+
gpucontext *dst_ctx, int may_share);
457477

458478
/**
459479
* Fetch a property.
@@ -471,7 +491,7 @@ typedef struct _gpuarray_buffer_ops {
471491
*
472492
* \returns GA_NO_ERROR or an error code if an error occurred.
473493
*/
474-
int (*property)(void *ctx, gpudata *buf, gpukernel *k, int prop_id,
494+
int (*property)(gpucontext *ctx, gpudata *buf, gpukernel *k, int prop_id,
475495
void *res);
476496

477497
/**
@@ -488,8 +508,8 @@ typedef struct _gpuarray_buffer_ops {
488508
*
489509
* \returns string description of the last error
490510
*/
491-
const char *(*ctx_error)(void *ctx);
492-
} gpuarray_buffer_ops;
511+
const char *(*ctx_error)(gpucontext *ctx);
512+
};
493513

494514
/**
495515
* \defgroup props Properties
@@ -640,7 +660,7 @@ typedef struct _gpuarray_buffer_ops {
640660
/**
641661
* Get the context in which this buffer was allocated.
642662
*
643-
* Type: `void *`
663+
* Type: `gpucontext *`
644664
*/
645665
#define GA_BUFFER_PROP_CTX 512
646666

@@ -665,7 +685,7 @@ typedef struct _gpuarray_buffer_ops {
665685
/**
666686
* Get the context for which this kernel was compiled.
667687
*
668-
* Type: `void *`
688+
* Type: `gpucontext *`
669689
*/
670690
#define GA_KERNEL_PROP_CTX 1024
671691

@@ -769,8 +789,8 @@ typedef enum _ga_usefl {
769789
*
770790
* \returns A string description of the error.
771791
*/
772-
GPUARRAY_PUBLIC const char *Gpu_error(const gpuarray_buffer_ops *o, void *ctx,
773-
int err);
792+
GPUARRAY_PUBLIC const char *Gpu_error(const gpuarray_buffer_ops *o,
793+
gpucontext *ctx, int err);
774794
/**
775795
* Get operations vector for a backend.
776796
*
@@ -790,14 +810,14 @@ GPUARRAY_PUBLIC const gpuarray_buffer_ops *gpuarray_get_ops(const char *name);
790810
* can be done.
791811
*/
792812
GPUARRAY_PUBLIC gpudata *gpuarray_buffer_transfer(gpudata *buf, size_t offset,
793-
size_t sz, void *src_ctx,
813+
size_t sz, gpucontext *src_ctx,
794814
const gpuarray_buffer_ops *src_ops,
795-
void *dst_ctx,
815+
gpucontext *dst_ctx,
796816
const gpuarray_buffer_ops *dst_ops,
797817
int may_share, int *ret);
798818

799-
GPUARRAY_PUBLIC void *gpuarray_buffer_context(const gpuarray_buffer_ops *ops,
800-
gpudata *b);
819+
GPUARRAY_PUBLIC gpucontext *gpuarray_buffer_context(const gpuarray_buffer_ops *ops,
820+
gpudata *b);
801821

802822
#ifdef __cplusplus
803823
}

src/gpuarray/buffer_blas.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ typedef enum _cb_uplo {
3333
} cb_uplo;
3434

3535
typedef struct _gpuarray_blas_ops {
36-
int (*setup)(void *ctx);
37-
void (*teardown)(void *ctx);
36+
int (*setup)(gpucontext *ctx);
37+
void (*teardown)(gpucontext *ctx);
3838
int (*hgemv)(cb_order order, cb_transpose transA, size_t M, size_t N,
3939
float alpha, gpudata *A, size_t offA, size_t lda,
4040
gpudata *X, size_t offX, int incX, float beta,

src/gpuarray/elemwise.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typedef struct _gpuelemwise_arg {
6868
} gpuelemwise_arg;
6969

7070
GPUARRAY_PUBLIC GpuElemwise *GpuElemwise_new(const gpuarray_buffer_ops *ops,
71-
void * ctx,
71+
gpucontext *ctx,
7272
const char *preamble,
7373
const char *expr,
7474
unsigned int n,

src/gpuarray/ext_cuda.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
extern "C" {
1212
#endif
1313

14-
static void (*cuda_enter)(void *);
15-
static void (*cuda_exit)(void *);
16-
static void *(*cuda_make_ctx)(CUcontext, int);
17-
static CUcontext (*cuda_get_ctx)(void *);
14+
static void (*cuda_enter)(gpucontext *);
15+
static void (*cuda_exit)(gpucontext *);
16+
static gpucontext *(*cuda_make_ctx)(CUcontext, int);
1817
static CUstream (*cuda_get_stream)(void *);
1918
static gpudata *(*cuda_make_buf)(void *, CUdeviceptr, size_t);
2019
static CUdeviceptr (*cuda_get_ptr)(gpudata *);
@@ -24,10 +23,9 @@ static int (*cuda_record)(gpudata *, int);
2423

2524
static void setup_ext_cuda(void) {
2625
// The casts are necessary to reassure C++ compilers
27-
cuda_enter = (void (*)(void *))gpuarray_get_extension("cuda_enter");
28-
cuda_exit = (void (*)(void *))gpuarray_get_extension("cuda_exit");
29-
cuda_make_ctx = (void *(*)(CUcontext, int))gpuarray_get_extension("cuda_make_ctx");
30-
cuda_get_ctx = (CUcontext (*)(void *))gpuarray_get_extension("cuda_get_ctx");
26+
cuda_enter = (void (*)(gpucontext *))gpuarray_get_extension("cuda_enter");
27+
cuda_exit = (void (*)(gpucontext *))gpuarray_get_extension("cuda_exit");
28+
cuda_make_ctx = (gpucontext *(*)(CUcontext, int))gpuarray_get_extension("cuda_make_ctx");
3129
cuda_get_stream = (CUstream (*)(void *))gpuarray_get_extension("cuda_get_stream");
3230
cuda_make_buf = (gpudata *(*)(void *, CUdeviceptr, size_t))gpuarray_get_extension("cuda_make_buf");
3331
cuda_get_ptr = (CUdeviceptr (*)(gpudata *))gpuarray_get_extension("cuda_get_ptr");

src/gpuarray/kernel.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ typedef struct _GpuKernel {
5555
* \return any other value if an error occured
5656
*/
5757
GPUARRAY_PUBLIC int GpuKernel_init(GpuKernel *k, const gpuarray_buffer_ops *ops,
58-
void *ctx, unsigned int count,
59-
const char **strs, const size_t *lens,
60-
const char *name, unsigned int argcount,
61-
const int *types, int flags, char **err_str);
58+
gpucontext *ctx, unsigned int count,
59+
const char **strs, const size_t *lens,
60+
const char *name, unsigned int argcount,
61+
const int *types, int flags, char **err_str);
6262

6363
/**
6464
* Clear and release data associated with a kernel.
@@ -74,7 +74,7 @@ GPUARRAY_PUBLIC void GpuKernel_clear(GpuKernel *k);
7474
*
7575
* \returns a context pointer
7676
*/
77-
GPUARRAY_PUBLIC void *GpuKernel_context(GpuKernel *k);
77+
GPUARRAY_PUBLIC gpucontext *GpuKernel_context(GpuKernel *k);
7878

7979

8080
GPUARRAY_PUBLIC int GpuKernel_setarg(GpuKernel *k, unsigned int i, void *val);

src/gpuarray_array.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void ga_boundaries(size_t *start, size_t *end, size_t offset,
5555
/* Value below which a size_t multiplication will never overflow. */
5656
#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
5757

58-
int GpuArray_empty(GpuArray *a, const gpuarray_buffer_ops *ops, void *ctx,
58+
int GpuArray_empty(GpuArray *a, const gpuarray_buffer_ops *ops, gpucontext *ctx,
5959
int typecode, unsigned int nd, const size_t *dims,
6060
ga_order ord) {
6161
size_t size = gpuarray_get_elsize(typecode);
@@ -124,7 +124,7 @@ int GpuArray_empty(GpuArray *a, const gpuarray_buffer_ops *ops, void *ctx,
124124
return GA_NO_ERROR;
125125
}
126126

127-
int GpuArray_zeros(GpuArray *a, const gpuarray_buffer_ops *ops, void *ctx,
127+
int GpuArray_zeros(GpuArray *a, const gpuarray_buffer_ops *ops, gpucontext *ctx,
128128
int typecode, unsigned int nd, const size_t *dims,
129129
ga_order ord) {
130130
int err;
@@ -169,7 +169,7 @@ int GpuArray_fromdata(GpuArray *a, const gpuarray_buffer_ops *ops,
169169
}
170170

171171
int GpuArray_copy_from_host(GpuArray *a, const gpuarray_buffer_ops *ops,
172-
void *ctx, void *buf, int typecode,
172+
gpucontext *ctx, void *buf, int typecode,
173173
unsigned int nd, const size_t *dims,
174174
const ssize_t *strides) {
175175
char *base = (char *)buf;
@@ -309,7 +309,7 @@ int GpuArray_index(GpuArray *r, const GpuArray *a, const ssize_t *starts,
309309
}
310310

311311
static int gen_take1_kernel(GpuKernel *k, const gpuarray_buffer_ops *ops,
312-
void *ctx, char **err_str,
312+
gpucontext *ctx, char **err_str,
313313
GpuArray *a, const GpuArray *v,
314314
const GpuArray *ind, int addr32) {
315315
strb sb = STRB_STATIC_INIT;
@@ -807,8 +807,8 @@ int GpuArray_share(const GpuArray *a, const GpuArray *b) {
807807
return a->ops->buffer_share(a->data, b->data, NULL);
808808
}
809809

810-
void *GpuArray_context(const GpuArray *a) {
811-
void *res = NULL;
810+
gpucontext *GpuArray_context(const GpuArray *a) {
811+
gpucontext *res = NULL;
812812
(void)a->ops->property(NULL, a->data, NULL, GA_BUFFER_PROP_CTX, &res);
813813
return res;
814814
}
@@ -873,7 +873,7 @@ int GpuArray_copy(GpuArray *res, const GpuArray *a, ga_order order) {
873873
return err;
874874
}
875875

876-
int GpuArray_transfer(GpuArray *res, const GpuArray *a, void *new_ctx,
876+
int GpuArray_transfer(GpuArray *res, const GpuArray *a, gpucontext *new_ctx,
877877
const gpuarray_buffer_ops *new_ops, int may_share) {
878878
size_t start, end;
879879
gpudata *tmp;
@@ -1018,7 +1018,7 @@ int GpuArray_concatenate(GpuArray *r, const GpuArray **as, size_t n,
10181018
}
10191019

10201020
const char *GpuArray_error(const GpuArray *a, int err) {
1021-
void *ctx;
1021+
gpucontext *ctx;
10221022
int err2 = a->ops->property(NULL, a->data, NULL, GA_BUFFER_PROP_CTX, &ctx);
10231023
if (err2 != GA_NO_ERROR) {
10241024
/* If CUDA refuses to work after any kind of error in kernels

0 commit comments

Comments
 (0)