Skip to content

Commit

Permalink
refactor: update add-on, examples, benchmarks, and docs
Browse files Browse the repository at this point in the history
This commit is part of the work discussed in #788 and updates `@stdlib/blas/base/sdot` according to current project conventions, particularly as concerns the native add-on bridge between JavaScript and C.

PR-URL: #798
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
Pranavchiku and kgryte authored Mar 1, 2023
1 parent 5b98beb commit 663577c
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 310 deletions.
39 changes: 11 additions & 28 deletions lib/node_modules/@stdlib/blas/base/sdot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@license Apache-2.0
Copyright (c) 2019 The Stdlib Authors.
Copyright (c) 2023 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.
Expand Down Expand Up @@ -69,18 +69,15 @@ The function has the following parameters:
- **y**: input [`Float32Array`][@stdlib/array/float32].
- **strideY**: index increment for `y`.

The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to calculate the dot product of every other value in `x` and the first `N` elements of `y` in reverse order,
The `N` and stride parameters determine which elements in `x` and `y` are accessed at runtime. For example, to calculate the dot product of every other value in `x` and the first `N` elements of `y` in reverse order,

```javascript
var Float32Array = require( '@stdlib/array/float32' );
var floor = require( '@stdlib/math/base/special/floor' );

var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Float32Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );

var N = floor( x.length / 2 );

var z = sdot( N, x, 2, y, -1 );
var z = sdot( 3, x, 2, y, -1 );
// returns 9.0
```

Expand All @@ -90,7 +87,6 @@ Note that indexing is relative to the first index. To introduce an offset, use [

```javascript
var Float32Array = require( '@stdlib/array/float32' );
var floor = require( '@stdlib/math/base/special/floor' );

// Initial arrays...
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
Expand All @@ -100,9 +96,7 @@ var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element

var N = floor( x0.length / 2 );

var z = sdot( N, x1, -2, y1, 1 );
var z = sdot( 3, x1, -2, y1, 1 );
// returns 128.0
```

Expand All @@ -125,18 +119,15 @@ The function has the following additional parameters:
- **offsetX**: starting index for `x`.
- **offsetY**: starting index for `y`.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offsetX` and `offsetY` parameters support indexing semantics based on starting indices. For example, to calculate the dot product of every other value in `x` starting from the second value with the last 3 elements in `y` in reverse order
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the offset parameters support indexing semantics based on starting indices. For example, to calculate the dot product of every other value in `x` starting from the second value with the last 3 elements in `y` in reverse order

```javascript
var Float32Array = require( '@stdlib/array/float32' );
var floor = require( '@stdlib/math/base/special/floor' );

var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );

var N = floor( x.length / 2 );

var z = sdot.ndarray( N, x, 2, 1, y, -1, y.length-1 );
var z = sdot.ndarray( 3, x, 2, 1, y, -1, y.length-1 );
// returns 128.0
```

Expand All @@ -162,22 +153,14 @@ var z = sdot.ndarray( N, x, 2, 1, y, -1, y.length-1 );
<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var Float32Array = require( '@stdlib/array/float32' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var sdot = require( '@stdlib/blas/base/sdot' );

var x;
var y;
var i;

x = new Float32Array( 10 );
y = new Float32Array( 10 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( randu()*100.0 );
y[ i ] = round( randu()*10.0 );
}
var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) );
console.log( x );

var y = filledarrayBy( x.length, 'float32', discreteUniform( 0, 10 ) );
console.log( y );

var z = sdot( x.length, x, 1, y, -1 );
Expand Down
23 changes: 10 additions & 13 deletions lib/node_modules/@stdlib/blas/base/sdot/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
* Copyright (c) 2023 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.
Expand All @@ -21,14 +21,19 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
var pkg = require( './../package.json' ).name;
var sdot = require( './../lib/sdot.js' );


// VARIABLES //

var rand = uniform( -100.0, 100.0 );


// FUNCTIONS //

/**
Expand All @@ -39,16 +44,8 @@ var sdot = require( './../lib/sdot.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
y[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = filledarrayBy( len, 'float32', rand );
var y = filledarrayBy( len, 'float32', rand );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2023 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.
Expand All @@ -22,10 +22,10 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;

Expand All @@ -36,6 +36,7 @@ var sdot = tryRequire( resolve( __dirname, './../lib/sdot.native.js' ) );
var opts = {
'skip': ( sdot instanceof Error )
};
var rand = uniform( -100.0, 100.0 );


// FUNCTIONS //
Expand All @@ -48,16 +49,8 @@ var opts = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
y[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = filledarrayBy( len, 'float32', rand );
var y = filledarrayBy( len, 'float32', rand );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
* Copyright (c) 2023 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.
Expand All @@ -21,14 +21,19 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
var pkg = require( './../package.json' ).name;
var sdot = require( './../lib/ndarray.js' );


// VARIABLES //

var rand = uniform( -100.0, 100.0 );


// FUNCTIONS //

/**
Expand All @@ -39,16 +44,8 @@ var sdot = require( './../lib/ndarray.js' );
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
y[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = filledarrayBy( len, 'float32', rand );
var y = filledarrayBy( len, 'float32', rand );
return benchmark;

function benchmark( b ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2023 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.
Expand All @@ -22,10 +22,10 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var Float32Array = require( '@stdlib/array/float32' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;

Expand All @@ -36,6 +36,7 @@ var sdot = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
var opts = {
'skip': ( sdot instanceof Error )
};
var rand = uniform( -100.0, 100.0 );


// FUNCTIONS //
Expand All @@ -48,16 +49,8 @@ var opts = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x;
var y;
var i;

x = new Float32Array( len );
y = new Float32Array( len );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = ( randu()*20.0 ) - 10.0;
y[ i ] = ( randu()*20.0 ) - 10.0;
}
var x = filledarrayBy( len, 'float32', rand );
var y = filledarrayBy( len, 'float32', rand );
return benchmark;

function benchmark( b ) {
Expand Down
24 changes: 10 additions & 14 deletions lib/node_modules/@stdlib/blas/base/sdot/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{{alias}}( N, x, strideX, y, strideY )
Computes the dot product of two single-precision floating-point vectors.

The `N`, `strideX`, and `strideY` parameters determine which elements in `x`
and `y` are accessed at runtime.
The `N` and stride parameters determine which elements in the strided arrays
are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
Expand All @@ -30,7 +30,7 @@
Returns
-------
dot: float
The dot product of `x` and `y`.
The dot product.

Examples
--------
Expand All @@ -43,26 +43,24 @@
// Strides:
> x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
> y = new {{alias:@stdlib/array/float32}}( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> dot = {{alias}}( N, x, 2, y, -1 )
> dot = {{alias}}( 3, x, 2, y, -1 )
9.0

// Using view offsets:
> x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
> y = new {{alias:@stdlib/array/float32}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
> var x1 = new {{alias:@stdlib/array/float32}}( x.buffer, x.BYTES_PER_ELEMENT*1 );
> var y1 = new {{alias:@stdlib/array/float32}}( y.buffer, y.BYTES_PER_ELEMENT*3 );
> N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> dot = {{alias}}( N, x1, -2, y1, 1 )
> dot = {{alias}}( 3, x1, -2, y1, 1 )
128.0


{{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
Computes the dot product of two single-precision floating-point vectors
using alternative indexing semantics.

While typed array views mandate a view offset based on the underlying
buffer, the `offsetX` and `offsetY` parameters support indexing based on a
starting index.
buffer, the offset parameters support indexing based on a starting index.

Parameters
----------
Expand Down Expand Up @@ -90,7 +88,7 @@
Returns
-------
dot: float
The dot product of `x` and `y`.
The dot product.

Examples
--------
Expand All @@ -103,15 +101,13 @@
// Strides:
> x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
> y = new {{alias:@stdlib/array/float32}}( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> dot = {{alias}}.ndarray( N, x, 2, 0, y, 2, 0 )
> dot = {{alias}}.ndarray( 3, x, 2, 0, y, 2, 0 )
9.0

// Using offset indices:
> x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
> y = new {{alias:@stdlib/array/float32}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
> N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> dot = {{alias}}.ndarray( N, x, -2, x.length-1, y, 1, 3 )
> dot = {{alias}}.ndarray( 3, x, -2, x.length-1, y, 1, 3 )
128.0

See Also
Expand Down
Loading

0 comments on commit 663577c

Please sign in to comment.