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

AoS / SoA Copy Benchmarks, main branch (2024.09.27.) #297

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ add_library( vecmem_benchmark_common STATIC
"common/make_jagged_sizes.hpp"
"common/make_jagged_sizes.cpp"
"common/make_jagged_vector.hpp"
"common/make_jagged_vector.cpp" )
"common/make_jagged_vector.cpp"
"common/setup_simple_copy_counters.hpp"
"common/setup_simple_copy_counters.cpp"
"common/simple_aos_container.hpp"
"common/simple_aos_copy_benchmarks.hpp"
"common/simple_aos_copy_benchmarks.cpp"
"common/simple_soa_container.hpp"
"common/simple_soa_copy_benchmarks.hpp"
"common/simple_soa_copy_benchmarks.cpp" )
target_link_libraries( vecmem_benchmark_common
PUBLIC vecmem::core )
PUBLIC benchmark::benchmark vecmem::core )
set_target_properties( vecmem_benchmark_common PROPERTIES
FOLDER "vecmem/benchmarks" )

Expand All @@ -42,6 +50,9 @@ add_subdirectory(core)
if(VECMEM_BUILD_CUDA_LIBRARY)
add_subdirectory(cuda)
endif()
if(VECMEM_BUILD_HIP_LIBRARY)
add_subdirectory(hip)
endif()
if(VECMEM_BUILD_SYCL_LIBRARY)
add_subdirectory(sycl)
endif()
34 changes: 34 additions & 0 deletions benchmarks/common/setup_simple_copy_counters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "setup_simple_copy_counters.hpp"

namespace vecmem::benchmark {

/// Set up the custom "counters" for the "simple copy" benchmarks.
std::size_t setup_simple_copy_counters(::benchmark::State& state) {

// Get the size of the host container to create.
const std::size_t size = static_cast<std::size_t>(state.range(0));
static constexpr std::size_t element_size =
2 * sizeof(int) + 2 * sizeof(float);
const double bytes = static_cast<double>(size * element_size);

// Set custom "counters" for the benchmark.
state.counters["Bytes"] = ::benchmark::Counter(
bytes, ::benchmark::Counter::kDefaults, ::benchmark::Counter::kIs1024);
state.counters["Rate"] = ::benchmark::Counter(
bytes, ::benchmark::Counter::kIsIterationInvariantRate,
::benchmark::Counter::kIs1024);

// Return the size of the benchmarked container.
return size;
}

} // namespace vecmem::benchmark
22 changes: 22 additions & 0 deletions benchmarks/common/setup_simple_copy_counters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
#pragma once

// Google include(s).
#include <benchmark/benchmark.h>

namespace vecmem::benchmark {

/// Set up the custom "counters" for the "simple copy" benchmarks.
///
/// @param state The benchmark state to set up the counters for.
/// @return The size of the benchmarked container.
///
std::size_t setup_simple_copy_counters(::benchmark::State& state);

} // namespace vecmem::benchmark
51 changes: 51 additions & 0 deletions benchmarks/common/simple_aos_container.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2023-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
#pragma once

// Local include(s).
#include "vecmem/containers/data/vector_buffer.hpp"
#include "vecmem/containers/data/vector_view.hpp"
#include "vecmem/containers/device_vector.hpp"
#include "vecmem/containers/vector.hpp"

namespace vecmem {
namespace benchmark {

/// Simple AoS struct used for benchmarking.
struct simple_aos {

int count;
float measurement;
float average;
int index;

}; // class simple_aos

/// "Simple" container for the benchmarks
struct simple_aos_container {

#if __cplusplus >= 201700L
/// Host container
using host = vector<simple_aos>;
/// Host buffer
using buffer = data::vector_buffer<simple_aos>;
#endif // __cplusplus >= 201700L

/// Non-const device collection for @c simple_aos
using device = device_vector<simple_aos>;
/// Constant device collection for @c simple_aos
using const_device = device_vector<const simple_aos>;

/// Non-constant view of an @c simple_aos collection
using view = data::vector_view<simple_aos>;
/// Constant view of an @c simple_aos collection
using const_view = data::vector_view<const simple_aos>;

}; // struct simple_aos_container

} // namespace benchmark
} // namespace vecmem
70 changes: 70 additions & 0 deletions benchmarks/common/simple_aos_copy_benchmarks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "simple_aos_copy_benchmarks.hpp"

#include "setup_simple_copy_counters.hpp"
#include "simple_aos_container.hpp"

namespace vecmem::benchmark {

void simple_aos_h2d_copy_benchmark(::benchmark::State& state,
memory_resource& host_mr,
memory_resource& device_mr,
copy& device_copy,
data::buffer_type buffer_type) {

// Get the size of the host container to create, while setting up the
// counters used by the benchmark.
const std::size_t size = setup_simple_copy_counters(state);

// Create the source (host) container.
simple_aos_container::host source{&host_mr};
source.resize(size);

// Create the destination buffer.
simple_aos_container::buffer dest{
static_cast<simple_aos_container::buffer::size_type>(size), device_mr,
buffer_type};
device_copy.setup(dest)->wait();

// Perform the copy benchmark.
for (auto _ : state) {
device_copy(get_data(source), dest, vecmem::copy::type::host_to_device)
->wait();
}
}

void simple_aos_d2h_copy_benchmark(::benchmark::State& state,
memory_resource& host_mr,
memory_resource& device_mr,
copy& device_copy) {

// Get the size of the host container to create, while setting up the
// counters used by the benchmark.
const std::size_t size = setup_simple_copy_counters(state);

// Create the source buffer.
simple_aos_container::buffer source{
static_cast<simple_aos_container::buffer::size_type>(size), device_mr};
device_copy.setup(source)->wait();

// Create the destination (host) container.
simple_aos_container::host dest{&host_mr};

// Perform the copy benchmark.
for (auto _ : state) {
state.PauseTiming();
dest.clear();
state.ResumeTiming();
device_copy(source, dest, vecmem::copy::type::device_to_host)->wait();
}
}

} // namespace vecmem::benchmark
35 changes: 35 additions & 0 deletions benchmarks/common/simple_aos_copy_benchmarks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
#pragma once

// Project include(s).
#include "vecmem/containers/data/buffer_type.hpp"
#include "vecmem/memory/memory_resource.hpp"
#include "vecmem/utils/copy.hpp"

// Google include(s).
#include <benchmark/benchmark.h>

namespace vecmem::benchmark {

/// Benchmark copying @c simple_aos_container::host to
/// @c simple_aos_container::buffer
void simple_aos_h2d_copy_benchmark(::benchmark::State& state,
memory_resource& host_mr,
memory_resource& device_mr,
copy& device_copy,
data::buffer_type buffer_type);

/// Benchmark copying @c simple_aos_container::buffer to
/// @c simple_aos_container::host
void simple_aos_d2h_copy_benchmark(::benchmark::State& state,
memory_resource& host_mr,
memory_resource& device_mr,
copy& device_copy);

} // namespace vecmem::benchmark
63 changes: 63 additions & 0 deletions benchmarks/common/simple_soa_container.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2023-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
#pragma once

// Local include(s).
#include "vecmem/edm/container.hpp"
#include "vecmem/utils/types.hpp"

namespace vecmem {
namespace benchmark {

/// Interface to a "simple container" used for benchmarking.
template <typename BASE>
class simple_soa : public BASE {

public:
/// Inherit the base class's constructor(s)
using BASE::BASE;

/// "Count" of something (non-const)
VECMEM_HOST_AND_DEVICE
auto& count() { return BASE::template get<0>(); }
/// "Count" of something (const)
VECMEM_HOST_AND_DEVICE
const auto& count() const { return BASE::template get<0>(); }

/// "Measurement" of something (non-const)
VECMEM_HOST_AND_DEVICE
auto& measurement() { return BASE::template get<1>(); }
/// "Measurement" of something (const)
VECMEM_HOST_AND_DEVICE
const auto& measurement() const { return BASE::template get<1>(); }

/// "Average" of something (non-const)
VECMEM_HOST_AND_DEVICE
auto& average() { return BASE::template get<2>(); }
/// "Average" of something (const)
VECMEM_HOST_AND_DEVICE
const auto& average() const { return BASE::template get<2>(); }

/// "Index" of something (non-const)
VECMEM_HOST_AND_DEVICE
auto& index() { return BASE::template get<3>(); }
/// "Index" of something (const)
VECMEM_HOST_AND_DEVICE
const auto& index() const { return BASE::template get<3>(); }

}; // class simple_soa

/// "Simple" container for the tests
///
/// Meaning that it would not have any jagged vector variables in it...
///
using simple_soa_container =
edm::container<simple_soa, edm::type::vector<int>, edm::type::vector<float>,
edm::type::vector<float>, edm::type::vector<int> >;

} // namespace benchmark
} // namespace vecmem
Loading