Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:04

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/data/PinnedAllocator.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cstdlib>
0010 
0011 #include "detail/PinnedAllocatorImpl.hh"
0012 
0013 namespace celeritas
0014 {
0015 //---------------------------------------------------------------------------//
0016 /*!
0017  * Allocate pinned host memory when using a device.
0018  *
0019  * Satisfies the Allocator named requirement.
0020  *
0021  * If Celeritas is built without device support, or if the device is disabled
0022  * at runtime (including if this is called before the device is initialized!),
0023  * the allocator will fall back to global \code ::operator new() or \c
0024  * ::operator delete() \endcode. Only use this when necessary (i.e.
0025  * asynchronous H<->D memory transfer is needed) as pinned memory reduces the
0026  * memory available to the systems.
0027  */
0028 template<class T>
0029 struct PinnedAllocator
0030 {
0031     using value_type = T;
0032 
0033     [[nodiscard]] CELER_FORCEINLINE T* allocate(std::size_t count)
0034     {
0035         return static_cast<T*>(detail::malloc_pinned(count, sizeof(T)));
0036     }
0037 
0038     CELER_FORCEINLINE void deallocate(T* ptr, std::size_t) noexcept
0039     {
0040         return detail::free_pinned(ptr);
0041     }
0042 };
0043 
0044 template<class T, class U>
0045 bool operator==(PinnedAllocator<T> const&, PinnedAllocator<U> const&)
0046 {
0047     return true;
0048 }
0049 
0050 template<class T, class U>
0051 bool operator!=(PinnedAllocator<T> const&, PinnedAllocator<U> const&)
0052 {
0053     return false;
0054 }
0055 
0056 //---------------------------------------------------------------------------//
0057 }  // namespace celeritas