Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:22:04

0001 /**
0002  * traccc library, part of the ACTS project (R&D line)
0003  *
0004  * (c) 2025 CERN for the benefit of the ACTS project
0005  *
0006  * Mozilla Public License Version 2.0
0007  */
0008 
0009 #pragma once
0010 
0011 // Local include(s).
0012 #include "utils.hpp"
0013 
0014 // Project include(s).
0015 #include "traccc/utils/memory_resource.hpp"
0016 
0017 // Thrust include(s).
0018 #if !defined(ALPAKA_ACC_SYCL_ENABLED)
0019 #include <thrust/binary_search.h>
0020 #include <thrust/copy.h>
0021 #include <thrust/execution_policy.h>
0022 #include <thrust/fill.h>
0023 #include <thrust/scan.h>
0024 #include <thrust/sort.h>
0025 #include <thrust/unique.h>
0026 #endif
0027 
0028 // OneDPL include.
0029 //
0030 // This is left to a separate file to turn off warnings from oneDPL.
0031 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0032 #include "oneDPL.hpp"
0033 #endif
0034 
0035 namespace traccc::alpaka::details {
0036 
0037 inline auto getExecutionPolicy([[maybe_unused]] Queue &q,
0038                                [[maybe_unused]] const memory_resource &mr) {
0039 #if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
0040   auto stream = ::alpaka::getNativeHandle(q);
0041   return thrust::cuda::par_nosync(
0042              std::pmr::polymorphic_allocator<std::byte>(&(mr.main)))
0043       .on(stream);
0044 #elif defined(ALPAKA_ACC_GPU_HIP_ENABLED)
0045   auto stream = ::alpaka::getNativeHandle(q);
0046   return thrust::hip_rocprim::par_nosync(
0047              std::pmr::polymorphic_allocator<std::byte>(&(mr.main)))
0048       .on(stream);
0049 #elif defined(ALPAKA_ACC_SYCL_ENABLED)
0050   auto queue = ::alpaka::getNativeHandle(q);
0051   return oneapi::dpl::execution::device_policy{queue};
0052 #else
0053   return thrust::host;
0054 #endif
0055 }
0056 
0057 template <typename RandomAccessIterator, typename Compare>
0058 void sort(Queue &q, const memory_resource mr, RandomAccessIterator first,
0059           RandomAccessIterator last, Compare comp) {
0060   auto execPolicy = getExecutionPolicy(q, mr);
0061 
0062 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0063   oneapi::dpl::sort(execPolicy, first, last, comp);
0064 #else
0065   thrust::sort(execPolicy, first, last, comp);
0066 #endif
0067 }
0068 
0069 template <typename RandomAccessIterator1, typename RandomAccessIterator2,
0070           typename Compare>
0071 void sort_by_key(Queue &q, const memory_resource &mr,
0072                  RandomAccessIterator1 keys_first,
0073                  RandomAccessIterator1 keys_last,
0074                  RandomAccessIterator2 values_first, Compare comp) {
0075   auto execPolicy = getExecutionPolicy(q, mr);
0076 
0077 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0078   oneapi::dpl::sort_by_key(execPolicy, keys_first, keys_last, values_first,
0079                            comp);
0080 #else
0081   thrust::sort_by_key(execPolicy, keys_first, keys_last, values_first, comp);
0082 #endif
0083 }
0084 
0085 template <typename RandomAccessIterator1, typename RandomAccessIterator2>
0086 void sort_by_key(Queue &q, const memory_resource &mr,
0087                  RandomAccessIterator1 keys_first,
0088                  RandomAccessIterator1 keys_last,
0089                  RandomAccessIterator2 values_first) {
0090   auto execPolicy = getExecutionPolicy(q, mr);
0091 
0092 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0093   oneapi::dpl::sort_by_key(execPolicy, keys_first, keys_last, values_first);
0094 #else
0095   thrust::sort_by_key(execPolicy, keys_first, keys_last, values_first);
0096 #endif
0097 }
0098 
0099 template <typename ForwardIt1, typename ForwardIt2, typename OutputIt,
0100           typename Compare>
0101 void upper_bound(Queue &q, const memory_resource &mr, ForwardIt1 first1,
0102                  ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2,
0103                  OutputIt d_first, Compare comp) {
0104   auto execPolicy = getExecutionPolicy(q, mr);
0105 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0106   oneapi::dpl::upper_bound(execPolicy, first1, last1, first2, last2, d_first,
0107                            comp);
0108 #else
0109   thrust::upper_bound(execPolicy, first1, last1, first2, last2, d_first, comp);
0110 #endif
0111 }
0112 
0113 template <typename InputIterator, typename OutputIterator>
0114 void inclusive_scan(Queue &q, const memory_resource &mr, InputIterator first,
0115                     InputIterator last, OutputIterator d_first) {
0116   auto execPolicy = getExecutionPolicy(q, mr);
0117 
0118 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0119   oneapi::dpl::inclusive_scan(execPolicy, first, last, d_first);
0120 #else
0121   thrust::inclusive_scan(execPolicy, first, last, d_first);
0122 #endif
0123 }
0124 
0125 template <typename InputIt, typename OutputIt, typename Compare>
0126 OutputIt unique_copy(Queue &q, const memory_resource &mr, InputIt first,
0127                      InputIt last, OutputIt d_first, Compare comp) {
0128   auto execPolicy = getExecutionPolicy(q, mr);
0129 
0130 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0131   return oneapi::dpl::unique_copy(execPolicy, first, last, d_first, comp);
0132 #else
0133   return thrust::unique_copy(execPolicy, first, last, d_first, comp);
0134 #endif
0135 }
0136 
0137 template <typename InputIterator, typename UnaryFunction>
0138 void for_each(Queue &q, const memory_resource &mr, InputIterator first,
0139               InputIterator last, UnaryFunction f) {
0140   auto execPolicy = getExecutionPolicy(q, mr);
0141 #if defined(ALPAKA_ACC_SYCL_ENABLED)
0142   oneapi::dpl::for_each(execPolicy, first, last, f);
0143 #else
0144   thrust::for_each(execPolicy, first, last, f);
0145 #endif
0146 }
0147 
0148 }  // namespace traccc::alpaka::details