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

Increase collection comparison performance #836

Merged
Merged
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
2 changes: 0 additions & 2 deletions performance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ traccc_add_library( traccc_performance performance TYPE SHARED
"include/traccc/performance/impl/comparator_factory.ipp"
"include/traccc/performance/impl/seed_comparator_factory.ipp"
"src/performance/details/comparator_factory.cpp"
"include/traccc/performance/details/comparison_progress.hpp"
"src/performance/details/comparison_progress.cpp"
# Collection/container comparison code.
"include/traccc/performance/collection_comparator.hpp"
"include/traccc/performance/impl/collection_comparator.ipp"
Expand Down

This file was deleted.

76 changes: 76 additions & 0 deletions performance/include/traccc/performance/details/projector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Library include(s).
#include "traccc/performance/details/is_same_object.hpp"

// Project include(s).
#include "traccc/definitions/common.hpp"
#include "traccc/definitions/primitives.hpp"

namespace traccc::details {

/// Factory creating instances of "comparator objects" for a given type
///
/// This level of abstraction is necessary to be able to construct comparator
/// objects that would have extra configuration parameters over the reference
/// object and the comparison uncertainty.
///
/// @tparam TYPE The type for which a comparator object should be generated
///
template <typename TYPE>
struct projector {
static constexpr bool exists = false;
};

template <>
struct projector<traccc::measurement> {
static constexpr bool exists = true;

float operator()(const traccc::measurement& i) {
return static_cast<float>(i.local[0]);
}
};

template <>
struct projector<traccc::spacepoint> {
static constexpr bool exists = true;

float operator()(const traccc::spacepoint& i) {
return static_cast<float>(i.x());
}
};

template <>
struct projector<traccc::seed> {
static constexpr bool exists = true;

float operator()(const traccc::seed& i) {
return static_cast<float>(i.z_vertex);
}
};

template <>
struct projector<traccc::bound_track_parameters> {
static constexpr bool exists = true;

float operator()(const traccc::bound_track_parameters& i) {
return static_cast<float>(i.phi());
}
};

template <>
struct projector<fitting_result<traccc::default_algebra>> {
static constexpr bool exists = true;

float operator()(const fitting_result<traccc::default_algebra>& i) {
return static_cast<float>(i.ndf);
}
};
} // namespace traccc::details
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#pragma once

// Library include(s).
#include "traccc/performance/details/comparison_progress.hpp"
#include "traccc/performance/details/is_same_object.hpp"
#include "traccc/performance/details/is_same_scalar.hpp"
#include "traccc/performance/details/projector.hpp"

// Project include(s).
#include "traccc/definitions/common.hpp"
Expand Down Expand Up @@ -37,7 +38,9 @@ collection_comparator<TYPE>::collection_comparator(
m_rhs_type(rhs_type),
m_comp_factory(comp_factory),
m_out(out),
m_uncertainties(uncertainties) {}
m_uncertainties(uncertainties) {
std::sort(m_uncertainties.begin(), m_uncertainties.end());
}

template <typename TYPE>
void collection_comparator<TYPE>::operator()(
Expand All @@ -48,30 +51,67 @@ void collection_comparator<TYPE>::operator()(
const typename collection_types<TYPE>::const_device lhs_coll{lhs},
rhs_coll{rhs};

using projector_t = details::projector<TYPE>;

std::vector<TYPE> rhs_sorted;

if constexpr (projector_t::exists) {
rhs_sorted.reserve(rhs_coll.size());
std::copy(rhs_coll.begin(), rhs_coll.end(),
std::back_inserter(rhs_sorted));
std::sort(rhs_sorted.begin(), rhs_sorted.end(),
[](const TYPE& a, const TYPE& b) {
return projector_t{}(a) < projector_t{}(b);
});
}

// Print some basic output.
m_out.get() << "Number of " << m_type_name << ": " << lhs_coll.size()
<< " (" << m_lhs_type << "), " << rhs_coll.size() << " ("
<< m_rhs_type << ")\n";

// Create a progress bar.
details::comparison_progress progress{
lhs_coll.size() * m_uncertainties.size(), m_out.get()};

// Calculate the agreements at various uncertainties.
std::vector<scalar> agreements;
agreements.reserve(m_uncertainties.size());

std::vector<bool> is_matched(lhs_coll.size(), false);

for (scalar uncertainty : m_uncertainties) {
// The number of matched items between the containers.
std::size_t matched = 0;
// Iterate over all elements of the LHS collection.
for (const TYPE& obj : lhs_coll) {
// Check if there's an equivalent element in the RHS collection.
if (std::find_if(rhs_coll.begin(), rhs_coll.end(),
m_comp_factory.make_comparator(
obj, uncertainty)) != rhs_coll.end()) {
for (typename decltype(lhs_coll)::size_type idx = 0;
idx < lhs_coll.size(); ++idx) {
if (is_matched[idx]) {
++matched;
} else {
const TYPE& obj = lhs_coll[idx];
// Check if there's an equivalent element in the RHS collection.
if constexpr (projector_t::exists) {
auto [lower_bound, upper_bound] = std::equal_range(
rhs_sorted.begin(), rhs_sorted.end(), obj,
[&uncertainty](const TYPE& a, const TYPE& val) {
return projector_t{}(a) <= projector_t{}(val) &&
!details::is_same_scalar(projector_t{}(a),
projector_t{}(val),
uncertainty);
});

if (std::find_if(lower_bound, upper_bound,
m_comp_factory.make_comparator(
obj, uncertainty)) != upper_bound) {
++matched;
is_matched[idx] = true;
}
} else {
if (std::find_if(rhs_coll.begin(), rhs_coll.end(),
m_comp_factory.make_comparator(
obj, uncertainty)) != rhs_coll.end()) {
++matched;
is_matched[idx] = true;
}
}
}
progress.tick();
}
// Calculate the agreement value.
agreements.push_back(
Expand Down
44 changes: 0 additions & 44 deletions performance/src/performance/details/comparison_progress.cpp

This file was deleted.

Loading