Skip to content

Commit c070a88

Browse files
committed
feat: add blas/tools/swap-factory
1 parent 4e74e24 commit c070a88

File tree

11 files changed

+1946
-0
lines changed

11 files changed

+1946
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# factory
22+
23+
> Return a function which interchange two vectors.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var factory = require( '@stdlib/blas/tools/swap-factory' );
37+
```
38+
39+
#### factory( base, dtype )
40+
41+
Returns a function which interchanges two vectors.
42+
43+
```javascript
44+
var dswap = require( '@stdlib/blas/base/dswap' ).ndarray;
45+
46+
var swap = factory( dswap, 'float64' );
47+
```
48+
49+
The function has the following parameters:
50+
51+
- **base**: "base" function which interchanges two vectors. Must have an `ndarray` function signature (i.e., must support index offsets).
52+
- **dtype**: array data type. The function assumes that the data type of all provided arrays is the same.
53+
54+
#### swap( x, y\[, dim] )
55+
56+
Interchanges two vectors.
57+
58+
```javascript
59+
var Float64Array = require( '@stdlib/array/float64' );
60+
var array = require( '@stdlib/ndarray/array' );
61+
var dswap = require( '@stdlib/blas/base/dswap' ).ndarray;
62+
63+
var swap = factory( dswap, 'float64' );
64+
65+
var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) );
66+
var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) );
67+
68+
swap( x, y );
69+
70+
var xbuf = x.data;
71+
// returns <Float64Array>[ 2.0, 6.0, -1.0, -4.0, 8.0 ]
72+
73+
var ybuf = y.data;
74+
// returns <Float64Array>[ 4.0, 2.0, -3.0, 5.0, -1.0 ]
75+
```
76+
77+
The returned function has the following parameters:
78+
79+
- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must have the same shape as `y`.
80+
- **y**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must have the same shape as `x`.
81+
- **dim**: dimension along which to interchange vectors. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`.
82+
83+
For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function interchanges each pair of vectors in `x` and `y` according to the specified dimension index.
84+
85+
```javascript
86+
var Float64Array = require( '@stdlib/array/float64' );
87+
var array = require( '@stdlib/ndarray/array' );
88+
var dswap = require( '@stdlib/blas/base/dswap' ).ndarray;
89+
90+
var swap = factory( dswap, 'float64' );
91+
92+
var opts = {
93+
'shape': [ 2, 3 ]
94+
};
95+
var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts );
96+
var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 2.0 ] ), opts );
97+
98+
var v1 = x.get( 0, 0 );
99+
// returns 4.0
100+
101+
var v2 = y.get( 0, 0 );
102+
// returns 2.0
103+
104+
swap( x, y );
105+
106+
v1 = x.get( 0, 0 );
107+
// returns 2.0
108+
109+
v2 = y.get( 0, 0 );
110+
// returns 4.0
111+
```
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
For the returned function,
122+
123+
- Both input [`ndarrays`][@stdlib/ndarray/ctor] must have the same shape.
124+
- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`.
125+
- For multi-dimensional [`ndarrays`][@stdlib/ndarray/ctor], batched computation effectively means swapping all of `x` with all of `y`; however, the choice of `dim` will significantly affect performance. For best performance, specify a `dim` which best aligns with the [memory layout][@stdlib/ndarray/orders] of provided [`ndarrays`][@stdlib/ndarray/ctor].
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<section class="examples">
132+
133+
## Examples
134+
135+
<!-- eslint no-undef: "error" -->
136+
137+
```javascript
138+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
139+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
140+
var array = require( '@stdlib/ndarray/array' );
141+
var dswap = require( '@stdlib/blas/base/dswap' ).ndarray;
142+
var factory = require( '@stdlib/blas/tools/swap-factory' );
143+
144+
var swap = factory( dswap, 'float64' );
145+
146+
var opts = {
147+
'dtype': 'float64'
148+
};
149+
150+
var x = array( discreteUniform( 10, 0, 100, opts ), {
151+
'shape': [ 5, 2 ]
152+
});
153+
console.log( ndarray2array( x ) );
154+
155+
var y = array( discreteUniform( 10, 0, 10, opts ), {
156+
'shape': x.shape
157+
});
158+
console.log( ndarray2array( y ) );
159+
160+
swap( x, y );
161+
console.log( ndarray2array( x ) );
162+
console.log( ndarray2array( y ) );
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
170+
171+
<section class="related">
172+
173+
</section>
174+
175+
<!-- /.related -->
176+
177+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
178+
179+
<section class="links">
180+
181+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
182+
183+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
184+
185+
</section>
186+
187+
<!-- /.links -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var array = require( '@stdlib/ndarray/array' );
28+
var dswap = require( '@stdlib/blas/base/dswap' ).ndarray;
29+
var pkg = require( './../package.json' ).name;
30+
var factory = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var opts = {
36+
'dtype': 'float64'
37+
};
38+
var swap = factory( dswap, opts.dtype );
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var x = array( uniform( len, -100.0, 100.0, opts ) );
52+
var y = array( uniform( len, -100.0, 100.0, opts ) );
53+
return benchmark;
54+
55+
function benchmark( b ) {
56+
var d;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
d = swap( x, y );
62+
if ( isnan( d.data[ i%len ] ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( d.data[ i%len ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
/**
79+
* Main execution sequence.
80+
*
81+
* @private
82+
*/
83+
function main() {
84+
var len;
85+
var min;
86+
var max;
87+
var f;
88+
var i;
89+
90+
min = 1; // 10^min
91+
max = 6; // 10^max
92+
93+
for ( i = min; i <= max; i++ ) {
94+
len = pow( 10, i );
95+
f = createBenchmark( len );
96+
bench( pkg+':len='+len, f );
97+
}
98+
}
99+
100+
main();

0 commit comments

Comments
 (0)