-
-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy pathmain.c
585 lines (545 loc) · 15.7 KB
/
main.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
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdint.h>
#include "stdlib/ndarray/base/bytes_per_element.h"
#include "stdlib/ndarray/base/ind.h"
#include "stdlib/ndarray/base/iteration_order.h"
#include "stdlib/ndarray/base/minmax_view_buffer_index.h"
#include "stdlib/ndarray/base/numel.h"
#include "stdlib/ndarray/base/strides2order.h"
#include "stdlib/ndarray/dtypes.h"
#include "stdlib/ndarray/index_modes.h"
#include "stdlib/ndarray/orders.h"
#include "stdlib/ndarray/ctor.h"
/**
* Returns a pointer to an ndarray's underlying byte array.
*
* @param arr input ndarray
* @return underlying byte array
*/
uint8_t * stdlib_ndarray_data( const struct ndarray *arr ) {
return arr->data;
}
/**
* Returns the number of dimensions in an ndarray.
*
* @param arr input ndarray
* @return number of dimensions
*/
int64_t stdlib_ndarray_ndims( const struct ndarray *arr ) {
return arr->ndims;
}
/**
* Returns a pointer to an array containing an ndarray shape (dimensions).
*
* @param arr input ndarray
* @return array shape (dimensions)
*/
int64_t * stdlib_ndarray_shape( const struct ndarray *arr ) {
return arr->shape;
}
/**
* Returns an ndarray dimension.
*
* ## Notes
*
* - The function does not perform any sanity checks.
*
* @param arr input ndarray
* @param i dimension index
* @return dimension
*/
int64_t stdlib_ndarray_dimension( const struct ndarray *arr, const int64_t i ) {
return arr->shape[ i ];
}
/**
* Returns a pointer to an array containing ndarray strides (in bytes).
*
* @param arr input ndarray
* @return array strides
*/
int64_t * stdlib_ndarray_strides( const struct ndarray *arr ) {
return arr->strides;
}
/**
* Returns an ndarray stride (in bytes).
*
* ## Notes
*
* - The function does not perform any sanity checks.
*
* @param arr input ndarray
* @param i stride index
* @return array stride
*/
int64_t stdlib_ndarray_stride( const struct ndarray *arr, const int64_t i ) {
return arr->strides[ i ];
}
/**
* Returns an ndarray index offset (in bytes).
*
* @param arr input ndarray
* @return array strides
*/
int64_t stdlib_ndarray_offset( const struct ndarray *arr ) {
return arr->offset;
}
/**
* Returns the order of an ndarray.
*
* @param arr input ndarray
* @return array order
*/
enum STDLIB_NDARRAY_ORDER stdlib_ndarray_order( const struct ndarray *arr ) {
return arr->order;
}
/**
* Returns an ndarray data type.
*
* @param arr input ndarray
* @return array data type
*/
enum STDLIB_NDARRAY_DTYPE stdlib_ndarray_dtype( const struct ndarray *arr ) {
return arr->dtype;
}
/**
* Returns the index mode of an ndarray.
*
* @param arr input ndarray
* @return index mode
*/
enum STDLIB_NDARRAY_INDEX_MODE stdlib_ndarray_index_mode( const struct ndarray *arr ) {
return arr->imode;
}
/**
* Returns the number of ndarray subscript modes.
*
* @param arr input ndarray
* @return number of subscript modes
*/
int64_t stdlib_ndarray_nsubmodes( const struct ndarray *arr ) {
return arr->nsubmodes;
}
/**
* Returns ndarray subscript modes.
*
* @param arr input ndarray
* @return subscript modes
*/
enum STDLIB_NDARRAY_INDEX_MODE * stdlib_ndarray_submodes( const struct ndarray *arr ) {
return arr->submodes;
}
/**
* Returns an ndarray subscript mode.
*
* ## Notes
*
* - If an ndarray has fewer subscript modes than dimensions, modes are recycled using modulo arithmetic.
*
* @param arr input ndarray
* @param i dimension
* @return subscript mode
*/
enum STDLIB_NDARRAY_INDEX_MODE stdlib_ndarray_submode( const struct ndarray *arr, const int64_t i ) {
return arr->submodes[ i%(arr->nsubmodes) ];
}
/**
* Returns the number of elements in an ndarray.
*
* @param arr input ndarray
* @return number of elements
*/
int64_t stdlib_ndarray_length( const struct ndarray *arr ) {
return arr->length;
}
/**
* Returns the size of an ndarray (in bytes).
*
* @param arr input ndarray
* @return array size
*/
int64_t stdlib_ndarray_bytelength( const struct ndarray *arr ) {
return arr->byteLength;
}
/**
* Returns ndarray flags.
*
* @param arr input ndarray
* @return flags
*/
int64_t stdlib_ndarray_flags( const struct ndarray *arr ) {
uint8_t contiguous;
int64_t *strides;
int64_t tmp[2];
int64_t ndims;
int64_t flags;
int64_t len;
uint8_t ord;
// Cache various ndarray data:
len = arr->length;
ndims = arr->ndims;
strides = arr->strides;
// Initialize the memory for `flags`:
flags = 0;
// Determine if the array can be stored contiguously...
if ( len == 0 || stdlib_ndarray_iteration_order( ndims, strides ) == 0 ) {
// If an array does not contain any elements, then no data to store, and, if the array is unordered, adjacent array elements are not guaranteed to be stored next to each other.
contiguous = 0;
} else {
// Ensure that the array is compatible with a single memory segment:
stdlib_ndarray_minmax_view_buffer_index( tmp, ndims, arr->shape, strides, arr->offset );
if ( len == (tmp[1]-tmp[0]+1) ) {
// Compatible:
contiguous = 1;
} else {
// Incompatible:
contiguous = 0;
}
}
// Determine if the array is row-major/column-major contiguous:
if ( contiguous == 1 ) {
// Infer the array "order" from the stride array (this is supplementary to `arr->order`):
ord = stdlib_ndarray_strides2order( ndims, strides );
if ( ord == 1 || ord == 3 ) {
flags |= STDLIB_NDARRAY_ROW_MAJOR_CONTIGUOUS_FLAG;
}
if ( ord == 2 || ord == 3 ) {
flags |= STDLIB_NDARRAY_COLUMN_MAJOR_CONTIGUOUS_FLAG;
}
}
return flags;
}
/**
* Tests whether an ndarray has specified flags.
*
* @param arr input ndarray
* @param flags bit mask specifying the flags to test against
* @return `1` if flags are set and `0` otherwise
*/
int8_t stdlib_ndarray_has_flags( const struct ndarray *arr, const int64_t flags ) {
if ( ( arr->flags & flags ) == flags ) {
return 1;
}
return 0;
}
/**
* Enables specified ndarray flags.
*
* ## Notes
*
* - The function does not perform any sanity checks and **assumes** the user knows what s/he is doing.
*
* @param arr input ndarray
* @param flags bit mask to enable flags
* @return status code
*/
int8_t stdlib_ndarray_enable_flags( struct ndarray *arr, const int64_t flags ) {
arr->flags |= flags;
return 0;
}
/**
* Disables specified ndarray flags.
*
* ## Notes
*
* - The function does not perform any sanity checks and **assumes** the user knows what s/he is doing.
*
* @param arr input ndarray
* @param flags bit mask to disable flags
* @return status code
*/
int8_t stdlib_ndarray_disable_flags( struct ndarray *arr, const int64_t flags ) {
arr->flags &= ~flags;
return 0;
}
/**
* Returns a pointer to an ndarray data element.
*
* ## Notes
*
* - The function returns a pointer in order to provide a generic API supporting ndarrays having different data types.
*
* @param arr input ndarray
* @param sub ndarray subscripts
* @return pointer
*/
void * stdlib_ndarray_get_ptr( const struct ndarray *arr, const int64_t *sub ) {
enum STDLIB_NDARRAY_INDEX_MODE *submodes;
enum STDLIB_NDARRAY_INDEX_MODE mode;
int64_t *strides;
int64_t *shape;
int64_t ndims;
uint8_t *idx;
int64_t ind;
int64_t M;
int64_t i;
ndims = arr->ndims;
shape = arr->shape;
strides = arr->strides;
submodes = arr->submodes;
M = arr->nsubmodes;
idx = (arr->data) + (arr->offset); // pointer arithmetic
for ( i = 0; i < ndims; i++ ) {
mode = submodes[ i%M ];
ind = stdlib_ndarray_ind( sub[ i ], shape[ i ]-1, mode );
if ( ind < 0 ) {
return NULL;
}
idx += strides[ i ] * ind; // pointer arithmetic
}
return (void *)idx;
}
/**
* Returns a pointer to an ndarray data element located at a specified linear index.
*
* ## Notes
*
* - The function returns a pointer in order to provide a generic API supporting ndarrays having different data types.
*
* @param arr input ndarray
* @param idx linear view index
* @return pointer
*/
void * stdlib_ndarray_iget_ptr( const struct ndarray *arr, int64_t idx ) {
int64_t *strides;
int64_t *shape;
int64_t ndims;
uint8_t *ind;
int64_t s;
int64_t i;
int8_t io;
// Resolve an ndarray index based on the ndarray index mode:
idx = stdlib_ndarray_ind( idx, (arr->length)-1, arr->imode );
if ( idx < 0 ) {
return NULL;
}
// Convert the index to units of bytes:
idx *= arr->BYTES_PER_ELEMENT;
// Determine the pointer to the first indexed element:
ind = (arr->data) + (arr->offset); // pointer arithmetic
// Determine the iteration order based on the ndarray strides:
io = stdlib_ndarray_iteration_order( arr->ndims, arr->strides );
// Trivial case where we have all positive strides...
if ( io == 1 ) {
return (void *)( ind + idx ); // pointer arithmetic
}
// Trivial case where we have all negative strides...
if ( io == -1 ) {
return (void *)( ind - idx ); // pointer arithmetic
}
// The approach which follows is to resolve a view index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the underlying byte array...
ndims = arr->ndims;
shape = arr->shape;
strides = arr->strides;
if ( (arr->order) == STDLIB_NDARRAY_COLUMN_MAJOR ) {
for ( i = 0; i < ndims; i++ ) {
s = idx % shape[ i ];
idx -= s;
idx /= shape[ i ];
ind += s * strides[ i ]; // pointer arithmetic
}
return (void *)ind;
}
// Case: row-major
for ( i = ndims-1; i >= 0; i-- ) {
s = idx % shape[ i ];
idx -= s;
idx /= shape[ i ];
ind += s * strides[ i ]; // pointer arithmetic
}
return (void *)ind;
}
/**
* Sets an ndarray data element specified by a provided pointer value.
*
* ## Notes
*
* - The function returns `-1` if unable to set an element and `0` otherwise.
* - The function requires a pointer to a data value `v` in order to provide a generic API supporting ndarrays having different data types.
*
* @param arr input ndarray
* @param idx pointer to an ndarray data element
* @param v value to set
* @return status code
*/
int8_t stdlib_ndarray_set_ptr( const struct ndarray *arr, const void *idx, const void *v ) {
switch ( arr->dtype ) {
case STDLIB_NDARRAY_FLOAT64:
*(double *)idx = *(double *)v;
return 0;
case STDLIB_NDARRAY_FLOAT32:
*(float *)idx = *(float *)v;
return 0;
case STDLIB_NDARRAY_UINT64:
*(uint64_t *)idx = *(uint64_t *)v;
return 0;
case STDLIB_NDARRAY_INT64:
*(int64_t *)idx = *(int64_t *)v;
return 0;
case STDLIB_NDARRAY_UINT32:
*(uint32_t *)idx = *(uint32_t *)v;
return 0;
case STDLIB_NDARRAY_INT32:
*(int32_t *)idx = *(int32_t *)v;
return 0;
case STDLIB_NDARRAY_UINT16:
*(uint16_t *)idx = *(uint16_t *)v;
return 0;
case STDLIB_NDARRAY_INT16:
*(int16_t *)idx = *(int16_t *)v;
return 0;
case STDLIB_NDARRAY_UINT8:
*(uint8_t *)idx = *(uint8_t *)v;
return 0;
case STDLIB_NDARRAY_INT8:
*(int8_t *)idx = *(int8_t *)v;
return 0;
}
return -1;
}
/**
* Sets an ndarray data element.
*
* ## Notes
*
* - The function returns `-1` if unable to set an element and `0` otherwise.
* - The function requires a pointer to a data value `v` in order to provide a generic API supporting ndarrays having different data types.
*
* @param arr input ndarray
* @param sub ndarray subscripts
* @param v value to set
* @return status code
*/
int8_t stdlib_ndarray_set( const struct ndarray *arr, const int64_t *sub, const void *v ) {
void *idx = stdlib_ndarray_get_ptr( arr, sub );
if ( idx == NULL ) {
return -1;
}
return stdlib_ndarray_set_ptr( arr, idx, v );
}
/**
* Sets an ndarray data element located at a specified linear index.
*
* ## Notes
*
* - The function returns `-1` if unable to set an element and `0` otherwise.
* - The function requires a pointer to a data value `v` in order to provide a generic API supporting ndarrays having different data types.
*
* @param arr input ndarray
* @param idx linear view index
* @param v value to set
* @return status code
*/
int8_t stdlib_ndarray_iset( const struct ndarray *arr, const int64_t idx, const void *v ) {
void *ind = stdlib_ndarray_iget_ptr( arr, idx );
if ( ind == NULL ) {
return -1;
}
return stdlib_ndarray_set_ptr( arr, ind, v );
}
/**
* Returns a pointer to a dynamically allocated ndarray.
*
* ## Notes
*
* - The user is responsible for freeing the allocated memory.
*
* @param dtype data type
* @param data pointer to the underlying byte array
* @param ndims number of dimensions
* @param shape array shape (dimensions)
* @param strides array strides (in bytes)
* @param offset byte offset specifying the location of the first element
* @param order specifies whether an array is row-major (C-style) or column-major (Fortran-style)
* @param imode specifies how to handle indices which exceed array dimensions
* @param nsubmodes number of subscript modes
* @param submodes specifies how to handle subscripts which exceed array dimensions on a per dimension basis (if provided fewer submodes than dimensions, submodes are recycled using modulo arithmetic)
* @return pointer to a dynamically allocated ndarray or, if unable to allocate memory, a null pointer
*
* @example
* #include <stdlib.h>
* #include <stdio.h>
* #include <stdint.h>
* #include "stdlib/ndarray/base/bytes_per_element.h"
* #include "stdlib/ndarray/dtypes.h"
* #include "stdlib/ndarray/index_modes.h"
* #include "stdlib/ndarray/orders.h"
* #include "stdlib/ndarray/ctor.h"
*
* // Specify the underlying data type:
* enum STDLIB_NDARRAY_DTYPE dtype = STDLIB_NDARRAY_FLOAT64;
*
* // Create an underlying byte array:
* uint8_t buffer[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
*
* // Specify the number of array dimensions:
* int64_t ndims = 1;
*
* // Specify the array shape:
* int64_t shape[] = { 3 }; // vector consisting of 3 doubles
*
* // Specify the array strides:
* int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
*
* // Specify the byte offset:
* int64_t offset = 0;
*
* // Specify the array order (note: this does not matter for a 1-dimensional array):
* enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
*
* // Specify the index mode:
* enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
*
* // Specify the subscript index modes:
* enum STDLIB_NDARRAY_INDEX_MODE submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
* int64_t nsubmodes = 1;
*
* // Create an ndarray:
* struct ndarray *x = stdlib_ndarray_constructor( dtype, buffer, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
* if ( x == NULL ) {
* fprintf( stderr, "Error allocating memory.\n" );
* exit( 1 );
* }
*
* // Free allocated memory:
* free( x );
*/
struct ndarray * stdlib_ndarray_constructor( enum STDLIB_NDARRAY_DTYPE dtype, uint8_t *data, int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, enum STDLIB_NDARRAY_ORDER order, enum STDLIB_NDARRAY_INDEX_MODE imode, int64_t nsubmodes, enum STDLIB_NDARRAY_INDEX_MODE *submodes ) {
int64_t len;
struct ndarray *arr = malloc( sizeof( struct ndarray ) );
if ( arr == NULL ) {
return NULL;
}
arr->data = data;
arr->dtype = dtype;
arr->imode = imode;
arr->ndims = ndims;
arr->nsubmodes = nsubmodes;
arr->offset = offset;
arr->order = order;
arr->shape = shape;
arr->strides = strides;
arr->submodes = submodes;
len = stdlib_ndarray_numel( ndims, shape );
arr->length = len;
arr->BYTES_PER_ELEMENT = stdlib_ndarray_bytes_per_element( dtype );
arr->byteLength = len * (arr->BYTES_PER_ELEMENT);
arr->flags = stdlib_ndarray_flags( arr );
return arr;
}