main.c (11671B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "stdlib/strided/base/function_object.h" 20 #include <stdlib.h> 21 #include <stdint.h> 22 23 /** 24 * Returns the first row index at which a given one-dimensional array of types can be found in a two-dimensional reference array of types (or `-1` if not found). 25 * 26 * ## Notes 27 * 28 * - The intended use case for this function is for type dispatch (i.e., given a set of array data types, find a matching interface according the interface's accepted array data types). 29 * - The function assumes that `X` is stored in row-major order. 30 * - The function assumes that the number of indexed elements in `Y` equals the number of columns in `X`. 31 * - The function returns a row index. To convert to a linear index, multiply `strideX1` by the return value. 32 * 33 * @private 34 * @param N number of rows in `X` (size of first dimension) 35 * @param M number of columns in `X` (size of second dimension) 36 * @param X input two-dimensional reference array 37 * @param strideX1 `X` stride length along first dimension 38 * @param strideX2 `X` stride length along second dimension 39 * @param Y search array 40 * @param strideY `Y` stride length 41 * @return row index (if found) and `-1` otherwise 42 * 43 * @example 44 * #include "stdlib/strided/base/function_object.h" 45 * #include "stdlib/strided/dtypes.h" 46 * #include <stdint.h> 47 * 48 * // Define a reference array to search: 49 * int32_t X[] = { 50 * STDLIB_STRIDED_FLOAT64, STDLIB_STRIDED_FLOAT64, STDLIB_STRIDED_FLOAT64, 51 * STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT32, 52 * STDLIB_STRIDED_UINT32, STDLIB_STRIDED_UINT32, STDLIB_STRIDED_FLOAT64, 53 * STDLIB_STRIDED_INT32, STDLIB_STRIDED_INT32, STDLIB_STRIDED_FLOAT64, 54 * STDLIB_STRIDED_UINT16, STDLIB_STRIDED_UINT16, STDLIB_STRIDED_FLOAT64, 55 * STDLIB_STRIDED_INT16, STDLIB_STRIDED_INT16, STDLIB_STRIDED_FLOAT64, 56 * STDLIB_STRIDED_UINT8, STDLIB_STRIDED_UINT8, STDLIB_STRIDED_FLOAT64, 57 * STDLIB_STRIDED_INT8, STDLIB_STRIDED_INT8, STDLIB_STRIDED_FLOAT64 58 * }; 59 * 60 * // Define reference array dimensions: 61 * int64_t N = 8; // rows 62 * int64_t M = 3; // columns 63 * 64 * // Define a search array: 65 * int32_t Y1[] = { 66 * STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT32, 67 * }; 68 * 69 * // Find the list of types: 70 * int64_t r1 = stdlib_strided_function_dispatch_types_index_of( N, M, X, M, 1, Y1, 1 ); 71 * // returns 1 72 * 73 * // Define a search array: 74 * int32_t Y2[] = { 75 * STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT64, 76 * }; 77 * 78 * // Find the list of types: 79 * int64_t r2 = stdlib_strided_function_dispatch_types_index_of( N, M, X, M, 1, Y2, 1 ); 80 * // returns -1 81 */ 82 static int64_t stdlib_strided_function_dispatch_types_index_of( const int64_t N, const int64_t M, const int32_t *X, const int64_t strideX1, const int64_t strideX2, const int32_t *Y, const int64_t strideY ) { 83 int64_t ox; 84 int64_t oy; 85 int64_t ix; 86 int64_t iy; 87 int64_t i; 88 int64_t j; 89 90 // Determine initial starting indices (offsets)... 91 if ( strideX1 < 0 ) { 92 ox = (1-N) * strideX1; 93 } else { 94 ox = 0; 95 } 96 if ( strideX2 < 0 ) { 97 ox += (1-M) * strideX2; 98 } 99 if ( strideY < 0 ) { 100 oy = (1-M) * strideY; 101 } else { 102 oy = 0; 103 } 104 // Search for the first row which matches `Y`... 105 ix = ox; 106 for ( i = 0; i < N; i++ ) { 107 iy = oy; 108 for ( j = 0; j < M; j++ ) { 109 if ( X[ ix+(j*strideX2) ] != Y[ iy ] ) { 110 break; 111 } 112 iy += strideY; 113 } 114 // If we successfully iterated over all columns, then that means we've found a match... 115 if ( j == M ) { 116 return i; 117 } 118 ix += strideX1; 119 } 120 return -1; 121 } 122 123 /** 124 * Returns a pointer to a dynamically allocated strided array function object. 125 * 126 * ## Notes 127 * 128 * - The user is responsible for freeing the allocated memory. 129 * 130 * @param name strided array function name 131 * @param nin number of input strided arrays 132 * @param nout number of output strided arrays 133 * @param functions array containing strided array functions 134 * @param nfunctions number of strided array functions 135 * @param types array of type "numbers", where the total number of types equals `(nin+nout)*nfunctions` and where each set of `nin+nout` consecutive types (non-overlapping) corresponds to the set of strided array argument types for a corresponding strided array function 136 * @param data array of void pointers corresponding to the "data" (e.g., callbacks) which should be passed to a respective strided array function 137 * @return pointer to a dynamically allocated strided array function object or, if unable to allocate memory, a null pointer 138 * 139 * @example 140 * #include "stdlib/strided/base/function_object.h" 141 * #include "stdlib/strided/base/unary.h" 142 * #include "stdlib/strided/dtypes.h" 143 * #include <stdlib.h> 144 * #include <stdio.h> 145 * 146 * // Define the function(s) we want to apply to strided arrays: 147 * double scale( const double x ) { 148 * return x * 10.0; 149 * } 150 * 151 * // Define a function name: 152 * const char name[] = "unary_strided_array_function"; 153 * 154 * // Define a list of strided array functions (in this case, as the function to be applied accepts doubles, we only use strided array functions which handle doubles as function arguments and, for the purposes of this example, we assume that the output strided array is always a double-precision floating-point number array): 155 * StridedArrayFcn functions[] = { 156 * stdlib_strided_d_d, 157 * stdlib_strided_f_f_as_d_d, 158 * stdlib_strided_u_d_as_d_d, 159 * stdlib_strided_i_d_as_d_d, 160 * stdlib_strided_t_d_as_d_d, 161 * stdlib_strided_k_d_as_d_d, 162 * stdlib_strided_b_d_as_d_d, 163 * stdlib_strided_s_d_as_d_d 164 * }; 165 * 166 * // Define the **strided array** argument types for each strided array function: 167 * int32_t types[] = { 168 * STDLIB_STRIDED_FLOAT64, STDLIB_STRIDED_FLOAT64, 169 * STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT64, 170 * STDLIB_STRIDED_UINT32, STDLIB_STRIDED_FLOAT64, 171 * STDLIB_STRIDED_INT32, STDLIB_STRIDED_FLOAT64, 172 * STDLIB_STRIDED_UINT16, STDLIB_STRIDED_FLOAT64, 173 * STDLIB_STRIDED_INT16, STDLIB_STRIDED_FLOAT64, 174 * STDLIB_STRIDED_UINT8, STDLIB_STRIDED_FLOAT64, 175 * STDLIB_STRIDED_INT8, STDLIB_STRIDED_FLOAT64 176 * }; 177 * 178 * // Define a list of strided array function "data" (in this case, callbacks): 179 * void *data[] = { 180 * (void *)scale, 181 * (void *)scale, 182 * (void *)scale, 183 * (void *)scale, 184 * (void *)scale, 185 * (void *)scale, 186 * (void *)scale, 187 * (void *)scale 188 * }; 189 * 190 * // Create a new strided function object: 191 * struct StridedFunctionObject *obj = stdlib_strided_function_allocate( name, 1, 1, functions, 8, types, data ); 192 * if ( obj == NULL ) { 193 * fprintf( stderr, "Error allocating memory.\n" ); 194 * exit( 1 ); 195 * } 196 * 197 * // Free allocated memory: 198 * stdlib_strided_function_free( obj ); 199 */ 200 struct StridedFunctionObject * stdlib_strided_function_allocate( const char *name, int32_t nin, int32_t nout, StridedArrayFcn *functions, int32_t nfunctions, int32_t *types, void *data[] ) { 201 struct StridedFunctionObject *obj = malloc( sizeof( struct StridedFunctionObject ) ); 202 if ( obj == NULL ) { 203 return NULL; 204 } 205 obj->name = name; 206 obj->nin = nin; 207 obj->nout = nout; 208 obj->narrays = nin + nout; 209 obj->functions = functions; 210 obj->nfunctions = nfunctions; 211 obj->types = types; 212 obj->data = data; 213 return obj; 214 } 215 216 /** 217 * Frees a strided array function object's allocated memory. 218 * 219 * @param obj strided array function object 220 * 221 * @example 222 * #include "stdlib/strided/base/function_object.h" 223 * #include "stdlib/strided/base/unary.h" 224 * #include "stdlib/strided/dtypes.h" 225 * #include <stdlib.h> 226 * #include <stdio.h> 227 * 228 * // Define the function(s) we want to apply to strided arrays: 229 * double scale( const double x ) { 230 * return x * 10.0; 231 * } 232 * 233 * // Define a function name: 234 * const char name[] = "unary_strided_array_function"; 235 * 236 * // Define a list of strided array functions: 237 * StridedArrayFcn functions[] = { 238 * stdlib_strided_d_d 239 * }; 240 * 241 * // Define the **strided array** argument types for each strided array function: 242 * int32_t types[] = { 243 * STDLIB_STRIDED_FLOAT64, STDLIB_STRIDED_FLOAT64 244 * }; 245 * 246 * // Define a list of strided array function "data" (in this case, callbacks): 247 * void *data[] = { 248 * (void *)scale 249 * }; 250 * 251 * // Create a new strided function object: 252 * struct StridedFunctionObject *obj = stdlib_strided_function_allocate( name, 1, 1, functions, 1, types, data ); 253 * if ( obj == NULL ) { 254 * fprintf( stderr, "Error allocating memory.\n" ); 255 * exit( 1 ); 256 * } 257 * 258 * // ... 259 * 260 * // Free allocated memory: 261 * stdlib_strided_function_free( obj ); 262 */ 263 void stdlib_strided_function_free( struct StridedFunctionObject *obj ) { 264 if ( obj == NULL ) { 265 return; 266 } 267 free( obj ); 268 } 269 270 /** 271 * Returns the first index of a function whose signature satisfies a provided list of array types. 272 * 273 * ## Notes 274 * 275 * - The function returns `-1` if unable to find a function. 276 * 277 * @param obj strided array function object 278 * @param types list of array types on which to dispatch 279 * @return function index (if found) and `-1` otherwise 280 * 281 * @example 282 * #include "stdlib/strided/base/function_object.h" 283 * #include "stdlib/strided/base/unary.h" 284 * #include "stdlib/strided/dtypes.h" 285 * #include <stdlib.h> 286 * #include <stdio.h> 287 * 288 * // Define the function(s) we want to apply to strided arrays: 289 * double scale( const double x ) { 290 * return x * 10.0; 291 * } 292 * 293 * // ... 294 * 295 * // Define a function name: 296 * const char name[] = "unary_strided_array_function"; 297 * 298 * // Define a list of strided array functions: 299 * StridedArrayFcn functions[] = { 300 * stdlib_strided_d_d, 301 * stdlib_strided_f_f_as_d_d 302 * }; 303 * 304 * // Define the **strided array** argument types for each strided array function: 305 * int32_t types[] = { 306 * STDLIB_STRIDED_FLOAT64, STDLIB_STRIDED_FLOAT64, 307 * STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT64 308 * }; 309 * 310 * // Define a list of strided array function "data" (in this case, callbacks): 311 * void *data[] = { 312 * (void *)scale, 313 * (void *)scale 314 * }; 315 * 316 * // Create a new strided function object: 317 * struct StridedFunctionObject *obj = stdlib_strided_function_allocate( name, 1, 1, functions, 2, types, data ); 318 * if ( obj == NULL ) { 319 * fprintf( stderr, "Error allocating memory.\n" ); 320 * exit( 1 ); 321 * } 322 * 323 * // ... 324 * 325 * // Define a list of types on which to dispatch: 326 * int32_t itypes[] = { 327 * STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT64 328 * }; 329 * 330 * // Find a function satisfying the list of types: 331 * int64_t idx = stdlib_strided_function_dispatch_index_of( obj, itypes ); 332 * if ( idx < 0 ) { 333 * fprintf( stderr, "Unable to find function.\n" ); 334 * exit( 1 ); 335 * } 336 * 337 * // ... 338 * 339 * // Free allocated memory: 340 * stdlib_strided_function_free( obj ); 341 */ 342 int64_t stdlib_strided_function_dispatch_index_of( const struct StridedFunctionObject *obj, const int32_t *types ) { 343 if ( obj == NULL ) { 344 return -1; 345 } 346 // Retrieve the number of functions (and thus the number of type signatures): 347 int32_t N = obj->nfunctions; 348 349 // Retrieve the number of array arguments: 350 int32_t M = obj->narrays; 351 352 // Find the index of the function satisfying the provided types: 353 return stdlib_strided_function_dispatch_types_index_of( (int64_t)N, M, obj->types, M, 1, types, 1 ); 354 }