Skip to content

Commit ea875d6

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add C implementation for blas/base/dgemv
PR-URL: #7013 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent f27e62f commit ea875d6

37 files changed

+4856
-86
lines changed

lib/node_modules/@stdlib/blas/base/dgemv/README.md

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ limitations under the License.
3030
var dgemv = require( '@stdlib/blas/base/dgemv' );
3131
```
3232

33-
#### dgemv( ord, trans, M, N, α, A, LDA, x, sx, β, y, sy )
33+
#### dgemv( order, trans, M, N, α, A, LDA, x, sx, β, y, sy )
3434

3535
Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A**T*x + β*y`, where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.
3636

@@ -47,18 +47,18 @@ dgemv( 'row-major', 'no-transpose', 2, 3, 1.0, A, 3, x, 1, 1.0, y, 1 );
4747

4848
The function has the following parameters:
4949

50-
- **ord**: storage layout.
50+
- **order**: storage layout.
5151
- **trans**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
5252
- **M**: number of rows in the matrix `A`.
5353
- **N**: number of columns in the matrix `A`.
5454
- **α**: scalar constant.
5555
- **A**: input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
5656
- **lda**: stride of the first dimension of `A` (leading dimension of `A`).
5757
- **x**: input [`Float64Array`][mdn-float64array].
58-
- **sx**: index increment for `x`.
58+
- **sx**: stride length for `x`.
5959
- **β**: scalar constant.
6060
- **y**: output [`Float64Array`][mdn-float64array].
61-
- **sy**: index increment for `y`.
61+
- **sy**: stride length for `y`.
6262

6363
The stride parameters determine how operations are performed. For example, to iterate over every other element in `x` and `y`,
6464

@@ -93,6 +93,8 @@ dgemv( 'row-major', 'no-transpose', 2, 2, 1.0, A, 2, x1, -1, 1.0, y1, -1 );
9393
// y0 => <Float64Array>[ 0.0, 8.0, 4.0 ]
9494
```
9595

96+
<!-- lint disable maximum-heading-length -->
97+
9698
#### dgemv.ndarray( trans, M, N, α, A, sa1, sa2, oa, x, sx, ox, β, y, sy, oy )
9799

98100
Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A**T*x + β*y`, using alternative indexing semantics and where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.
@@ -199,18 +201,73 @@ console.log( y );
199201
#include "stdlib/blas/base/dgemv.h"
200202
```
201203

202-
#### TODO
204+
#### c_dgemv( layout, trans, M, N, alpha, \*A, LDA, \*X, strideX, beta, \*Y, strideY )
203205

204-
TODO.
206+
Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A^T*x + β*y`, where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.
205207

206208
```c
207-
TODO
209+
#include "stdlib/blas/base/shared.h"
210+
211+
const double A[] = { 1.0, 0.0, 0.0, 2.0, 1.0, 0.0, 3.0, 2.0, 1.0 };
212+
const double x[] = { 1.0, 2.0, 3.0 };
213+
double y[] = { 1.0, 2.0, 3.0 };
214+
215+
c_dgemv( CblasColMajor, CblasNoTrans, 3, 3, 1.0, A, 3, x, 1, 1.0, y, 1 );
208216
```
209217
210-
TODO
218+
The function accepts the following arguments:
219+
220+
- **layout**: `[in] CBLAS_LAYOUT` storage layout.
221+
- **trans**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
222+
- **M**: `[in] CBLAS_INT` number of rows in the matrix `A`.
223+
- **N**: `[in] CBLAS_INT` number of columns in the matrix `A`.
224+
- **alpha**: `[in] double` scalar constant.
225+
- **A**: `[in] double*` input matrix.
226+
- **LDA**: `[in] CBLAS_INT` stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
227+
- **X**: `[in] double*` first input vector.
228+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
229+
- **beta**: `[in] double` scalar constant.
230+
- **Y**: `[inout] double*` second input vector.
231+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
211232
212233
```c
213-
TODO
234+
void c_dgemv( const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE trans, const CBLAS_INT M, const CBLAS_INT N, const double alpha, const double *A, const CBLAS_INT LDA, const double *X, const CBLAS_INT strideX, const double beta, double *Y, const CBLAS_INT strideY )
235+
```
236+
237+
#### c_dgemv_ndarray( trans, M, N, alpha, \*A, sa1, sa2, oa, \*X, sx, ox, beta, \*Y, sy, oy )
238+
239+
Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A^T*x + β*y`, using indexing alternative semantics and where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.
240+
241+
```c
242+
#include "stdlib/blas/base/shared.h"
243+
244+
const double A[] = { 1.0, 0.0, 0.0, 2.0, 1.0, 0.0, 3.0, 2.0, 1.0 };
245+
const double x[] = { 1.0, 2.0, 3.0 };
246+
double y[] = { 1.0, 2.0, 3.0 };
247+
248+
c_dgemv_ndarray( CblasNoTrans, 3, 3, 1.0, A, 1, 3, 0, x, 1, 0, 1.0, y, 1, 0 );
249+
```
250+
251+
The function accepts the following arguments:
252+
253+
- **trans**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
254+
- **M**: `[in] CBLAS_INT` number of rows in the matrix `A`.
255+
- **N**: `[in] CBLAS_INT` number of columns in the matrix `A`.
256+
- **alpha**: `[in] double` scalar.
257+
- **A**: `[in] double*` input matrix.
258+
- **sa1**: `[in] CBLAS_INT` stride of the first dimension of `A`.
259+
- **sa2**: `[in] CBLAS_INT` stride of the second dimension of `A`.
260+
- **oa**: `[in] CBLAS_INT` starting index for `A`.
261+
- **X**: `[in] double*` first input vector.
262+
- **sx**: `[in] CBLAS_INT` stride length for `X`.
263+
- **ox**: `[in] CBLAS_INT` starting index for `X`.
264+
- **beta**: `[in] double` scalar.
265+
- **Y**: `[inout] double*` second input vector.
266+
- **sy**: `[in] CBLAS_INT` stride length for `Y`.
267+
- **oy**: `[in] CBLAS_INT` starting index for `Y`.
268+
269+
```c
270+
void c_dgemv_ndarray( const CBLAS_TRANSPOSE trans, const CBLAS_INT M, const CBLAS_INT N, const double alpha, const double *A, const CBLAS_INT strideA1, const CBLAS_INT strideA2, const CBLAS_INT offsetA, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double beta, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY )
214271
```
215272

216273
</section>
@@ -232,7 +289,42 @@ TODO
232289
### Examples
233290

234291
```c
235-
TODO
292+
#include "stdlib/blas/base/dgemv.h"
293+
#include "stdlib/blas/base/shared.h"
294+
#include <stdio.h>
295+
296+
int main( void ) {
297+
// Define a 3x3 matrix stored in row-major order:
298+
const double A[ 3*3 ] = {
299+
1.0, 2.0, 3.0,
300+
4.0, 5.0, 6.0,
301+
7.0, 8.0, 9.0
302+
};
303+
304+
// Define `x` and `y` vectors:
305+
const double x[ 3 ] = { 1.0, 2.0, 3.0 };
306+
double y[ 3 ] = { 1.0, 2.0, 3.0 };
307+
308+
// Specify the number of elements along each dimension of `A`:
309+
const int M = 3;
310+
const int N = 3;
311+
312+
// Perform the matrix-vector operation `y = α*A*x + β*y`:
313+
c_dgemv( CblasRowMajor, CblasNoTrans, M, N, 1.0, A, M, x, 1, 1.0, y, 1 );
314+
315+
// Print the result:
316+
for ( int i = 0; i < N; i++ ) {
317+
printf( "y[ %i ] = %lf\n", i, y[ i ] );
318+
}
319+
320+
// Perform the matrix-vector operation `y = α*A*x + β*y` using alternative indexing semantics:
321+
c_dgemv_ndarray( CblasNoTrans, M, N, 1.0, A, N, 1, 0, x, 1, 0, 1.0, y, 1, 0 );
322+
323+
// Print the result:
324+
for ( int i = 0; i < N; i++ ) {
325+
printf( "y[ %i ] = %lf\n", i, y[ i ] );
326+
}
327+
}
236328
```
237329
238330
</section>

lib/node_modules/@stdlib/blas/base/dgemv/benchmark/benchmark.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ function createBenchmark( N ) {
8686
* @private
8787
*/
8888
function main() {
89-
var len;
9089
var min;
9190
var max;
91+
var N;
9292
var f;
9393
var i;
9494

9595
min = 1; // 10^min
9696
max = 6; // 10^max
9797

9898
for ( i = min; i <= max; i++ ) {
99-
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100-
f = createBenchmark( len );
101-
bench( pkg+':size='+(len*len), f );
99+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100+
f = createBenchmark( N );
101+
bench( pkg+':size='+(N*N), f );
102102
}
103103
}
104104

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var floor = require( '@stdlib/math/base/special/floor' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var dgemv = tryRequire( resolve( __dirname, './../lib/dgemv.native.js' ) );
36+
var opts = {
37+
'skip': ( dgemv instanceof Error )
38+
};
39+
var options = {
40+
'dtype': 'float64'
41+
};
42+
43+
44+
// FUNCTIONS //
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} N - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( N ) {
54+
var x = uniform( N, -10.0, 10.0, options );
55+
var y = uniform( N, -10.0, 10.0, options );
56+
var A = uniform( N*N, -10.0, 10.0, options );
57+
return benchmark;
58+
59+
function benchmark( b ) {
60+
var z;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
z = dgemv( 'row-major', 'no-transpose', N, N, 1.0, A, N, x, 1, 1.0, y, 1 );
66+
if ( isnan( z ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( z ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var min;
89+
var max;
90+
var N;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
99+
f = createBenchmark( N );
100+
bench( pkg+'::native:size='+(N*N), opts, f );
101+
}
102+
}
103+
104+
main();

lib/node_modules/@stdlib/blas/base/dgemv/benchmark/benchmark.ndarray.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ function createBenchmark( N ) {
8686
* @private
8787
*/
8888
function main() {
89-
var len;
9089
var min;
9190
var max;
91+
var N;
9292
var f;
9393
var i;
9494

9595
min = 1; // 10^min
9696
max = 6; // 10^max
9797

9898
for ( i = min; i <= max; i++ ) {
99-
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100-
f = createBenchmark( len );
101-
bench( pkg+':ndarray:size='+(len*len), f );
99+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100+
f = createBenchmark( N );
101+
bench( pkg+':ndarray:size='+(N*N), f );
102102
}
103103
}
104104

0 commit comments

Comments
 (0)