Repository navigation
feat: add C implementation for blas/ext/base/ndarray/sdiff - #16640
utkarshpatwa393-sys wants to merge 6 commits into
Conversation
Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
Coverage Report
The above coverage report was generated for the changes in this PR. |
| static double random_uniform( const double min, const double max ) { | ||
| double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); |
There was a problem hiding this comment.
For single precision, random_uniform should use float, as in saxpb.
| static double random_uniform( const double min, const double max ) { | |
| double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); | |
| static float random_uniform( const float min, const float max ) { | |
| float v = (float)rand() / ( (float)RAND_MAX + 1.0f ); |
| int8_t submodes[ 1 ]; | ||
| int64_t strides[ 1 ]; | ||
| struct ndarray *prepend; | ||
| int64_t shapePrepend[ 1 ]; | ||
| int64_t shapeOut[ 1 ]; | ||
| int64_t shapeX[ 1 ]; | ||
| struct ndarray *append; | ||
| int64_t nsubmodes; | ||
| struct ndarray *out; | ||
| float *dataPrepend; | ||
| float *dataAppend; | ||
| float *dataWorkspace; | ||
| int32_t valK; | ||
| int32_t *dataK; | ||
| int64_t offset; | ||
| struct ndarray *k; | ||
| struct ndarray *x; |
There was a problem hiding this comment.
Declarations should be ordered by length, longest first. Also, dataPrepend, dataAppend and dataK each hold a single element, so they don't need malloc (see adata[ 1 ] in saxpb). This needs to be applied together with the suggestions below.
| int8_t submodes[ 1 ]; | |
| int64_t strides[ 1 ]; | |
| struct ndarray *prepend; | |
| int64_t shapePrepend[ 1 ]; | |
| int64_t shapeOut[ 1 ]; | |
| int64_t shapeX[ 1 ]; | |
| struct ndarray *append; | |
| int64_t nsubmodes; | |
| struct ndarray *out; | |
| float *dataPrepend; | |
| float *dataAppend; | |
| float *dataWorkspace; | |
| int32_t valK; | |
| int32_t *dataK; | |
| int64_t offset; | |
| struct ndarray *k; | |
| struct ndarray *x; | |
| int64_t shapePrepend[ 1 ]; | |
| struct ndarray *prepend; | |
| float dataPrepend[ 1 ]; | |
| struct ndarray *append; | |
| float dataAppend[ 1 ]; | |
| int64_t shapeOut[ 1 ]; | |
| int64_t kstrides[ 1 ]; | |
| float *dataWorkspace; | |
| int8_t submodes[ 1 ]; | |
| int64_t strides[ 1 ]; | |
| int64_t shapeX[ 1 ]; | |
| struct ndarray *out; | |
| int32_t dataK[ 1 ]; | |
| int64_t nsubmodes; | |
| struct ndarray *k; | |
| struct ndarray *x; | |
| int64_t offset; |
| shapePrepend[ 0 ] = 1; | ||
| shapeOut[ 0 ] = len + 1; | ||
| strides[ 0 ] = STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT; | ||
| int64_t kstrides[] = { 0 }; |
There was a problem hiding this comment.
Declare it with the others above, and only assign here.
| int64_t kstrides[] = { 0 }; | |
| kstrides[ 0 ] = 0; |
| dataPrepend = (float *)malloc( 1 * sizeof( float ) ); | ||
| dataAppend = (float *)malloc( 1 * sizeof( float ) ); | ||
| dataOut = (float *)malloc( ( len + 1 ) * sizeof( float ) ); | ||
| dataWorkspace = (float *)malloc( ( len + 1 ) * sizeof( float ) ); | ||
| dataK = (int32_t *)malloc( 1 * sizeof( int32_t ) ); | ||
|
|
||
| valK = 1; | ||
| dataK[ 0 ] = valK; | ||
| dataPrepend[ 0 ] = 1.0; | ||
| dataAppend[ 0 ] = 11.0; | ||
|
|
||
| for ( i = 0; i < len; i++ ) { | ||
| dataX[ i ] = random_uniform( -100.0, 100.0 ); | ||
| } | ||
| for ( i = 0; i < len + 1; i++ ) { | ||
| dataOut[ i ] = 0.0; | ||
| dataWorkspace[ i ] = 0.0; |
There was a problem hiding this comment.
| dataPrepend = (float *)malloc( 1 * sizeof( float ) ); | |
| dataAppend = (float *)malloc( 1 * sizeof( float ) ); | |
| dataOut = (float *)malloc( ( len + 1 ) * sizeof( float ) ); | |
| dataWorkspace = (float *)malloc( ( len + 1 ) * sizeof( float ) ); | |
| dataK = (int32_t *)malloc( 1 * sizeof( int32_t ) ); | |
| valK = 1; | |
| dataK[ 0 ] = valK; | |
| dataPrepend[ 0 ] = 1.0; | |
| dataAppend[ 0 ] = 11.0; | |
| for ( i = 0; i < len; i++ ) { | |
| dataX[ i ] = random_uniform( -100.0, 100.0 ); | |
| } | |
| for ( i = 0; i < len + 1; i++ ) { | |
| dataOut[ i ] = 0.0; | |
| dataWorkspace[ i ] = 0.0; | |
| dataOut = (float *)malloc( ( len + 1 ) * sizeof( float ) ); | |
| dataWorkspace = (float *)malloc( ( len + 1 ) * sizeof( float ) ); | |
| dataK[ 0 ] = 1; | |
| dataPrepend[ 0 ] = 1.0f; | |
| dataAppend[ 0 ] = 11.0f; | |
| for ( i = 0; i < len; i++ ) { | |
| dataX[ i ] = random_uniform( -100.0f, 100.0f ); | |
| } | |
| for ( i = 0; i < len + 1; i++ ) { | |
| dataOut[ i ] = 0.0f; | |
| dataWorkspace[ i ] = 0.0f; |
| // cppcheck-suppress invalidPointerCast | ||
| workspace = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataWorkspace, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | ||
|
|
||
| // 0-dimensional scalar ndarray for k: |
There was a problem hiding this comment.
| // 0-dimensional scalar ndarray for k: |
| free( dataPrepend ); | ||
| free( dataAppend ); | ||
| free( dataOut ); | ||
| free( dataWorkspace ); | ||
| free( dataK ); | ||
|
|
There was a problem hiding this comment.
| free( dataPrepend ); | |
| free( dataAppend ); | |
| free( dataOut ); | |
| free( dataWorkspace ); | |
| free( dataK ); | |
| free( dataOut ); | |
| free( dataWorkspace ); |
There was a problem hiding this comment.
Since lib/index.js now loads the native add-on when it's available, the JS benchmark should require main.js directly, as merged packages do (e.g., ssum).
| var sdiff = require( './../lib/main.js' ); |
| for ( i = min; i <= max; i++ ) { | ||
| len = pow( 10, i ); | ||
| f = createBenchmark( len ); | ||
| bench( format( '%s:len=%d', pkg, len ), f ); |
There was a problem hiding this comment.
The native benchmark needs the ::native label and opts. Otherwise it isn't skipped when the add-on isn't built, and its name is the same as the JS benchmark's. See dsum.
| bench( format( '%s:len=%d', pkg, len ), f ); | |
| bench( format( '%s::native:len=%d', pkg, len ), opts, f ); |
| const float dataX[] = { 2.0, 4.0, 6.0, 8.0, 10.0 }; | ||
| const float dataPrepend[] = { 1.0 }; | ||
| const float dataAppend[] = { 11.0 }; | ||
| float dataOut[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; | ||
| float dataW[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
There was a problem hiding this comment.
Use float literals for float buffers, as in ssum. The README already uses them.
| const float dataX[] = { 2.0, 4.0, 6.0, 8.0, 10.0 }; | |
| const float dataPrepend[] = { 1.0 }; | |
| const float dataAppend[] = { 11.0 }; | |
| float dataOut[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; | |
| float dataW[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; | |
| const float dataX[] = { 2.0f, 4.0f, 6.0f, 8.0f, 10.0f }; | |
| const float dataPrepend[] = { 1.0f }; | |
| const float dataAppend[] = { 11.0f }; | |
| float dataOut[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; | |
| float dataW[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| # @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) | ||
| # @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) | ||
| # @param {string} [SOURCE_FILES] - list of source files | ||
| # @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar`) |
There was a problem hiding this comment.
| # @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar`) | |
| # @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) |
| var workspace; | ||
| var prepend; | ||
| var append; | ||
| var out; | ||
| var x; | ||
| var k; | ||
|
|
||
| x = arrays[ 0 ]; | ||
| prepend = arrays[ 1 ]; | ||
| append = arrays[ 2 ]; | ||
| out = arrays[ 3 ]; | ||
| workspace = arrays[ 4 ]; | ||
| k = arrays[ 5 ]; | ||
|
|
||
| addon( | ||
| getData( x ), serialize( x ), | ||
| getData( prepend ), serialize( prepend ), | ||
| getData( append ), serialize( append ), | ||
| getData( out ), serialize( out ), | ||
| getData( workspace ), serialize( workspace ), | ||
| getData( k ), serialize( k ) | ||
| ); |
There was a problem hiding this comment.
Following the pattern in merged packages (e.g., dwxdy):
| var workspace; | |
| var prepend; | |
| var append; | |
| var out; | |
| var x; | |
| var k; | |
| x = arrays[ 0 ]; | |
| prepend = arrays[ 1 ]; | |
| append = arrays[ 2 ]; | |
| out = arrays[ 3 ]; | |
| workspace = arrays[ 4 ]; | |
| k = arrays[ 5 ]; | |
| addon( | |
| getData( x ), serialize( x ), | |
| getData( prepend ), serialize( prepend ), | |
| getData( append ), serialize( append ), | |
| getData( out ), serialize( out ), | |
| getData( workspace ), serialize( workspace ), | |
| getData( k ), serialize( k ) | |
| ); | |
| var workspace = arrays[ 4 ]; | |
| var prepend = arrays[ 1 ]; | |
| var append = arrays[ 2 ]; | |
| var out = arrays[ 3 ]; | |
| var x = arrays[ 0 ]; | |
| var k = arrays[ 5 ]; | |
| addon( getData( x ), serialize( x ), getData( prepend ), serialize( prepend ), getData( append ), serialize( append ), getData( out ), serialize( out ), getData( workspace ), serialize( workspace ), getData( k ), serialize( k ) ); // eslint-disable-line max-len |
| int32_t k; | ||
|
|
There was a problem hiding this comment.
Same layout as daxpb.
| int32_t k; | |
| int32_t k; |
| "dependencies": [ | ||
| "@stdlib/blas/base/shared", | ||
| "@stdlib/blas/ext/base/sdiff", | ||
| "@stdlib/blas/base/scopy", |
There was a problem hiding this comment.
scopy isn't used directly in src/main.c. It's already pulled in through @stdlib/blas/ext/base/sdiff. Apply same suggestion on on L64, L86 and L108
| "@stdlib/blas/base/scopy", |
| int64_t shapeOut[] = { 6 }; | ||
|
|
||
| // Specify the array strides: | ||
| int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; |
There was a problem hiding this comment.
0-d ndarrays are allocated with a zero-stride array and the submodes, as in saxpb.
| int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; | |
| int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; | |
| int64_t kstrides[] = { 0 }; |
| struct ndarray *append = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataAppend, ndims, shapePrepend, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *out = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataOut, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, NULL, 0, order, imode, 0, NULL ); |
There was a problem hiding this comment.
| struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, NULL, 0, order, imode, 0, NULL ); | |
| struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, kstrides, 0, order, imode, nsubmodes, submodes ); |
| int64_t shapeOut[] = { 6 }; | ||
|
|
||
| // Specify the array strides: | ||
| int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; |
There was a problem hiding this comment.
The README example should match examples/c/example.c exactly.
| int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; | |
| int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; | |
| int64_t kstrides[] = { 0 }; |
| struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataX, ndims, shapeX, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *prepend = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataPrepend, ndims, shapePrepend, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *append = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataAppend, ndims, shapePrepend, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *out = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataOut, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | ||
| struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, NULL, 0, order, imode, 0, NULL ); |
There was a problem hiding this comment.
| struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataX, ndims, shapeX, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *prepend = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataPrepend, ndims, shapePrepend, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *append = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataAppend, ndims, shapePrepend, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *out = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataOut, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, NULL, 0, order, imode, 0, NULL ); | |
| struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataX, ndims, shapeX, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *prepend = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataPrepend, ndims, shapePrepend, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *append = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataAppend, ndims, shapePrepend, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *out = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataOut, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, ndims, shapeOut, strides, offset, order, imode, nsubmodes, submodes ); | |
| struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, kstrides, 0, order, imode, nsubmodes, submodes ); |
|
|
||
| // Print the result: | ||
| for ( int i = 0; i < 6; i++ ) { | ||
| printf( "out[ %i ] = %f\n", i, (double)dataOut[ i ] ); |
There was a problem hiding this comment.
| printf( "out[ %i ] = %f\n", i, (double)dataOut[ i ] ); | |
| printf( "out[ %i ] = %f\n", i, dataOut[ i ] ); |
|
Also, the PR description doesn't follow the PR template. The "Related Issues", "Reference Packages", "Questions", "Other" and "AI Assistance" sections are missing. Update it too. |
|
@utkarshpatwa393-sys , In general, you need to pay much closer attention to how other packages are structured. Only the minimal essential changes should be present among separate similar packages. |
…enchmarks, README, and native bindings Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
|
Hi @ujjwalv01, thank you for the detailed and constructive review! I have addressed all the feedback in the latest commit:
Please take another look when you have a chance. Thanks! |
ujjwalv01
left a comment
There was a problem hiding this comment.
LGTM. Thanks @utkarshpatwa393-sys , for working through the review!
|
I noticed your PR description contains closing keywords ("Resolves", "Closes", or "Fixes") referencing a "Tracking Issue". Why this matters: Required action: Thank you for your contribution to the project! |
Resolves a part of #14819.
Description
This pull request:
@stdlib/blas/ext/base/ndarray/sdiff.include/stdlib/blas/ext/base/ndarray/sdiff.h), C implementation (src/main.c), Node-API add-on wrapper (src/addon.c), build configuration (binding.gyp,include.gypi,manifest.json,src/Makefile), and loader dispatch (lib/native.js).test/test.main.js,test/test.native.js), C benchmarks (benchmark/c/), native benchmarks (benchmark/benchmark.native.js), C example (examples/c/example.c), and TypeScript definitions (docs/types/).Related Issues
This pull request has the following related issues:
blas/ext/base/ndarraypackages #14819Reference Packages
This pull request followed these packages:
blas/ext/base/ndarray/saxpbblas/ext/base/ndarray/ssumblas/ext/base/ndarray/dwxdykparameter handling across C and Node-API boundaries.Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
I consulted AI assistance for understanding codebase conventions, cross-referencing package documentation styles, and structuring the PR description per the template, but all implementation code, C bindings, and test files were authored and verified directly against stdlib conventions.
@stdlib-js/reviewers