Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
cublas/execution.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include <cublas_v2.h>
13
14#include <cstddef>
15#include <optional>
16
17namespace uni20::cuda
18{
19class DeviceResources;
20}
21
22namespace uni20::cublas
23{
24
25class ExecutionAcquireAwaiter;
26
29 public:
30 explicit HandleSlot(int device);
31 HandleSlot(HandleSlot const&) = delete;
32 HandleSlot& operator=(HandleSlot const&) = delete;
33 HandleSlot(HandleSlot&& other) noexcept;
34 HandleSlot& operator=(HandleSlot&& other) noexcept;
36
38 [[nodiscard]] int device() const noexcept { return device_; }
39
41 [[nodiscard]] cublasHandle_t native_handle() const noexcept { return handle_; }
42
44 void bind(cuda::Stream const& stream);
45
46 private:
47 void reset() noexcept;
48
49 int device_ = -1;
50 cublasHandle_t handle_ = nullptr;
51};
52
57 public:
59
60 ExecutionLease() noexcept = default;
62 ExecutionLease& operator=(ExecutionLease const&) = delete;
63 ExecutionLease(ExecutionLease&& other) noexcept;
64 ExecutionLease& operator=(ExecutionLease&& other) noexcept;
66
68 [[nodiscard]] explicit operator bool() const noexcept
69 {
70 return static_cast<bool>(handle_) && static_cast<bool>(stream_);
71 }
72
74 [[nodiscard]] HandleSlot& handle() const { return handle_.get(); }
75
77 [[nodiscard]] cuda::Stream const& stream() const noexcept { return stream_; }
78
80 void release() noexcept;
81
82 private:
83 ExecutionLease(handle_lease_type handle, cuda::Stream stream);
84
85 handle_lease_type handle_;
86 cuda::Stream stream_;
87
88 friend class ExecutionPool;
90};
91
99 public:
101 ExecutionPool(cuda::StreamPool& streams, std::size_t handle_count);
102 ExecutionPool(ExecutionPool const&) = delete;
106
108 [[nodiscard]] std::optional<ExecutionLease> try_acquire();
109
111 [[nodiscard]] ExecutionLease acquire();
112
114 [[nodiscard]] int device() const noexcept { return streams_->device(); }
115
117 [[nodiscard]] std::size_t handle_count() const noexcept { return handles_.size(); }
118
120 [[nodiscard]] std::size_t idle_handle_count() const noexcept { return handles_.idle_count(); }
121
122 private:
123 [[nodiscard]] ExecutionLease make_lease(cuda::ResourceLease<HandleSlot> handle, cuda::Stream stream);
124
125 cuda::StreamPool* streams_;
127
129};
130
135[[nodiscard]] ExecutionPool& execution_pool(cuda::DeviceResources& resources);
136
143[[nodiscard]] ExecutionPool& execution_pool(cuda::DeviceResources& resources, std::size_t handle_count);
144
145} // namespace uni20::cublas
Awaiter that reserves a cuBLAS handle before waiting for an idle stream.
Definition cublas/task_awaiters.hpp:22
Move-only dynamic pairing of one cuBLAS handle slot and one idle stream.
Definition cublas/execution.hpp:56
cuda::Stream const & stream() const noexcept
Access the dynamically selected CUDA stream.
Definition cublas/execution.hpp:77
ExecutionLease() noexcept=default
HandleSlot & handle() const
Access the exclusively leased cuBLAS handle slot.
Definition cublas/execution.hpp:74
Device-local cuBLAS handle pool combined with a shared idle-stream pool.
Definition cublas/execution.hpp:98
int device() const noexcept
Return the CUDA device served by this execution pool.
Definition cublas/execution.hpp:114
std::size_t idle_handle_count() const noexcept
Return the number of currently idle provider handles.
Definition cublas/execution.hpp:120
ExecutionPool(ExecutionPool &&)=delete
ExecutionPool(ExecutionPool const &)=delete
std::size_t handle_count() const noexcept
Return the fixed number of provider handles.
Definition cublas/execution.hpp:117
ExecutionPool & operator=(ExecutionPool &&)=delete
ExecutionPool & operator=(ExecutionPool const &)=delete
One expensive reusable cuBLAS handle and its handle-local state.
Definition cublas/execution.hpp:28
void bind(cuda::Stream const &stream)
Establish deterministic host-pointer mode and bind one operation stream.
Definition cublas/execution.cpp:97
HandleSlot(HandleSlot const &)=delete
~HandleSlot()
Definition cublas/execution.cpp:95
HandleSlot & operator=(HandleSlot const &)=delete
cublasHandle_t native_handle() const noexcept
Return the raw handle for narrow provider-wrapper use.
Definition cublas/execution.hpp:41
int device() const noexcept
Return the device on which this handle was created.
Definition cublas/execution.hpp:38
Mutable stream and provider resources associated with one CUDA device.
Definition runtime.hpp:222
Fixed-capacity pool of preconstructed provider resources.
Definition resource_pool.hpp:96
Device-local pool whose streams become available only after their queued work completes.
Definition runtime.hpp:163
Reference-counted lease of one CUDA stream-pool slot.
Definition runtime.hpp:65
cuda::ResourceLease< HandleSlot > handle
Definition cublas/execution.cpp:33
Definition cublas_error.cpp:11
ExecutionPool & execution_pool(cuda::DeviceResources &resources)
Return the lazily constructed cuBLAS execution pool for one CUDA device.
Definition cublas/execution.cpp:215
Definition cublas/execution.hpp:18
Fixed-capacity pools and move-only leases for CUDA provider resources.
CUDA device, stream, resource, and scoped runtime primitives.