MLX
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1// Copyright © 2023 Apple Inc.
2
3#pragma once
4
5#include <atomic>
6#include <future>
7#include <queue>
8#include <thread>
9#include <unordered_map>
10
13#include "mlx/device.h"
14#include "mlx/stream.h"
15
17
19 std::mutex mtx;
20 std::queue<std::function<void()>> q;
21 std::condition_variable cond;
22 bool stop;
24 std::thread thread;
25
30
33 {
34 std::lock_guard<std::mutex> lk(mtx);
35 stop = true;
36 }
37 cond.notify_one();
38 thread.join();
39 }
40
41 void thread_fn() {
42 while (true) {
43 std::function<void()> task;
44 {
45 std::unique_lock<std::mutex> lk(mtx);
46 cond.wait(lk, [this] { return !this->q.empty() || this->stop; });
47 if (q.empty() && stop) {
48 return;
49 }
50 task = std::move(q.front());
51 q.pop();
52 }
53
54 task();
55 }
56 }
57
58 template <typename F>
59 void enqueue(F&& f) {
60 {
61 std::lock_guard<std::mutex> lk(mtx);
62 if (stop) {
63 throw std::runtime_error(
64 "Cannot enqueue work after stream is stopped.");
65 }
66 q.emplace(std::forward<F>(f));
67 }
68 cond.notify_one();
69 }
70};
71
72class Scheduler {
73 public:
74 Scheduler() : n_active_tasks_(0) {
75 if (metal::is_available()) {
76 default_streams_.insert({Device::gpu, new_stream(Device::gpu)});
77 }
78 default_streams_.insert({Device::cpu, new_stream(Device::cpu)});
79 }
80
81 // Not copyable or moveable
82 Scheduler(const Scheduler&) = delete;
83 Scheduler(Scheduler&&) = delete;
84 Scheduler& operator=(const Scheduler&) = delete;
86
88 auto stream = Stream(streams_.size(), d);
89 streams_.push_back(new StreamThread{stream});
90 return stream;
91 }
92
93 template <typename F>
94 void enqueue(const Stream& stream, F&& f);
95
97 return default_streams_.at(d.type);
98 }
99
100 void set_default_stream(const Stream& s) {
101 default_streams_.at(s.device.type) = s;
102 }
103
104 void notify_new_task(const Stream& stream) {
105 {
106 std::lock_guard<std::mutex> lk(mtx);
107 n_active_tasks_++;
108 }
109 completion_cv.notify_all();
110 }
111
112 void notify_task_completion(const Stream& stream) {
113 {
114 std::lock_guard<std::mutex> lk(mtx);
115 n_active_tasks_--;
116 }
117 completion_cv.notify_all();
118 }
119
120 int n_active_tasks() const {
121 return n_active_tasks_;
122 }
123
125 std::unique_lock<std::mutex> lk(mtx);
126 int n_tasks_old = n_active_tasks();
127 if (n_tasks_old > 1) {
128 completion_cv.wait(lk, [this, n_tasks_old] {
129 return this->n_active_tasks() != n_tasks_old;
130 });
131 }
132 }
133
135 for (auto s : streams_) {
136 delete s;
137 }
138 }
139
140 private:
141 int n_active_tasks_;
142 std::vector<StreamThread*> streams_;
143 std::unordered_map<Device::DeviceType, Stream> default_streams_;
144 std::condition_variable completion_cv;
145 std::mutex mtx;
146};
147
148template <typename F>
149void Scheduler::enqueue(const Stream& stream, F&& f) {
150 streams_[stream.index]->enqueue(std::forward<F>(f));
151}
152
154
155template <typename F>
156void enqueue(const Stream& stream, F&& f) {
157 scheduler().enqueue(stream, std::forward<F>(f));
158}
159
160inline int n_active_tasks() {
161 return scheduler().n_active_tasks();
162}
163
164inline void notify_new_task(const Stream& stream) {
165 scheduler().notify_new_task(stream);
166}
167
168inline void notify_task_completion(const Stream& stream) {
170}
171
172inline void wait_for_one() {
174}
175
176} // namespace mlx::core::scheduler
Definition scheduler.h:72
void wait_for_one()
Definition scheduler.h:124
Scheduler & operator=(Scheduler &&)=delete
void enqueue(const Stream &stream, F &&f)
Definition scheduler.h:149
Stream new_stream(const Device &d)
Definition scheduler.h:87
Stream get_default_stream(const Device &d) const
Definition scheduler.h:96
Scheduler()
Definition scheduler.h:74
int n_active_tasks() const
Definition scheduler.h:120
Scheduler(const Scheduler &)=delete
~Scheduler()
Definition scheduler.h:134
void set_default_stream(const Stream &s)
Definition scheduler.h:100
Scheduler & operator=(const Scheduler &)=delete
void notify_task_completion(const Stream &stream)
Definition scheduler.h:112
Scheduler(Scheduler &&)=delete
void notify_new_task(const Stream &stream)
Definition scheduler.h:104
void new_stream(Stream stream)
Definition scheduler.h:16
void notify_task_completion(const Stream &stream)
Definition scheduler.h:168
void notify_new_task(const Stream &stream)
Definition scheduler.h:164
void wait_for_one()
Definition scheduler.h:172
int n_active_tasks()
Definition scheduler.h:160
void enqueue(const Stream &stream, F &&f)
Definition scheduler.h:156
Scheduler & scheduler()
void synchronize()
Definition device.h:7
static constexpr DeviceType gpu
Definition device.h:14
static constexpr DeviceType cpu
Definition device.h:13
DeviceType type
Definition device.h:18
Definition stream.h:9
Device device
Definition stream.h:11
int index
Definition stream.h:10
Definition scheduler.h:18
void thread_fn()
Definition scheduler.h:41
std::thread thread
Definition scheduler.h:24
bool stop
Definition scheduler.h:22
void enqueue(F &&f)
Definition scheduler.h:59
std::condition_variable cond
Definition scheduler.h:21
std::mutex mtx
Definition scheduler.h:19
~StreamThread()
Definition scheduler.h:31
Stream stream
Definition scheduler.h:23
StreamThread(Stream stream)
Definition scheduler.h:26
std::queue< std::function< void()> > q
Definition scheduler.h:20
float f
Definition bf16.h:16