atomr-accel-cuda 0.10.0

GPU acceleration via the actor model. Wraps NVIDIA CUDA libraries (cuBLAS, cuDNN, cuFFT, cuRAND, cuSOLVER, cuSPARSE, cuTENSOR, cuBLASLt, NVRTC, NCCL) as supervised atomr actors with generation-validated buffers and a uniform async surface.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! `PinnedBufferPool` actor + `PinnedBuf<T>` handle.
//!
//! Lifecycle:
//! 1. Construct the pool actor with [`PinnedBufferPool::props`].
//! 2. Send [`PinnedPoolMsg::Acquire { len_bytes, reply }`] — actor
//!    pops from the free-list (or grows up to `max_buffers`) and
//!    replies with a [`PinnedBuf<T>`] handle whose Drop sends an
//!    `InternalReturn` back to the pool.
//! 3. Use the buffer: `as_mut_slice()` for fill, then move into a
//!    `DeviceMsg::CopyToDeviceT` / `CopyToHostT` call.
//!
//! cudarc 0.19 only exposes raw [`cudarc::driver::result::malloc_host`]
//! and [`cudarc::driver::result::free_host`]; no safe wrapper. We
//! build [`PinnedSlot`] around the raw pointer + capacity + Drop and
//! treat it as the moral equivalent of `Box<[u8]>` over pinned
//! memory.

use std::collections::VecDeque;
use std::ffi::c_void;
use std::marker::PhantomData;

use async_trait::async_trait;
use atomr_core::actor::{Actor, ActorRef, Context, Props};
use tokio::sync::{mpsc, oneshot};
use tracing::{debug, warn};

use crate::error::GpuError;

#[derive(Debug, Clone, Copy)]
pub struct PinnedBufferPoolConfig {
    pub initial_buffers: usize,
    pub max_buffers: usize,
    pub buffer_capacity_bytes: usize,
    /// If true, requests larger than `buffer_capacity_bytes` get a
    /// one-shot oversize allocation that's freed (not pooled) on
    /// release. If false, oversize requests fail.
    pub allow_oversize: bool,
}

impl Default for PinnedBufferPoolConfig {
    fn default() -> Self {
        Self {
            initial_buffers: 4,
            max_buffers: 32,
            buffer_capacity_bytes: 4 * 1024 * 1024,
            allow_oversize: true,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct PinnedPoolStats {
    pub in_use: usize,
    pub free: usize,
    pub total: usize,
    pub bytes_allocated: usize,
}

/// Internal slot — owns the raw `cuMemHostAlloc`'d region.
pub struct PinnedSlot {
    ptr: *mut c_void,
    capacity_bytes: usize,
    /// True if this slot was minted as an oversize one-shot; freed
    /// rather than returned to the pool on release.
    oversize: bool,
}

// SAFETY: pinned memory is host RAM. Sending the raw pointer between
// threads is safe; dereferencing requires care which we constrain to
// the actor + the holder of `PinnedBuf<T>`.
unsafe impl Send for PinnedSlot {}
unsafe impl Sync for PinnedSlot {}

impl PinnedSlot {
    fn new(capacity_bytes: usize, oversize: bool) -> Result<Self, GpuError> {
        // `cuMemHostAlloc` flags: 0 = default (portable across devices
        // off, write-combined off, mapped off). Sufficient for
        // standard async memcpy.
        let ptr = unsafe { cudarc::driver::result::malloc_host(capacity_bytes, 0) }
            .map_err(|e| GpuError::OutOfMemory(format!("pinned alloc {capacity_bytes}B: {e}")))?;
        Ok(Self {
            ptr,
            capacity_bytes,
            oversize,
        })
    }

    fn free(self) {
        // Drop the slot; the Drop impl below frees the pinned memory.
        drop(self);
    }
}

impl Drop for PinnedSlot {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                let _ = cudarc::driver::result::free_host(self.ptr);
            }
            self.ptr = std::ptr::null_mut();
        }
    }
}

/// Generation token for pool-level invalidation. Bumped on actor
/// restart so that surviving `PinnedBuf<T>` instances cannot
/// accidentally return into a fresh pool.
type PinnedGeneration = u64;

/// Public messages.
pub enum PinnedPoolMsg {
    Acquire {
        len_bytes: usize,
        reply: oneshot::Sender<Result<PinnedBufHandle, GpuError>>,
    },
    /// Drop-driven return path used by `PinnedBuf::Drop`. Not part of
    /// the public API for callers.
    InternalReturn {
        slot: PinnedSlot,
        generation: PinnedGeneration,
    },
    Stats {
        reply: oneshot::Sender<PinnedPoolStats>,
    },
}

/// Untyped handle returned by `Acquire`. Convert to a typed
/// [`PinnedBuf<T>`] via [`PinnedBufHandle::into_typed`].
pub struct PinnedBufHandle {
    slot: Option<PinnedSlot>,
    generation: PinnedGeneration,
    return_tx: mpsc::UnboundedSender<PinnedPoolMsg>,
}

impl PinnedBufHandle {
    pub fn capacity_bytes(&self) -> usize {
        self.slot.as_ref().map(|s| s.capacity_bytes).unwrap_or(0)
    }

    /// Convert to a typed buffer. `len` is the number of `T`s the
    /// caller intends to use; must satisfy
    /// `len * size_of::<T>() <= capacity_bytes`.
    pub fn into_typed<T>(mut self, len: usize) -> Result<PinnedBuf<T>, GpuError> {
        let needed = len.checked_mul(std::mem::size_of::<T>()).ok_or_else(|| {
            GpuError::Unrecoverable("pinned buf: len * size_of overflowed".into())
        })?;
        if needed > self.capacity_bytes() {
            return Err(GpuError::Unrecoverable(format!(
                "pinned buf: requested {len} elements ({needed} B) exceeds capacity {} B",
                self.capacity_bytes()
            )));
        }
        let slot = self.slot.take().expect("PinnedBufHandle slot was None");
        let ptr = slot.ptr as *mut T;
        Ok(PinnedBuf {
            inner: Some(PinnedBufInner {
                slot,
                len,
                return_tx: self.return_tx.clone(),
                generation: self.generation,
            }),
            ptr,
            len,
            _marker: PhantomData,
        })
    }
}

impl Drop for PinnedBufHandle {
    fn drop(&mut self) {
        // If into_typed wasn't called, return the slot ourselves.
        if let Some(slot) = self.slot.take() {
            let _ = self.return_tx.send(PinnedPoolMsg::InternalReturn {
                slot,
                generation: self.generation,
            });
        }
    }
}

/// Typed pinned buffer.
///
/// Send + Sync — the raw pointer is page-locked host memory; the
/// actor + this handle are the only writers, and the pool's
/// generation gate prevents post-restart aliasing.
pub struct PinnedBuf<T> {
    inner: Option<PinnedBufInner>,
    ptr: *mut T,
    len: usize,
    _marker: PhantomData<T>,
}

struct PinnedBufInner {
    slot: PinnedSlot,
    #[allow(dead_code)]
    len: usize,
    return_tx: mpsc::UnboundedSender<PinnedPoolMsg>,
    generation: PinnedGeneration,
}

unsafe impl<T: Send> Send for PinnedBuf<T> {}
unsafe impl<T: Sync> Sync for PinnedBuf<T> {}

impl<T> PinnedBuf<T> {
    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn as_ptr(&self) -> *const T {
        self.ptr
    }

    pub fn as_mut_ptr(&mut self) -> *mut T {
        self.ptr
    }

    /// View as a host slice. SAFETY: relies on the buffer being
    /// initialized for the requested length. For zero-init you must
    /// fill before reading.
    pub fn as_slice(&self) -> &[T] {
        // SAFETY: ptr was returned by cuMemHostAlloc and the slot
        // owns it for at least the lifetime of `self`. len matches
        // the typed view we requested in into_typed.
        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
    }

    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { std::slice::from_raw_parts_mut(self.ptr, self.len) }
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for PinnedBuf<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PinnedBuf").field("len", &self.len).finish()
    }
}

impl<T> Drop for PinnedBuf<T> {
    fn drop(&mut self) {
        if let Some(inner) = self.inner.take() {
            let _ = inner.return_tx.send(PinnedPoolMsg::InternalReturn {
                slot: inner.slot,
                generation: inner.generation,
            });
        }
    }
}

/// The pool actor.
pub struct PinnedBufferPool {
    config: PinnedBufferPoolConfig,
    free: VecDeque<PinnedSlot>,
    in_use: usize,
    total_minted: usize,
    bytes_allocated: usize,
    /// Bumped on `pre_restart`. Slots returning with an old
    /// generation are dropped instead of pooled.
    generation: PinnedGeneration,
    /// Sender used by `PinnedBuf::Drop` to post `InternalReturn`. We
    /// keep one mpsc that funnels into `handle()` — the actor's own
    /// mailbox is async-trait `tell`-only, but mpsc gives us
    /// non-blocking sync sends from `Drop`.
    return_tx: mpsc::UnboundedSender<PinnedPoolMsg>,
    return_rx_observer: Option<ActorRef<PinnedPoolMsg>>,
}

impl PinnedBufferPool {
    pub fn props(config: PinnedBufferPoolConfig) -> Props<Self> {
        Props::create(move || {
            let (tx, _rx) = mpsc::unbounded_channel();
            // Initial buffers are minted lazily on first acquire; in
            // F2 we trade a tiny first-acquire latency for simpler
            // post_restart handling.
            PinnedBufferPool {
                config,
                free: VecDeque::new(),
                in_use: 0,
                total_minted: 0,
                bytes_allocated: 0,
                generation: 0,
                return_tx: tx,
                return_rx_observer: None,
            }
        })
    }

    /// Test/diagnostic: snapshot stats without going through the
    /// mailbox.
    pub fn stats(&self) -> PinnedPoolStats {
        PinnedPoolStats {
            in_use: self.in_use,
            free: self.free.len(),
            total: self.total_minted,
            bytes_allocated: self.bytes_allocated,
        }
    }

    fn try_acquire(&mut self, len_bytes: usize) -> Result<PinnedBufHandle, GpuError> {
        let cap = self.config.buffer_capacity_bytes;
        let oversize = len_bytes > cap;

        let slot = if oversize {
            if !self.config.allow_oversize {
                return Err(GpuError::OutOfMemory(format!(
                    "pinned pool: oversize request {len_bytes}B exceeds slot capacity {cap}B"
                )));
            }
            // One-shot allocation, freed on release.
            self.bytes_allocated += len_bytes;
            self.total_minted += 1;
            PinnedSlot::new(len_bytes, true)?
        } else if let Some(slot) = self.free.pop_front() {
            slot
        } else {
            if self.total_minted >= self.config.max_buffers {
                return Err(GpuError::OutOfMemory(format!(
                    "pinned pool: max_buffers={} reached",
                    self.config.max_buffers
                )));
            }
            self.bytes_allocated += cap;
            self.total_minted += 1;
            PinnedSlot::new(cap, false)?
        };

        self.in_use += 1;
        Ok(PinnedBufHandle {
            slot: Some(slot),
            generation: self.generation,
            return_tx: self.return_tx.clone(),
        })
    }

    fn return_slot(&mut self, slot: PinnedSlot, generation: PinnedGeneration) {
        if generation != self.generation {
            // Cross-generation return. Drop instead of pool to avoid
            // mixing. The Drop impl on PinnedSlot frees the memory.
            self.bytes_allocated = self.bytes_allocated.saturating_sub(slot.capacity_bytes);
            self.total_minted = self.total_minted.saturating_sub(1);
            slot.free();
            return;
        }
        self.in_use = self.in_use.saturating_sub(1);
        if slot.oversize {
            // Don't pool oversize allocations.
            self.bytes_allocated = self.bytes_allocated.saturating_sub(slot.capacity_bytes);
            self.total_minted = self.total_minted.saturating_sub(1);
            slot.free();
        } else {
            self.free.push_back(slot);
        }
    }
}

#[async_trait]
impl Actor for PinnedBufferPool {
    type Msg = PinnedPoolMsg;

    async fn pre_start(&mut self, ctx: &mut Context<Self>) {
        // Wire the mpsc → actor pump so Drop-driven returns funnel
        // into `handle()`. We replace the sender with a fresh one and
        // start a forwarder task that bridges the receiver into the
        // actor's mailbox.
        let (tx, mut rx) = mpsc::unbounded_channel::<PinnedPoolMsg>();
        self.return_tx = tx;
        let self_ref = ctx.self_ref().clone();
        self.return_rx_observer = Some(self_ref.clone());
        tokio::spawn(async move {
            while let Some(msg) = rx.recv().await {
                self_ref.tell(msg);
            }
        });
        debug!(
            initial = self.config.initial_buffers,
            max = self.config.max_buffers,
            cap = self.config.buffer_capacity_bytes,
            "PinnedBufferPool started"
        );
    }

    async fn handle(&mut self, _ctx: &mut Context<Self>, msg: PinnedPoolMsg) {
        match msg {
            PinnedPoolMsg::Acquire { len_bytes, reply } => {
                let r = self.try_acquire(len_bytes);
                let _ = reply.send(r);
            }
            PinnedPoolMsg::InternalReturn { slot, generation } => {
                self.return_slot(slot, generation);
            }
            PinnedPoolMsg::Stats { reply } => {
                let _ = reply.send(self.stats());
            }
        }
    }

    async fn pre_restart(&mut self, _ctx: &mut Context<Self>, err: &str) {
        warn!(%err, "PinnedBufferPool pre_restart — dropping all in-flight buffers");
        // Drop the free-list. Slots that haven't returned yet will
        // arrive with the pre-restart generation and be dropped (not
        // pooled) by `return_slot`.
        self.free.clear();
        self.generation += 1;
        self.in_use = 0;
        self.total_minted = 0;
        self.bytes_allocated = 0;
    }

    async fn post_stop(&mut self, _ctx: &mut Context<Self>) {
        debug!("PinnedBufferPool post_stop");
        // Slots in `self.free` drop here, freeing pinned memory via
        // PinnedSlot::Drop.
    }
}