MLX
 
Loading...
Searching...
No Matches
fence.h
Go to the documentation of this file.
1// Copyright © 2024 Apple Inc.
2
4
5namespace mlx::core {
6
7/* A fence to be used for synchronizing work between the CPU and GPU
8 *
9 * Calls to `update_gpu` should be paired with calls to `wait`. This ensures
10 * that the array passed to `update_gpu` is computed and visible to the CPU
11 * after the call to `wait` returns.
12 *
13 * Calls to `update` should be paired with calls to `wait_gpu`. This ensures
14 * that the array passed to `wait_gpu` will not be read by the GPU until the CPU
15 * has called `update`.
16 *
17 * The fence supports slow (default) and fast mode. Fast mode requires setting
18 * the environment variable `MLX_METAL_FAST_SYNCH=1`. Fast mode also requires
19 * Metal 3.2+ (macOS 15+, iOS 18+).
20 */
21class Fence {
22 public:
23 Fence(const Stream& stream);
24
25 void update_gpu(const array& x);
26 void wait_gpu(array& x);
27
28 void wait();
29 void update();
30
31 private:
32 Stream stream_;
33 std::shared_ptr<void> fence_;
34 uint32_t cpu_count_{0};
35 uint32_t gpu_count_{0};
36 bool use_fast_;
37 std::atomic_uint* cpu_value();
38};
39
40} // namespace mlx::core
Fence(const Stream &stream)
void update_gpu(const array &x)
void wait_gpu(array &x)
Definition array.h:24
Definition allocator.h:7
Definition stream.h:9