Skip to content

Commit f36c85b

Browse files
committed
feat: add C / Fortran implementation
1 parent 440b1b0 commit f36c85b

25 files changed

+2528
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 Complex64Array = require( '@stdlib/array/complex64' );
29+
var Complex64 = require( '@stdlib/complex/float32' );
30+
var reinterpret = require('@stdlib/strided/base/reinterpret-complex64');
31+
var tryRequire = require( '@stdlib/utils/try-require' );
32+
var pkg = require( './../package.json' ).name;
33+
34+
35+
// VARIABLES //
36+
37+
var cscal = tryRequire( resolve( __dirname, './../lib/cscal.native.js' ) );
38+
var opts = {
39+
'skip': ( cscal instanceof Error )
40+
};
41+
var options = {
42+
'dtype': 'float32'
43+
};
44+
45+
46+
// FUNCTIONS //
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var viewX;
57+
var ca;
58+
var cx;
59+
60+
cx = uniform( len*2, -100.0, 100.0, options );
61+
cx = new Complex64Array( cx.buffer );
62+
63+
viewX = reinterpret( cx, 0 );
64+
65+
ca = new Complex64( 1.0, 0.0 );
66+
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
cscal( cx.length, ca, cx, 1 );
81+
if ( isnan( viewX[ i%(len*2) ] ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
}
85+
b.toc();
86+
if ( isnan( viewX[ i%(len*2) ] ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
}
92+
}
93+
94+
95+
// MAIN //
96+
97+
/**
98+
* Main execution sequence.
99+
*
100+
* @private
101+
*/
102+
function main() {
103+
var len;
104+
var min;
105+
var max;
106+
var f;
107+
var i;
108+
109+
min = 1; // 10^min
110+
max = 6; // 10^max
111+
112+
for ( i = min; i <= max; i++ ) {
113+
len = pow( 10, i );
114+
f = createBenchmark( len );
115+
bench( pkg+'::native:len='+len, opts, f );
116+
}
117+
}
118+
119+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 Complex64Array = require( '@stdlib/array/complex64' );
29+
var Complex64 = require( '@stdlib/complex/float32' );
30+
var reinterpret = require('@stdlib/strided/base/reinterpret-complex64');
31+
var tryRequire = require( '@stdlib/utils/try-require' );
32+
var pkg = require( './../package.json' ).name;
33+
34+
35+
// VARIABLES //
36+
37+
var cscal = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
38+
var opts = {
39+
'skip': ( cscal instanceof Error )
40+
};
41+
var options = {
42+
'dtype': 'float32'
43+
};
44+
45+
46+
// FUNCTIONS //
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var viewX;
57+
var ca;
58+
var cx;
59+
60+
cx = uniform( len*2, -100.0, 100.0, options );
61+
cx = new Complex64Array( cx.buffer );
62+
63+
viewX = reinterpret( cx, 0 );
64+
65+
ca = new Complex64( 1.0, 0.0 );
66+
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
cscal( cx.length, ca, cx, 1, 0 );
81+
if ( isnan( viewX[ i%(len*2) ] ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
}
85+
b.toc();
86+
if ( isnan( viewX[ i%(len*2) ] ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
}
92+
}
93+
94+
95+
// MAIN //
96+
97+
/**
98+
* Main execution sequence.
99+
*
100+
* @private
101+
*/
102+
function main() {
103+
var len;
104+
var min;
105+
var max;
106+
var f;
107+
var i;
108+
109+
min = 1; // 10^min
110+
max = 6; // 10^max
111+
112+
for ( i = min; i <= max; i++ ) {
113+
len = pow( 10, i );
114+
f = createBenchmark( len );
115+
bench( pkg+'::native:ndarray:len='+len, opts, f );
116+
}
117+
}
118+
119+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2024 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+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
# Define the program used for compiling C source files:
49+
ifdef C_COMPILER
50+
CC := $(C_COMPILER)
51+
else
52+
CC := gcc
53+
endif
54+
55+
# Define the command-line options when compiling C files:
56+
CFLAGS ?= \
57+
-std=c99 \
58+
-O3 \
59+
-Wall \
60+
-pedantic
61+
62+
# Determine whether to generate position independent code ([1][1], [2][2]).
63+
#
64+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
65+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
66+
ifeq ($(OS), WINNT)
67+
fPIC ?=
68+
else
69+
fPIC ?= -fPIC
70+
endif
71+
72+
# List of source files:
73+
c_src := ../../src/ccopy.c
74+
75+
# List of C targets:
76+
c_targets := benchmark.length.out
77+
78+
79+
# RULES #
80+
81+
#/
82+
# Compiles C source files.
83+
#
84+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
85+
# @param {string} [CFLAGS] - C compiler options
86+
# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code (e.g., `-fPIC`)
87+
#
88+
# @example
89+
# make
90+
#
91+
# @example
92+
# make all
93+
#/
94+
all: $(c_targets)
95+
96+
.PHONY: all
97+
98+
#/
99+
# Compiles C source files.
100+
#
101+
# @private
102+
# @param {string} CC - C compiler
103+
# @param {string} CFLAGS - C compiler flags
104+
# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code
105+
#/
106+
$(c_targets): %.out: %.c
107+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -I ../../include -o $@ $(c_src) $< -lm
108+
109+
#/
110+
# Runs compiled benchmarks.
111+
#
112+
# @example
113+
# make run
114+
#/
115+
run: $(c_targets)
116+
$(QUIET) ./$<
117+
118+
.PHONY: run
119+
120+
#/
121+
# Removes generated files.
122+
#
123+
# @example
124+
# make clean
125+
#/
126+
clean:
127+
$(QUIET) -rm -f *.o *.out
128+
129+
.PHONY: clean

0 commit comments

Comments
 (0)