-
Notifications
You must be signed in to change notification settings - Fork 12
/
xnd.c
1826 lines (1501 loc) · 45.2 KB
/
xnd.c
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BSD 3-Clause License
*
* Copyright (c) 2017-2018, plures
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "ndtypes.h"
#include "xnd.h"
#include "inline.h"
#include "contrib.h"
#include "contrib/bfloat16.h"
#include "cuda/cuda_memory.h"
#ifndef _MSC_VER
#include "config.h"
#endif
static int xnd_init(xnd_t * const x, const uint32_t flags, ndt_context_t *ctx);
/*****************************************************************************/
/* Error handling */
/*****************************************************************************/
/* error return value */
const xnd_t xnd_error = {
.bitmap = {.data=NULL, .size=0, .next=NULL},
.index = 0,
.type = NULL,
.ptr = NULL
};
int
xnd_err_occurred(const xnd_t *x)
{
return x->ptr == NULL;
}
/*****************************************************************************/
/* Create and initialize a new master buffer */
/*****************************************************************************/
static bool
requires_init(const ndt_t * const t)
{
return !ndt_is_ref_free(t);
}
static bool
is_primary_type(const ndt_t * const t, ndt_context_t *ctx)
{
if (ndt_is_abstract(t)) {
ndt_err_format(ctx, NDT_ValueError,
"cannot create xnd container from abstract type");
return false;
}
if (t->flags & NDT_CHAR) {
ndt_err_format(ctx, NDT_NotImplementedError, "char is not implemented");
return false;
}
switch (t->tag) {
case FixedDim: {
if (!ndt_is_c_contiguous(t) && !ndt_is_f_contiguous(t)) {
ndt_err_format(ctx, NDT_ValueError,
"cannot create xnd container from non-contiguous type");
return false;
}
return true;
}
case VarDim: case VarDimElem: {
if (!ndt_is_var_contiguous(t)) {
ndt_err_format(ctx, NDT_ValueError,
"cannot create xnd container from non-contiguous type");
return false;
}
return true;
}
case Array: {
if (requires_init(t)) {
ndt_err_format(ctx, NDT_ValueError,
"flexible arrays cannot have dtypes that require "
"initialization");
return false;
}
return true;
}
default:
return true;
}
ndt_err_format(ctx, NDT_ValueError,
"cannot create xnd container from non-contiguous type");
return false;
}
/* Create and initialize memory with type 't'. */
#ifdef HAVE_CUDA
static char *
xnd_cuda_new(const ndt_t * const t, ndt_context_t *ctx)
{
void *ptr;
if (!is_primary_type(t, ctx)) {
return NULL;
}
if (!ndt_is_pointer_free(t)) {
ndt_err_format(ctx, NDT_ValueError,
"only pointer-free types are supported on cuda");
return NULL;
}
ptr = xnd_cuda_calloc_managed(t->align, t->datasize, ctx);
if (ptr == NULL) {
return NULL;
}
return ptr;
}
#else
static char *
xnd_cuda_new(const ndt_t * const t, ndt_context_t *ctx)
{
(void)t;
ndt_err_format(ctx, NDT_ValueError, "xnd compiled without cuda support");
return NULL;
}
#endif
static char *
xnd_new(const ndt_t * const t, const uint32_t flags, ndt_context_t *ctx)
{
xnd_t x;
if (flags & XND_CUDA_MANAGED) {
return xnd_cuda_new(t, ctx);
}
if (!is_primary_type(t, ctx)) {
return NULL;
}
x.index = 0;
x.type = t;
x.ptr = ndt_aligned_calloc(t->align, t->datasize);
if (x.ptr == NULL) {
ndt_memory_error(ctx);
return NULL;
}
if (requires_init(t) && xnd_init(&x, flags, ctx) < 0) {
ndt_aligned_free(x.ptr);
return NULL;
}
return x.ptr;
}
/*
* Initialize typed memory. If the XND_OWN_POINTERS flag is set, allocate
* memory for all ref subtypes and initialize that memory. Otherwise, set
* refs to NULL.
*
* Ref subtypes include any type of the form "Ref(t)".
*
* Never allocated are (sizes are not known):
* - "string" type (pointer to NUL-terminated UTF8 string)
* - data of the "bytes" type: {size: size_t, data: uint8_t *bytes}
*
* At all times the data pointers must be NULL or pointers to valid memory.
*/
static int
xnd_init(xnd_t * const x, const uint32_t flags, ndt_context_t *ctx)
{
const ndt_t * const t = x->type;
if (flags & XND_CUDA_MANAGED) {
ndt_err_format(ctx, NDT_RuntimeError,
"internal error: cannot initialize cuda memory with a type "
"that contains pointers");
return -1;
}
if (ndt_is_abstract(t)) {
ndt_err_format(ctx, NDT_ValueError,
"cannot initialize concrete memory from abstract type");
return -1;
}
switch (t->tag) {
case FixedDim: {
int64_t i;
for (i = 0; i < t->FixedDim.shape; i++) {
xnd_t next = _fixed_dim_next(x, i);
if (xnd_init(&next, flags, ctx) < 0) {
return -1;
}
}
return 0;
}
case VarDim: {
int64_t start, step, shape;
int64_t i;
shape = ndt_var_indices(&start, &step, t, x->index, ctx);
if (shape < 0) {
return -1;
}
for (i = 0; i < shape; i++) {
xnd_t next = _var_dim_next(x, start, step, i);
if (xnd_init(&next, flags, ctx) < 0) {
return -1;
}
}
return 0;
}
case Tuple: {
for (int64_t i = 0; i < t->Tuple.shape; i++) {
xnd_t next = _tuple_next(x, i);
if (xnd_init(&next, flags, ctx) < 0) {
xnd_clear(&next, flags);
return -1;
}
}
return 0;
}
case Record: {
for (int64_t i = 0; i < t->Record.shape; i++) {
xnd_t next = _record_next(x, i);
if (xnd_init(&next, flags, ctx) < 0) {
xnd_clear(&next, flags);
return -1;
}
}
return 0;
}
case Union: {
xnd_t next = _union_next(x);
if (xnd_init(&next, flags, ctx) < 0) {
xnd_clear(&next, flags);
return -1;
}
return 0;
}
/*
* Ref represents a pointer to an explicit type. If XND_OWN_POINTERS
* is set, allocate memory for that type and set the pointer.
*/
case Ref: {
if (flags & XND_OWN_POINTERS) {
const ndt_t *u = t->Ref.type;
void *ref;
ref = ndt_aligned_calloc(u->align, u->datasize);
if (ref == NULL) {
ndt_err_format(ctx, NDT_MemoryError, "out of memory");
return -1;
}
XND_POINTER_DATA(x->ptr) = ref;
xnd_t next = _ref_next(x);
if (xnd_init(&next, flags, ctx) < 0) {
xnd_clear(&next, flags);
return -1;
}
}
return 0;
}
/* Constr is a named explicit type. */
case Constr: {
xnd_t next = _constr_next(x);
if (xnd_init(&next, flags, ctx) < 0) {
xnd_clear(&next, flags);
return -1;
}
return 0;
}
/* Nominal is a globally unique typedef. */
case Nominal: {
xnd_t next = _nominal_next(x);
if (xnd_init(&next, flags, ctx) < 0) {
xnd_clear(&next, flags);
return -1;
}
return 0;
}
/* Array is already initialized by calloc(). */
case Array:
return 0;
/* Categorical is already initialized by calloc(). */
case Categorical:
return 0;
case VarDimElem:
ndt_err_format(ctx, NDT_ValueError,
"cannot initialize var elem dimension");
return -1;
case Char:
ndt_err_format(ctx, NDT_NotImplementedError, "char not implemented");
return -1;
/* Primitive types are already initialized by calloc(). */
case Bool:
case Int8: case Int16: case Int32: case Int64:
case Uint8: case Uint16: case Uint32: case Uint64:
case BFloat16: case Float16: case Float32: case Float64:
case BComplex32: case Complex32: case Complex64: case Complex128:
case FixedString: case FixedBytes:
case String: case Bytes:
return 0;
/* NOT REACHED: intercepted by ndt_is_abstract(). */
case Module: case Function:
case AnyKind: case SymbolicDim: case EllipsisDim: case Typevar:
case ScalarKind: case SignedKind: case UnsignedKind: case FloatKind:
case ComplexKind: case FixedStringKind: case FixedBytesKind:
ndt_err_format(ctx, NDT_RuntimeError, "unexpected abstract type");
return -1;
}
/* NOT REACHED: tags should be exhaustive */
ndt_err_format(ctx, NDT_RuntimeError, "invalid type tag");
return -1;
}
/*
* Create a type from a string and return a new master buffer for that type.
* Any combination of flags that include XND_OWN_TYPE can be passed.
*/
xnd_master_t *
xnd_empty_from_string(const char *s, uint32_t flags, ndt_context_t *ctx)
{
xnd_bitmap_t b = {.data=NULL, .size=0, .next=NULL};
xnd_master_t *x;
const ndt_t *t;
char *ptr;
if (!(flags & XND_OWN_TYPE)) {
ndt_err_format(ctx, NDT_InvalidArgumentError,
"xnd_empty_from_string: XND_OWN_TYPE must be set");
return NULL;
}
x = ndt_alloc(1, sizeof *x);
if (x == NULL) {
return ndt_memory_error(ctx);
}
t = ndt_from_string(s, ctx);
if (t == NULL) {
ndt_free(x);
return NULL;
}
if (!ndt_is_concrete(t)) {
ndt_err_format(ctx, NDT_ValueError, "type must be concrete");
ndt_decref(t);
ndt_free(x);
return NULL;
}
if (xnd_bitmap_init(&b, t,ctx) < 0) {
ndt_decref(t);
ndt_free(x);
return NULL;
}
ptr = xnd_new(t, flags, ctx);
if (ptr == NULL) {
xnd_bitmap_clear(&b);
ndt_decref(t);
ndt_free(x);
return NULL;
}
x->flags = flags;
x->master.bitmap = b;
x->master.index = 0;
x->master.type = t;
x->master.ptr = ptr;
return x;
}
/*
* Return a new master buffer. Any combination of flags except for XND_OWN_TYPE
* can be passed. 't' must be kept valid as long as the master buffer is valid.
*/
xnd_master_t *
xnd_empty_from_type(const ndt_t *t, uint32_t flags, ndt_context_t *ctx)
{
xnd_bitmap_t b = {.data=NULL, .size=0, .next=NULL};
xnd_master_t *x;
char *ptr;
if (flags & XND_OWN_TYPE) {
ndt_err_format(ctx, NDT_InvalidArgumentError,
"xnd_empty_from_type: XND_OWN_TYPE must not be set");
return NULL;
}
if (!ndt_is_concrete(t)) {
ndt_err_format(ctx, NDT_ValueError, "type must be concrete");
return NULL;
}
x = ndt_alloc(1, sizeof *x);
if (x == NULL) {
return ndt_memory_error(ctx);
}
if (xnd_bitmap_init(&b, t, ctx) < 0) {
ndt_free(x);
return NULL;
}
ptr = xnd_new(t, flags, ctx);
if (ptr == NULL) {
xnd_bitmap_clear(&b);
ndt_free(x);
return NULL;
}
x->flags = flags;
x->master.bitmap = b;
x->master.index = 0;
x->master.type = t;
x->master.ptr = ptr;
return x;
}
/*
* Create master buffer from an existing xnd_t. Ownership of bitmaps, type,
* ptr is transferred to the master buffer.
*
* 'flags' are the master buffer's flags after the transfer. The flags of
* 'src' are always assumed to be XND_OWN_ALL.
*
* This is a convenience function that should only be used if the xnd_t src
* owns everything and its internals have not been exposed to other views.
*/
xnd_master_t *
xnd_from_xnd(xnd_t *src, uint32_t flags, ndt_context_t *ctx)
{
xnd_master_t *x;
/* XXX xnd_from_xnd() will probably be replaced. */
assert(!(flags & XND_CUDA_MANAGED));
x = ndt_alloc(1, sizeof *x);
if (x == NULL) {
xnd_clear(src, XND_OWN_ALL);
ndt_decref(src->type);
ndt_aligned_free(src->ptr);
xnd_bitmap_clear(&src->bitmap);
return ndt_memory_error(ctx);
}
x->flags = flags;
x->master = *src;
return x;
}
/*****************************************************************************/
/* Deallocate and clear a master buffer */
/*****************************************************************************/
static bool
requires_clear(const ndt_t * const t)
{
if (t->tag == Array) {
return true;
}
const ndt_t *dtype = ndt_dtype(t);
switch (dtype->tag) {
case Categorical:
case Bool:
case Int8: case Int16: case Int32: case Int64:
case Uint8: case Uint16: case Uint32: case Uint64:
case BFloat16: case Float16: case Float32: case Float64:
case BComplex32: case Complex32: case Complex64: case Complex128:
case FixedString: case FixedBytes:
return false;
default:
return true;
}
}
/* Clear an embedded pointer. */
static void
xnd_clear_ref(xnd_t *x, const uint32_t flags)
{
assert(x->type->tag == Ref);
assert(!(flags & XND_CUDA_MANAGED));
if (flags & XND_OWN_POINTERS) {
ndt_aligned_free(XND_POINTER_DATA(x->ptr));
XND_POINTER_DATA(x->ptr) = NULL;
}
}
/* Strings must always be allocated by non-aligned allocators. */
static void
xnd_clear_string(xnd_t *x, const uint32_t flags)
{
assert(x->type->tag == String);
assert(!(flags & XND_CUDA_MANAGED));
if (flags & XND_OWN_STRINGS) {
ndt_free(XND_POINTER_DATA(x->ptr));
XND_POINTER_DATA(x->ptr) = NULL;
}
}
/* Bytes must always be allocated by aligned allocators. */
static void
xnd_clear_bytes(xnd_t *x, const uint32_t flags)
{
assert(x->type->tag == Bytes);
assert(!(flags & XND_CUDA_MANAGED));
if (flags & XND_OWN_BYTES) {
ndt_aligned_free(XND_BYTES_DATA(x->ptr));
XND_BYTES_SIZE(x->ptr) = 0;
XND_BYTES_DATA(x->ptr) = NULL;
}
}
/* Flexible array data must always be allocated by aligned allocators. */
static void
xnd_clear_array(xnd_t *x, const uint32_t flags)
{
assert(x->type->tag == Array);
assert(!(flags & XND_CUDA_MANAGED));
if (flags & XND_OWN_ARRAYS) {
ndt_aligned_free(XND_ARRAY_DATA(x->ptr));
XND_ARRAY_SHAPE(x->ptr) = 0;
XND_ARRAY_DATA(x->ptr) = NULL;
}
}
/* Clear embedded pointers in the data according to flags. */
void
xnd_clear(xnd_t * const x, const uint32_t flags)
{
NDT_STATIC_CONTEXT(ctx);
const ndt_t * const t = x->type;
assert(ndt_is_concrete(t));
assert(!(flags & XND_CUDA_MANAGED));
switch (t->tag) {
case FixedDim: {
for (int64_t i = 0; i < t->FixedDim.shape; i++) {
xnd_t next = _fixed_dim_next(x, i);
xnd_clear(&next, flags);
}
return;
}
case VarDim: {
int64_t start, step, shape;
int64_t i;
shape = ndt_var_indices(&start, &step, t, x->index, &ctx);
if (shape < 0) {
/* This cannot happen: indices are checked in xnd_init() and
* should remain constant. */
ndt_context_del(&ctx);
fprintf(stderr, "xnd_clear: internal error: var indices changed\n");
return;
}
for (i = 0; i < shape; i++) {
xnd_t next = _var_dim_next(x, start, step, i);
xnd_clear(&next, flags);
}
return;
}
case VarDimElem: {
fprintf(stderr, "xnd_clear: internal error: unexpected var elem dimension\n");
return;
}
case Array: {
const int64_t shape = XND_ARRAY_SHAPE(x->ptr);
for (int64_t i = 0; i < shape; i++) {
xnd_t next = _array_next(x, i);
xnd_clear(&next, flags);
}
xnd_clear_array(x, flags);
return;
}
case Tuple: {
for (int64_t i = 0; i < t->Tuple.shape; i++) {
xnd_t next = _tuple_next(x, i);
xnd_clear(&next, flags);
}
return;
}
case Record: {
for (int64_t i = 0; i < t->Record.shape; i++) {
xnd_t next = _record_next(x, i);
xnd_clear(&next, flags);
}
return;
}
case Union: {
xnd_t next = _union_next(x);
xnd_clear(&next, flags);
return;
}
case Ref: {
if (flags & XND_OWN_POINTERS) {
xnd_t next = _ref_next(x);
xnd_clear(&next, flags);
xnd_clear_ref(x, flags);
}
return;
}
case Constr: {
xnd_t next = _constr_next(x);
xnd_clear(&next, flags);
return;
}
case Nominal: {
xnd_t next = _nominal_next(x);
xnd_clear(&next, flags);
return;
}
case Bool:
case Int8: case Int16: case Int32: case Int64:
case Uint8: case Uint16: case Uint32: case Uint64:
case BFloat16: case Float16: case Float32: case Float64:
case BComplex32: case Complex32: case Complex64: case Complex128:
case FixedString: case FixedBytes:
return;
case String:
xnd_clear_string(x, flags);
return;
case Bytes:
xnd_clear_bytes(x, flags);
return;
case Categorical:
/* Categorical values are just indices into the categories. */
return;
case Char:
/* Just a scalar. */
return;
/* NOT REACHED: intercepted by ndt_is_abstract(). */
case Module: case Function:
case AnyKind: case SymbolicDim: case EllipsisDim: case Typevar:
case ScalarKind: case SignedKind: case UnsignedKind: case FloatKind:
case ComplexKind: case FixedStringKind: case FixedBytesKind:
return;
}
}
/*
* Delete an xnd_t buffer according to 'flags'. Outside xnd_del(), this
* function should only be used if an xnd_t owns all its members.
*/
void
xnd_del_buffer(xnd_t *x, uint32_t flags)
{
if (x != NULL) {
if (x->ptr != NULL && x->type != NULL) {
if ((flags&XND_OWN_DATA) && requires_clear(x->type)) {
xnd_clear(x, flags);
}
if (flags & XND_OWN_TYPE) {
ndt_decref(x->type);
}
if (flags & XND_OWN_DATA) {
if (flags & XND_CUDA_MANAGED) {
#ifdef HAVE_CUDA
xnd_cuda_free(x->ptr);
#else
fprintf(stderr,
"xnd_del_buffer: internal error: XND_CUDA_MANAGED set "
"without cuda support\n");
#endif
}
else {
ndt_aligned_free(x->ptr);
}
}
}
if (flags & XND_OWN_DATA) {
xnd_bitmap_clear(&x->bitmap);
}
}
}
/*
* Delete the master buffer. The type and embedded pointers are deallocated
* according to x->flags.
*/
void
xnd_del(xnd_master_t *x)
{
if (x != NULL) {
xnd_del_buffer(&x->master, x->flags);
ndt_free(x);
}
}
/*****************************************************************************/
/* Index checks */
/*****************************************************************************/
static int64_t
get_index(const xnd_index_t *key, int64_t shape, ndt_context_t *ctx)
{
switch (key->tag) {
case Index:
return adjust_index(key->Index, shape, ctx);
case FieldName:
ndt_err_format(ctx, NDT_ValueError,
"expected integer index, got field name: '%s'", key->FieldName);
return -1;
case Slice:
ndt_err_format(ctx, NDT_ValueError,
"expected integer index, got slice");
return -1;
}
/* NOT REACHED: tags should be exhaustive */
ndt_err_format(ctx, NDT_RuntimeError, "invalid index tag");
return -1;
}
/*
* Ragged arrays have multiple shapes in a single dimension that are not known
* when a VarDimElem is created. Adjusting the index must be done when the
* VarDimElem is accessed and the slices have been applied.
*/
static int64_t
get_index_var_elem(const xnd_index_t *key, ndt_context_t *ctx)
{
switch (key->tag) {
case Index: {
int64_t i = key->Index;
if (i < INT32_MIN || i > INT32_MAX) {
ndt_err_format(ctx, NDT_IndexError,
"index with value %" PRIi64 " out of bounds", key->Index);
return INT64_MIN;
}
return i;
}
case FieldName:
ndt_err_format(ctx, NDT_ValueError,
"expected integer index, got field name: '%s'", key->FieldName);
return INT64_MIN;
case Slice:
ndt_err_format(ctx, NDT_ValueError,
"expected integer index, got slice");
return INT64_MIN;
}
/* NOT REACHED: tags should be exhaustive */
ndt_err_format(ctx, NDT_RuntimeError, "invalid index tag");
return INT64_MIN;
}
static int64_t
get_index_record(const ndt_t *t, const xnd_index_t *key, ndt_context_t *ctx)
{
assert(t->tag == Record);
switch (key->tag) {
case FieldName: {
int64_t i;
for (i = 0; i < t->Record.shape; i++) {
if (strcmp(key->FieldName, t->Record.names[i]) == 0) {
return i;
}
}
ndt_err_format(ctx, NDT_ValueError,
"invalid field name '%s'", key->FieldName);
return -1;
}
case Index: case Slice:
return get_index(key, t->Record.shape, ctx);
}
/* NOT REACHED: tags should be exhaustive */
ndt_err_format(ctx, NDT_RuntimeError, "invalid index tag");
return -1;
}
static int64_t
get_index_union(const ndt_t *t, const xnd_index_t *key, ndt_context_t *ctx)
{
assert(t->tag == Union);
switch (key->tag) {
case FieldName: {
int64_t i;
for (i = 0; i < t->Union.ntags; i++) {
if (strcmp(key->FieldName, t->Union.tags[i]) == 0) {
return i;
}
}
ndt_err_format(ctx, NDT_ValueError,
"invalid field name '%s'", key->FieldName);
return -1;
}
case Index: case Slice:
return get_index(key, t->Union.ntags, ctx);
}
/* NOT REACHED: tags should be exhaustive */
ndt_err_format(ctx, NDT_RuntimeError, "invalid index tag");
return -1;
}
static void
set_index_exception(bool indexable, ndt_context_t *ctx)
{
if (indexable) {
ndt_err_format(ctx, NDT_IndexError, "too many indices");
}
else {
ndt_err_format(ctx, NDT_TypeError, "type not indexable");
}
}
/*****************************************************************************/
/* Stored indices */
/*****************************************************************************/
bool
have_stored_index(const ndt_t *t)
{
return t->tag == VarDimElem;
}
int64_t
get_stored_index(const ndt_t *t)
{
return t->VarDimElem.index;
}
/* skip stored indices */
xnd_t
apply_stored_index(const xnd_t *x, ndt_context_t *ctx)
{
const ndt_t * const t = x->type;
int64_t start, step, shape;
if (t->tag != VarDimElem) {
ndt_err_format(ctx, NDT_RuntimeError,
"apply_stored_index: need VarDimElem");
return xnd_error;
}
shape = ndt_var_indices(&start, &step, t, x->index, ctx);
if (shape < 0) {
return xnd_error;
}
const int64_t i = adjust_index(t->VarDimElem.index, shape, ctx);
if (i < 0) {
return xnd_error;
}
return xnd_var_dim_next(x, start, step, i);
}
xnd_t
apply_stored_indices(const xnd_t *x, ndt_context_t *ctx)
{
xnd_t tl = *x;
while (tl.type->tag == VarDimElem) {
tl = apply_stored_index(&tl, ctx);
}
return tl;
}
/*****************************************************************************/
/* Subtrees (single elements are a special case) */
/*****************************************************************************/
/* Return a typed subtree of a memory block */
static xnd_t
_xnd_subtree_index(const xnd_t *x, const int64_t *indices, int len, ndt_context_t *ctx)
{
APPLY_STORED_INDICES_XND(x)
const ndt_t * const t = x->type;
assert(ndt_is_concrete(t));
if (t->ndim > 0 && ndt_is_optional(t)) {
ndt_err_format(ctx, NDT_NotImplementedError,
"optional dimensions are not supported");
return xnd_error;