Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:52:43

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/detail/AsyncMemoryResource.device.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <thrust/mr/memory_resource.h>
0010 
0011 #include "corecel/DeviceRuntimeApi.hh"  // IWYU pragma: keep
0012 
0013 #include "corecel/Macros.hh"
0014 
0015 namespace celeritas
0016 {
0017 namespace detail
0018 {
0019 //---------------------------------------------------------------------------//
0020 //! CUDA/HIP opaque stream handle
0021 using DeviceStream_t = CELER_DEVICE_API_SYMBOL(Stream_t);
0022 
0023 //! Allocate memory asynchronously if supported
0024 void* malloc_async(std::size_t bytes, DeviceStream_t s);
0025 //! Free memory asynchronously if supported
0026 void free_async(void* ptr, DeviceStream_t s);
0027 
0028 //---------------------------------------------------------------------------//
0029 /*!
0030  * Thrust async memory resource associated with a CUDA/HIP stream.
0031  */
0032 class AsyncMemoryResource final : public thrust::mr::memory_resource<void*>
0033 {
0034   public:
0035     //!@{
0036     //! \name Type aliases
0037     using pointer = void*;
0038     using StreamT = DeviceStream_t;
0039     //!@}
0040 
0041     // Construct memory resource for the stream
0042     explicit AsyncMemoryResource(StreamT stream) : stream_{stream} {}
0043 
0044     // Construct with default Stream
0045     AsyncMemoryResource() = default;
0046 
0047     // Allocate device memory
0048     pointer do_allocate(std::size_t bytes, std::size_t) final;
0049 
0050     // Deallocate device memory
0051     void do_deallocate(pointer p, std::size_t, std::size_t) final;
0052 
0053   private:
0054     StreamT stream_{nullptr};
0055 };
0056 
0057 //---------------------------------------------------------------------------//
0058 }  // namespace detail
0059 }  // namespace celeritas