Skip to content

Commit 6db3057

Browse files
committed
Add 3d ndarray set benchmarks
1 parent 0a6f463 commit 6db3057

1 file changed

Lines changed: 174 additions & 0 deletions

File tree

  • lib/node_modules/@stdlib/ndarray/ctor/benchmark/c

lib/node_modules/@stdlib/ndarray/ctor/benchmark/c/benchmark.c

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,159 @@ double benchmark41() {
20222022
return elapsed;
20232023
}
20242024

2025+
/**
2026+
* Runs a benchmark.
2027+
*
2028+
* @return elapsed time in seconds
2029+
*/
2030+
double benchmark42() {
2031+
double elapsed;
2032+
int64_t sub[3];
2033+
uint8_t v;
2034+
double t;
2035+
int8_t s;
2036+
int i;
2037+
2038+
uint8_t buffer[] = { 1, 2, 3, 4, 5, 6 };
2039+
int64_t ndims = 3;
2040+
int64_t shape[] = { 1, 3, 2 };
2041+
int64_t strides[] = { 6, 2, 1 };
2042+
int64_t offset = 0;
2043+
int64_t nsubmodes = 1;
2044+
enum STDLIB_NDARRAY_INDEX_MODE mode = STDLIB_NDARRAY_INDEX_ERROR;
2045+
enum STDLIB_NDARRAY_INDEX_MODE submodes[] = { mode };
2046+
2047+
struct ndarray *arr = stdlib_ndarray_constructor( STDLIB_NDARRAY_UINT8, buffer, ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, mode, nsubmodes, submodes );
2048+
if ( arr == NULL ) {
2049+
printf( "unable to allocate memory\n" );
2050+
exit( 1 );
2051+
}
2052+
2053+
sub[ 0 ] = 0;
2054+
sub[ 2 ] = 1;
2055+
2056+
t = tic();
2057+
for ( i = 0; i < ITERATIONS; i++ ) {
2058+
sub[ 1 ] = (int64_t)( rand_double()*shape[1] );
2059+
v = (uint8_t)( rand_double()*255.0 );
2060+
s = stdlib_ndarray_set( arr, sub, (void *)&v );
2061+
if ( s != 0 ) {
2062+
printf( "unexpected result\n" );
2063+
break;
2064+
}
2065+
}
2066+
elapsed = tic() - t;
2067+
2068+
if ( s != 0 || *(stdlib_ndarray_get_ptr( arr, sub )) != v ) {
2069+
printf( "unexpected result\n" );
2070+
}
2071+
free( arr );
2072+
2073+
return elapsed;
2074+
}
2075+
2076+
/**
2077+
* Runs a benchmark.
2078+
*
2079+
* @return elapsed time in seconds
2080+
*/
2081+
double benchmark43() {
2082+
double elapsed;
2083+
int64_t sub[3];
2084+
uint8_t v;
2085+
double t;
2086+
int8_t s;
2087+
int i;
2088+
2089+
uint8_t buffer[] = { 1, 2, 3, 4, 5, 6 };
2090+
int64_t ndims = 3;
2091+
int64_t shape[] = { 1, 3, 2 };
2092+
int64_t strides[] = { 6, 2, 1 };
2093+
int64_t offset = 0;
2094+
int64_t nsubmodes = 1;
2095+
enum STDLIB_NDARRAY_INDEX_MODE mode = STDLIB_NDARRAY_INDEX_WRAP;
2096+
enum STDLIB_NDARRAY_INDEX_MODE submodes[] = { mode };
2097+
2098+
struct ndarray *arr = stdlib_ndarray_constructor( STDLIB_NDARRAY_UINT8, buffer, ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, mode, nsubmodes, submodes );
2099+
if ( arr == NULL ) {
2100+
printf( "unable to allocate memory\n" );
2101+
exit( 1 );
2102+
}
2103+
2104+
sub[ 0 ] = 0;
2105+
sub[ 2 ] = 1;
2106+
2107+
t = tic();
2108+
for ( i = 0; i < ITERATIONS; i++ ) {
2109+
sub[ 1 ] = (int64_t)( (rand_double()*30.0)-15.0 );
2110+
v = (uint8_t)( rand_double()*255.0 );
2111+
s = stdlib_ndarray_set( arr, sub, (void *)&v );
2112+
if ( s != 0 ) {
2113+
printf( "unexpected result\n" );
2114+
break;
2115+
}
2116+
}
2117+
elapsed = tic() - t;
2118+
2119+
if ( s != 0 || *(stdlib_ndarray_get_ptr( arr, sub )) != v ) {
2120+
printf( "unexpected result\n" );
2121+
}
2122+
free( arr );
2123+
2124+
return elapsed;
2125+
}
2126+
2127+
/**
2128+
* Runs a benchmark.
2129+
*
2130+
* @return elapsed time in seconds
2131+
*/
2132+
double benchmark44() {
2133+
double elapsed;
2134+
int64_t sub[3];
2135+
uint8_t v;
2136+
double t;
2137+
int8_t s;
2138+
int i;
2139+
2140+
uint8_t buffer[] = { 1, 2, 3, 4, 5, 6 };
2141+
int64_t ndims = 3;
2142+
int64_t shape[] = { 1, 3, 2 };
2143+
int64_t strides[] = { 6, 2, 1 };
2144+
int64_t offset = 0;
2145+
int64_t nsubmodes = 1;
2146+
enum STDLIB_NDARRAY_INDEX_MODE mode = STDLIB_NDARRAY_INDEX_CLAMP;
2147+
enum STDLIB_NDARRAY_INDEX_MODE submodes[] = { mode };
2148+
2149+
struct ndarray *arr = stdlib_ndarray_constructor( STDLIB_NDARRAY_UINT8, buffer, ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, mode, nsubmodes, submodes );
2150+
if ( arr == NULL ) {
2151+
printf( "unable to allocate memory\n" );
2152+
exit( 1 );
2153+
}
2154+
2155+
sub[ 0 ] = 0;
2156+
sub[ 2 ] = 1;
2157+
2158+
t = tic();
2159+
for ( i = 0; i < ITERATIONS; i++ ) {
2160+
sub[ 1 ] = (int64_t)( (rand_double()*30.0)-15.0 );
2161+
v = (uint8_t)( rand_double()*255.0 );
2162+
s = stdlib_ndarray_set( arr, sub, (void *)&v );
2163+
if ( s != 0 ) {
2164+
printf( "unexpected result\n" );
2165+
break;
2166+
}
2167+
}
2168+
elapsed = tic() - t;
2169+
2170+
if ( s != 0 || *(stdlib_ndarray_get_ptr( arr, sub )) != v ) {
2171+
printf( "unexpected result\n" );
2172+
}
2173+
free( arr );
2174+
2175+
return elapsed;
2176+
}
2177+
20252178
/**
20262179
* Main execution sequence.
20272180
*/
@@ -2323,5 +2476,26 @@ int main( void ) {
23232476
print_results( elapsed );
23242477
printf( "ok %d benchmark finished\n", count );
23252478
}
2479+
for ( i = 0; i < REPEATS; i++ ) {
2480+
count += 1;
2481+
printf( "# c::native::%s::3d:set:mode=error\n", NAME );
2482+
elapsed = benchmark41();
2483+
print_results( elapsed );
2484+
printf( "ok %d benchmark finished\n", count );
2485+
}
2486+
for ( i = 0; i < REPEATS; i++ ) {
2487+
count += 1;
2488+
printf( "# c::native::%s::3d:set:mode=wrap\n", NAME );
2489+
elapsed = benchmark42();
2490+
print_results( elapsed );
2491+
printf( "ok %d benchmark finished\n", count );
2492+
}
2493+
for ( i = 0; i < REPEATS; i++ ) {
2494+
count += 1;
2495+
printf( "# c::native::%s::3d:set:mode=clamp\n", NAME );
2496+
elapsed = benchmark43();
2497+
print_results( elapsed );
2498+
printf( "ok %d benchmark finished\n", count );
2499+
}
23262500
print_summary( count, count );
23272501
}

0 commit comments

Comments
 (0)