11#include <gpuarray/elemwise.h>
2+ #include <gpuarray/array.h>
3+ #include <gpuarray/error.h>
4+ #include <gpuarray/kernel.h>
5+ #include <gpuarray/util.h>
26
7+ #include "private.h"
38#include "util/strb.h"
49
510struct _GpuElemwise {
@@ -20,12 +25,12 @@ static inline const char *ctype(int typecode) {
2025/* dst has to be zero-initialized on entry */
2126static int copy_arg (gpuelemwise_arg * dst , gpuelemwise_arg * src ) {
2227 if (src -> dims != NULL ) {
23- dst -> dims = memdup (src -> dims , n * sizeof (size_t ));
28+ dst -> dims = memdup (src -> dims , src -> nd * sizeof (size_t ));
2429 if (dst -> dims == NULL ) goto fail_dims ;
2530 }
2631
2732 if (src -> strs != NULL ) {
28- dst -> strs = memdup (src -> strs , n * sizeof (ssize_t ));
33+ dst -> strs = memdup (src -> strs , src -> nd * sizeof (ssize_t ));
2934 if (dst -> strs == NULL ) goto fail_strs ;
3035 }
3136
@@ -54,12 +59,12 @@ static void clear_arg(gpuelemwise_arg *a) {
5459 a -> dims = NULL ;
5560 free (a -> strs );
5661 a -> strs = NULL ;
57- free (a -> name );
62+ free (( void * ) a -> name );
5863 a -> name = NULL ;
5964}
6065
61- static gpuelemwise_args * copy_args (unsigned int n , gpuelemwise_arg * a ) {
62- gpuelemwise_args * res = calloc (n , sizeof (gpuelemwise_arg ));
66+ static gpuelemwise_arg * copy_args (unsigned int n , gpuelemwise_arg * a ) {
67+ gpuelemwise_arg * res = calloc (n , sizeof (gpuelemwise_arg ));
6368 unsigned int i ;
6469
6570 if (res == NULL ) return NULL ;
@@ -73,9 +78,10 @@ static gpuelemwise_args *copy_args(unsigned int n, gpuelemwise_arg *a) {
7378 for (; i > 0 ; i -- ) {
7479 clear_arg (& res [i ]);
7580 }
81+ return NULL ;
7682}
7783
78- static void free_args (unsigned int n , gpuarray_arg * args ) {
84+ static void free_args (unsigned int n , gpuelemwise_arg * args ) {
7985 unsigned int i ;
8086
8187 for (i = 0 ; i < n ; i ++ )
@@ -89,7 +95,7 @@ static int gen_elemwise_basic_kernel(GpuKernel *k,
8995 const char * preamble ,
9096 const char * expr ,
9197 unsigned int nd ,
92- unsigned int n , gpuarray_arg * args ) {
98+ unsigned int n , gpuelemwise_arg * args ) {
9399 strb sb = STRB_STATIC_INIT ;
94100 unsigned int i , _i , j ;
95101 int * ktypes ;
@@ -115,25 +121,25 @@ static int gen_elemwise_basic_kernel(GpuKernel *k,
115121 strb_appends (& sb , "\nKERNEL void elem(const ga_size n, " );
116122 ktypes [p ++ ] = GA_SIZE ;
117123 for (i = 0 ; i < nd ; i ++ ) {
118- strb_appendf (& sb "const ga_size dim%u" , i );
124+ strb_appendf (& sb , "const ga_size dim%u" , i );
119125 ktypes [p ++ ] = GA_SIZE ;
120126 }
121127 for (j = 0 ; j < n ; j ++ ) {
122- if (ISCRL (args [j ]. flags , GE_SCALAR )) {
123- strb_appendf (& sb "GLOBAL_MEM *%s %s_data, const ga_size %s_offset%s" ,
128+ if (is_array (args [j ])) {
129+ strb_appendf (& sb , "GLOBAL_MEM *%s %s_data, const ga_size %s_offset%s" ,
124130 ctype (args [j ].typecode ), args [j ].name , args [j ].name ,
125131 nd == 0 ? "" : ", " );
126132 ktypes [p ++ ] = GA_BUFFER ;
127133 ktypes [p ++ ] = GA_SIZE ;
128134
129135 for (i = 0 ; i < nd ; i ++ ) {
130136 strb_appendf (& sb , "const ga_ssize %s_str_%u%s" , args [j ].name , i ,
131- (i == (nd - 1 )) ? "" , ", " );
137+ (i == (nd - 1 )) ? "" : ", " );
132138 ktypes [p ++ ] = GA_SSIZE ;
133139 }
134140 } else {
135- strb_appendsf (& sb "%s %s" , args [j ].name );
136- ktypes [p ++ ] = types [j ];
141+ strb_appendf (& sb , "%s %s" , args [j ].name );
142+ ktypes [p ++ ] = args [j ]. typecode ;
137143 }
138144 if (j != (n - 1 )) strb_appends (& sb , ", " );
139145 }
@@ -217,6 +223,7 @@ static int check_basic(GpuElemwise *ge, void **args, int flags,
217223 ssize_t * * * _strides ) {
218224 ssize_t * * strs ;
219225 size_t * dims ;
226+ size_t n ;
220227 GpuArray * a = NULL , * v ;
221228 unsigned int i , j , p , num_arrays = 0 , nd ;
222229 int err ;
@@ -252,11 +259,11 @@ static int check_basic(GpuElemwise *ge, void **args, int flags,
252259 }
253260
254261 /* And copy their initial values in */
255- memcpy (dims , a -> dims , nd * sizeof (size_t ));
262+ memcpy (dims , a -> dimensions , nd * sizeof (size_t ));
256263 p = 0 ;
257264 for (i = 0 ; i < ge -> n ; i ++ ) {
258265 if (is_array (ge -> args [i ])) {
259- memcpy (strs [p ], ((GpuArray * )args [i ])-> strs , nd * sizeof (ssize_t ));
266+ memcpy (strs [p ], ((GpuArray * )args [i ])-> strides , nd * sizeof (ssize_t ));
260267 p ++ ;
261268 }
262269 }
@@ -272,17 +279,17 @@ static int check_basic(GpuElemwise *ge, void **args, int flags,
272279 for (j = 0 ; j < nd ; j ++ ) {
273280 for (i = 0 ; i < ge -> n ; i ++ ) {
274281 if (is_array (ge -> args [i ])) {
275- v = (GpuArray * )ge -> args [i ];
276- if (dims [j ] != v -> dims [j ]) {
282+ v = (GpuArray * )args [i ];
283+ if (dims [j ] != v -> dimensions [j ]) {
277284 if (ISCLR (flags , GE_BROADCAST )) {
278285 err = GA_VALUE_ERROR ;
279286 goto error ;
280287 }
281288 /* GE_BROADCAST is set */
282289 if (dims [j ] == 1 ) {
283- dims [j ] = v -> dims [j ];
290+ dims [j ] = v -> dimensions [j ];
284291 } else {
285- if (v -> dims [j ] == 1 ) {
292+ if (v -> dimensions [j ] == 1 ) {
286293 strs [p ][j ] = 0 ;
287294 } else {
288295 err = GA_VALUE_ERROR ;
@@ -297,7 +304,7 @@ static int check_basic(GpuElemwise *ge, void **args, int flags,
297304 n *= dims [j ];
298305 } /* for each dim */
299306
300- if (ISSET (flags , GE_COLLAPSE ) && nd > 1 ) {
307+ if (ISCLR (flags , GE_NOCOLLAPSE ) && nd > 1 ) {
301308 gpuarray_elemwise_collapse (num_arrays , & nd , dims , strs );
302309 }
303310
@@ -317,6 +324,7 @@ static int check_basic(GpuElemwise *ge, void **args, int flags,
317324
318325static int call_basic (GpuElemwise * ge , void * * args , ...) {
319326
327+ return GA_UNSUPPORTED_ERROR ;
320328}
321329
322330static int gen_elemwise_contig_kernel (GpuKernel * k ,
@@ -337,7 +345,7 @@ static int gen_elemwise_contig_kernel(GpuKernel *k,
337345
338346 p = 1 ;
339347 for (j = 0 ; j < n ; j ++ )
340- p += ISSET (args [j ].flags & GE_SCALAR ) ? 1 : 2 ;
348+ p += ISSET (args [j ].flags , GE_SCALAR ) ? 1 : 2 ;
341349
342350 ktypes = calloc (p , sizeof (int ));
343351 if (ktypes == NULL )
@@ -409,21 +417,21 @@ static int check_contig(GpuElemwise *ge, void **args,
409417 v = (GpuArray * )args [i ];
410418 if (a != NULL ) {
411419 a = v ;
412- for (j = 0 ; j < a -> nd ; j ++ ) n *= a -> dims [j ];
420+ for (j = 0 ; j < a -> nd ; j ++ ) n *= a -> dimensions [j ];
413421 }
414422 c_contig &= GpuArray_IS_C_CONTIGUOUS (v );
415423 f_contig &= GpuArray_IS_F_CONTIGUOUS (v );
416424 if (a != v ) {
417425 if (a -> nd != v -> nd )
418426 return GA_INVALID_ERROR ;
419427 for (j = 0 ; j < a -> nd ; j ++ ) {
420- if (v -> dims [j ] != a -> dims [j ])
428+ if (v -> dimensions [j ] != a -> dimensions [j ])
421429 return GA_VALUE_ERROR ;
422430 }
423431 }
424432 }
425433 }
426- * contig = f_config || c_contig ;
434+ * contig = f_contig || c_contig ;
427435 * _n = n ;
428436 return GA_NO_ERROR ;
429437}
@@ -451,8 +459,7 @@ static int call_contig(GpuElemwise *ge, void **args, size_t n) {
451459 }
452460 err = GpuKernel_sched (& ge -> k_contig , n , & ls , & gs );
453461 if (err != GA_NO_ERROR ) return err ;
454- err = GpuKernel_call (& ge -> k_contig , 1 , & ls , & gs , 0 , NULL );
455- if (err != GA_NO_ERROR ) return err ;
462+ return GpuKernel_call (& ge -> k_contig , 1 , & ls , & gs , 0 , NULL );
456463}
457464
458465GpuElemwise * GpuElemwise_new (const gpuarray_buffer_ops * ops , void * ctx ,
@@ -494,9 +501,9 @@ GpuElemwise *GpuElemwise_new(const gpuarray_buffer_ops *ops, void * ctx,
494501 fail_contig :
495502 free_args (res -> n , res -> args );
496503 fail_args :
497- free (res -> preamble );
504+ free (( void * ) res -> preamble );
498505 fail_preamble :
499- free (res -> expr );
506+ free (( void * ) res -> expr );
500507 fail_expr :
501508 free (res );
502509 return NULL ;
@@ -505,8 +512,8 @@ GpuElemwise *GpuElemwise_new(const gpuarray_buffer_ops *ops, void * ctx,
505512void GpuElemwise_free (GpuElemwise * ge ) {
506513 GpuKernel_clear (& ge -> k_contig );
507514 free_args (ge -> n , ge -> args );
508- free (ge -> preamble );
509- free (ge -> expr );
515+ free (( void * ) ge -> preamble );
516+ free (( void * ) ge -> expr );
510517 free (ge );
511518}
512519
@@ -517,11 +524,11 @@ int GpuElemwise_call(GpuElemwise *ge, void **args, int flags) {
517524 unsigned int nd ;
518525 int contig ;
519526 int err ;
520- err = check_contig (ge -> args , args , & n , & contig );
527+ err = check_contig (ge , args , & n , & contig );
521528 if (err == GA_NO_ERROR && contig ) {
522529 return call_contig (ge , args , n );
523530 }
524- err = check_basic (ge -> args , args , & n , & nd , & dims , & strides );
531+ err = check_basic (ge , args , flags , & n , & nd , & dims , & strides );
525532 if (err == GA_NO_ERROR )
526533 /* WIP
527534 return call_basic(ge, args, ...);
0 commit comments