Skip to content

Commit ae4e35e

Browse files
committed
Introduced device::update_tip_length_buffer.
While modifying cuda::kernels::update_tip_length_buffer to make use of that common function, and updating device::build_tracks to be able to collaborate with the slightly different data that update_tip_length_buffer is now producing.
1 parent 0507d6c commit ae4e35e

File tree

9 files changed

+125
-76
lines changed

9 files changed

+125
-76
lines changed

device/alpaka/src/finding/combinatorial_kalman_filter.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** TRACCC library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2024-2025 CERN for the benefit of the ACTS project
3+
* (c) 2024-2026 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
@@ -643,7 +643,7 @@ combinatorial_kalman_filter(
643643
.links_view = links_buffer,
644644
.tips_view = tips_buffer,
645645
.tracks_view = track_candidates_buffer,
646-
.tip_to_output_map = nullptr,
646+
.tip_to_output_map = {},
647647
.jacobian_ptr = jacobian_ptr.get(),
648648
.link_predicted_parameter_view =
649649
link_predicted_parameter_buffer,

device/common/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ traccc_add_library( traccc_device_common device_common
7777
"include/traccc/finding/device/gather_measurement_votes.hpp"
7878
"include/traccc/finding/device/fill_finding_propagation_sort_keys.hpp"
7979
"include/traccc/finding/device/propagate_to_next_surface.hpp"
80+
"include/traccc/finding/device/update_tip_length_buffer.hpp"
8081
"include/traccc/finding/device/impl/apply_interaction.ipp"
8182
"include/traccc/finding/device/impl/build_tracks.ipp"
8283
"include/traccc/finding/device/impl/find_tracks.ipp"
8384
"include/traccc/finding/device/impl/gather_best_tips_per_measurement.ipp"
8485
"include/traccc/finding/device/impl/gather_measurement_votes.ipp"
8586
"include/traccc/finding/device/impl/fill_finding_propagation_sort_keys.ipp"
8687
"include/traccc/finding/device/impl/propagate_to_next_surface.ipp"
88+
"include/traccc/finding/device/impl/update_tip_length_buffer.ipp"
8789
# Track fitting funtions(s).
8890
"include/traccc/fitting/device/fit.hpp"
8991
"include/traccc/fitting/device/fill_fitting_sort_keys.hpp"

device/common/include/traccc/finding/device/build_tracks.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct build_tracks_payload {
4848
/**
4949
* @brief Optional mapping from tip index to output index
5050
*/
51-
const unsigned int* tip_to_output_map = nullptr;
51+
vecmem::data::vector_view<const unsigned int> tip_to_output_map;
5252
bound_matrix<default_algebra>* jacobian_ptr = nullptr;
5353
bound_track_parameters_collection_types::view link_predicted_parameter_view;
5454
bound_track_parameters_collection_types::view link_filtered_parameter_view;

device/common/include/traccc/finding/device/impl/build_tracks.ipp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ TRACCC_HOST_DEVICE inline void build_tracks(
4545
return;
4646
}
4747

48-
const unsigned int output_idx = payload.tip_to_output_map != nullptr
49-
? payload.tip_to_output_map[globalIndex]
48+
const vecmem::device_vector<const unsigned int> tip_to_output_map(
49+
payload.tip_to_output_map);
50+
const unsigned int output_idx = tip_to_output_map.size()
51+
? tip_to_output_map.at(globalIndex)
5052
: globalIndex;
5153

5254
if (output_idx == std::numeric_limits<unsigned int>::max()) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/** traccc library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025-2026 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// VecMem include(s).
11+
#include <vecmem/containers/device_vector.hpp>
12+
#include <vecmem/memory/device_atomic_ref.hpp>
13+
14+
// System include(s).
15+
#include <cassert>
16+
#include <limits>
17+
18+
namespace traccc::device {
19+
20+
TRACCC_HOST_DEVICE inline void update_tip_length_buffer(
21+
global_index_t thread_id, const update_tip_length_buffer_payload& payload) {
22+
23+
const vecmem::device_vector<const unsigned int> measurement_votes(
24+
payload.measurement_votes);
25+
if (thread_id >= measurement_votes.size()) {
26+
return;
27+
}
28+
29+
const vecmem::device_vector<const unsigned int> old_tip_length(
30+
payload.old_tip_length);
31+
vecmem::device_vector<unsigned int> new_tip_length(payload.new_tip_length);
32+
33+
const unsigned int total_measurements = old_tip_length.at(thread_id);
34+
const unsigned int total_votes = measurement_votes.at(thread_id);
35+
36+
assert(total_votes <= total_measurements);
37+
38+
const float vote_fraction = static_cast<float>(total_votes) /
39+
static_cast<float>(total_measurements);
40+
41+
vecmem::device_vector<unsigned int> tip_to_output_map(
42+
payload.tip_to_output_map);
43+
44+
if (vote_fraction < payload.min_measurement_voting_fraction) {
45+
tip_to_output_map.at(thread_id) =
46+
std::numeric_limits<unsigned int>::max();
47+
} else {
48+
const vecmem::device_vector<unsigned int>::size_type new_idx =
49+
new_tip_length.push_back(total_measurements);
50+
tip_to_output_map.at(thread_id) = new_idx;
51+
}
52+
}
53+
54+
} // namespace traccc::device
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** traccc library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2025-2026 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Local include(s).
11+
#include "traccc/device/global_index.hpp"
12+
13+
// Project include(s).
14+
#include "traccc/definitions/qualifiers.hpp"
15+
16+
// VecMem include(s).
17+
#include <vecmem/containers/data/vector_view.hpp>
18+
19+
namespace traccc::device {
20+
21+
/// Payload for the @c device::update_tip_length_buffer function
22+
struct update_tip_length_buffer_payload {
23+
vecmem::data::vector_view<const unsigned int> old_tip_length;
24+
vecmem::data::vector_view<unsigned int> new_tip_length;
25+
vecmem::data::vector_view<const unsigned int> measurement_votes;
26+
vecmem::data::vector_view<unsigned int> tip_to_output_map;
27+
float min_measurement_voting_fraction;
28+
};
29+
30+
TRACCC_HOST_DEVICE inline void update_tip_length_buffer(
31+
global_index_t thread_id, const update_tip_length_buffer_payload& payload);
32+
33+
} // namespace traccc::device
34+
35+
// Include the implementation.
36+
#include "traccc/finding/device/impl/update_tip_length_buffer.ipp"

device/cuda/src/finding/combinatorial_kalman_filter.cuh

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ combinatorial_kalman_filter(
569569
}
570570

571571
vecmem::vector<unsigned int> tips_length_host(mr.host);
572-
vecmem::unique_alloc_ptr<unsigned int[]> tip_to_output_map = nullptr;
572+
vecmem::data::vector_buffer<unsigned int> tip_to_output_map;
573573

574574
unsigned int n_tips_total_filtered = n_tips_total;
575575

@@ -640,40 +640,36 @@ combinatorial_kalman_filter(
640640
}
641641

642642
tip_to_output_map =
643-
vecmem::make_unique_alloc<unsigned int[]>(mr.main, n_tips_total);
643+
vecmem::data::vector_buffer<unsigned int>(n_tips_total, mr.main);
644+
copy.setup(tip_to_output_map)->wait();
644645

645646
{
646647
const unsigned int num_threads = 512;
647648
const unsigned int num_blocks =
648649
(n_tips_total + num_threads - 1) / num_threads;
649650

650651
vecmem::data::vector_buffer<unsigned int> new_tip_length_buffer{
651-
n_tips_total, mr.main};
652+
n_tips_total, mr.main, vecmem::data::buffer_type::resizable};
652653
copy.setup(new_tip_length_buffer)->wait();
653654

654-
auto tip_to_output_map_idx =
655-
vecmem::make_unique_alloc<unsigned int>(mr.main);
656-
657-
TRACCC_CUDA_ERROR_CHECK(cudaMemsetAsync(
658-
tip_to_output_map_idx.get(), 0, sizeof(unsigned int), stream));
659-
660655
kernels::update_tip_length_buffer<<<num_blocks, num_threads, 0,
661656
stream>>>(
662-
tip_length_buffer, new_tip_length_buffer, votes_per_tip_buffer,
663-
tip_to_output_map.get(), tip_to_output_map_idx.get(),
664-
config.min_measurement_voting_fraction);
657+
{tip_length_buffer, new_tip_length_buffer, votes_per_tip_buffer,
658+
tip_to_output_map, config.min_measurement_voting_fraction});
665659

666660
TRACCC_CUDA_ERROR_CHECK(cudaGetLastError());
667661

668-
str.synchronize();
669-
670-
TRACCC_CUDA_ERROR_CHECK(cudaMemcpyAsync(
671-
&n_tips_total_filtered, tip_to_output_map_idx.get(),
672-
sizeof(unsigned int), cudaMemcpyDeviceToHost, stream));
662+
if (mr.host) {
663+
vecmem::async_size size =
664+
copy.get_size(tip_to_output_map, *(mr.host));
665+
// Here we could give control back to the caller, once our code
666+
// allows for it. (coroutines...)
667+
n_tips_total_filtered = size.get();
668+
} else {
669+
n_tips_total_filtered = copy.get_size(tip_to_output_map);
670+
}
673671

674672
tip_length_buffer = std::move(new_tip_length_buffer);
675-
676-
str.synchronize();
677673
}
678674
}
679675

@@ -709,7 +705,7 @@ combinatorial_kalman_filter(
709705
.links_view = links_buffer,
710706
.tips_view = tips_buffer,
711707
.tracks_view = {track_candidates_buffer},
712-
.tip_to_output_map = tip_to_output_map.get(),
708+
.tip_to_output_map = tip_to_output_map,
713709
.jacobian_ptr = jacobian_ptr.get(),
714710
.link_predicted_parameter_view = link_predicted_parameter_buffer,
715711
.link_filtered_parameter_view = link_filtered_parameter_buffer,
Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,20 @@
11
/** traccc library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2025 CERN for the benefit of the ACTS project
3+
* (c) 2025-2026 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
77

8+
// Local include(s).
9+
#include "../../utils/global_index.hpp"
810
#include "update_tip_length_buffer.cuh"
911

1012
namespace traccc::cuda::kernels {
1113

1214
__global__ void update_tip_length_buffer(
13-
const vecmem::data::vector_view<const unsigned int> old_tip_length_view,
14-
vecmem::data::vector_view<unsigned int> new_tip_length_view,
15-
const vecmem::data::vector_view<const unsigned int> measurement_votes_view,
16-
unsigned int* tip_to_output_map, unsigned int* tip_to_output_map_idx,
17-
float min_measurement_voting_fraction) {
18-
const unsigned int thread_idx = blockIdx.x * blockDim.x + threadIdx.x;
15+
const device::update_tip_length_buffer_payload payload) {
1916

20-
assert(tip_to_output_map != nullptr);
21-
assert(tip_to_output_map_idx != nullptr);
22-
23-
const vecmem::device_vector<const unsigned int> old_tip_length(
24-
old_tip_length_view);
25-
vecmem::device_vector<unsigned int> new_tip_length(new_tip_length_view);
26-
const vecmem::device_vector<const unsigned int> measurement_votes(
27-
measurement_votes_view);
28-
29-
if (thread_idx >= measurement_votes_view.size()) {
30-
return;
31-
}
32-
33-
const unsigned int total_measurements = old_tip_length.at(thread_idx);
34-
const unsigned int total_votes = measurement_votes.at(thread_idx);
35-
36-
assert(total_votes <= total_measurements);
37-
38-
const scalar vote_fraction = static_cast<scalar>(total_votes) /
39-
static_cast<scalar>(total_measurements);
40-
41-
if (vote_fraction < min_measurement_voting_fraction) {
42-
tip_to_output_map[thread_idx] =
43-
std::numeric_limits<unsigned int>::max();
44-
} else {
45-
const auto new_idx =
46-
vecmem::device_atomic_ref(*tip_to_output_map_idx).fetch_add(1u);
47-
new_tip_length.at(new_idx) = total_measurements;
48-
tip_to_output_map[thread_idx] = new_idx;
49-
}
17+
device::update_tip_length_buffer(details::global_index1(), payload);
5018
}
5119

5220
} // namespace traccc::cuda::kernels
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
/** traccc library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2025 CERN for the benefit of the ACTS project
3+
* (c) 2025-2026 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
77

8-
#include <vecmem/containers/device_vector.hpp>
9-
#include <vecmem/containers/vector.hpp>
8+
#pragma once
109

11-
#include "traccc/definitions/common.hpp"
12-
#include "traccc/device/array_insertion_mutex.hpp"
13-
#include "traccc/edm/measurement_collection.hpp"
14-
#include "traccc/finding/candidate_link.hpp"
15-
#include "traccc/finding/finding_config.hpp"
16-
#include "traccc/utils/prob.hpp"
10+
// Project include(s).
11+
#include "traccc/finding/device/update_tip_length_buffer.hpp"
1712

1813
namespace traccc::cuda::kernels {
1914

2015
__global__ void update_tip_length_buffer(
21-
const vecmem::data::vector_view<const unsigned int> old_tip_length_view,
22-
vecmem::data::vector_view<unsigned int> new_tip_length_view,
23-
const vecmem::data::vector_view<const unsigned int> measurement_votes_view,
24-
unsigned int* tip_to_output_map, unsigned int* tip_to_output_map_idx,
25-
float min_measurement_voting_fraction);
16+
const device::update_tip_length_buffer_payload payload);
2617

2718
} // namespace traccc::cuda::kernels

0 commit comments

Comments
 (0)