Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:50

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/ScopedLimitSaver.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <cstddef>
0011 
0012 #include "corecel/DeviceRuntimeApi.hh"
0013 
0014 #include "corecel/Assert.hh"
0015 #include "corecel/Macros.hh"
0016 #include "corecel/cont/Array.hh"
0017 #include "corecel/io/Logger.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Save and restore CUDA limits inside the current scope.
0024  *
0025  * This is useful for calling poorly behaved external libraries that change
0026  * CUDA limits unexpectedly.
0027  */
0028 class ScopedLimitSaver
0029 {
0030   public:
0031     ScopedLimitSaver();
0032     ~ScopedLimitSaver();
0033 
0034     //!@{
0035     //! Prevent copying and moving for RAII class
0036     CELER_DELETE_COPY_MOVE(ScopedLimitSaver);
0037     //!@}
0038 
0039   private:
0040 #if CELER_USE_DEVICE
0041     using Limit_t = CELER_DEVICE_PREFIX(Limit);
0042 #else
0043     using Limit_t = int;
0044 #endif
0045 
0046     static Array<Limit_t, 2> const cuda_attrs_;
0047     static Array<char const*, 2> const cuda_attr_labels_;
0048     Array<std::size_t, 2> orig_limits_;
0049 };
0050 
0051 //---------------------------------------------------------------------------//
0052 // INLINE DEFINITIONS
0053 //---------------------------------------------------------------------------//
0054 #if !CELERITAS_USE_CUDA
0055 // Construction is a null-op since we only save with CUDA
0056 inline ScopedLimitSaver::ScopedLimitSaver()
0057 {
0058     CELER_DISCARD(orig_limits_);
0059 #    if CELERITAS_USE_HIP
0060     CELER_NOT_IMPLEMENTED("HIP limit restoration");
0061 #    endif
0062 }
0063 
0064 // Destruction is a null-op since we only save with CUDA
0065 inline ScopedLimitSaver::~ScopedLimitSaver() {}
0066 #endif
0067 
0068 //---------------------------------------------------------------------------//
0069 }  // namespace celeritas