|
| 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 | +# sasum |
| 22 | + |
| 23 | +> Compute the sum of absolute values. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var sasum = require( '@stdlib/blas/base/sasum-wasm' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### sasum.main( N, x, stride ) |
| 34 | + |
| 35 | +Computes the sum of absolute values. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 39 | + |
| 40 | +var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 41 | + |
| 42 | +var sum = sasum.main( x.length, x, 1 ); |
| 43 | +// returns 15.0 |
| 44 | +``` |
| 45 | + |
| 46 | +The function has the following parameters: |
| 47 | + |
| 48 | +- **N**: number of indexed elements. |
| 49 | +- **x**: input [`Float32Array`][@stdlib/array/float32]. |
| 50 | +- **strideX**: index increment for `x`. |
| 51 | + |
| 52 | +The `N` and stride parameters determine which elements in the input strided array are accessed at runtime. For example, to compute the sum of every other value, |
| 53 | + |
| 54 | +```javascript |
| 55 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 56 | + |
| 57 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 58 | + |
| 59 | +var sum = sasum.main( 4, x, 2 ); |
| 60 | +// returns 10.0 |
| 61 | +``` |
| 62 | + |
| 63 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 64 | + |
| 65 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 66 | + |
| 67 | +```javascript |
| 68 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 69 | + |
| 70 | +// Initial array: |
| 71 | +var x0 = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 72 | + |
| 73 | +// Create a typed array view: |
| 74 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 75 | + |
| 76 | +var sum = sasum.main( 3, x1, 2 ); |
| 77 | +// returns 12.0 |
| 78 | +``` |
| 79 | + |
| 80 | +#### sasum.ndarray( N, x, strideX, offsetX ) |
| 81 | + |
| 82 | +Computes the sum of absolute values using alternative indexing semantics. |
| 83 | + |
| 84 | +```javascript |
| 85 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 86 | + |
| 87 | +var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 88 | + |
| 89 | +var sum = sasum.ndarray( x.length, x, 1, 0 ); |
| 90 | +// returns 19.0 |
| 91 | +``` |
| 92 | + |
| 93 | +The function has the following additional parameters: |
| 94 | + |
| 95 | +- **offsetX**: starting index for `x`. |
| 96 | + |
| 97 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to compute the sum of last three elements, |
| 98 | + |
| 99 | +```javascript |
| 100 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 101 | + |
| 102 | +var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 103 | + |
| 104 | +var sum = sasum.ndarray( 3, x, 1, x.length-3 ); |
| 105 | +// returns 15.0 |
| 106 | + |
| 107 | +// Using a negative stride to sum from the last element: |
| 108 | +sum = sasum.ndarray( 3, x, -1, x.length-1 ); |
| 109 | +// returns 15.0 |
| 110 | +``` |
| 111 | + |
| 112 | +* * * |
| 113 | + |
| 114 | +### Module |
| 115 | + |
| 116 | +#### sasum.Module( memory ) |
| 117 | + |
| 118 | +Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory. |
| 119 | + |
| 120 | +<!-- eslint-disable node/no-sync --> |
| 121 | + |
| 122 | +```javascript |
| 123 | +var Memory = require( '@stdlib/wasm/memory' ); |
| 124 | + |
| 125 | +// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB): |
| 126 | +var mem = new Memory({ |
| 127 | + 'initial': 10, |
| 128 | + 'maximum': 100 |
| 129 | +}); |
| 130 | + |
| 131 | +// Create a BLAS routine: |
| 132 | +var mod = new sasum.Module( mem ); |
| 133 | +// returns <Module> |
| 134 | + |
| 135 | +// Initialize the routine: |
| 136 | +mod.initializeSync(); |
| 137 | +``` |
| 138 | + |
| 139 | +#### sasum.Module.prototype.main( N, xp, sx ) |
| 140 | + |
| 141 | +Computes the sum of absolute values. |
| 142 | + |
| 143 | +<!-- eslint-disable node/no-sync --> |
| 144 | + |
| 145 | +```javascript |
| 146 | +var Memory = require( '@stdlib/wasm/memory' ); |
| 147 | +var oneTo = require( '@stdlib/array/one-to' ); |
| 148 | +var zeros = require( '@stdlib/array/zeros' ); |
| 149 | + |
| 150 | +// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB): |
| 151 | +var mem = new Memory({ |
| 152 | + 'initial': 10, |
| 153 | + 'maximum': 100 |
| 154 | +}); |
| 155 | + |
| 156 | +// Create a BLAS routine: |
| 157 | +var mod = new sasum.Module( mem ); |
| 158 | +// returns <Module> |
| 159 | + |
| 160 | +// Initialize the routine: |
| 161 | +mod.initializeSync(); |
| 162 | + |
| 163 | +// Define a vector data type: |
| 164 | +var dtype = 'float32'; |
| 165 | + |
| 166 | +// Specify a vector length: |
| 167 | +var N = 5; |
| 168 | + |
| 169 | +// Define pointer (i.e., byte offsets) for storing the input vectors: |
| 170 | +var xptr = 0; |
| 171 | + |
| 172 | +// Write vector values to module memory: |
| 173 | +mod.write( xptr, oneTo( N, dtype ) ); |
| 174 | + |
| 175 | +// Perform computation: |
| 176 | +var sum = mod.main( N, xptr, 1 ); |
| 177 | +// returns 15.0 |
| 178 | +``` |
| 179 | + |
| 180 | +The function has the following parameters: |
| 181 | + |
| 182 | +- **N**: number of indexed elements. |
| 183 | +- **xp**: input [`Float32Array`][@stdlib/array/float32] pointer (i.e., byte offset). |
| 184 | +- **sx**: index increment for `x`. |
| 185 | + |
| 186 | +#### sasum.Module.prototype.ndarray( N, xp, sx, ox ) |
| 187 | + |
| 188 | +Computes the sum of absolute values using alternative indexing semantics. |
| 189 | + |
| 190 | +<!-- eslint-disable node/no-sync --> |
| 191 | + |
| 192 | +```javascript |
| 193 | +var Memory = require( '@stdlib/wasm/memory' ); |
| 194 | +var oneTo = require( '@stdlib/array/one-to' ); |
| 195 | +var zeros = require( '@stdlib/array/zeros' ); |
| 196 | + |
| 197 | +// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB): |
| 198 | +var mem = new Memory({ |
| 199 | + 'initial': 10, |
| 200 | + 'maximum': 100 |
| 201 | +}); |
| 202 | + |
| 203 | +// Create a BLAS routine: |
| 204 | +var mod = new sasum.Module( mem ); |
| 205 | +// returns <Module> |
| 206 | + |
| 207 | +// Initialize the routine: |
| 208 | +mod.initializeSync(); |
| 209 | + |
| 210 | +// Define a vector data type: |
| 211 | +var dtype = 'float32'; |
| 212 | + |
| 213 | +// Specify a vector length: |
| 214 | +var N = 5; |
| 215 | + |
| 216 | +// Define pointer (i.e., byte offsets) for storing the input vector: |
| 217 | +var xptr = 0; |
| 218 | + |
| 219 | +// Write vector values to module memory: |
| 220 | +mod.write( xptr, oneTo( N, dtype ) ); |
| 221 | + |
| 222 | +// Perform computation: |
| 223 | +var sum = mod.ndarray( N, xptr, 1, 0 ); |
| 224 | +// returns 15.0 |
| 225 | +``` |
| 226 | + |
| 227 | +The function has the following additional parameters: |
| 228 | + |
| 229 | +- **ox**: starting index for `x`. |
| 230 | + |
| 231 | +</section> |
| 232 | + |
| 233 | +<!-- /.usage --> |
| 234 | + |
| 235 | +<section class="notes"> |
| 236 | + |
| 237 | +* * * |
| 238 | + |
| 239 | +## Notes |
| 240 | + |
| 241 | +- If `N <= 0`, both `main` and `ndarray` methods return `0.0`. |
| 242 | +- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `sasum` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/blas/base/sasum`][@stdlib/blas/base/sasum]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/blas/base/sasum`][@stdlib/blas/base/sasum]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other. |
| 243 | +- `sasum()` corresponds to the [BLAS][blas] level 1 function [`sasum`][sasum]. |
| 244 | + |
| 245 | +</section> |
| 246 | + |
| 247 | +<!-- /.notes --> |
| 248 | + |
| 249 | +<section class="examples"> |
| 250 | + |
| 251 | +* * * |
| 252 | + |
| 253 | +## Examples |
| 254 | + |
| 255 | +<!-- eslint no-undef: "error" --> |
| 256 | + |
| 257 | +```javascript |
| 258 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 259 | +var sasum = require( '@stdlib/blas/base/sasum-wasm' ); |
| 260 | + |
| 261 | +var opts = { |
| 262 | + 'dtype': 'float32' |
| 263 | +}; |
| 264 | +var x = discreteUniform( 10, 0, 100, opts ); |
| 265 | +console.log( x ); |
| 266 | + |
| 267 | +var sum = sasum.ndarray( x.length, x, 1, 0 ); |
| 268 | +console.log( sum ); |
| 269 | +``` |
| 270 | + |
| 271 | +</section> |
| 272 | + |
| 273 | +<!-- /.examples --> |
| 274 | + |
| 275 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 276 | + |
| 277 | +<section class="related"> |
| 278 | + |
| 279 | +</section> |
| 280 | + |
| 281 | +<!-- /.related --> |
| 282 | + |
| 283 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 284 | + |
| 285 | +<section class="links"> |
| 286 | + |
| 287 | +[blas]: http://www.netlib.org/blas |
| 288 | + |
| 289 | +[sasum]: https://www.netlib.org/lapack/explore-html/d5/d72/group__asum_ga5fb1932fad7d47868a711867e4f2c804.html#ga5fb1932fad7d47868a711867e4f2c804 |
| 290 | + |
| 291 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 292 | + |
| 293 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 294 | + |
| 295 | +[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory |
| 296 | + |
| 297 | +[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper |
| 298 | + |
| 299 | +[@stdlib/blas/base/sasum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sasum |
| 300 | + |
| 301 | +</section> |
| 302 | + |
| 303 | +<!-- /.links --> |
0 commit comments