Skip to content

Commit dd45a8f

Browse files
committed
feat: add function to return a dimension stride in units of elements
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent b00ba84 commit dd45a8f

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

lib/node_modules/@stdlib/ndarray/ctor/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,49 @@ Notes:
15921592

15931593
- The function does perform bounds checking for the dimension index.
15941594

1595+
#### stdlib_ndarray_stride_elements( \*arr, i )
1596+
1597+
Returns an ndarray stride (in elements).
1598+
1599+
```c
1600+
#include "stdlib/ndarray/ctor.h"
1601+
#include <stdint.h>
1602+
#include <stdlib.h>
1603+
#include <stdio.h>
1604+
1605+
// ...
1606+
1607+
// Create an ndarray:
1608+
struct ndarray *x = stdlib_ndarray_allocate( ... );
1609+
if ( x == NULL ) {
1610+
fprintf( stderr, "Error allocating memory.\n" );
1611+
exit( 1 );
1612+
}
1613+
1614+
// ...
1615+
1616+
// Retrieve a stride:
1617+
int64_t s = stdlib_ndarray_stride_elements( x, 0 );
1618+
1619+
// ...
1620+
1621+
// Free allocated memory:
1622+
stdlib_ndarray_free( x );
1623+
```
1624+
1625+
The function accepts the following arguments:
1626+
1627+
- **arr**: `[in] struct ndarray*` input ndarray.
1628+
- **i**: `[in] int64_t` dimension index.
1629+
1630+
```c
1631+
int64_t stdlib_ndarray_stride_elements( const struct ndarray *arr, const int64_t i );
1632+
```
1633+
1634+
Notes:
1635+
1636+
- The function does perform bounds checking for the dimension index.
1637+
15951638
#### stdlib_ndarray_strides( \*arr )
15961639

15971640
Returns a pointer to an array containing ndarray strides (in bytes).

lib/node_modules/@stdlib/ndarray/ctor/benchmark/c/benchmark.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3218,6 +3218,51 @@ static double benchmark65( void ) {
32183218
return elapsed;
32193219
}
32203220

3221+
/**
3222+
* Runs a benchmark.
3223+
*
3224+
* @return elapsed time in seconds
3225+
*/
3226+
static double benchmark66( void ) {
3227+
double elapsed;
3228+
int64_t v;
3229+
int64_t j;
3230+
double t;
3231+
int i;
3232+
3233+
uint8_t buffer[] = { 0, 0, 0, 0, 0, 0 };
3234+
int64_t ndims = 2;
3235+
int64_t shape[] = { 3, 2 };
3236+
int64_t strides[] = { -2, -1 };
3237+
int64_t offset = 5;
3238+
int64_t nsubmodes = 1;
3239+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
3240+
3241+
struct ndarray *arr = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, buffer, ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, nsubmodes, submodes );
3242+
if ( arr == NULL ) {
3243+
printf( "unable to allocate memory\n" );
3244+
exit( 1 );
3245+
}
3246+
3247+
t = tic();
3248+
for ( i = 0; i < ITERATIONS; i++ ) {
3249+
j = (int64_t)( rand_double()*ndims );
3250+
v = stdlib_ndarray_stride_elements( arr, j );
3251+
if ( v != strides[ j ] ) {
3252+
printf( "unexpected result\n" );
3253+
break;
3254+
}
3255+
}
3256+
elapsed = tic() - t;
3257+
3258+
if ( v != strides[ j ] ) {
3259+
printf( "unexpected result\n" );
3260+
}
3261+
stdlib_ndarray_free( arr );
3262+
3263+
return elapsed;
3264+
}
3265+
32213266
/**
32223267
* Main execution sequence.
32233268
*/
@@ -3687,5 +3732,12 @@ int main( void ) {
36873732
print_results( elapsed );
36883733
printf( "ok %d benchmark finished\n", count );
36893734
}
3735+
for ( i = 0; i < REPEATS; i++ ) {
3736+
count += 1;
3737+
printf( "# c::native::%s::get:stride_elements\n", NAME );
3738+
elapsed = benchmark66();
3739+
print_results( elapsed );
3740+
printf( "ok %d benchmark finished\n", count );
3741+
}
36903742
print_summary( count, count );
36913743
}

lib/node_modules/@stdlib/ndarray/ctor/include/stdlib/ndarray/ctor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ int64_t * stdlib_ndarray_shape( const struct ndarray *arr );
133133
*/
134134
int64_t stdlib_ndarray_stride( const struct ndarray *arr, const int64_t i );
135135

136+
/**
137+
* Returns an ndarray stride (in elements).
138+
*/
139+
int64_t stdlib_ndarray_stride_elements( const struct ndarray *arr, const int64_t i );
140+
136141
/**
137142
* Returns a pointer to an array containing ndarray strides (in bytes).
138143
*/

lib/node_modules/@stdlib/ndarray/ctor/src/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,21 @@ int64_t stdlib_ndarray_stride( const struct ndarray *arr, const int64_t i ) {
413413
return arr->strides[ i ];
414414
}
415415

416+
/**
417+
* Returns an ndarray stride (in elements).
418+
*
419+
* ## Notes
420+
*
421+
* - The function does not perform any sanity checks.
422+
*
423+
* @param arr input ndarray
424+
* @param i dimension index
425+
* @return array stride
426+
*/
427+
int64_t stdlib_ndarray_stride_elements( const struct ndarray *arr, const int64_t i ) {
428+
return arr->strides[ i ] / arr->BYTES_PER_ELEMENT;
429+
}
430+
416431
/**
417432
* Returns a pointer to an array containing ndarray strides (in bytes).
418433
*

0 commit comments

Comments
 (0)