Skip to content

Commit

Permalink
refactor: add support for stack
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-095 committed Sep 12, 2024
1 parent 44ebe3c commit fd81f7d
Show file tree
Hide file tree
Showing 10 changed files with 810 additions and 156 deletions.
84 changes: 44 additions & 40 deletions lib/node_modules/@stdlib/blas/sdot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The [dot product][dot-product] (or scalar product) is defined as
```

<!-- <div class="equation" align="center" data-raw-text="\mathbf{x}\cdot\mathbf{y} = \sum_{i=0}^{N-1} x_i y_i = x_0 y_0 + x_1 y_1 + \ldots + x_{N-1} y_{N-1}" data-equation="eq:dot_product">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@03fff24f5a7ba807a292f08cfef75ed0748e40de/lib/node_modules/@stdlib/blas/sdot/docs/img/equation_dot_product.svg" alt="Dot product definition.">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d0afc603cdda35b11d5bd1633dd4dddb0d59e117/lib/node_modules/@stdlib/blas/sdot/docs/img/equation_dot_product.svg" alt="Dot product definition.">
<br>
</div> -->

Expand All @@ -51,9 +51,9 @@ The [dot product][dot-product] (or scalar product) is defined as
var sdot = require( '@stdlib/blas/sdot' );
```

#### sdot( x, y )
#### sdot( x, y\[, dim] )

Calculates the dot product of vectors `x` and `y`.
Calculates the dot product of two single-precision floating-point vectors `x` and `y`.

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -63,25 +63,38 @@ var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) );
var y = array( new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) );

var z = sdot( x, y );
// returns <ndarray>

var v = z.get();
// returns -5.0
```

The function has the following parameters:

- **x**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] whose underlying data type is `float32`.
- **y**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] whose underlying data type is `float32`.
- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] whose underlying data type is `float32`. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with `y`.
- **y**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] whose underlying data type is `float32`. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with `x`.
- **dim**: dimension for which to compute the dot product. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`.

If provided empty vectors, the function returns `0.0`.
If provided at least one input [`ndarray`][@stdlib/ndarray/ctor] having more than one dimension, the input [`ndarrays`][@stdlib/ndarray/ctor] are [broadcasted][@stdlib/ndarray/base/broadcast-shapes] to a common shape. For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function computes the dot product for each pair of vectors in `x` and `y` according to the specified dimension index.

```javascript
var Float32Array = require( '@stdlib/array/float32' );
var array = require( '@stdlib/ndarray/array' );

var x = array( new Float32Array() );
var y = array( new Float32Array() );
var opts = {
'shape': [ 2, 3 ]
};
var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts );
var y = array( new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 2.0 ] ), opts );

var z = sdot( x, y );
// returns 0.0
// returns <ndarray>

var v1 = z.get( 0 );
// returns 23.0

var v2 = z.get( 1 );
// returns -22.0
```

</section>
Expand All @@ -92,6 +105,11 @@ var z = sdot( x, y );

## Notes

- The size of the contracted dimension must be the same for both input [`ndarrays`][@stdlib/ndarray/ctor].
- The function resolves the dimension index for which to compute the dot product **before** broadcasting.
- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`.
- The output [`ndarray`][@stdlib/ndarray/ctor] has the same data type as the input [`ndarrays`][@stdlib/ndarray/ctor] and has a shape which is determined by broadcasting and excludes the contracted dimension.
- If provided empty vectors, the dot product is `0`.
- `sdot()` provides a higher-level interface to the [BLAS][blas] level 1 function [`sdot`][@stdlib/blas/base/sdot].

</section>
Expand All @@ -105,27 +123,27 @@ var z = sdot( x, y );
<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var Float32Array = require( '@stdlib/array/float32' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var array = require( '@stdlib/ndarray/array' );
var sdot = require( '@stdlib/blas/sdot' );

var x = array( new Float32Array( 10 ) );
var y = array( new Float32Array( 10 ) );
var opts = {
'dtype': 'float32'
};

var rand1 = discreteUniform.factory( 0, 100 );
var rand2 = discreteUniform.factory( 0, 10 );
var x = array( discreteUniform( 10, 0, 100, opts ), {
'shape': [ 5, 2 ]
});
console.log( ndarray2array( x ) );

var i;
for ( i = 0; i < x.length; i++ ) {
x.set( i, rand1() );
y.set( i, rand2() );
}
console.log( x.toString() );
console.log( y.toString() );
var y = array( discreteUniform( 10, 0, 10, opts ), {
'shape': x.shape
});
console.log( ndarray2array( y ) );

var z = sdot( x, y );
console.log( z );
var z = sdot( x, y, -1 );
console.log( ndarray2array( z ) );
```

</section>
Expand All @@ -136,14 +154,6 @@ console.log( z );

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/blas/base/sdot`][@stdlib/blas/base/sdot]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two single-precision floating-point vectors.</span>
- <span class="package-name">[`@stdlib/blas/ddot`][@stdlib/blas/ddot]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two double-precision floating-point vectors.</span>
- <span class="package-name">[`@stdlib/blas/gdot`][@stdlib/blas/gdot]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two vectors.</span>

</section>

<!-- /.related -->
Expand All @@ -156,18 +166,12 @@ console.log( z );

[blas]: http://www.netlib.org/blas

[@stdlib/ndarray/array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/array
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

<!-- <related-links> -->
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes

[@stdlib/blas/base/sdot]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sdot

[@stdlib/blas/ddot]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ddot

[@stdlib/blas/gdot]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/gdot

<!-- </related-links> -->

</section>

<!-- /.links -->
37 changes: 19 additions & 18 deletions lib/node_modules/@stdlib/blas/sdot/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
// MODULES //

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


// VARIABLES //

var opts = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
Expand All @@ -40,34 +46,29 @@ var sdot = require( './../lib/main.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 < len; i++ ) {
x[ i ] = ( randu()*10.0 ) - 20.0;
y[ i ] = ( randu()*10.0 ) - 20.0;
}
x = array( x );
y = array( y );

var x = array( uniform( len, -100.0, 100.0, opts ) );
var y = array( uniform( len, -100.0, 100.0, opts ) );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var d;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
d = sdot( x, y );
if ( isnan( d ) ) {
if ( isnan( d.get() ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( d ) ) {
if ( isnan( d.get() ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
Expand Down Expand Up @@ -96,7 +97,7 @@ function main() {
for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':len='+len, f );
bench( pkg+'::vectors:len='+len, f );
}
}

Expand Down
122 changes: 122 additions & 0 deletions lib/node_modules/@stdlib/blas/sdot/benchmark/benchmark.stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var uniform = require( '@stdlib/random/array/uniform' );
var numel = require( '@stdlib/ndarray/base/numel' );
var array = require( '@stdlib/ndarray/array' );
var pkg = require( './../package.json' ).name;
var sdot = require( './../lib/main.js' );


// VARIABLES //

var OPTS = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveIntegerArray} shape - array shape
* @returns {Function} benchmark function
*/
function createBenchmark( shape ) {
var x;
var y;
var N;
var o;

N = numel( shape );
o = {
'shape': shape
};
x = array( uniform( N, -100.0, 100.0, OPTS ), o );
y = array( uniform( N, -100.0, 100.0, OPTS ), o );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var d;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
d = sdot( x, y );
if ( isnan( d.iget( 0 ) ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( d.iget( 0 ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var shape;
var min;
var max;
var N;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
N = pow( 10, i );

shape = [ 2, N/2 ];
f = createBenchmark( shape );
bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f );

shape = [ N/2, 2 ];
f = createBenchmark( shape );
bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f );
}
}

main();
Loading

0 comments on commit fd81f7d

Please sign in to comment.