Skip to content

Commit

Permalink
Add the tuple array container to the detector and puts the toy
Browse files Browse the repository at this point in the history
detector grids inside (Only works on host so far). Apart from that,
a brute force surface finder is also added. All can be accessed with
custom type IDs, the same way as it is implemented for the masks.
The surface finders can be called from the navigator using a
neighborhood_kernel, but for now they always return the complete
volume range, until the zone function of the grid is properly adapted.
The grids are built in the toy geometry in dedicated functions and
the detector now uses the custom bin_association to fill surfaces
into grids. For now, the grid building is commented out until we have
a working implementation of a grid container in a subsequent PR.

Further small changes:

 - The previous surface_finder class is no longer used in favour of
   the tuple_array_container. All typedefs are done by the
   type_registry.
 - The type ID enums are no longer implicitly convertible to an index
   for type safety, but can be converted, if necessary.
 - Where possible, all code that contains 'unrolling' has been put
   into functors and is called in the tuple_container.
 - The volume index is now fixed to dindex, since this is unlikely to
   change.
 - The surface finder link has been removed from the masks, since it
   is now part of the volume class.
 - Some minor refactoring (e.g. the edge types are renamed to
   volume_link for clarity).
 - More elaborate comments on some geometry classes and their
   template arguments.
 - For some reason, the usage of the detector memory resource in the
   navigator buffer broke, so they get an external resource now.
  • Loading branch information
niermann999 committed Oct 7, 2022
1 parent ad1de25 commit e856af4
Show file tree
Hide file tree
Showing 39 changed files with 1,948 additions and 1,530 deletions.
4 changes: 4 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ detray_add_library( detray_core core
"include/detray/propagator/navigator.hpp"
"include/detray/propagator/propagator.hpp"
"include/detray/propagator/rk_stepper.hpp"
# surface finder include(s)
"include/detray/surface_finders/brute_force_finder.hpp"
"include/detray/surface_finders/grid2_finder.hpp"
"include/detray/surface_finders/neighborhood_kernel.hpp"
# tools include(s)
"include/detray/tools/associator.hpp"
"include/detray/tools/bin_association.hpp"
Expand Down
2 changes: 2 additions & 0 deletions core/include/detray/core/detail/tuple_array_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class tuple_array_container<tuple_t, array_t, id_t, std::index_sequence<Ns...>,

using container_type = typename base_type::container_type;

tuple_array_container() = default;

/**
* Constructor with a vecmem memory resource. (host-side only)
*
Expand Down
138 changes: 96 additions & 42 deletions core/include/detray/core/detail/tuple_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class tuple_container {
*/
template <id_t ID>
DETRAY_HOST_DEVICE size_t size() const {
return detail::get<ID>(m_container).size();
return detail::get<to_index(ID)>(m_container).size();
}

/**
Expand All @@ -93,7 +93,7 @@ class tuple_container {
*/
template <id_t ID>
DETRAY_HOST_DEVICE bool empty() const {
return detail::get<ID>(m_container).empty();
return detail::get<to_index(ID)>(m_container).empty();
}

/**
Expand All @@ -104,7 +104,7 @@ class tuple_container {
*/
template <id_t ID>
DETRAY_HOST_DEVICE constexpr auto &group() {
return detail::get<ID>(m_container);
return detail::get<to_index(ID)>(m_container);
}

/**
Expand All @@ -115,25 +115,25 @@ class tuple_container {
*/
template <id_t ID>
DETRAY_HOST_DEVICE constexpr const auto &group() const {
return detail::get<ID>(m_container);
return detail::get<to_index(ID)>(m_container);
}

/** Enforce usage id_t in the code and do some (limited)
* checking.
*
* @tparam ref_idx matches to index arg to perform static checks
* @param index argument to be converted to valid id type
*
* @return the matching ID type.
*/
/// @brief Convert index of the tuple to an id.
///
/// The function uses unrolling, so that it can be used as a constant expr.
///
/// @tparam ref_idx matches to index arg to perform static checks
/// @param index argument to be converted to valid id type
///
/// @return the matching ID.
template <std::size_t ref_idx = 0>
DETRAY_HOST_DEVICE static constexpr id_t to_id(const std::size_t index) {
if (ref_idx == index) {
// Produce a more helpful error than the usual tuple index error
static_assert(
ref_idx < sizeof...(Ts),
"Index out of range: Please make sure that indices and type "
"enums match the number of types in container.");
"enums match the number of types in the tuple container.");
return static_cast<id_t>(index);
}
if constexpr (ref_idx < sizeof...(Ts) - 1) {
Expand All @@ -143,50 +143,104 @@ class tuple_container {
return static_cast<id_t>(sizeof...(Ts));
}

/** Execute functor for a group with specific ID. The group is found by
* unrolling varidically
*
* @tparam functor_t is the functor type
* @tparam size_type is type for index
* @tparam Args is argument type for the functor
*
* @param id is the target group index
* @param As is the functor arguments
*
* @return the functor output
*/
template <typename functor_t, typename size_type, typename... Args>
DETRAY_HOST_DEVICE typename functor_t::output_type execute(
const size_type id, Args &&... As) const {
/// @brief Convert an id to an index of the tuple.
///
/// The function uses unrolling, so that it can be used as a constant expr.
///
/// @tparam ref_idx matches to index arg to perform static checks
/// @param id type id that should be used to index a tuple element
///
/// @return the matching index.
template <std::size_t ref_idx = 0>
DETRAY_HOST_DEVICE static constexpr std::size_t to_index(const id_t id) {
if (to_id(ref_idx) == id) {
// Produce a more helpful error than the usual tuple index error
static_assert(
ref_idx < sizeof...(Ts),
"Index out of range: This ID cannot be used to index the tuple "
"container.");
return ref_idx;
}
if constexpr (ref_idx < sizeof...(Ts) - 1) {
return to_index<ref_idx + 1>(id);
}
// This produces a compiler error when used in type unrolling code
return sizeof...(Ts);
}

return unroll<functor_t>(id, std::make_index_sequence<sizeof...(Ts)>{},
/// Calls a functor for a group with specific ID. The group is found by
/// unrolling varidically
///
/// @tparam functor_t functor that will be called on the group.
/// @tparam Args argument types for the functor
///
/// @param id is the target group id
/// @param As additional functor arguments
///
/// @return the functor output
template <typename functor_t, typename... Args>
DETRAY_HOST_DEVICE typename functor_t::output_type call(
const id_t id, Args &&... As) const {

// An invalid range will be interpreted by the detray range iterator to
// mean the entire range. Otherwise use overload function below to
// specify a valid range
return unroll<functor_t>(id, dindex_range{0, dindex_invalid},
std::make_index_sequence<sizeof...(Ts)>{},
std::forward<Args>(As)...);
}

/// Calls a functor for a group with specific ID. The group is found by
/// unrolling varidically
///
/// @tparam functor_t functor that will be called on the group.
/// @tparam link_t how to reference a group and its entries.
/// @tparam Args argument types for the functor
///
/// @param link contains the group id and an index into the group
/// @param As additional functor arguments
///
/// @return the functor output
template <typename functor_t, typename link_t, typename... Args>
DETRAY_HOST_DEVICE typename functor_t::output_type call(
const link_t link, Args &&... As) const {

return unroll<functor_t>(detail::get<0>(link), detail::get<1>(link),
std::make_index_sequence<sizeof...(Ts)>{},
std::forward<Args>(As)...);
}

protected:
container_type m_container;

private:
/** Variadic unroll function used for execute function
*/
template <typename functor_t, typename size_type, typename... Args,
std::size_t first_id, std::size_t... remaining_ids>
/// Variadic unroll function used for execute function
///
/// @tparam functor_t functor that will be called on the group (members).
/// @tparam index_t how to reference a member(s) in the group. Can be a
/// single index/range/multiindex
/// @tparam Args argument types for the functor
/// @tparam first_idx Current index into the container tuple. Is converted
/// to an id_t and tested aginst the given id.
/// @tparam remaining_idcs te remaining tuple indices to be tested.
template <typename functor_t, typename index_t, typename... Args,
std::size_t first_idx, std::size_t... remaining_idcs>
DETRAY_HOST_DEVICE typename functor_t::output_type unroll(
const size_type id,
std::index_sequence<first_id, remaining_ids...> /*seq*/,
const id_t id, const index_t index,
std::index_sequence<first_idx, remaining_idcs...> /*seq*/,
Args &&... As) const {

// Check if the first ID is matched to the target ID
if (id == first_id) {
const auto &gr = this->group<to_id(first_id)>();
// Check if the first tuple index is matched to the target ID
if (id == to_id(first_idx)) {
const auto &gr = this->group<to_id(first_idx)>();

return functor_t()(gr, std::forward<Args>(As)...);
return functor_t()(gr, index, std::forward<Args>(As)...);
}

// Check the next ID
if constexpr (sizeof...(remaining_ids) >= 1) {
return unroll<functor_t>(id,
std::index_sequence<remaining_ids...>{},
if constexpr (sizeof...(remaining_idcs) >= 1) {
return unroll<functor_t>(id, index,
std::index_sequence<remaining_idcs...>{},
std::forward<Args>(As)...);
}

Expand Down
14 changes: 7 additions & 7 deletions core/include/detray/core/detail/tuple_vector_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class tuple_vector_container final
using base_type = tuple_container<tuple_t, id_t, vector_t<Ts>...>;
using base_type::base_type;
using id_type = typename base_type::id_type;
using base_type::to_index;

static constexpr std::size_t m_tuple_size = base_type::m_tuple_size;

Expand Down Expand Up @@ -107,7 +108,7 @@ class tuple_vector_container final
template <id_t ID, typename... Args>
DETRAY_HOST auto &add_value(Args &&... args) noexcept(false) {

auto &gr = detail::get<ID>(this->m_container);
auto &gr = detail::get<to_index(ID)>(this->m_container);

return gr.emplace_back(std::forward<Args>(args)...);
}
Expand Down Expand Up @@ -157,21 +158,20 @@ class tuple_vector_container final

/** Append a container to the current one
*
* @tparam current_id is the index to start unrolling (if th index is known,
* unrolling can be started there)
* @tparam current_idx is the index to start unrolling
*
* @param other The other container
*
* @note in general can throw an exception
*/
template <std::size_t current_id = 0>
template <std::size_t current_idx = 0>
DETRAY_HOST inline void append_container(
tuple_vector_container &other) noexcept(false) {
auto &gr = detail::get<current_id>(other);
auto &gr = detail::get<current_idx>(other);
add_vector(gr);

if constexpr (current_id < sizeof...(Ts) - 1) {
append_container<current_id + 1>(other);
if constexpr (current_idx < sizeof...(Ts) - 1) {
append_container<current_idx + 1>(other);
}
}
};
Expand Down
Loading

0 comments on commit e856af4

Please sign in to comment.