Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2025 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 // Local include(s).
0009 #include "traccc/cuda/utils/make_magnetic_field.hpp"
0010 
0011 // Project include(s).
0012 #include "traccc/bfield/magnetic_field_types.hpp"
0013 
0014 // Covfie include(s).
0015 #include <covfie/core/backend/primitive/constant.hpp>
0016 #include <covfie/core/backend/transformer/affine.hpp>
0017 #include <covfie/core/backend/transformer/clamp.hpp>
0018 #include <covfie/core/backend/transformer/linear.hpp>
0019 #include <covfie/core/backend/transformer/strided.hpp>
0020 #include <covfie/core/concepts.hpp>
0021 #include <covfie/core/field.hpp>
0022 #include <covfie/core/vector.hpp>
0023 #include <covfie/cuda/backend/primitive/cuda_device_array.hpp>
0024 #include <covfie/cuda/backend/primitive/cuda_texture.hpp>
0025 
0026 // System include(s).
0027 #include <stdexcept>
0028 
0029 namespace traccc::cuda {
0030 
0031 /// Inhomogeneous B-field backend type using CUDA global memory
0032 template <typename scalar_t>
0033 using inhom_global_bfield_backend_t =
0034     covfie::backend::affine<covfie::backend::linear<covfie::backend::clamp<
0035         covfie::backend::strided<covfie::vector::vector_d<std::size_t, 3>,
0036                                  covfie::backend::cuda_device_array<
0037                                      covfie::vector::vector_d<scalar_t, 3>>>>>>;
0038 // Test that the type is a valid backend for a field
0039 static_assert(
0040     covfie::concepts::field_backend<inhom_global_bfield_backend_t<float>>,
0041     "cuda::inhom_global_bfield_backend_t is not a valid field backend type");
0042 
0043 /// Inhomogeneous B-field backend type using CUDA texture memory
0044 using inhom_texture_bfield_backend_t = covfie::backend::affine<
0045     covfie::backend::cuda_texture<covfie::vector::vector_d<float, 3>,
0046                                   covfie::vector::vector_d<float, 3>>>;
0047 // Test that the type is a valid backend for a field
0048 static_assert(
0049     covfie::concepts::field_backend<inhom_texture_bfield_backend_t>,
0050     "cuda::inhom_texture_bfield_backend_t is not a valid field backend type");
0051 
0052 magnetic_field make_magnetic_field(const magnetic_field& bfield,
0053                                    const magnetic_field_storage storage) {
0054   if (bfield.is<const_bfield_backend_t<float>>()) {
0055     return magnetic_field{covfie::field<const_bfield_backend_t<float>>{
0056         bfield.as_field<const_bfield_backend_t<float>>()}};
0057   } else if (bfield.is<const_bfield_backend_t<double>>()) {
0058     return magnetic_field{covfie::field<const_bfield_backend_t<double>>{
0059         bfield.as_field<const_bfield_backend_t<double>>()}};
0060   } else if (bfield.is<host::inhom_bfield_backend_t<float>>()) {
0061     // Convenience access to the Covfie field object.
0062     const auto& in_field =
0063         bfield.as_field<host::inhom_bfield_backend_t<float>>();
0064     // At single precision we can use either global or texture memory.
0065     if (storage == magnetic_field_storage::global_memory) {
0066       return magnetic_field{
0067           covfie::field<cuda::inhom_global_bfield_backend_t<float>>(in_field)};
0068     } else if (storage == magnetic_field_storage::texture_memory) {
0069       return magnetic_field{covfie::field<cuda::inhom_texture_bfield_backend_t>(
0070           covfie::make_parameter_pack(
0071               in_field.backend().get_configuration(),
0072               in_field.backend().get_backend().get_backend().get_backend()))};
0073     } else {
0074       throw std::invalid_argument(
0075           "Unsupported storage method chosen for inhomogeneous b-field");
0076     }
0077   } else if (bfield.is<host::inhom_bfield_backend_t<double>>()) {
0078     // At double precision we can only use global memory.
0079     if (storage == magnetic_field_storage::global_memory) {
0080       return magnetic_field{
0081           covfie::field<cuda::inhom_global_bfield_backend_t<double>>(
0082               bfield.as_field<host::inhom_bfield_backend_t<double>>())};
0083     } else {
0084       throw std::invalid_argument(
0085           "Unsupported storage method chosen for inhomogeneous b-field");
0086     }
0087   } else {
0088     throw std::invalid_argument("Unsupported b-field type received");
0089   }
0090 }
0091 
0092 }  // namespace traccc::cuda