Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:54:09

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/MemRegistry.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cstddef>
0010 #include <string>
0011 #include <vector>
0012 
0013 #include "corecel/OpaqueId.hh"
0014 #include "corecel/Types.hh"
0015 #include "corecel/math/Quantity.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 //! SI prefix for multiples of 1024
0021 struct Kibi
0022 {
0023     using value_type = std::size_t;
0024 
0025     static CELER_CONSTEXPR_FUNCTION value_type value() { return 1024u; }
0026     static char const* label() { return "kibi"; }
0027 };
0028 
0029 //! 1024 bytes
0030 using KibiBytes = Quantity<Kibi, Kibi::value_type>;
0031 //! Ordered identifiers for memory allocation segments
0032 using MemUsageId = OpaqueId<struct MemUsageEntry>;
0033 
0034 //! Statistics about a block of memory usage
0035 struct MemUsageEntry
0036 {
0037     //! Name of this entry
0038     std::string label;
0039     //! Index of the umbrella entry
0040     MemUsageId parent_index{};
0041     //! Difference in CPU memory usage from beginning to end
0042     KibiBytes cpu_delta{};
0043     //! Reported CPU "high water mark" at the end the block
0044     KibiBytes cpu_hwm{};
0045     //! Difference in GPU memory usage from beginning to end
0046     KibiBytes gpu_delta{};
0047     //! Reported GPU "high water mark" at the end the block
0048     KibiBytes gpu_usage{};
0049 };
0050 
0051 //---------------------------------------------------------------------------//
0052 /*!
0053  * Track memory usage across the application.
0054  *
0055  * This class is \em not thread-safe and should generally be used during setup.
0056  * The memory usage entries are a tree. Pushing and popping should be done with
0057  * \c ScopedMem .
0058  */
0059 class MemRegistry
0060 {
0061   public:
0062     // Construct with no entries
0063     MemRegistry() = default;
0064 
0065     //// ACCESSORS ////
0066 
0067     //! Number of entries
0068     MemUsageId::size_type size() const { return entries_.size(); }
0069 
0070     // Get the entry for an ID
0071     inline MemUsageEntry& get(MemUsageId id);
0072 
0073     // Get the entry for an ID
0074     inline MemUsageEntry const& get(MemUsageId id) const;
0075 
0076     //! Number of memory entries deep
0077     size_type depth() const { return stack_.size(); }
0078 
0079     //// MUTATORS ////
0080 
0081     // Create a new entry and push it onto the stack, returning the new ID
0082     MemUsageId push();
0083 
0084     // Pop the last entry
0085     void pop();
0086 
0087   private:
0088     std::vector<MemUsageEntry> entries_;
0089     std::vector<MemUsageId> stack_;
0090 };
0091 
0092 //---------------------------------------------------------------------------//
0093 // FREE FUNCTIONS
0094 //---------------------------------------------------------------------------//
0095 // Globally shared registry of memory usage
0096 MemRegistry& mem_registry();
0097 
0098 //---------------------------------------------------------------------------//
0099 // INLINE DEFINITIONS
0100 //---------------------------------------------------------------------------//
0101 /*!
0102  * Get the entry for an ID.
0103  */
0104 MemUsageEntry const& MemRegistry::get(MemUsageId id) const
0105 {
0106     CELER_EXPECT(id < this->size());
0107     return entries_[id.unchecked_get()];
0108 }
0109 
0110 //---------------------------------------------------------------------------//
0111 /*!
0112  * Get the entry for an ID.
0113  */
0114 MemUsageEntry& MemRegistry::get(MemUsageId id)
0115 {
0116     CELER_EXPECT(id < this->size());
0117     return entries_[id.unchecked_get()];
0118 }
0119 
0120 //---------------------------------------------------------------------------//
0121 }  // namespace celeritas