MLX
 
Loading...
Searching...
No Matches
allocator.h
Go to the documentation of this file.
1// Copyright © 2023-2024 Apple Inc.
2
3#pragma once
4
5#include <map>
6#include <mutex>
7#include <vector>
8
9#include "mlx/allocator.h"
12
14
16
17namespace {
18
19class BufferCache {
20 public:
21 BufferCache(MTL::Device* device);
22 ~BufferCache();
23
24 MTL::Buffer* reuse_from_cache(size_t size);
25 void recycle_to_cache(MTL::Buffer* buf);
26 int release_cached_buffers(size_t min_bytes_to_free);
27 size_t cache_size() {
28 return pool_size_;
29 }
30 int clear();
31
32 private:
33 struct BufferHolder {
34 public:
35 BufferHolder(MTL::Buffer* buf_) : buf(buf_), prev(nullptr), next(nullptr) {}
36
37 BufferHolder* prev;
38 BufferHolder* next;
39 MTL::Buffer* buf;
40 };
41
42 void add_at_head(BufferHolder* to_add);
43 void remove_from_list(BufferHolder* to_remove);
44
45 MTL::Device* device_;
46
47 std::multimap<size_t, BufferHolder*> buffer_pool_;
48 BufferHolder* head_;
49 BufferHolder* tail_;
50 size_t pool_size_;
51};
52
53} // namespace
54
55class MetalAllocator : public allocator::Allocator {
57 public:
58 virtual Buffer malloc(size_t size, bool allow_swap = false) override;
59 virtual void free(Buffer buffer) override;
60 virtual size_t size(Buffer buffer) const override;
62 return active_memory_;
63 };
64 size_t get_peak_memory() {
65 return peak_memory_;
66 };
68 std::unique_lock lk(mutex_);
69 peak_memory_ = 0;
70 };
72 return buffer_cache_.cache_size();
73 };
74 size_t set_cache_limit(size_t limit);
75 size_t set_memory_limit(size_t limit, bool relaxed);
76 size_t set_wired_limit(size_t limit);
78
79 private:
80 MTL::Device* device_;
81 MetalAllocator();
82 friend MetalAllocator& allocator();
83
84 // Caching allocator
85 BufferCache buffer_cache_;
86
87 ResidencySet residency_set_;
88
89 // Allocation stats
90 size_t block_limit_;
91 size_t gc_limit_;
92 size_t active_memory_{0};
93 size_t peak_memory_{0};
94 size_t max_pool_size_;
95 size_t wired_limit_{0};
96 bool relaxed_{true};
97 size_t num_resources_{0};
98 size_t resource_limit_{0};
99
100 std::mutex mutex_;
101};
102
104
105} // namespace mlx::core::metal
Definition allocator.h:39
Definition allocator.h:12
Definition allocator.h:55
virtual void free(Buffer buffer) override
size_t set_memory_limit(size_t limit, bool relaxed)
void reset_peak_memory()
Definition allocator.h:67
virtual size_t size(Buffer buffer) const override
virtual Buffer malloc(size_t size, bool allow_swap=false) override
Allocator for Metal GPUs.
size_t get_active_memory()
Definition allocator.h:61
size_t set_wired_limit(size_t limit)
size_t get_peak_memory()
Definition allocator.h:64
size_t get_cache_memory()
Definition allocator.h:71
size_t set_cache_limit(size_t limit)
friend MetalAllocator & allocator()
Definition resident.h:9
Definition allocator.h:13
MetalAllocator & allocator()
Device & device(mlx::core::Device)