|
|
|||
File indexing completed on 2026-11-25 09:55:44
0001 //------------------------------- -*- C++ -*- -------------------------------// 0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details 0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT) 0004 //---------------------------------------------------------------------------// 0005 //! \file corecel/sys/ScopedProfiling.hh 0006 //---------------------------------------------------------------------------// 0007 #pragma once 0008 0009 #include <cstdint> 0010 #include <string_view> 0011 0012 #include "corecel/Config.hh" 0013 0014 #include "corecel/Macros.hh" 0015 0016 namespace celeritas 0017 { 0018 //---------------------------------------------------------------------------// 0019 /*! 0020 * Input arguments for the most richly annotated implementation (NVTX). 0021 */ 0022 struct ScopedProfilingInput 0023 { 0024 std::string_view name; //!< Name of the range 0025 uint32_t color{}; //!< ARGB 0026 int32_t payload{}; //!< User data 0027 uint32_t category{}; //!< Category, used to group ranges together 0028 0029 ScopedProfilingInput(std::string_view n) : name{n} {} 0030 }; 0031 0032 //---------------------------------------------------------------------------// 0033 /*! 0034 * Enable and annotate performance profiling during the lifetime of this class. 0035 * 0036 * This RAII class annotates the profiling output so that, during its scope, 0037 * events and timing are associated with the given name. For use cases inside 0038 * separate begin/end functions of a class (often seen in Geant4), use 0039 * \c std::optional to start and end the class lifetime. 0040 * 0041 * This is useful for wrapping specific code fragment in a range for profiling, 0042 * e.g., ignoring of VecGeom instantiation kernels, or profiling a specific 0043 * action. It is very similar to the [NVTX 0044 * `scoped_range`](https://nvidia.github.io/NVTX/doxygen-cpp/#scoped_range). 0045 * 0046 * Profiling is off by default but must be enabled (in conjunction with other 0047 * tools; see \rstref{the profiling section,profiling} for more details). The 0048 * \c CELER_ENABLE_PROFILING environment variable is used to override this 0049 * behavior. Profiling is never enabled if CUDA/ROC-TX/Perfetto are 0050 * unavailable. 0051 * 0052 * \par Example: 0053 * Profile only the run, not the setup. 0054 * \code 0055 * void do_program() 0056 * { 0057 * do_setup() 0058 * ScopedProfiling profile_this{"run"}; 0059 * do_run(); 0060 * } 0061 * \endcode 0062 * 0063 * \par Caveats: 0064 * - The Nvidia/CUDA implementation of \c ScopedProfiling only does something 0065 * when the application using Celeritas is run through a tool that supports 0066 * NVTX, e.g., nsight compute with the --nvtx argument. If this is not the 0067 * case, API calls to nvtx are no-ops. 0068 * - The HIP/AMD ROCTX implementation requires the roctx library, which may not 0069 * be available on all systems. 0070 * - The CPU implementation requires Perfetto. It is not available when 0071 * Celeritas is built with device support (CUDA/HIP). 0072 * 0073 * \internal All profiling library implementations must support multithreaded 0074 * contexts since each thread may have one or more active instances of this 0075 * class. 0076 */ 0077 class ScopedProfiling 0078 { 0079 public: 0080 //!@{ 0081 //! \name Type aliases 0082 using Input = ScopedProfilingInput; 0083 //!@} 0084 0085 public: 0086 // Whether profiling is enabled 0087 static bool enabled(); 0088 0089 // Activate profiling with options 0090 explicit inline ScopedProfiling(Input const& input); 0091 // Activate profiling with just a name 0092 explicit inline ScopedProfiling(std::string_view name); 0093 0094 // Deactivate profiling 0095 inline ~ScopedProfiling(); 0096 0097 //!@{ 0098 //! Prevent copying and moving for RAII class 0099 CELER_DELETE_COPY_MOVE(ScopedProfiling); 0100 //!@} 0101 0102 private: 0103 bool activated_; 0104 0105 void activate(Input const& input) noexcept; 0106 void deactivate() noexcept; 0107 0108 static bool is_nvtx_enabled(); 0109 }; 0110 0111 //---------------------------------------------------------------------------// 0112 // INLINE DEFINITIONS 0113 //---------------------------------------------------------------------------// 0114 /*! 0115 * Activate device profiling with options. 0116 */ 0117 ScopedProfiling::ScopedProfiling(Input const& input) 0118 : activated_{ScopedProfiling::enabled()} 0119 { 0120 if (activated_) 0121 { 0122 this->activate(input); 0123 } 0124 } 0125 0126 //---------------------------------------------------------------------------// 0127 /*! 0128 * Activate device profiling with just a name. 0129 */ 0130 ScopedProfiling::ScopedProfiling(std::string_view name) 0131 : ScopedProfiling{Input{name}} 0132 { 0133 } 0134 0135 //---------------------------------------------------------------------------// 0136 /*! 0137 * Deactivate a profiling scope. 0138 */ 0139 ScopedProfiling::~ScopedProfiling() 0140 { 0141 if (activated_) 0142 { 0143 this->deactivate(); 0144 } 0145 } 0146 0147 #if !CELER_USE_DEVICE && !CELERITAS_USE_PERFETTO 0148 inline void ScopedProfiling::activate(Input const&) noexcept 0149 { 0150 CELER_UNREACHABLE; 0151 } 0152 inline void ScopedProfiling::deactivate() noexcept 0153 { 0154 CELER_UNREACHABLE; 0155 } 0156 #endif 0157 #if !CELERITAS_USE_CUDA 0158 inline bool ScopedProfiling::is_nvtx_enabled() 0159 { 0160 return false; 0161 } 0162 #endif 0163 //---------------------------------------------------------------------------// 0164 } // namespace celeritas
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|