LCOV - code coverage report
Current view: top level - elsa/storage/memory_resource - MemoryResource.h (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 2 2 100.0 %
Date: 2024-05-16 04:22:26 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <cstring>
       4             : #include <atomic>
       5             : #include <memory>
       6             : #include <cinttypes>
       7             : 
       8             : namespace elsa::mr
       9             : {
      10             :     /*
      11             :      *  Describes one memory resource interface.
      12             :      *  Allows for polymorphic allocators.
      13             :      *  Should be bound once to a MemoryResource wrapper at construction and from
      14             :      *  there on only be passed through the MemoryResource wrapper.
      15             :      *
      16             :      *  Deallocate and tryResize must not throw exceptions.
      17             :      */
      18             :     class MemResInterface
      19             :     {
      20             :     protected:
      21         736 :         MemResInterface() = default;
      22         624 :         virtual ~MemResInterface() = default;
      23             : 
      24             :     public:
      25             :         MemResInterface(MemResInterface&&) = delete;
      26             :         MemResInterface(const MemResInterface&) = delete;
      27             : 
      28             :     public:
      29             :         virtual void* allocate(size_t size, size_t alignment) = 0;
      30             :         virtual bool tryResize(void* ptr, size_t size, size_t alignment,
      31             :                                size_t newSize) noexcept = 0;
      32             :         virtual void deallocate(void* ptr, size_t size, size_t alignment) noexcept = 0;
      33             :     };
      34             :     using MemoryResource = std::shared_ptr<MemResInterface>;
      35             : 
      36             :     /// BaselineInstance will at all times return a reference to the
      37             :     /// memory-resource from the last call to setGlobalResource.
      38             :     ///
      39             :     /// If setGlobalResource has never been called, an instance
      40             :     ///     of UniversalResource will be instantiated.
      41             :     /// @param r Must be a synchronized memory resource!
      42             :     void setGlobalResource(const MemoryResource& r);
      43             :     /// Return the memory-resource currently set as global resource.
      44             :     /// The global-resource can be reassigned, see setGlobalResource.
      45             :     ///
      46             :     /// IMPORTANT! This means that there is no guarantee, that two calls
      47             :     /// to globalResource return the same memory-resource. Always make
      48             :     /// sure, pointers are deallocated in the memory-resource that allocated
      49             :     /// them.
      50             :     MemoryResource globalResource();
      51             :     bool isBaselineMRSet();
      52             : 
      53             :     /// Return the most recent thread-local set resource (when using hints/scoped-mr).
      54             :     /// If no hints/scopes have been applied, defaults to globalResource.
      55             :     MemoryResource defaultResource();
      56             : 
      57             :     enum class StorageType : std::uint8_t {
      58             :         host,
      59             :         device,
      60             :         /* universal corresponds to memory
      61             :            that is accessible on both host and device */
      62             :         universal,
      63             :     };
      64             : 
      65             :     /// The storage type used by the storage subsystem
      66             : #ifdef ELSA_CUDA_ENABLED
      67             :     static constexpr const StorageType sysStorageType = StorageType::universal;
      68             : #else
      69             :     static constexpr const StorageType sysStorageType = StorageType::host;
      70             : #endif
      71             : 
      72             :     template <mr::StorageType t1, mr::StorageType t2>
      73             :     struct are_storages_compatible {
      74             :         static constexpr bool value =
      75             :             t1 == t2
      76             :             || (t1 == mr::StorageType::universal
      77             :                 && (t2 == mr::StorageType::device || t2 == mr::StorageType::host))
      78             :             || (t2 == mr::StorageType::universal
      79             :                 && (t1 == mr::StorageType::device || t1 == mr::StorageType::host));
      80             :     };
      81             : } // namespace elsa::mr

Generated by: LCOV version 1.14