Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:03:45

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file corecel/sys/ScopedMem.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <string_view>
0011 
0012 #include "corecel/cont/InitializedValue.hh"
0013 
0014 #include "MemRegistry.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Record the change in memory usage between construction and destruction.
0021  *
0022  * \code
0023     {
0024       ScopedMem record_mem("create objects");
0025       this->create_stuff();
0026     }
0027    \endcode
0028  *
0029  * This class is \em not thread safe because it writes to a shared global
0030  * index.  In a multithreaded environment a "null" scoped memory can be used:
0031  * \code
0032  * {
0033      auto record_mem = (stream_id == StreamId{0} ? ScopedMem{"label"}
0034                                                  : ScopedMem{});
0035      this->do_stuff();
0036  * }
0037  * \endcode
0038  *
0039  * \note The memory reported will likely only be valid if running a single
0040  * task on the GPU, because the start and stop values are per \em GPU rather
0041  * than per \em process. Be wary of the result.
0042  */
0043 class ScopedMem
0044 {
0045   public:
0046     // Default constructor for "null-op" recording
0047     ScopedMem() = default;
0048 
0049     // Construct with name and registries
0050     ScopedMem(std::string_view label, MemRegistry* registry);
0051 
0052     //! Construct with name and default registry
0053     explicit ScopedMem(std::string_view label)
0054         : ScopedMem{label, &celeritas::mem_registry()}
0055     {
0056     }
0057 
0058     // Register data on destruction
0059     ~ScopedMem();
0060 
0061     //! Prevent copying but allow moving
0062     CELER_DEFAULT_MOVE_DELETE_COPY(ScopedMem);
0063 
0064   private:
0065     using value_type = KibiBytes::value_type;
0066 
0067     InitializedValue<MemRegistry*> registry_;
0068     MemUsageId id_;
0069     value_type cpu_start_hwm_{0};
0070     value_type gpu_start_used_{0};
0071 };
0072 
0073 //---------------------------------------------------------------------------//
0074 }  // namespace celeritas