-
-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy pathexample.c
199 lines (166 loc) · 5.8 KB
/
example.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
/**
* @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 <stdio.h>
#include <stdint.h>
#include "stdlib/ndarray/base/bytes_per_element.h"
#include "stdlib/ndarray/base/dtype_char.h"
#include "stdlib/ndarray/dtypes.h"
#include "stdlib/ndarray/index_modes.h"
#include "stdlib/ndarray/orders.h"
#include "stdlib/ndarray/ctor.h"
void print_ndarray_contents( const struct ndarray *x ) {
uint8_t *ptr;
int64_t i;
for ( i = 0; i < stdlib_ndarray_length( x ); i++ ) {
ptr = stdlib_ndarray_iget_ptr( x, i );
if ( ptr == NULL ) {
printf( "Unable to resolve data pointer.\n" );
exit( 1 );
}
printf( "data[%lld] = %f\n", i, *(double *)ptr ); // WARNING: assumes `x->dtype` is float64
}
}
int main() {
// Manually create an ndarray (WARNING: this is for illustration purposes only, as the fields of an ndarray are subject to change; for ABI compatibility, use utility functions for accessing ndarray data)...
struct ndarray *x1 = malloc( sizeof( struct ndarray ) );
if ( x1 == NULL ) {
printf( "Error allocating memory.\n" );
exit( 1 );
}
// Specify the underlying data type:
enum STDLIB_NDARRAY_DTYPE dtype = STDLIB_NDARRAY_FLOAT64;
x1->dtype = dtype;
// 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 };
x1->data = buffer;
// Explicitly specify the number of bytes per element:
x1->BYTES_PER_ELEMENT = STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT;
// Specify the array shape:
int64_t shape[] = { 3 }; // vector consisting of 3 doubles
x1->shape = shape;
// Specify the array strides:
int64_t strides[] = { x1->BYTES_PER_ELEMENT };
x1->strides = strides;
// Specify the byte offset:
x1->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;
x1->order = order;
// Specify the index mode:
enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
x1->imode = imode;
// Specify the subscript index modes:
enum STDLIB_NDARRAY_INDEX_MODE submodes[] = { imode };
x1->submodes = submodes;
x1->nsubmodes = 1;
// Explicitly specify the number of array dimensions:
x1->ndims = 1; // vector
// Explicitly specify the number of array elements (doubles):
x1->length = x1->shape[ 0 ];
// Explicitly specify the number of bytes:
x1->byteLength = (x1->length) * (x1->BYTES_PER_ELEMENT);
// Explicitly set the array flags:
x1->flags = stdlib_ndarray_flags( x1 );
printf( "dtype = %u\n", stdlib_ndarray_dtype( x1 ) );
printf( "length = %lld\n", stdlib_ndarray_length( x1 ) );
printf( "byteLength = %lld\n", stdlib_ndarray_bytelength( x1 ) );
printf( "ltr = %u\n", stdlib_ndarray_dtype_char( stdlib_ndarray_dtype( x1 ) ) );
// Use the function interface to create an ndarray (NOTE: for future ABI compatibility, using the following function interface should be preferred)...
struct ndarray *x2 = stdlib_ndarray_constructor( dtype, buffer, 1, shape, strides, 0, order, imode, 1, submodes );
if ( x2 == NULL ) {
printf( "Error allocating memory.\n" );
exit( 1 );
}
printf( "dtype = %u\n", stdlib_ndarray_dtype( x2 ) );
printf( "length = %lld\n", stdlib_ndarray_length( x2 ) );
printf( "byteLength = %lld\n", stdlib_ndarray_bytelength( x2 ) );
printf( "ltr = %u\n", stdlib_ndarray_dtype_char( stdlib_ndarray_dtype( x2 ) ) );
// Set values in the underlying byte array using pointers:
int64_t sub[] = { 0 };
uint8_t *ptr = stdlib_ndarray_get_ptr( x2, sub );
if ( ptr == NULL ) {
printf( "Unable to resolve data pointer.\n" );
exit( 1 );
}
*(double *)ptr = 1.0;
sub[ 0 ] = 1;
ptr = stdlib_ndarray_get_ptr( x2, sub );
if ( ptr == NULL ) {
printf( "Unable to resolve data pointer.\n" );
exit( 1 );
}
*(double *)ptr = 2.0;
sub[ 0 ] = 2;
ptr = stdlib_ndarray_get_ptr( x2, sub );
if ( ptr == NULL ) {
printf( "Unable to resolve data pointer.\n" );
exit( 1 );
}
*(double *)ptr = 3.0;
// Print out the current ndarray elements...
print_ndarray_contents( x2 );
// Set values in the underlying byte array using a "generic" function:
sub[ 0 ] = 0;
double v = 4.0;
int8_t status = stdlib_ndarray_set( x2, sub, (void *)&v );
if ( status != 0 ) {
printf( "Unable to set data element.\n" );
exit( 1 );
}
sub[ 0 ] = 1;
v = 5.0;
status = stdlib_ndarray_set( x2, sub, (void *)&v );
if ( status != 0 ) {
printf( "Unable to set data element.\n" );
exit( 1 );
}
sub[ 0 ] = 2;
v = 6.0;
status = stdlib_ndarray_set( x2, sub, (void *)&v );
if ( status != 0 ) {
printf( "Unable to set data element.\n" );
exit( 1 );
}
// Print out the current ndarray elements...
print_ndarray_contents( x2 );
// Set values in the underlying byte array using a specialized function:
sub[ 0 ] = 0;
status = stdlib_ndarray_set_float64( x2, sub, 7.0 );
if ( status != 0 ) {
printf( "Unable to set data element.\n" );
exit( 1 );
}
sub[ 0 ] = 1;
status = stdlib_ndarray_set_float64( x2, sub, 8.0 );
if ( status != 0 ) {
printf( "Unable to set data element.\n" );
exit( 1 );
}
sub[ 0 ] = 2;
status = stdlib_ndarray_set_float64( x2, sub, 9.0 );
if ( status != 0 ) {
printf( "Unable to set data element.\n" );
exit( 1 );
}
// Print out the current ndarray elements...
print_ndarray_contents( x2 );
// Free the allocated memory:
free( x1 );
free( x2 );
}