Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-03 10:41:11

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/Macros.hh
0007  * \brief Language and compiler abstraction macro definitions.
0008  *
0009  * The Macros file defines cross-platform (CUDA, C++, HIP) macros that
0010  * expand to attributes depending on the compiler and build configuration.
0011  */
0012 //---------------------------------------------------------------------------//
0013 #pragma once
0014 
0015 #include "corecel/Config.hh"
0016 
0017 //---------------------------------------------------------------------------//
0018 //!@{
0019 //! \name Compiler type/version macros
0020 
0021 /*!
0022  * \def CELER_FUNCTION
0023  *
0024  * Decorate a function that works on both host and device, with and without
0025  * NVCC. The name of this function and its siblings is based on the Kokkos
0026  * naming scheme.
0027  */
0028 #if defined(__NVCC__) || defined(__HIP__)
0029 #    define CELER_FUNCTION __host__ __device__
0030 #else
0031 #    define CELER_FUNCTION
0032 #endif
0033 
0034 #if defined(__NVCC__)
0035 #    define CELER_FORCEINLINE __forceinline__
0036 #elif defined(_MSC_VER)
0037 #    define CELER_FORCEINLINE inline __forceinline
0038 #elif defined(__clang__) || defined(__GNUC__) || defined(__HIP__) \
0039     || defined(__INTEL_COMPILER)
0040 #    define CELER_FORCEINLINE inline __attribute__((always_inline))
0041 #else
0042 #    define CELER_FORCEINLINE inline
0043 #endif
0044 
0045 //! Detection for the current compiler isn't supported yet
0046 #define CELER_COMPILER_UNKNOWN 0
0047 //! Compiling with clang, or a clang-based compiler defining __clang__ (hipcc)
0048 #define CELER_COMPILER_CLANG 1
0049 /*!
0050  * \def CELER_COMPILER
0051  *
0052  * Compare to on of the CELER_COMPILER_<compiler> macros to check
0053  * which compiler is in use.
0054  *
0055  * TODO: add and test more compilers as needed.
0056  */
0057 #if defined(__clang__)
0058 #    define CELER_COMPILER CELER_COMPILER_CLANG
0059 #else
0060 #    define CELER_COMPILER CELER_COMPILER_UNKNOWN
0061 #endif
0062 
0063 /*!
0064  * \def CELER_CTAD_FUNCTION
0065  *
0066  * Compatibility function for older versions of Clang/ROCm that require
0067  * host/device decorators for Class Template Argument Deduction (CTAD) guides.
0068  * This bug exists in HIP/Clang versions prior to Clang 22 (ROCm 6.3): see
0069  * https://github.com/ROCm/ROCm/issues/5646 and LLVM fix
0070  * https://github.com/llvm/llvm-project/pull/168711 .
0071  *
0072  * This is only required for device-compatible classes and can be removed when
0073  * older versions of Clang are no longer supported (maybe in 2031, five years
0074  * after the fix). Most use cases will probably go away when we require C++20.
0075  */
0076 #if defined(__HIP__) && defined(__clang__) && (__clang_major__ < 22)
0077 #    define CELER_CTAD_FUNCTION __host__ __device__
0078 #else
0079 #    define CELER_CTAD_FUNCTION
0080 #endif
0081 
0082 /*!
0083  * \def CELER_FORCEINLINE_FUNCTION
0084  *
0085  * Like CELER_FUNCTION but forces inlining. Compiler optimizers usually can
0086  * tell what needs optimizing, but this function can provide speedups (and
0087  * smaller sampling profiles) when inlining optimizations are not enabled. It
0088  * should be used sparingly.
0089  */
0090 #define CELER_FORCEINLINE_FUNCTION CELER_FUNCTION CELER_FORCEINLINE
0091 
0092 // For use in detail code only for thin wrapper functions
0093 #define CELER_FIF CELER_FORCEINLINE_FUNCTION
0094 
0095 /*!
0096  * \def CELER_CONSTEXPR_FUNCTION
0097  *
0098  * Decorate a function that works on both host and device, with and without
0099  * NVCC, can be evaluated at compile time, and should be forcibly inlined.
0100  */
0101 #define CELER_CONSTEXPR_FUNCTION constexpr CELER_FORCEINLINE_FUNCTION
0102 
0103 // For use in detail code only for thin wrapper functions
0104 #define CELER_CEF CELER_CONSTEXPR_FUNCTION
0105 
0106 /*!
0107  * \def CELER_UNLIKELY(condition)
0108  *
0109  * Mark the result of this condition to be "unlikely".
0110  *
0111  * This asks the compiler to move the section of code to a "cold" part of the
0112  * instructions, improving instruction locality. It should be used primarily
0113  * for error checking conditions.
0114  */
0115 #if defined(__clang__) || defined(__GNUC__)
0116 // GCC and Clang support the same builtin
0117 #    define CELER_UNLIKELY(COND) __builtin_expect(!!(COND), 0)
0118 #else
0119 // No other compilers seem to have a similar builtin
0120 #    define CELER_UNLIKELY(COND) (COND)
0121 #endif
0122 
0123 /*!
0124  * \def CELER_UNREACHABLE
0125  *
0126  * Mark a point in code as being impossible to reach in normal execution.
0127  *
0128  * See https://clang.llvm.org/docs/LanguageExtensions.html#builtin-unreachable
0129  * or https://msdn.microsoft.com/en-us/library/1b3fsfxw.aspx
0130  * or
0131  * https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#__builtin_unreachable
0132  *
0133  * (The "unreachable" and "assume" compiler optimizations for CUDA are only
0134  * available in API version 11.3 or higher, which is encoded as
0135  * \code major*1000 + minor*10 \endcode).
0136  *
0137  * \note This macro should not generally be used; instead, the macro \c
0138  * CELER_ASSERT_UNREACHABLE() defined in base/Assert.hh should be used instead
0139  * (to provide a more detailed error message in case the point *is* reached).
0140  */
0141 #if (!defined(__CUDA_ARCH__) && (defined(__clang__) || defined(__GNUC__))) \
0142     || defined(__NVCOMPILER) \
0143     || (defined(__CUDA_ARCH__) && CUDART_VERSION >= 11030) \
0144     || defined(__HIP_DEVICE_COMPILE__)
0145 #    define CELER_UNREACHABLE __builtin_unreachable()
0146 #elif defined(_MSC_VER)
0147 #    define CELER_UNREACHABLE __assume(false)
0148 #else
0149 #    define CELER_UNREACHABLE
0150 #endif
0151 
0152 /*!
0153  * \def CELER_USE_DEVICE
0154  *
0155  * True if HIP or CUDA are enabled, false otherwise.
0156  */
0157 #if CELERITAS_USE_CUDA || CELERITAS_USE_HIP
0158 #    define CELER_USE_DEVICE 1
0159 #else
0160 #    define CELER_USE_DEVICE 0
0161 #endif
0162 
0163 /*!
0164  * \def CELER_DEVICE_SOURCE
0165  *
0166  * Defined and true if building a HIP or CUDA source file. This is a generic
0167  * replacement for \c __CUDACC__ .
0168  */
0169 #if defined(__CUDACC__) || defined(__HIP__)
0170 #    define CELER_DEVICE_SOURCE 1
0171 #elif defined(__DOXYGEN__)
0172 #    define CELER_DEVICE_SOURCE 0
0173 #endif
0174 
0175 /*!
0176  * \def CELER_USE_THRUST
0177  * \def CELER_CUB_HAS_TRANSFORM
0178  * \def CELER_CUB_HAS_FLAGGEDIF
0179  * \def CELER_HIPCUB_HAS_TRANSFORM
0180  *
0181  * Determine if CUB or hipCUB is available, and if so, check the version.
0182  *
0183  * CUDA has included CUB since CUDA 11, but ROCm does not include hipCUB by
0184  * default, so test for the availability of hipCUB and use thrust instead if
0185  * it's unavailable.
0186  *
0187  * DeviceTransform::Transform is unavailable in earlier versions of CUB/hipCUB,
0188  * so the code uses thrust::transform in that case.
0189  *
0190  * DeviceSelect::FlaggedIf is unavailable in earlier versions of CUB and
0191  * doesn't work with hipCUB versions 3.4.0 through 4.1.0 when using celeritas
0192  * OpaqueId data types, so use a transform and DeviceSelect::Flagged instead.
0193  *
0194  * \note These are unneeded and undefined if this isn't a HIP or CUDA source
0195  * file. This check is necessary because the version header file needs to be
0196  * included.
0197  */
0198 #if CELER_DEVICE_SOURCE
0199 #    if CELERITAS_USE_CUDA
0200 #        include <cub/version.cuh>
0201 #    elif CELERITAS_USE_HIP && CELERITAS_HAVE_HIPCUB
0202 #        include <hipcub/hipcub_version.hpp>
0203 #    endif
0204 #    if CELERITAS_USE_HIP && !CELERITAS_HAVE_HIPCUB
0205 #        define CELER_USE_THRUST 1
0206 #    endif
0207 #    if CELERITAS_USE_CUDA && CUB_VERSION >= 200800
0208 #        define CELER_CUB_HAS_TRANSFORM 1
0209 #        define CELER_CUB_HAS_FLAGGEDIF 1
0210 #    elif CELERITAS_USE_CUDA && CUB_VERSION >= 200500
0211 #        define CELER_CUB_HAS_FLAGGEDIF 1
0212 #    elif CELERITAS_USE_HIP && HIPCUB_VERSION >= 400100
0213 #        define CELER_HIPCUB_HAS_TRANSFORM 1
0214 #    endif
0215 #elif defined(__DOXYGEN__)
0216 #    define CELER_USE_THRUST 0
0217 #    define CELER_CUB_HAS_TRANSFORM 0
0218 #    define CELER_CUB_HAS_FLAGGEDIF 0
0219 #    define CELER_HIPCUB_HAS_TRANSFORM 0
0220 #endif
0221 
0222 /*!
0223  * \def CELER_DEVICE_COMPILE
0224  *
0225  * Defined and true if building device code in HIP or CUDA. This is a generic
0226  * replacement for \c __CUDA_ARCH__ .
0227  */
0228 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
0229 #    define CELER_DEVICE_COMPILE 1
0230 #elif defined(__DOXYGEN__)
0231 #    define CELER_DEVICE_COMPILE 0
0232 #endif
0233 
0234 #if CELERITAS_USE_CUDA \
0235     && (__CUDACC_VER_MAJOR__ < 11 \
0236         || (__CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ < 5))
0237 /*!
0238  * Work around older NVCC bugs with `if constexpr`.
0239  *
0240  * These cause errors such as \verbatim
0241  *    error: missing return statement at end of non-void function
0242  * \endverbatim
0243  */
0244 #    define CELER_CUDACC_BUGGY_IF_CONSTEXPR 1
0245 #else
0246 #    define CELER_CUDACC_BUGGY_IF_CONSTEXPR 0
0247 #endif
0248 
0249 /*!
0250  * \def CELER_EXPLICIT_IF(COND)
0251  *
0252  * Conditionally explicit constructor or conversion operator annotation,
0253  * analogous to \c std::span and other standard library types.
0254  *
0255  * In C++20 and later, this expands to <code>explicit(COND)</code>: when
0256  * \c COND is \c false the constructor is implicit; when \c true it is
0257  * explicit. In C++17 the condition is ignored and the annotated function
0258  * is unconditionally explicit.
0259  *
0260  * Example usage mirroring \c std::span 's array constructor:
0261  * \code
0262  *   CELER_EXPLICIT_IF(Extent != dynamic_extent)
0263  *   Span(Array<value_type, N>& arr);
0264  * \endcode
0265  */
0266 #if __cplusplus >= 202002L
0267 #    define CELER_EXPLICIT_IF(COND) explicit(COND)
0268 #else
0269 #    define CELER_EXPLICIT_IF(COND) explicit
0270 #endif
0271 
0272 //!@}
0273 //---------------------------------------------------------------------------//
0274 //!@{
0275 //! \name Exception handling macros
0276 
0277 /*!
0278  * \def CELER_TRY_HANDLE
0279  *
0280  * "Try" to execute the statement, and "handle" *all* thrown errors by calling
0281  * the given function-like error handler with a \c std::exception_ptr object.
0282  */
0283 #define CELER_TRY_HANDLE(STATEMENT, HANDLE_EXCEPTION) \
0284     do \
0285     { \
0286         try \
0287         { \
0288             STATEMENT; \
0289         } \
0290         catch (...) \
0291         { \
0292             HANDLE_EXCEPTION(std::current_exception()); \
0293         } \
0294     } while (0)
0295 
0296 /*!
0297  * \def CELER_TRY_HANDLE_CONTEXT
0298  *
0299  * Try the given statement, and if it fails, chain it into the given exception.
0300  *
0301  * The given \c CONTEXT_EXCEPTION must be an expression that yields an rvalue
0302  * to a \c std::exception subclass that is not marked \c final . The resulting
0303  * chained exception will be passed into \c HANDLE_EXCEPTION for processing.
0304  *
0305  * This example shows how to safely propagate exceptions out of an OpenMP
0306  * parallel block using celeritas::MultiExceptionHandler :
0307  * \code
0308     MultiExceptionHandler capture_exception;
0309     #pragma omp parallel for
0310     for (size_type i = 0; i < num_threads; ++i)
0311     {
0312         CELER_TRY_HANDLE_CONTEXT(
0313             execute_thread(ThreadId{i}),
0314             capture_exception,
0315             KernelContextException(ThreadId{i}));
0316     }
0317     log_and_rethrow(std::move(capture_exception));
0318  * \endcode
0319  */
0320 #define CELER_TRY_HANDLE_CONTEXT( \
0321     STATEMENT, HANDLE_EXCEPTION, CONTEXT_EXCEPTION) \
0322     CELER_TRY_HANDLE( \
0323         do { \
0324             try \
0325             { \
0326                 STATEMENT; \
0327             } \
0328             catch (...) \
0329             { \
0330                 std::throw_with_nested(CONTEXT_EXCEPTION); \
0331             } \
0332         } while (0), \
0333         HANDLE_EXCEPTION)
0334 
0335 //!@}
0336 //---------------------------------------------------------------------------//
0337 //!@{
0338 //! \name Class definition macros
0339 
0340 /*!
0341  * \def CELER_DEFAULT_COPY_MOVE
0342  *
0343  * Explicitly declare defaulted copy and move constructors and assignment
0344  * operators. Use this if the destructor is declared explicitly, or as part of
0345  * the "protected" section of an interface class to prevent assignment between
0346  * incompatible classes.
0347  */
0348 #define CELER_DEFAULT_COPY_MOVE(CLS) \
0349     CLS(CLS const&) = default; \
0350     CLS& operator=(CLS const&) = default; \
0351     CLS(CLS&&) = default; \
0352     CLS& operator=(CLS&&) = default
0353 
0354 /*!
0355  * \def CELER_DELETE_COPY_MOVE
0356  *
0357  * Explicitly declare *deleted* copy and move constructors and assignment
0358  * operators. Use this for scoped RAII classes.
0359  */
0360 #define CELER_DELETE_COPY_MOVE(CLS) \
0361     CLS(CLS const&) = delete; \
0362     CLS& operator=(CLS const&) = delete; \
0363     CLS(CLS&&) = delete; \
0364     CLS& operator=(CLS&&) = delete
0365 
0366 /*!
0367  * \def CELER_DEFAULT_MOVE_DELETE_COPY
0368  *
0369  * Explicitly declare defaulted copy and move constructors and assignment
0370  * operators. Use this if the destructor is declared explicitly.
0371  */
0372 #define CELER_DEFAULT_MOVE_DELETE_COPY(CLS) \
0373     CLS(CLS const&) = delete; \
0374     CLS& operator=(CLS const&) = delete; \
0375     CLS(CLS&&) = default; \
0376     CLS& operator=(CLS&&) = default
0377 
0378 //!@}
0379 //---------------------------------------------------------------------------//
0380 
0381 /*!
0382  * \def CELER_DISCARD
0383  *
0384  * The argument is an unevaluated operand which will generate no code but force
0385  * the expression to be used. This is used in place of the \code
0386  * [[maybe_unused]] \endcode attribute, which actually generates warnings in
0387  * older versions of GCC.
0388  */
0389 #define CELER_DISCARD(CODE) static_cast<void>(sizeof(CODE));
0390 
0391 //---------------------------------------------------------------------------//