Skip to content

Implement benchmarks for vector operations #3

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "external/benchmark"]
path = external/benchmark
url = https://github.com/google/benchmark.git
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -500,6 +500,13 @@ else()
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()

if(testing)
add_subdirectory(testx)
if (testing)
add_subdirectory(testx)
endif()

if (benchmarking)
Copy link
Contributor

@mdessole mdessole Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I would require google benchmark here instead of relying on git modules
find_package(benchmark REQUIRED)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I am using it as a submodule because I thought that it would make it easier to use on remote machine where maybe it isn't installed, but if you think it's better I can remove the submodule and just use the find package.

# use google benchmarks
set(BENCHMARK_ENABLE_GTEST_TESTS OFF)
add_subdirectory(external/benchmark)
add_subdirectory(benchmarks)
endif()
45 changes: 45 additions & 0 deletions benchmarks/Boost.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

#include "SYCLMath/Boost.h"
#include "SYCLMath/VecOps.h"
#include "SYCLMath/Vector4D.h"
#include <benchmark/benchmark.h>
#include <chrono>
#include <memory>

#ifdef SINGLE_PRECISION
using Scalar = float;
#else
using Scalar = double;
#endif

using LVector =
ROOT::Experimental::LorentzVector<ROOT::Experimental::PtEtaPhiM4D<Scalar>>;
using Boost = ROOT::Experimental::Boost;

auto GenVectors(int n) {
auto vectors = std::make_unique<LVector[]>(n);

// generate n -4 momentum quantities
std::for_each(vectors.get(), vectors.get() + n, [](auto &vec) -> void {
vec = {1., 1., 1., 1.};
});

return std::move(vectors);
}

static void BM_ApplyBoost(benchmark::State &state) {
for (auto _ : state) {
const auto N = state.range(0);
auto lvectors = GenVectors(N);
auto* lvectorsboosted = new LVector[N];

Boost bst(0.3, 0.4, 0.5);
lvectorsboosted = ROOT::Experimental::ApplyBoost(lvectors.get(), bst, N);

delete[] lvectorsboosted;
}
}

BENCHMARK(BM_ApplyBoost)->RangeMultiplier(2)->Range(1 << 10, 1 << 20);

BENCHMARK_MAIN();
55 changes: 55 additions & 0 deletions benchmarks/BoostCUDA.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "SYCLMath/Boost.h"
#include "SYCLMath/GenVector/MathUtil.h"
#include "SYCLMath/VecOps.h"
#include "SYCLMath/Vector4D.h"
#include <benchmark/benchmark.h>
#include <cuda_runtime.h>
#include <memory>
#include <vector>

#ifdef SINGLE_PRECISION
using Scalar = float;
#else
using Scalar = double;
#endif

using LVector =
ROOT::Experimental::LorentzVector<ROOT::Experimental::PxPyPzE4D<Scalar>>;
using Boost = ROOT::Experimental::Boost;

template <class T> using Vector = std::vector<T>; // ROOT::RVec<T>;

auto GenVectors(int n) {
auto vectors = std::make_unique<LVector[]>(n);

// generate n -4 momentum quantities
std::for_each(vectors.get(), vectors.get() + n,
[](auto &vec) -> void { vec = {1., 1., 1., 1.}; });

return std::move(vectors);
}

static void BM_ApplyBoost(benchmark::State &state) {
int count;
cudaGetDeviceCount(&count);
cudaSetDevice(count - 1);
for (auto _ : state) {
const auto N = state.range(0);
size_t local_size = 128;


auto lvectors = GenVectors(N);
LVector *lvectorsboosted = new LVector[N];

Boost bst(0.3, 0.4, 0.5);

lvectorsboosted = ROOT::Experimental::ApplyBoost<Boost, LVector>(
lvectors.get(), bst, N, local_size);

delete[] lvectorsboosted;
}
}

BENCHMARK(BM_ApplyBoost)->RangeMultiplier(2)->Range(1 << 10, 1 << 20)->UseRealTime();

BENCHMARK_MAIN();
58 changes: 58 additions & 0 deletions benchmarks/BoostCUDAStreamed.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "SYCLMath/Boost.h"
#include "SYCLMath/GenVector/MathUtil.h"
#include "SYCLMath/VecOps.h"
#include "SYCLMath/Vector4D.h"
#include <benchmark/benchmark.h>
#include <cuda_runtime.h>
#include <memory>
#include <vector>

#ifdef SINGLE_PRECISION
using Scalar = float;
#else
using Scalar = double;
#endif

using LVector =
ROOT::Experimental::LorentzVector<ROOT::Experimental::PxPyPzE4D<Scalar>>;
using Boost = ROOT::Experimental::Boost;

template <class T> using Vector = std::vector<T>; // ROOT::RVec<T>;

auto GenVectors(int n) {
auto vectors = std::make_unique<LVector[]>(n);

// generate n -4 momentum quantities
std::for_each(vectors.get(), vectors.get() + n,
[](auto &vec) -> void { vec = {1., 1., 1., 1.}; });

return std::move(vectors);
}

static void BM_ApplyBoost(benchmark::State &state) {
int count;
cudaGetDeviceCount(&count);
cudaSetDevice(count - 1);
cudaStream_t stream;
cudaStreamCreate(&stream);

for (auto _ : state) {
const auto N = state.range(0);
size_t local_size = 128;

auto lvectors = GenVectors(N);
LVector *lvectorsboosted = new LVector[N];

Boost bst(0.3, 0.4, 0.5);

lvectorsboosted = ROOT::Experimental::ApplyBoost<Boost, LVector>(
lvectors.get(), bst, N, local_size, stream);

delete[] lvectorsboosted;
}
cudaStreamDestroy(stream);
}

BENCHMARK(BM_ApplyBoost)->RangeMultiplier(2)->Range(1 << 10, 1 << 20)->UseRealTime();

BENCHMARK_MAIN();
49 changes: 49 additions & 0 deletions benchmarks/BoostSYCL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#include "SYCLMath/Boost.h"
#include "SYCLMath/VecOps.h"
#include "SYCLMath/Vector4D.h"
#include <benchmark/benchmark.h>
#include <chrono>
#include <memory>
#include <sycl/sycl.hpp>

#ifdef SINGLE_PRECISION
using Scalar = float;
#else
using Scalar = double;
#endif

using LVector =
ROOT::Experimental::LorentzVector<ROOT::Experimental::PtEtaPhiM4D<Scalar>>;
using Boost = ROOT::Experimental::Boost;

auto GenVectors(int n) {
auto vectors = std::make_unique<LVector[]>(n);

// generate n -4 momentum quantities
std::for_each(vectors.get(), vectors.get() + n, [](auto &vec) -> void {
vec = {1., 1., 1., 1.};
});

return std::move(vectors);
}

static void BM_ApplyBoost(benchmark::State &state) {
for (auto _ : state) {
const auto N = state.range(0);
const auto local_size = 128;
auto lvectors = GenVectors(N);
auto* lvectorsboosted = new LVector[N];

static sycl::queue queue{sycl::default_selector_v};

Boost bst(0.3, 0.4, 0.5);
lvectorsboosted = ROOT::Experimental::ApplyBoost(lvectors.get(), bst, queue, N, local_size);

delete[] lvectorsboosted;
}
}

BENCHMARK(BM_ApplyBoost)->RangeMultiplier(2)->Range(1 << 10, 1 << 20);

BENCHMARK_MAIN();
158 changes: 158 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

add_executable(Boost_bench Boost.cpp)
target_link_libraries(Boost_bench PRIVATE benchmark::benchmark)
target_compile_definitions(Boost_bench PRIVATE ${compile_list})

add_executable(InvariantMasses_bench InvariantMasses.cpp)
target_link_libraries(InvariantMasses_bench PRIVATE benchmark::benchmark)
target_compile_definitions(InvariantMasses_bench PRIVATE ${compile_list})

if (single_precision)
add_executable(SBoost_bench Boost.cpp)
target_link_libraries(SBoost_bench PRIVATE benchmark::benchmark)
target_compile_definitions(SBoost_bench PRIVATE ${compile_list_single})

add_executable(SInvariantMasses_bench InvariantMasses.cpp)
target_link_libraries(SInvariantMasses_bench PRIVATE benchmark::benchmark)
target_compile_definitions(SInvariantMasses_bench PRIVATE ${compile_list_single})
endif()

if (oneapi)

add_executable(InvariantMassesSYCL_bench InvariantMassesSYCL.cpp)
target_link_options(InvariantMassesSYCL_bench PUBLIC -E)
add_sycl_to_root_target_exe(TARGET InvariantMassesSYCL_bench SOURCES InvariantMassesSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list})

add_executable(BoostSYCL_bench BoostSYCL.cpp)
target_link_options(BoostSYCL_bench PUBLIC -E)
add_sycl_to_root_target_exe(TARGET BoostSYCL_bench SOURCES BoostSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list})

if (single_precision)
add_executable(SInvariantMassesSYCL_bench InvariantMassesSYCL.cpp)
target_link_options(SInvariantMassesSYCL_bench PUBLIC -E)
add_sycl_to_root_target_exe(TARGET SInvariantMassesSYCL_bench SOURCES InvariantMassesSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list_single})


add_executable(SBoostSYCL_bench BoostSYCL.cpp)
target_link_options(SBoostSYCL_bench PUBLIC -E)
add_sycl_to_root_target_exe(TARGET SBoostSYCL_bench SOURCES BoostSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list_single})

endif()

endif()

if (adaptivecpp)
add_executable(InvariantMassesSYCL_bench InvariantMassesSYCL.cpp)
add_sycl_to_root_target(TARGET InvariantMassesSYCL_bench SOURCES InvariantMassesSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list})


add_executable(BoostSYCL_bench BoostSYCL.cpp)
add_sycl_to_root_target(TARGET BoostSYCL_bench SOURCES BoostSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list})


if (single_precision)
add_executable(SInvariantMassesSYCL_bench InvariantMassesSYCL.cpp)
add_sycl_to_root_target(TARGET SInvariantMassesSYCL_bench SOURCES InvariantMassesSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list_single})


add_executable(SBoostSYCL_bench BoostSYCL.cpp)
add_sycl_to_root_target(TARGET SBoostSYCL_bench SOURCES BoostSYCL.cpp
DEPENDENCIES GenVectorX SYCLMath VecOps benchmark
INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/inc
COMPILE_DEFINITIONS ${sycl_compile_list_single})

endif()

endif()


if (cuda)
add_executable(BoostCUDA_bench BoostCUDA.cu)
set_target_properties(BoostCUDA_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(BoostCUDA_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(BoostCUDA_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(BoostCUDA_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(BoostCUDA_bench PRIVATE benchmark::benchmark)
target_compile_definitions(BoostCUDA_bench PRIVATE ${cuda_compile_list})

add_executable(BoostCUDAStreamed_bench BoostCUDAStreamed.cu)
set_target_properties(BoostCUDAStreamed_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(BoostCUDAStreamed_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(BoostCUDAStreamed_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(BoostCUDAStreamed_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(BoostCUDAStreamed_bench PRIVATE benchmark::benchmark)
target_compile_definitions(BoostCUDAStreamed_bench PRIVATE ${cuda_compile_list})

add_executable(InvariantMassesCUDA_bench InvariantMassesCUDA.cu)
set_target_properties(InvariantMassesCUDA_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(InvariantMassesCUDA_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(InvariantMassesCUDA_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(InvariantMassesCUDA_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(InvariantMassesCUDA_bench PRIVATE benchmark::benchmark)
target_compile_definitions(InvariantMassesCUDA_bench PRIVATE ${cuda_compile_list})

add_executable(InvariantMassesCUDAStreamed_bench InvariantMassesCUDAStreamed.cu)
set_target_properties(InvariantMassesCUDAStreamed_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(InvariantMassesCUDAStreamed_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(InvariantMassesCUDAStreamed_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(InvariantMassesCUDAStreamed_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(InvariantMassesCUDAStreamed_bench PRIVATE benchmark::benchmark)
target_compile_definitions(InvariantMassesCUDAStreamed_bench PRIVATE ${cuda_compile_list})

if (single_precision)
add_executable(SBoostCUDA_bench BoostCUDA.cu)
set_target_properties(SBoostCUDA_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(SBoostCUDA_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(SBoostCUDA_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(SBoostCUDA_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(SBoostCUDA_bench PRIVATE benchmark::benchmark)
target_compile_definitions(SBoostCUDA_bench PRIVATE ${cuda_compile_list_single})

add_executable(SBoostCUDAStreamed_bench BoostCUDAStreamed.cu)
set_target_properties(SBoostCUDAStreamed_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(SBoostCUDAStreamed_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(SBoostCUDAStreamed_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(SBoostCUDAStreamed_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(SBoostCUDAStreamed_bench PRIVATE benchmark::benchmark)
target_compile_definitions(SBoostCUDAStreamed_bench PRIVATE ${cuda_compile_list_single})

add_executable(SInvariantMassesCUDA_bench InvariantMassesCUDA.cu)
set_target_properties(SInvariantMassesCUDA_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(SInvariantMassesCUDA_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(SInvariantMassesCUDA_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(SInvariantMassesCUDA_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(SInvariantMassesCUDA_bench PRIVATE benchmark::benchmark)
target_compile_definitions(SInvariantMassesCUDA_bench PRIVATE ${cuda_compile_list_single})

add_executable(SInvariantMassesCUDAStreamed_bench InvariantMassesCUDAStreamed.cu)
set_target_properties(SInvariantMassesCUDAStreamed_bench PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(SInvariantMassesCUDAStreamed_bench PUBLIC GenVectorX SYCLMath VecOps)
target_compile_options(SInvariantMassesCUDAStreamed_bench PRIVATE -rdc=true -lineinfo --expt-relaxed-constexpr)
set_target_properties(SInvariantMassesCUDAStreamed_bench PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(SInvariantMassesCUDAStreamed_bench PRIVATE benchmark::benchmark)
target_compile_definitions(SInvariantMassesCUDAStreamed_bench PRIVATE ${cuda_compile_list_single})
endif()

endif()

Loading