DPC++ Runtime
Runtime libraries for oneAPI DPC++
usm_allocator.hpp
Go to the documentation of this file.
1 //===---------- usm_allocator.hpp - Allocator for USM memory --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef USM_ALLOCATOR
10 #define USM_ALLOCATOR
11 
12 #include <atomic>
13 #include <memory>
14 
15 // USM system memory allocation/deallocation interface.
16 class SystemMemory {
17 public:
18  virtual void *allocate(size_t size) = 0;
19  virtual void *allocate(size_t size, size_t aligned) = 0;
20  virtual void deallocate(void *ptr) = 0;
21  virtual ~SystemMemory() = default;
22 };
23 
24 class USMLimits {
25 public:
26  // Maximum memory left unfreed
27  size_t MaxSize = 16 * 1024 * 1024;
28 
29  // Total size of pooled memory
30  std::atomic<size_t> TotalSize = 0;
31 };
32 
33 // Configuration for specific USM allocator instance
35 public:
36  const char *memoryTypeName = "";
37 
38  // Minimum allocation size that will be requested from the system.
39  // By default this is the minimum allocation size of each memory type.
40  size_t SlabMinSize = 0;
41 
42  // Allocations up to this limit will be subject to chunking/pooling
43  size_t MaxPoolableSize = 0;
44 
45  // When pooling, each bucket will hold a max of 4 unfreed slabs
46  size_t Capacity = 0;
47 
48  // Holds the minimum bucket size valid for allocation of a memory type.
49  size_t MinBucketSize = 0;
50 
51  // Holds size of the pool managed by the allocator.
52  size_t CurPoolSize = 0;
53 
54  // Whether to print pool usage statistics
55  int PoolTrace = 0;
56 
57  std::shared_ptr<USMLimits> limits;
58 };
59 
61 public:
62  // Keep it public since it needs to be accessed by the lower layer(Buckets)
63  class USMAllocImpl;
64 
65  USMAllocContext(std::unique_ptr<SystemMemory> memHandle,
66  USMAllocatorParameters params);
68 
69  void *allocate(size_t size);
70  void *allocate(size_t size, size_t alignment);
71  void deallocate(void *ptr);
72 
73 private:
74  std::unique_ptr<USMAllocImpl> pImpl;
75 };
76 
77 #endif
USMLimits
Definition: usm_allocator.hpp:24
USMLimits::TotalSize
std::atomic< size_t > TotalSize
Definition: usm_allocator.hpp:30
SystemMemory::allocate
virtual void * allocate(size_t size)=0
USMAllocatorParameters::limits
std::shared_ptr< USMLimits > limits
Definition: usm_allocator.hpp:57
SystemMemory
Definition: usm_allocator.hpp:16
USMAllocatorParameters::SlabMinSize
size_t SlabMinSize
Definition: usm_allocator.hpp:40
sycl::_V1::ext::oneapi::experimental::alignment
constexpr alignment_key::value_t< K > alignment
Definition: properties.hpp:349
USMLimits::MaxSize
size_t MaxSize
Definition: usm_allocator.hpp:27
USMAllocatorParameters::MinBucketSize
size_t MinBucketSize
Definition: usm_allocator.hpp:49
USMAllocContext::deallocate
void deallocate(void *ptr)
Definition: usm_allocator.cpp:815
SystemMemory::~SystemMemory
virtual ~SystemMemory()=default
USMAllocatorParameters::MaxPoolableSize
size_t MaxPoolableSize
Definition: usm_allocator.hpp:43
USMAllocContext::allocate
void * allocate(size_t size)
Definition: usm_allocator.cpp:788
USMAllocatorParameters::memoryTypeName
const char * memoryTypeName
Definition: usm_allocator.hpp:36
USMAllocatorParameters::Capacity
size_t Capacity
Definition: usm_allocator.hpp:46
USMAllocContext::USMAllocImpl
Definition: usm_allocator.cpp:260
USMAllocatorParameters::PoolTrace
int PoolTrace
Definition: usm_allocator.hpp:55
USMAllocContext::~USMAllocContext
~USMAllocContext()
Definition: usm_allocator.cpp:831
USMAllocContext
Definition: usm_allocator.hpp:60
SystemMemory::deallocate
virtual void deallocate(void *ptr)=0
USMAllocatorParameters
Definition: usm_allocator.hpp:34
USMAllocContext::USMAllocContext
USMAllocContext(std::unique_ptr< SystemMemory > memHandle, USMAllocatorParameters params)
Definition: usm_allocator.cpp:784
USMAllocatorParameters::CurPoolSize
size_t CurPoolSize
Definition: usm_allocator.hpp:52