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/dsorthp #5018

Merged
merged 7 commits into from
Feb 5, 2025
Merged
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
159 changes: 132 additions & 27 deletions lib/node_modules/@stdlib/blas/ext/base/dsorthp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ limitations under the License.
var dsorthp = require( '@stdlib/blas/ext/base/dsorthp' );
```

#### dsorthp( N, order, x, stride )
#### dsorthp( N, order, x, strideX )

Sorts a double-precision floating-point strided array `x` using heapsort.
Sorts a double-precision floating-point strided array using heapsort.

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

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand All @@ -77,9 +77,9 @@ dsorthp( 2, -1.0, x1, 2 );
// x0 => <Float64Array>[ 1.0, 4.0, 3.0, 2.0 ]
```

#### dsorthp.ndarray( N, order, x, stride, offset )
#### dsorthp.ndarray( N, order, x, strideX, offsetX )

Sorts a double-precision floating-point strided array `x` using heapsort and alternative indexing semantics.
Sorts a double-precision floating-point strided array using heapsort and alternative indexing semantics.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand All @@ -92,9 +92,9 @@ dsorthp.ndarray( x.length, 1.0, x, 1, 0 );

The function has the following additional parameters:

- **offset**: starting index.
- **offsetX**: 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 parameter supports indexing semantics based on starting a index. For example, to access only the last three elements:

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand Down Expand Up @@ -131,27 +131,12 @@ dsorthp.ndarray( 3, 1.0, x, 1, x.length-3 );
<!-- 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 dsorthp = require( '@stdlib/blas/ext/base/dsorthp' );

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

x = new Float64Array( 10 );
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;
}
var x = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
console.log( x );

dsorthp( x.length, -1.0, x, -1 );
Expand All @@ -164,6 +149,126 @@ console.log( x );

* * *

<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/dsorthp.h"
```

#### stdlib_strided_dsorthp( N, order, \*X, strideX )

Sorts a double-precision floating-point strided array using heapsort.

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

stdlib_strided_dsorthp( 2, -1.0, x, 1 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **order**: `[in] double` 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.

```c
stdlib_strided_dsorthp( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX );
```

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

#### stdlib_strided_dsorthp_ndarray( N, order, \*X, strideX, offsetX )

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

Sorts a double-precision floating-point strided array using heapsort and alternative indexing semantics.

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

stdlib_strided_dsorthp_ndarray( 4, 1.0, x, 1, 0 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **order**: `[in] double` 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.
- **offsetX**: `[in] CBLAS_INT` starting index.

```c
stdlib_strided_dsorthp_ndarray( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</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/dsorthp.h"
#include <stdio.h>

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

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

// Specify a stride:
int strideX = 1;

// Sort the array:
stdlib_strided_dsorthp( N, 1.0, x, strideX );

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

```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<section class="references">

## References
Expand Down
146 changes: 146 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/dsorthp/benchmark/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#/
# @license Apache-2.0
#
# Copyright (c) 2025 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 := example.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 examples.
#
# @example
# make run
#/
run: $(c_targets)
$(QUIET) ./$<

.PHONY: run

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

.PHONY: clean
Loading