Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 154 additions & 8 deletions lib/node_modules/@stdlib/blas/base/dgemm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.
Copyright (c) 2026 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,6 +22,12 @@ limitations under the License.

> Perform the matrix-matrix operation `C = α*op(A)*op(B) + β*C` where `op(X)` is one of the `op(X) = X`, or `op(X) = X^T`.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage
Expand Down Expand Up @@ -62,7 +68,7 @@ The function has the following parameters:
- **C**: third input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
- **ldc**: stride of the first dimension of `C` (leading dimension of `C`).

The stride parameters determine how elements in the input arrays are accessed at runtime. For example, to perform matrix multiplication of two subarrays
The stride parameters determine how elements in the input arrays are accessed at runtime. For example, to perform matrix multiplication of two subarrays,

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand All @@ -75,6 +81,27 @@ dgemm( 'row-major', 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A, 4, B, 4, 1.
// C => <Float64Array>[ 2.0, 5.0, 6.0, 11.0 ]
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

<!-- eslint-disable stdlib/capitalized-comments -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );

// Initial arrays (with extra leading element to be skipped):
var A0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] );
var B0 = new Float64Array( [ 0.0, 1.0, 1.0, 0.0, 1.0 ] );
var C0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] );

// Create offset views...
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var B1 = new Float64Array( B0.buffer, B0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var C1 = new Float64Array( C0.buffer, C0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

dgemm( 'row-major', 'no-transpose', 'no-transpose', 2, 2, 2, 1.0, A1, 2, B1, 2, 1.0, C1, 2 );
// C0 => <Float64Array>[ 0.0, 2.0, 5.0, 6.0, 11.0 ]
```

<!-- lint disable maximum-heading-length -->

#### dgemm.ndarray( ta, tb, M, N, K, α, A, sa1, sa2, oa, B, sb1, sb2, ob, β, C, sc1, sc2, oc )
Expand Down Expand Up @@ -190,18 +217,99 @@ console.log( C );
#include "stdlib/blas/base/dgemm.h"
```

#### TODO
#### c_dgemm( layout, transA, transB, M, N, K, alpha, \*A, LDA, \*B, LDB, beta, \*C, LDC )

TODO.
Performs the matrix-matrix operation `C = alpha*op(A)*op(B) + beta*C`, where `op(X)` is either `op(X) = X` or `op(X) = X^T`, `alpha` and `beta` are scalars, and `A`, `B`, and `C` are matrices, with `op(A)` an `M`-by-`K` matrix, `op(B)` a `K`-by-`N` matrix, and `C` an `M`-by-`N` matrix.

```c
TODO
#include "stdlib/blas/base/shared.h"

double A[ 2*3 ] = {
1.0, 2.0, 3.0,
4.0, 5.0, 6.0
};
double B[ 3*2 ] = {
7.0, 8.0,
9.0, 10.0,
11.0, 12.0
};
double C[ 2*2 ] = {
0.0, 0.0,
0.0, 0.0
};

c_dgemm( CblasRowMajor, CblasNoTrans, CblasNoTrans, 2, 2, 3, 1.0, A, 3, B, 2, 0.0, C, 2 );
```

TODO
The function accepts the following arguments:

- **layout**: `[in] CBLAS_LAYOUT` storage layout.
- **transA**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
- **transB**: `[in] CBLAS_TRANSPOSE` specifies whether `B` should be transposed, conjugate-transposed, or not transposed.
- **M**: `[in] CBLAS_INT` number of rows in the matrix `op(A)` and in the matrix `C`.
- **N**: `[in] CBLAS_INT` number of columns in the matrix `op(B)` and in the matrix `C`.
- **K**: `[in] CBLAS_INT` number of columns in the matrix `op(A)` and number of rows in the matrix `op(B)`.
- **alpha**: `[in] double` scalar constant.
- **A**: `[in] double*` first input matrix.
- **LDA**: `[in] CBLAS_INT` stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
- **B**: `[in] double*` second input matrix.
- **LDB**: `[in] CBLAS_INT` stride of the first dimension of `B` (a.k.a., leading dimension of the matrix `B`).
- **beta**: `[in] double` scalar constant.
- **C**: `[inout] double*` result matrix.
- **LDC**: `[in] CBLAS_INT` stride of the first dimension of `C` (a.k.a., leading dimension of the matrix `C`).

```c
TODO
void c_dgemm( const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE transA, const CBLAS_TRANSPOSE transB, const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT K, const double alpha, const double *A, const CBLAS_INT LDA, const double *B, const CBLAS_INT LDB, const double beta, double *C, const CBLAS_INT LDC );
```

#### c_dgemm_ndarray( transA, transB, M, N, K, alpha, \*A, sa1, sa2, oa, \*B, sb1, sb2, ob, beta, \*C, sc1, sc2, oc )

Performs the matrix-matrix operation `C = alpha*op(A)*op(B) + beta*C`, using alternative indexing semantics and where `op(X)` is either `op(X) = X` or `op(X) = X^T`, `alpha` and `beta` are scalars, and `A`, `B`, and `C` are matrices, with `op(A)` an `M`-by-`K` matrix, `op(B)` a `K`-by-`N` matrix, and `C` an `M`-by-`N` matrix.

```c
#include "stdlib/blas/base/shared.h"

double A[ 2*3 ] = {
1.0, 2.0, 3.0,
4.0, 5.0, 6.0
};
double B[ 3*2 ] = {
7.0, 8.0,
9.0, 10.0,
11.0, 12.0
};
double C[ 2*2 ] = {
0.0, 0.0,
0.0, 0.0
};

c_dgemm_ndarray( CblasNoTrans, CblasNoTrans, 2, 2, 3, 1.0, A, 3, 1, 0, B, 2, 1, 0, 0.0, C, 2, 1, 0 );
```

The function accepts the following arguments:

- **transA**: `[in] CBLAS_TRANSPOSE` specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
- **transB**: `[in] CBLAS_TRANSPOSE` specifies whether `B` should be transposed, conjugate-transposed, or not transposed.
- **M**: `[in] CBLAS_INT` number of rows in the matrix `op(A)` and in the matrix `C`.
- **N**: `[in] CBLAS_INT` number of columns in the matrix `op(B)` and in the matrix `C`.
- **K**: `[in] CBLAS_INT` number of columns in the matrix `op(A)` and number of rows in the matrix `op(B)`.
- **alpha**: `[in] double` scalar constant.
- **A**: `[in] double*` first input matrix.
- **sa1**: `[in] CBLAS_INT` stride of the first dimension of `A`.
- **sa2**: `[in] CBLAS_INT` stride of the second dimension of `A`.
- **oa**: `[in] CBLAS_INT` starting index for `A`.
- **B**: `[in] double*` second input matrix.
- **sb1**: `[in] CBLAS_INT` stride of the first dimension of `B`.
- **sb2**: `[in] CBLAS_INT` stride of the second dimension of `B`.
- **ob**: `[in] CBLAS_INT` starting index for `B`.
- **beta**: `[in] double` scalar constant.
- **C**: `[inout] double*` result matrix.
- **sc1**: `[in] CBLAS_INT` stride of the first dimension of `C`.
- **sc2**: `[in] CBLAS_INT` stride of the second dimension of `C`.
- **oc**: `[in] CBLAS_INT` starting index for `C`.

```c
void c_dgemm_ndarray( const CBLAS_TRANSPOSE transA, const CBLAS_TRANSPOSE transB, const CBLAS_INT M, const CBLAS_INT N, const CBLAS_INT K, const double alpha, const double *A, const CBLAS_INT strideA1, const CBLAS_INT strideA2, const CBLAS_INT offsetA, const double *B, const CBLAS_INT strideB1, const CBLAS_INT strideB2, const CBLAS_INT offsetB, const double beta, double *C, const CBLAS_INT strideC1, const CBLAS_INT strideC2, const CBLAS_INT offsetC );
```

</section>
Expand All @@ -223,7 +331,45 @@ TODO
### Examples

```c
TODO
#include "stdlib/blas/base/dgemm.h"
#include "stdlib/blas/base/shared.h"
#include <stdio.h>

int main( void ) {
// Define a 2x2 output matrix stored in row-major order:
double C[ 2*2 ] = {
0.0, 0.0,
0.0, 0.0
};

// Define a 2x3 matrix `A` stored in row-major order:
const double A[ 2*3 ] = {
1.0, 2.0, 3.0,
4.0, 5.0, 6.0
};

// Define a 3x2 matrix `B` stored in row-major order:
const double B[ 3*2 ] = {
7.0, 8.0,
9.0, 10.0,
11.0, 12.0
};

// Specify matrix dimensions:
const int M = 2; // rows of op(A) and C
const int N = 2; // columns of op(B) and C
const int K = 3; // columns of op(A) and rows of op(B)

// Perform operation: C = 1.0*A*B + 0.0*C
c_dgemm( CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, 1.0, A, K, B, N, 0.0, C, N );

// Print the result:
for ( int i = 0; i < M; i++ ) {
for ( int j = 0; j < N; j++ ) {
printf( "C[%i,%i] = %lf\n", i, j, C[ (i*N)+j ] );
}
}
}
```

</section>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var format = require( '@stdlib/string/format' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var dgemm = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
var opts = {
'skip': ( dgemm instanceof Error )
};
var options = {
'dtype': 'float64'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} N - array dimension size
* @returns {Function} benchmark function
*/
function createBenchmark( N ) {
var A = uniform( N*N, -10.0, 10.0, options );
var B = uniform( N*N, -10.0, 10.0, options );
var C = uniform( N*N, -10.0, 10.0, options );
return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = dgemm( 'no-transpose', 'no-transpose', N, N, N, 1.0, A, 1, N, 0, B, 1, N, 0, 1.0, C, 1, N, 0 );
if ( isnan( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

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

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

for ( i = min; i <= max; i++ ) {
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
f = createBenchmark( len );
bench( format( '%s::native:ndarray:order(A)=column-major,order(B)=column-major,order(C)=column-major,trans(A)=false,trans(B)=false,size=%d', pkg, len*len ), opts, f );
}
}

main();
Loading