|
| 1 | +/** |
| 2 | + * traccc library, part of the ACTS project (R&D line) |
| 3 | + * |
| 4 | + * (c) 2024 CERN for the benefit of the ACTS project |
| 5 | + * |
| 6 | + * Mozilla Public License Version 2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +// Project include(s). |
| 12 | +#include "../utils/cuda_error_handling.hpp" |
| 13 | +#include "traccc/cuda/utils/stream.hpp" |
| 14 | + |
| 15 | +// VecMem include(s). |
| 16 | +#include <vecmem/containers/data/vector_view.hpp> |
| 17 | +#include <vecmem/containers/device_vector.hpp> |
| 18 | +#include <vecmem/memory/memory_resource.hpp> |
| 19 | +#include <vecmem/memory/unique_ptr.hpp> |
| 20 | +#include <vecmem/utils/copy.hpp> |
| 21 | + |
| 22 | +// CUDA include |
| 23 | +#include <cuda_runtime.h> |
| 24 | + |
| 25 | +// System include |
| 26 | +#include <concepts> |
| 27 | + |
| 28 | +namespace traccc::cuda { |
| 29 | +namespace kernels { |
| 30 | +template <typename P, typename T> |
| 31 | +requires std::predicate<P, T> __global__ void true_for_all_kernel( |
| 32 | + P projection, vecmem::data::vector_view<T> _in, bool* out) { |
| 33 | + int tid = threadIdx.x + blockIdx.x * blockDim.x; |
| 34 | + |
| 35 | + vecmem::device_vector<T> in(_in); |
| 36 | + |
| 37 | + if (tid < in.size()) { |
| 38 | + if (!projection(in.at(tid))) { |
| 39 | + *out = false; |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +} // namespace kernels |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Sanity check that a predicate is true for all elements of a vector. |
| 47 | + * |
| 48 | + * @note This function runs in O(n) time. |
| 49 | + * |
| 50 | + * @tparam P The type of the predicate. |
| 51 | + * @tparam T The type of the vector. |
| 52 | + * @param predicate A projection object of type `P`. |
| 53 | + * @param mr A memory resource used for allocating intermediate memory. |
| 54 | + * @param copy A copy object. |
| 55 | + * @param stream A wrapped CUDA stream. |
| 56 | + * @param vector The vector which to check for contiguity. |
| 57 | + * @return true If `predicate` is true for all elements of `vector`. |
| 58 | + * @return false Otherwise. |
| 59 | + */ |
| 60 | +template <typename P, typename T> |
| 61 | +requires std::predicate<P, T> bool true_for_all( |
| 62 | + P&& predicate, vecmem::memory_resource& mr, vecmem::copy& copy, |
| 63 | + stream& stream, vecmem::data::vector_view<T> vector) { |
| 64 | + // This should never be a performance-critical step, so we can keep the |
| 65 | + // block size fixed. |
| 66 | + constexpr int block_size = 512; |
| 67 | + |
| 68 | + cudaStream_t cuda_stream = |
| 69 | + reinterpret_cast<cudaStream_t>(stream.cudaStream()); |
| 70 | + |
| 71 | + // Grab the number of elements in our vector. |
| 72 | + const std::uint32_t n = copy.get_size(vector); |
| 73 | + |
| 74 | + // Allocate memory for outputs, then set them up. |
| 75 | + vecmem::unique_alloc_ptr<bool> device_out = |
| 76 | + vecmem::make_unique_alloc<bool>(mr); |
| 77 | + |
| 78 | + bool initial_out = true; |
| 79 | + |
| 80 | + TRACCC_CUDA_ERROR_CHECK( |
| 81 | + cudaMemcpyAsync(device_out.get(), &initial_out, sizeof(bool), |
| 82 | + cudaMemcpyHostToDevice, cuda_stream)); |
| 83 | + |
| 84 | + // Launch the main kernel. |
| 85 | + kernels::true_for_all_kernel<P, T> |
| 86 | + <<<(n + block_size - 1) / block_size, block_size, 0, cuda_stream>>>( |
| 87 | + predicate, vector, device_out.get()); |
| 88 | + |
| 89 | + TRACCC_CUDA_ERROR_CHECK(cudaGetLastError()); |
| 90 | + |
| 91 | + // Copy the total number of squashed elements, e.g. the size of the |
| 92 | + // resulting vector. |
| 93 | + bool host_out; |
| 94 | + |
| 95 | + TRACCC_CUDA_ERROR_CHECK( |
| 96 | + cudaMemcpyAsync(&host_out, device_out.get(), sizeof(bool), |
| 97 | + cudaMemcpyDeviceToHost, cuda_stream)); |
| 98 | + |
| 99 | + stream.synchronize(); |
| 100 | + |
| 101 | + return host_out; |
| 102 | +} |
| 103 | + |
| 104 | +template <typename P, typename T> |
| 105 | +requires std::predicate<P, T> bool false_for_all( |
| 106 | + P&& projection, vecmem::memory_resource& mr, vecmem::copy& copy, |
| 107 | + stream& stream, vecmem::data::vector_view<T> vector) { |
| 108 | + return true_for_all( |
| 109 | + [projection] __device__<typename... Args>(Args && ... args) { |
| 110 | + return !projection(std::forward<Args>(args)...); |
| 111 | + }, |
| 112 | + mr, copy, stream, vector); |
| 113 | +} |
| 114 | + |
| 115 | +template <typename P, typename T> |
| 116 | +requires std::predicate<P, T> bool true_for_any( |
| 117 | + P&& projection, vecmem::memory_resource& mr, vecmem::copy& copy, |
| 118 | + stream& stream, vecmem::data::vector_view<T> vector) { |
| 119 | + return !false_for_all(std::forward<P>(projection), mr, copy, stream, |
| 120 | + vector); |
| 121 | +} |
| 122 | + |
| 123 | +template <typename P, typename T> |
| 124 | +requires std::predicate<P, T> bool false_for_any( |
| 125 | + P&& projection, vecmem::memory_resource& mr, vecmem::copy& copy, |
| 126 | + stream& stream, vecmem::data::vector_view<T> vector) { |
| 127 | + return !true_for_all(std::forward<P>(projection), mr, copy, stream, vector); |
| 128 | +} |
| 129 | +} // namespace traccc::cuda |
0 commit comments