Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-03 10:19:22

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 /*!
0006  * \file corecel/Types.hh
0007  * \brief Type definitions for common Celeritas functionality.
0008  * \sa corecel/Types.test.cc
0009  *
0010  * This file includes types and properties particular to the build
0011  * configuration.
0012  */
0013 //---------------------------------------------------------------------------//
0014 #pragma once
0015 
0016 #include <cstddef>
0017 #include <string>
0018 #include <type_traits>
0019 
0020 #include "corecel/Config.hh"
0021 
0022 #include "Macros.hh"
0023 
0024 namespace celeritas
0025 {
0026 //---------------------------------------------------------------------------//
0027 // Forward declarations for aliases
0028 template<class T, std::size_t N>
0029 class Array;
0030 
0031 //---------------------------------------------------------------------------//
0032 // TYPE ALIASES
0033 //---------------------------------------------------------------------------//
0034 #if CELER_USE_DEVICE || defined(__DOXYGEN__)
0035 //! Standard type for container sizes, optimized for GPU use
0036 using size_type = unsigned int;
0037 #else
0038 using size_type = std::size_t;
0039 #endif
0040 
0041 #if CELERITAS_REAL_TYPE == CELERITAS_REAL_TYPE_DOUBLE || defined(__DOXYGEN__)
0042 //! Numerical type for real numbers
0043 using real_type = double;
0044 #elif CELERITAS_REAL_TYPE == CELERITAS_REAL_TYPE_FLOAT
0045 using real_type = float;
0046 #else
0047 using real_type = void;
0048 #endif
0049 
0050 //! Equivalent to std::size_t but compatible with CUDA atomics
0051 using ull_int = unsigned long long int;
0052 
0053 //! Three-dimensional cartesian coordinates
0054 using Real3 = Array<real_type, 3>;
0055 
0056 //---------------------------------------------------------------------------//
0057 // ENUMERATIONS
0058 //---------------------------------------------------------------------------//
0059 //! Memory location of data
0060 enum class MemSpace
0061 {
0062     host,  //!< CPU memory
0063     device,  //!< GPU memory
0064     mapped,  //!< Unified virtual address space (both host and device)
0065     size_,
0066 #if defined(CELER_DEVICE_SOURCE) || defined(__DOXYGEN__)
0067     native = device,  //!< When compiling CUDA files, \c device else \c host
0068 #else
0069     native = host,
0070 #endif
0071 };
0072 
0073 //! Data ownership flag
0074 enum class Ownership
0075 {
0076     value,  //!< The collection \em owns the data
0077     reference,  //!< Mutable reference to data
0078     const_reference,  //!< Immutable reference to data
0079 };
0080 
0081 //---------------------------------------------------------------------------//
0082 //! Unit system used by Celeritas
0083 enum class UnitSystem
0084 {
0085     none,  //!< Invalid unit system
0086     cgs,  //!< Gaussian CGS
0087     si,  //!< International System
0088     clhep,  //!< Geant4 native
0089     size_,
0090     native = CELERITAS_UNITS,  //!< Compile time selected system
0091 };
0092 
0093 //---------------------------------------------------------------------------//
0094 // TEMPLATE ALIASES
0095 //---------------------------------------------------------------------------//
0096 //!@{
0097 //! \name Convenience typedefs for params and states.
0098 
0099 //! Managed host memory
0100 template<template<Ownership, MemSpace> class P>
0101 using HostVal = P<Ownership::value, MemSpace::host>;
0102 //! Immutable reference to host memory
0103 template<template<Ownership, MemSpace> class P>
0104 using HostCRef = P<Ownership::const_reference, MemSpace::host>;
0105 //! Mutable reference to host memory
0106 template<template<Ownership, MemSpace> class S>
0107 using HostRef = S<Ownership::reference, MemSpace::host>;
0108 
0109 //! Immutable reference to device memory
0110 template<template<Ownership, MemSpace> class P>
0111 using DeviceCRef = P<Ownership::const_reference, MemSpace::device>;
0112 //! Mutable reference to device memory
0113 template<template<Ownership, MemSpace> class S>
0114 using DeviceRef = S<Ownership::reference, MemSpace::device>;
0115 
0116 //! Immutable reference to native memory
0117 template<template<Ownership, MemSpace> class P>
0118 using NativeCRef = P<Ownership::const_reference, MemSpace::native>;
0119 //! Mutable reference to native memory
0120 template<template<Ownership, MemSpace> class S>
0121 using NativeRef = S<Ownership::reference, MemSpace::native>;
0122 
0123 template<class T, MemSpace M>
0124 class ObserverPtr;
0125 
0126 //! Pointer to same-memory *const* collection group
0127 template<template<Ownership, MemSpace> class P, MemSpace M>
0128 using CRefPtr = ObserverPtr<P<Ownership::const_reference, M> const, M>;
0129 //! Pointer to same-memory *mutable* collection group
0130 template<template<Ownership, MemSpace> class S, MemSpace M>
0131 using RefPtr = ObserverPtr<S<Ownership::reference, M>, M>;
0132 
0133 //!@}
0134 
0135 //---------------------------------------------------------------------------//
0136 //!@{
0137 //! \name Type trait utilities overridable by downstream/user code
0138 
0139 //! Switch between host or device type based on memspace
0140 template<MemSpace M, class HT, class DT>
0141 using MemSpaceCond_t
0142     = std::conditional_t<M == MemSpace::host,
0143                          HT,
0144                          std::conditional_t<M == MemSpace::device, DT, void>>;
0145 
0146 /*!
0147  * Traits class marking compatibility with Collection and Copier.
0148  *
0149  * This should usually apply only to trivially copyable objects, but there are
0150  * \em rare exceptions for external libraries that we know from source
0151  * inspection are essentially trivial.
0152  */
0153 template<class T>
0154 struct IsTriviallyCopyable : std::is_trivially_copyable<T>
0155 {
0156 };
0157 
0158 /*!
0159  * Traits class to automatically fetch device data through read-only cache.
0160  *
0161  * Specialize this on the cv-removed class type.
0162  *
0163  * \sa AutoLdgSpan
0164  */
0165 template<class T>
0166 struct IsAutoLdg
0167     : std::bool_constant<std::is_arithmetic_v<T> || std::is_enum_v<T>>
0168 {
0169     static_assert(!std::is_const_v<T> && !std::is_reference_v<T>
0170                   && !std::is_pointer_v<T>);
0171 };
0172 
0173 //! Get the appropriate size_type for an integer or OpaqueId
0174 template<class T>
0175 struct MakeSize : std::make_unsigned_t<T>
0176 {
0177 };
0178 
0179 //!@}
0180 
0181 //---------------------------------------------------------------------------//
0182 //!@{
0183 //! \name Type trait values for use by downstream code
0184 
0185 //! True if a type can be copied from host/device without UB
0186 template<class T>
0187 constexpr inline bool is_trivially_copyable_v = IsTriviallyCopyable<T>::value;
0188 
0189 //! Whether a base type should automatically \c ldg: default to false
0190 template<class T>
0191 constexpr inline bool is_auto_ldg_v = IsAutoLdg<T>::value;
0192 
0193 //! True if an object (functor) is compatible with kernel launchers
0194 template<class F>
0195 constexpr inline bool is_launchable_v
0196     = (is_trivially_copyable_v<F> || CELERITAS_USE_HIP
0197        || CELER_COMPILER == CELER_COMPILER_CLANG)
0198       && !std::is_pointer_v<F> && !std::is_reference_v<F>;
0199 
0200 //! Get the unsigned integer corresponding to an ID's capacity
0201 template<class T>
0202 using MakeSize_t = typename MakeSize<std::remove_cv_t<T>>::type;
0203 
0204 //!@}
0205 //---------------------------------------------------------------------------//
0206 // HELPER FUNCTIONS (HOST)
0207 //---------------------------------------------------------------------------//
0208 
0209 // Get a string corresponding to a memory space
0210 char const* to_cstring(MemSpace m);
0211 
0212 // Get a string corresponding to a unit system
0213 char const* to_cstring(UnitSystem);
0214 
0215 // Get a unit system corresponding to a string
0216 UnitSystem to_unit_system(std::string const& s);
0217 
0218 //---------------------------------------------------------------------------//
0219 // LITERALS
0220 //---------------------------------------------------------------------------//
0221 /*!
0222  * \name User-defined literals for Celeritas scalar types
0223  */
0224 namespace literals
0225 {
0226 
0227 //! Convert a literal to the configured \c real_type
0228 CELER_CONSTEXPR_FUNCTION real_type operator""_r(long double value)
0229 {
0230     return static_cast<real_type>(value);
0231 }
0232 
0233 //! Convert an integer literal to the configured \c real_type
0234 CELER_CONSTEXPR_FUNCTION real_type operator""_r(unsigned long long int value)
0235 {
0236     return static_cast<real_type>(value);
0237 }
0238 
0239 //! Convert an integer literal to the configured \c size_type
0240 CELER_CONSTEXPR_FUNCTION size_type operator""_sz(unsigned long long int value)
0241 {
0242     return static_cast<size_type>(value);
0243 }
0244 
0245 //---------------------------------------------------------------------------//
0246 }  // namespace literals
0247 }  // namespace celeritas