1#![warn(missing_docs)]
38#![warn(clippy::unwrap_used)]
39
40#[cfg(feature = "ptx-cache")]
41pub mod compile;
42#[cfg(feature = "cooperative")]
43pub mod cooperative;
44#[cfg(feature = "cuda")]
45mod device;
46#[cfg(feature = "cuda")]
47pub mod driver_api;
48#[cfg(feature = "cuda")]
49pub mod hopper;
50#[cfg(feature = "cuda")]
51pub mod k2k_gpu;
52#[cfg(feature = "cuda")]
53mod kernel;
54#[cfg(feature = "cuda")]
55pub mod launch_config;
56#[cfg(feature = "cuda")]
57mod memory;
58#[cfg(feature = "cuda")]
59pub mod memory_pool;
60#[cfg(feature = "cuda")]
61pub mod multi_gpu;
62#[cfg(feature = "cuda")]
63pub mod persistent;
64#[cfg(feature = "cuda")]
65pub mod phases;
66#[cfg(feature = "profiling")]
67pub mod profiling;
68#[cfg(feature = "cuda")]
69pub mod reduction;
70#[cfg(feature = "cuda")]
71mod runtime;
72#[cfg(feature = "cuda")]
73mod stencil;
74#[cfg(feature = "cuda")]
75pub mod stream;
76
77#[cfg(feature = "cuda")]
78pub use device::CudaDevice;
79#[cfg(feature = "cuda")]
80pub use kernel::CudaKernel;
81#[cfg(feature = "cuda")]
82pub use memory::{CudaBuffer, CudaControlBlock, CudaMemoryPool, CudaMessageQueue};
83#[cfg(feature = "cuda")]
84pub use persistent::CudaMappedBuffer;
85#[cfg(feature = "cuda")]
86pub use phases::{
87 InterPhaseReduction, KernelPhase, MultiPhaseConfig, MultiPhaseExecutor, PhaseExecutionStats,
88 SyncMode,
89};
90#[cfg(feature = "cuda")]
91pub use reduction::{
92 generate_block_reduce_code, generate_grid_reduce_code, generate_reduce_and_broadcast_code,
93 CacheKey, CacheStats, CachedReductionBuffer, ReductionBuffer, ReductionBufferBuilder,
94 ReductionBufferCache,
95};
96#[cfg(feature = "cuda")]
97pub use runtime::CudaRuntime;
98#[cfg(feature = "cuda")]
99pub use stencil::{CompiledStencilKernel, LaunchConfig, StencilKernelLoader};
100
101#[cfg(feature = "profiling")]
103pub use profiling::{
104 CudaEvent, CudaEventFlags, CudaMemoryKind, CudaMemoryTracker, CudaNvtxProfiler,
105 GpuChromeTraceBuilder, GpuEventArgs, GpuTimer, GpuTimerPool, GpuTraceEvent, KernelMetrics,
106 ProfilingSession, TrackedAllocation, TransferDirection, TransferMetrics,
107};
108
109#[cfg(feature = "ptx-cache")]
111pub use compile::{PtxCache, PtxCacheError, PtxCacheResult, PtxCacheStats, CACHE_VERSION};
112
113#[cfg(feature = "cuda")]
115pub use memory_pool::{
116 GpuBucketStats, GpuPoolConfig, GpuPoolDiagnostics, GpuSizeClass, GpuStratifiedPool,
117};
118
119#[cfg(feature = "cuda")]
121pub use stream::{
122 OverlapMetrics, StreamConfig, StreamConfigBuilder, StreamError, StreamId, StreamManager,
123 StreamPool, StreamPoolStats, StreamResult,
124};
125
126#[cfg(feature = "cuda")]
128pub mod memory_exports {
129 pub use super::memory::{CudaBuffer, CudaControlBlock, CudaMemoryPool, CudaMessageQueue};
130}
131
132#[cfg(not(feature = "cuda"))]
134mod stub {
135 ringkernel_core::unavailable_backend!(
136 CudaRuntime,
137 ringkernel_core::runtime::Backend::Cuda,
138 "CUDA"
139 );
140}
141
142#[cfg(not(feature = "cuda"))]
143pub use stub::CudaRuntime;
144
145pub fn is_cuda_available() -> bool {
154 #[cfg(feature = "cuda")]
155 {
156 std::panic::catch_unwind(|| {
158 cudarc::driver::CudaContext::device_count()
159 .map(|c| c > 0)
160 .unwrap_or(false)
161 })
162 .unwrap_or(false)
163 }
164 #[cfg(not(feature = "cuda"))]
165 {
166 false
167 }
168}
169
170pub fn cuda_device_count() -> usize {
174 #[cfg(feature = "cuda")]
175 {
176 std::panic::catch_unwind(|| {
178 cudarc::driver::CudaContext::device_count().unwrap_or(0) as usize
179 })
180 .unwrap_or(0)
181 }
182 #[cfg(not(feature = "cuda"))]
183 {
184 0
185 }
186}
187
188#[cfg(feature = "cuda")]
216pub fn compile_ptx(cuda_source: &str) -> ringkernel_core::error::Result<String> {
217 use ringkernel_core::error::RingKernelError;
218
219 let ptx = cudarc::nvrtc::compile_ptx(cuda_source).map_err(|e| {
220 RingKernelError::CompilationError(format!("NVRTC compilation failed: {}", e))
221 })?;
222
223 Ok(ptx.to_src().to_string())
224}
225
226#[cfg(not(feature = "cuda"))]
228pub fn compile_ptx(_cuda_source: &str) -> ringkernel_core::error::Result<String> {
229 Err(ringkernel_core::error::RingKernelError::BackendUnavailable(
230 "CUDA feature not enabled".to_string(),
231 ))
232}
233
234pub fn ring_kernel_ptx_template_for(major: u32, minor: u32) -> String {
246 let ptx_version = match (major, minor) {
247 (6, 0) | (6, 1) | (6, 2) => "5.0",
249 (7, 0) | (7, 2) => "6.0",
251 (7, 5) => "6.3",
252 (8, 0) => "7.0",
254 (8, 6) | (8, 7) => "7.1",
255 (8, 9) => "8.0",
256 (9, 0) => "8.0",
258 (major, _) if major >= 10 => "8.5",
260 _ => "5.0",
263 };
264 format!(
265 r#"
266.version {ptx_version}
267.target sm_{major}{minor}
268.address_size 64
269
270.visible .entry ring_kernel_main(
271 .param .u64 control_block_ptr,
272 .param .u64 input_queue_ptr,
273 .param .u64 output_queue_ptr,
274 .param .u64 shared_state_ptr
275) {{
276 .reg .u64 %cb_ptr;
277 .reg .u32 %one;
278
279 // Load control block pointer
280 ld.param.u64 %cb_ptr, [control_block_ptr];
281
282 // Mark as terminated immediately (offset 8)
283 mov.u32 %one, 1;
284 st.global.u32 [%cb_ptr + 8], %one;
285
286 ret;
287}}
288"#
289 )
290}
291
292#[deprecated(
303 since = "1.1.1",
304 note = "hardcodes sm_75, breaking on Pascal and older GPUs — use ring_kernel_ptx_template_for(major, minor) with the real device's compute capability instead"
305)]
306pub const RING_KERNEL_PTX_TEMPLATE: &str = r#"
307.version 8.0
308.target sm_75
309.address_size 64
310
311.visible .entry ring_kernel_main(
312 .param .u64 control_block_ptr,
313 .param .u64 input_queue_ptr,
314 .param .u64 output_queue_ptr,
315 .param .u64 shared_state_ptr
316) {
317 .reg .u64 %cb_ptr;
318 .reg .u32 %one;
319
320 // Load control block pointer
321 ld.param.u64 %cb_ptr, [control_block_ptr];
322
323 // Mark as terminated immediately (offset 8)
324 mov.u32 %one, 1;
325 st.global.u32 [%cb_ptr + 8], %one;
326
327 ret;
328}
329"#;