Skip to content

Commit

Permalink
Added cpu raw performance benchmark for all generators against std:mt…
Browse files Browse the repository at this point in the history
…19937 using google-benchmark (#6)
  • Loading branch information
Shihab-Shahriar authored Oct 16, 2023
1 parent e7fbad6 commit 365f64a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 145 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
enable_testing()
add_subdirectory(tests)


add_subdirectory(examples)
add_subdirectory(benchmarks)

Expand All @@ -32,6 +31,15 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# TODO: it's still being built as DEBUG
FetchContent_Declare(
google_benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG main
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
)
FetchContent_MakeAvailable(google_benchmark)
endif()


Expand Down
5 changes: 3 additions & 2 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_executable(raw_speed raw_speed.cpp)
target_include_directories(raw_speed PRIVATE ${CMAKE_SOURCE_DIR}/include)
add_executable(raw_speed_cpu raw_speed_cpu.cpp)
target_include_directories(raw_speed_cpu PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(raw_speed_cpu benchmark::benchmark)


include(CheckLanguage)
Expand Down
142 changes: 0 additions & 142 deletions benchmarks/raw_speed.cpp

This file was deleted.

96 changes: 96 additions & 0 deletions benchmarks/raw_speed_cpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// @HEADER
// *******************************************************************************
// OpenRAND *
// A Performance Portable, Reproducible Random Number Generation Library *
// *
// Copyright (c) 2023, Michigan State University *
// *
// Permission is hereby granted, free of charge, to any person obtaining a copy *
// of this software and associated documentation files (the "Software"), to deal *
// in the Software without restriction, including without limitation the rights *
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
// copies of the Software, and to permit persons to whom the Software is *
// furnished to do so, subject to the following conditions: *
// *
// The above copyright notice and this permission notice shall be included in *
// all copies or substantial portions of the Software. *
// *
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
// SOFTWARE. *
//********************************************************************************
// @HEADER

// This compares the raw speed of all the generators on both CPU and GPU.

#include <benchmark/benchmark.h>
#include <openrand/phillox.h>
#include <openrand/squares.h>
#include <openrand/threefry.h>
#include <openrand/tyche.h>

#include <random>

template <typename RNG>
static void bench_rng(benchmark::State& state) {
for (auto _ : state) {
RNG rng(12345, 0);
for (int i = 0; i < state.range(0); ++i)
benchmark::DoNotOptimize(rng.template draw<uint32_t>());
}
}

template <typename RNG>
static void bench_cpp(benchmark::State& state) {
for (auto _ : state) {
RNG rng(12345);
for (int i = 0; i < state.range(0); ++i) benchmark::DoNotOptimize(rng());
}
//benchmark::ClobberMemory();
}

#define TUnit benchmark::kNanosecond

// Register the function as a benchmark
BENCHMARK(bench_rng<openrand::Phillox>)
->Unit(TUnit)
->Arg(10)
->Arg(100)
->Arg(1000)
->Arg(10000)
->Arg(100000);
BENCHMARK(bench_rng<openrand::Tyche>)
->Unit(TUnit)
->Arg(10)
->Arg(100)
->Arg(1000)
->Arg(10000)
->Arg(100000);
BENCHMARK(bench_rng<openrand::Squares>)
->Unit(TUnit)
->Arg(10)
->Arg(100)
->Arg(1000)
->Arg(10000)
->Arg(100000);
BENCHMARK(bench_rng<openrand::Threefry>)
->Unit(TUnit)
->Arg(10)
->Arg(100)
->Arg(1000)
->Arg(10000)
->Arg(100000);

BENCHMARK(bench_cpp<std::mt19937>)
->Unit(TUnit)
->Arg(10)
->Arg(100)
->Arg(1000)
->Arg(10000)
->Arg(100000);

BENCHMARK_MAIN();

0 comments on commit 365f64a

Please sign in to comment.