Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add C ndarray API and refactor blas/ext/base/dsort2hp #3292

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
172 changes: 144 additions & 28 deletions lib/node_modules/@stdlib/blas/ext/base/dsort2hp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var dsort2hp = require( '@stdlib/blas/ext/base/dsort2hp' );

#### dsort2hp( N, order, x, strideX, y, strideY )

Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array `x` using heapsort.
Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array using heapsort.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand All @@ -54,11 +54,11 @@ The function has the following parameters:
- **N**: number of indexed elements.
- **order**: sort order. If `order < 0.0`, the input strided array `x` is sorted in **decreasing** order. If `order > 0.0`, the input strided array `x` is sorted in **increasing** order. If `order == 0.0`, the input strided arrays are left unchanged.
- **x**: first input [`Float64Array`][@stdlib/array/float64].
- **strideX**: `x` index increment.
- **strideX**: `x` stride length.
- **y**: second input [`Float64Array`][@stdlib/array/float64].
- **strideY**: `y` index increment.
- **strideY**: `y` stride length.

The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to sort every other element
The `N` and stride parameters determine which elements in `x` and `y` are accessed at runtime. For example, to sort every other element:

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand Down Expand Up @@ -104,7 +104,7 @@ console.log( y0 );

#### dsort2hp.ndarray( N, order, x, strideX, offsetX, y, strideY, offsetY )

Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array `x` using heapsort and alternative indexing semantics.
Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array using heapsort and alternative indexing semantics.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand All @@ -126,7 +126,7 @@ The function has the following additional parameters:
- **offsetX**: `x` starting index.
- **offsetY**: `y` starting index.

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 access only the last three elements of `x`
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to access only the last three elements:

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand Down Expand Up @@ -169,30 +169,15 @@ console.log( y );
<!-- eslint no-undef: "error" -->

```javascript
var round = require( '@stdlib/math/base/special/round' );
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var dsort2hp = require( '@stdlib/blas/ext/base/dsort2hp' );

var rand;
var sign;
var x;
var y;
var i;

x = new Float64Array( 10 );
y = new Float64Array( 10 ); // index array
for ( i = 0; i < x.length; i++ ) {
rand = round( randu()*100.0 );
sign = randu();
if ( sign < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
x[ i ] = sign * rand;
y[ i ] = i;
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
var y = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );
console.log( y );

Expand All @@ -205,8 +190,139 @@ console.log( y );

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/blas/ext/base/dsort2hp.h"
```

#### stdlib_strided_dsort2hp( N, order, \*X, strideX, \*Y, strideY )

Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array using heapsort.

```c
double x[] = { 1.0, -2.0, 3.0, -4.0 };
double y[] = { 0.0, 1.0, 2.0, 3.0 };

stdlib_strided_dsort2hp( 4, 1, x, 1, y, 1 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **order**: `[in] CBLAS_INT` sort order. If `order < 0.0`, the input strided array `x` is sorted in **decreasing** order. If `order > 0.0`, the input strided array `x` is sorted in **increasing** order. If `order == 0.0`, the input strided arrays are left unchanged.
- **X**: `[inout] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **Y**: `[inout] double*` input array.
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.

```c
stdlib_strided_dsort2hp( const CBLAS_INT N, const CBLAS_INT order, double *X, CBLAS_INT strideX, double *Y, CBLAS_INT strideY );
```

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

#### stdlib_strided_dsort2hp_ndarray( N, order, \*X, strideX, offsetX, \*Y, strideY, offsetY )

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

Simultaneously sorts two double-precision floating-point strided arrays based on the sort order of the first array using heapsort and alternative indexing semantics.

```c
double x[] = { 1.0, -2.0, 3.0, -4.0 };
double y[] = { 0.0, 1.0, 2.0, 3.0 };

stdlib_strided_dsort2hp_ndarray( 4, 1, x, 1, 0, y, 1, 0 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **order**: `[in] CBLAS_INT` sort order.
- **X**: `[inout] double*` input array. If `order < 0.0`, the input strided array `x` is sorted in **decreasing** order. If `order > 0.0`, the input strided array `x` is sorted in **increasing** order. If `order == 0.0`, the input strided arrays are left unchanged.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
- **Y**: `[inout] double*` input array.
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.

```c
stdlib_strided_dsort2hp_ndarray( const CBLAS_INT N, const CBLAS_INT order, double *X, CBLAS_INT strideX, CBLAS_INT offsetX, double *Y, CBLAS_INT strideY, CBLAS_INT offsetY );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/blas/ext/base/dsort2hp.h"
#include <stdio.h>

int main( void ) {
// Create strided arrays:
double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
double y[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };

// Specify the number of elements:
int N = 8;

// Specify the strides:
int strideX = 1;
int strideY = 1;

// Sort the arrays:
stdlib_strided_dsort2hp( N, 1.0, x, strideX, y, strideY );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
printf( "x[ %i ] = %lf\n", i, x[ i ] );
printf( "y[ %i ] = %lf\n", i, y[ i ] );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<section class="references">

## References
Expand Down
146 changes: 146 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/dsort2hp/benchmark/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#/
# @license Apache-2.0
#
# Copyright (c) 2024 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.
#/

# VARIABLES #

ifndef VERBOSE
QUIET := @
else
QUIET :=
endif

# Determine the OS ([1][1], [2][2]).
#
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
# [2]: http://stackoverflow.com/a/27776822/2225624
OS ?= $(shell uname)
ifneq (, $(findstring MINGW,$(OS)))
OS := WINNT
else
ifneq (, $(findstring MSYS,$(OS)))
OS := WINNT
else
ifneq (, $(findstring CYGWIN,$(OS)))
OS := WINNT
else
ifneq (, $(findstring Windows_NT,$(OS)))
OS := WINNT
endif
endif
endif
endif

# Define the program used for compiling C source files:
ifdef C_COMPILER
CC := $(C_COMPILER)
else
CC := gcc
endif

# Define the command-line options when compiling C files:
CFLAGS ?= \
-std=c99 \
-O3 \
-Wall \
-pedantic

# Determine whether to generate position independent code ([1][1], [2][2]).
#
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
ifeq ($(OS), WINNT)
fPIC ?=
else
fPIC ?= -fPIC
endif

# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`):
INCLUDE ?=

# List of source files:
SOURCE_FILES ?=

# List of libraries (e.g., `-lopenblas -lpthread`):
LIBRARIES ?=

# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
LIBPATH ?=

# List of C targets:
c_targets := benchmark.unsorted_random.length.out


# RULES #

#/
# Compiles source files.
#
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
# @param {string} [CFLAGS] - C compiler options
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
# @param {string} [SOURCE_FILES] - list of source files
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
#
# @example
# make
#
# @example
# make all
#/
all: $(c_targets)

.PHONY: all

#/
# Compiles C source files.
#
# @private
# @param {string} CC - C compiler (e.g., `gcc`)
# @param {string} CFLAGS - C compiler options
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
# @param {string} SOURCE_FILES - list of source files
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
#/
$(c_targets): %.out: %.c
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)

#/
# Runs compiled benchmarks.
#
# @example
# make run
#/
run: $(c_targets)
$(QUIET) ./$<

.PHONY: run

#/
# Removes generated files.
#
# @example
# make clean
#/
clean:
$(QUIET) -rm -f *.o *.out

.PHONY: clean
Loading
Loading