Skip to content

Commit 0eedd05

Browse files
author
Vakho Tsulaia
committed
New function for creating misaligned detector views
Introduced a new function `misaligned_detector_view()` that makes a misaligned detector view by combining "static" buffers of the detector (volumes, surfaces, etc.) with a buffer of potentially misaligned transforms. The new mechanism is tested by a new unit test `detector_align_cuda`. A demonstration of the usage of this mechanism added to the `cuda/detector_construction` tutorial code
1 parent cefce90 commit 0eedd05

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** Detray library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2023-2024 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
namespace detray::detail {
11+
12+
/// Creates detector view using "static" detector components and
13+
/// a "misaligned" transform store
14+
template <typename host_detector_type>
15+
typename host_detector_type::view_type misaligned_detector_view(
16+
typename host_detector_type::buffer_type& det_buffer,
17+
typename host_detector_type::transform_container::buffer_type& trf_buffer) {
18+
typename host_detector_type::view_type detview{
19+
detray::get_data(
20+
detray::detail::get<0>(det_buffer.m_buffer)), // volumes
21+
detray::get_data(
22+
detray::detail::get<1>(det_buffer.m_buffer)), // surfaces
23+
detray::get_data(trf_buffer), // transforms
24+
detray::get_data(detray::detail::get<3>(det_buffer.m_buffer)), // masks
25+
detray::get_data(
26+
detray::detail::get<4>(det_buffer.m_buffer)), // materials
27+
detray::get_data(
28+
detray::detail::get<5>(det_buffer.m_buffer)), // accelerators
29+
detray::get_data(detray::detail::get<6>(
30+
det_buffer.m_buffer))}; // volume search grid
31+
return detview;
32+
}
33+
34+
} // namespace detray::detail

tests/unit_tests/device/cuda/detector_cuda.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
// Detray test include(s)
99
#include "detector_cuda_kernel.hpp"
10+
#include "detray/core/detail/alignment.hpp"
11+
#include "detray/definitions/detail/algebra.hpp"
12+
#include "detray/test/utils/types.hpp"
1013
#include "detray/test/utils/detectors/build_toy_detector.hpp"
1114

1215
// Vecmem include(s)
@@ -96,3 +99,49 @@ TEST(detector_cuda, detector) {
9699
EXPECT_EQ(cylinders_host[i] == cylinders_device[i], true);
97100
}
98101
}
102+
103+
TEST(detector_cuda, detector_alignment) {
104+
// Memory resources
105+
vecmem::host_memory_resource host_mr;
106+
vecmem::cuda::managed_memory_resource mng_mr;
107+
vecmem::cuda::device_memory_resource dev_mr;
108+
109+
// Helper object for performing memory copies to CUDA devices
110+
vecmem::cuda::copy cuda_cpy;
111+
112+
// Create toy geometry in host memory
113+
auto [det_host, names_host] = build_toy_detector(host_mr);
114+
115+
// Copy static detector data (including the initial set of transforms) to
116+
// the device Use synchronous copy and fixed size buffers
117+
auto det_fixed_buff = detray::get_buffer(det_host, dev_mr, cuda_cpy);
118+
119+
// Construct an "aligned" transform store
120+
using test_algebra = test::algebra;
121+
using scalar = dscalar<test_algebra>;
122+
using point3 = dpoint3D<test_algebra>;
123+
124+
typename decltype(det_host)::transform_container tf_store_aligned_host;
125+
using tf_type = decltype(det_host)::transform_container::value_type;
126+
127+
point3 shift{.1f * unit<scalar>::mm, .2f * unit<scalar>::mm,
128+
.3f * unit<scalar>::mm};
129+
tf_store_aligned_host.reserve(det_host.transform_store().size(),
130+
typename decltype(det_host)::transform_container::context_type{});
131+
for (const auto& tf : det_host.transform_store()) {
132+
point3 shifted{tf.translation()[0] + shift[0],
133+
tf.translation()[1] + shift[1],
134+
tf.translation()[2] + shift[2]};
135+
tf_store_aligned_host.push_back(
136+
tf_type{shifted, tf.x(), tf.y(), tf.z()});
137+
}
138+
139+
auto tf_buff_shifted =
140+
get_buffer(tf_store_aligned_host, dev_mr, cuda_cpy, copy::sync,
141+
vecmem::data::buffer_type::fixed_size);
142+
143+
using host_detector_type = decltype(det_host);
144+
[[maybe_unused]] auto detector_view =
145+
detail::misaligned_detector_view<host_detector_type>(det_fixed_buff,
146+
tf_buff_shifted);
147+
}

tutorials/src/device/cuda/detector_construction.cpp

+33-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// Project include(s)
99
#include "detector_construction.hpp"
1010

11+
#include "detray/core/detail/alignment.hpp"
1112
#include "detray/test/utils/detectors/build_toy_detector.hpp"
1213

1314
// Vecmem include(s)
@@ -77,10 +78,9 @@ int main() {
7778
auto sf_buff = detray::get_buffer(det_host.surfaces(), dev_mr, cuda_cpy,
7879
detray::copy::sync,
7980
vecmem::data::buffer_type::fixed_size);
80-
// Use resizable buffer and asynchronous copy for alignment
8181
auto trf_buff = detray::get_buffer(det_host.transform_store(), dev_mr,
82-
cuda_cpy, detray::copy::async,
83-
vecmem::data::buffer_type::resizable);
82+
cuda_cpy, detray::copy::sync,
83+
vecmem::data::buffer_type::fixed_size);
8484
auto msk_buff = detray::get_buffer(det_host.mask_store(), dev_mr, cuda_cpy,
8585
detray::copy::sync,
8686
vecmem::data::buffer_type::fixed_size);
@@ -95,11 +95,40 @@ int main() {
9595
vecmem::data::buffer_type::fixed_size);
9696

9797
// Assemble the detector buffer
98-
auto det_custom_buff = typename decltype(det_host)::buffer_type(
98+
using host_detector_type = decltype(det_host);
99+
auto det_custom_buff = typename host_detector_type::buffer_type(
99100
std::move(vol_buff), std::move(sf_buff), std::move(trf_buff),
100101
std::move(msk_buff), std::move(mat_buff), std::move(acc_buff),
101102
std::move(vgrid_buff));
102103

103104
std::cout << "\nCustom buffer setup:" << std::endl;
104105
detray::tutorial::print(detray::get_data(det_custom_buff));
106+
107+
// Construct an "aligned" transform store
108+
using host_transform_type =
109+
host_detector_type::transform_container::value_type;
110+
111+
typename host_detector_type::transform_container host_aligned_transforms;
112+
detray::tutorial::point3 shift{.1f * detray::unit<detray::scalar>::mm,
113+
.2f * detray::unit<detray::scalar>::mm,
114+
.3f * detray::unit<detray::scalar>::mm};
115+
116+
for (const auto& tf : det_host.transform_store()) {
117+
detray::tutorial::point3 shifted{tf.translation()[0] + shift[0],
118+
tf.translation()[1] + shift[1],
119+
tf.translation()[2] + shift[2]};
120+
host_aligned_transforms.push_back(
121+
host_transform_type{shifted, tf.x(), tf.y(), tf.z()});
122+
}
123+
124+
auto trf_buff_shifted = detray::get_buffer(
125+
host_aligned_transforms, dev_mr, cuda_cpy, detray::copy::sync,
126+
vecmem::data::buffer_type::fixed_size);
127+
128+
auto detector_view =
129+
detray::detail::misaligned_detector_view<host_detector_type>(
130+
det_custom_buff, trf_buff_shifted);
131+
132+
std::cout << "\nCustom buffer setup (shifted):" << std::endl;
133+
detray::tutorial::print(detector_view);
105134
}

tutorials/src/device/cuda/detector_construction.cu

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ __global__ void print_kernel(
2525

2626
printf("Number of volumes: %d\n", det.volumes().size());
2727
printf("Number of transforms: %d\n", det.transform_store().size());
28+
printf("First translation: {%f,%f,%f}\n",
29+
det.transform_store().at(0).translation()[0],
30+
det.transform_store().at(0).translation()[1],
31+
det.transform_store().at(0).translation()[2]);
2832
printf("Number of rectangles: %d\n",
2933
det.mask_store().get<mask_id::e_rectangle2>().size());
3034
printf("Number of trapezoids: %d\n",

0 commit comments

Comments
 (0)