torsh-backend 0.1.2

Backend abstraction layer for ToRSh
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
//! Unified memory buffer implementation for CUDA

use crate::cuda::device::CudaDevice;
use crate::cuda::error::CudaResult;
use crate::cuda::memory::{MemoryAdvice, UnifiedAllocation};
use std::sync::Arc;
use torsh_core::DType;

/// Generic buffer trait for type-safe buffer operations
pub trait BufferTrait<T> {
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
    fn dtype(&self) -> DType;
    fn device(&self) -> &dyn DeviceTrait;
    fn copy_from_host(&mut self, data: &[T]) -> Result<(), crate::BackendError>;
    fn copy_to_host(&self, data: &mut [T]) -> Result<(), crate::BackendError>;
    fn as_any(&self) -> &dyn std::any::Any;
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
}

/// Generic device trait
pub trait DeviceTrait: std::fmt::Debug + Send + Sync {
    fn id(&self) -> usize;
}

impl DeviceTrait for CudaDevice {
    fn id(&self) -> usize {
        self.id()
    }
}

/// Buffer operations trait
pub trait BufferOpsTrait<T> {
    fn fill(&mut self, value: T) -> Result<(), crate::BackendError>;
    fn copy_from_buffer(&mut self, src: &dyn BufferTrait<T>) -> Result<(), crate::BackendError>;
    fn set_zero(&mut self) -> Result<(), crate::BackendError>;
}

/// Debug information for unified buffers
#[derive(Debug, Clone)]
pub struct UnifiedBufferDebugInfo {
    pub ptr: *mut u8,
    pub length: usize,
    pub size_bytes: usize,
    pub dtype: DType,
    pub device_id: usize,
}

/// Unified memory buffer that can be accessed from both CPU and GPU
#[derive(Debug)]
pub struct UnifiedBuffer<T> {
    allocation: UnifiedAllocation,
    length: usize,
    dtype: DType,
    device: Arc<CudaDevice>,
    _phantom: std::marker::PhantomData<T>,
}

impl<T: Clone + Send + Sync + 'static> UnifiedBuffer<T> {
    /// Create a new unified buffer
    pub fn new(device: Arc<CudaDevice>, length: usize, dtype: DType) -> CudaResult<Self> {
        let byte_size = length * std::mem::size_of::<T>();
        let allocation = device.memory_manager().allocate_unified(byte_size)?;

        Ok(Self {
            allocation,
            length,
            dtype,
            device,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Get the underlying allocation
    pub fn allocation(&self) -> &UnifiedAllocation {
        &self.allocation
    }

    /// Get mutable reference to the underlying allocation
    pub fn allocation_mut(&mut self) -> &mut UnifiedAllocation {
        &mut self.allocation
    }

    /// Get raw pointer to the data
    pub fn as_ptr(&self) -> *const T {
        self.allocation.as_ptr() as *const T
    }

    /// Get mutable raw pointer to the data
    pub fn as_mut_ptr(&mut self) -> *mut T {
        self.allocation.as_ptr() as *mut T
    }

    /// Get a slice view of the data (safe when accessed from CPU)
    pub unsafe fn as_slice(&self) -> &[T] {
        std::slice::from_raw_parts(self.as_ptr(), self.length)
    }

    /// Get a mutable slice view of the data (safe when accessed from CPU)
    pub unsafe fn as_mut_slice(&mut self) -> &mut [T] {
        std::slice::from_raw_parts_mut(self.as_mut_ptr(), self.length)
    }

    /// Prefetch data to GPU
    pub fn prefetch_to_device(&self, _device_id: Option<usize>) -> CudaResult<()> {
        let byte_size = self.length * std::mem::size_of::<T>();
        // Note: device_id is now derived from the memory manager's internal device
        self.device
            .memory_manager()
            .prefetch_to_device(self.allocation.ptr(), byte_size)
    }

    /// Prefetch data to CPU
    pub fn prefetch_to_host(&self) -> CudaResult<()> {
        let byte_size = self.length * std::mem::size_of::<T>();
        self.device
            .memory_manager()
            .prefetch_to_host(self.allocation.ptr(), byte_size)
    }

    /// Set memory advice for performance optimization
    pub fn set_memory_advice(
        &self,
        advice: MemoryAdvice,
        device_id: Option<usize>,
    ) -> CudaResult<()> {
        let byte_size = self.length * std::mem::size_of::<T>();
        let device = device_id.unwrap_or(0) as i32;
        self.device.memory_manager().set_memory_advice(
            self.allocation.ptr(),
            byte_size,
            advice,
            device,
        )
    }

    /// Set data as read-mostly for optimization
    pub fn set_read_mostly(&self) -> CudaResult<()> {
        self.set_memory_advice(MemoryAdvice::SetReadMostly, None)
    }

    /// Set preferred location for the data
    pub fn set_preferred_location(&self, device_id: usize) -> CudaResult<()> {
        self.set_memory_advice(MemoryAdvice::SetPreferredLocation, Some(device_id))
    }

    /// Indicate which device will access this data
    pub fn set_accessed_by(&self, device_id: usize) -> CudaResult<()> {
        self.set_memory_advice(MemoryAdvice::SetAccessedBy, Some(device_id))
    }

    /// Get memory statistics for debugging
    pub fn debug_info(&self) -> UnifiedBufferDebugInfo {
        UnifiedBufferDebugInfo {
            ptr: self.allocation.ptr(),
            length: self.length,
            size_bytes: self.length * std::mem::size_of::<T>(),
            dtype: self.dtype,
            device_id: self.device.id(),
        }
    }
}

impl<T: Clone + Send + Sync + 'static> BufferTrait<T> for UnifiedBuffer<T> {
    fn len(&self) -> usize {
        self.length
    }

    fn dtype(&self) -> DType {
        self.dtype
    }

    fn device(&self) -> &dyn DeviceTrait {
        self.device.as_ref()
    }

    fn copy_from_host(&mut self, data: &[T]) -> Result<(), crate::BackendError> {
        if data.len() != self.length {
            return Err(crate::BackendError::InvalidBuffer {
                message: format!(
                    "Data length {} does not match buffer length {}",
                    data.len(),
                    self.length
                ),
            });
        }

        self.allocation
            .copy_from_host(data)
            .map_err(|e| crate::BackendError::Runtime {
                message: format!("Failed to copy from host: {}", e),
            })
    }

    fn copy_to_host(&self, data: &mut [T]) -> Result<(), crate::BackendError> {
        if data.len() != self.length {
            return Err(crate::BackendError::InvalidBuffer {
                message: format!(
                    "Data length {} does not match buffer length {}",
                    data.len(),
                    self.length
                ),
            });
        }

        self.allocation
            .copy_to_host(data)
            .map_err(|e| crate::BackendError::Runtime {
                message: format!("Failed to copy to host: {}", e),
            })
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

impl<T: Clone + Send + Sync + 'static> BufferOpsTrait<T> for UnifiedBuffer<T> {
    fn fill(&mut self, value: T) -> Result<(), crate::BackendError> {
        unsafe {
            let slice = self.as_mut_slice();
            slice.fill(value);
        }
        Ok(())
    }

    fn copy_from_buffer(&mut self, src: &dyn BufferTrait<T>) -> Result<(), crate::BackendError> {
        if src.len() != self.length {
            return Err(crate::BackendError::InvalidBuffer {
                message: format!(
                    "Source buffer length {} does not match target length {}",
                    src.len(),
                    self.length
                ),
            });
        }

        // Try to copy directly if source is also a unified buffer
        if let Some(src_unified) = src.as_any().downcast_ref::<UnifiedBuffer<T>>() {
            unsafe {
                std::ptr::copy_nonoverlapping(src_unified.as_ptr(), self.as_mut_ptr(), self.length);
            }
            return Ok(());
        }

        // Otherwise, copy via host
        let mut temp_data = Vec::<T>::with_capacity(self.length);
        unsafe {
            temp_data.set_len(self.length);
        }
        src.copy_to_host(&mut temp_data)?;
        self.copy_from_host(&temp_data)
    }

    fn set_zero(&mut self) -> Result<(), crate::BackendError> {
        unsafe {
            std::ptr::write_bytes(
                self.as_mut_ptr() as *mut u8,
                0,
                self.length * std::mem::size_of::<T>(),
            );
        }
        Ok(())
    }
}

// UnifiedBuffer doesn't need an explicit Drop implementation
// The UnifiedAllocation field will be automatically dropped and
// its Drop implementation will free the CUDA unified memory

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cuda::device::CudaDevice;
    use torsh_core::DType;

    #[test]
    fn test_unified_buffer_creation() {
        if crate::is_available() {
            let device = Arc::new(CudaDevice::new(0).expect("Arc should succeed"));

            // Check if device supports unified memory
            if device
                .supports_feature(crate::cuda::device::CudaFeature::ManagedMemory)
                .unwrap_or(false)
            {
                let buffer = UnifiedBuffer::<f32>::new(device, 1024, DType::F32);
                assert!(buffer.is_ok());

                let buffer = buffer.expect("operation should succeed");
                assert_eq!(buffer.len(), 1024);
                assert_eq!(buffer.dtype(), DType::F32);
            }
        }
    }

    #[test]
    fn test_unified_buffer_operations() {
        if crate::is_available() {
            let device = Arc::new(CudaDevice::new(0).expect("Arc should succeed"));

            if device
                .supports_feature(crate::cuda::device::CudaFeature::ManagedMemory)
                .unwrap_or(false)
            {
                let mut buffer = UnifiedBuffer::<f32>::new(device, 4, DType::F32)
                    .expect("construction with valid parameters should succeed");

                // Test copy operations
                let test_data = vec![1.0, 2.0, 3.0, 4.0];
                buffer
                    .copy_from_host(&test_data)
                    .expect("copy from host memory should succeed");

                let mut result_data = vec![0.0; 4];
                buffer
                    .copy_to_host(&mut result_data)
                    .expect("copy to host memory should succeed");
                assert_eq!(result_data, test_data);

                // Test prefetching
                buffer
                    .prefetch_to_device(None)
                    .expect("prefetch to device should succeed");
                buffer
                    .prefetch_to_host()
                    .expect("prefetch to host should succeed");

                // Test memory advice
                buffer
                    .set_read_mostly()
                    .expect("read-mostly hint should be applied successfully");
                buffer
                    .set_preferred_location(0)
                    .expect("preferred location should be set successfully");
                buffer
                    .set_accessed_by(0)
                    .expect("accessed-by hint should be set successfully");
            }
        }
    }

    #[test]
    fn test_unified_buffer_slice_access() {
        if crate::is_available() {
            let device = Arc::new(CudaDevice::new(0).expect("Arc should succeed"));

            if device
                .supports_feature(crate::cuda::device::CudaFeature::ManagedMemory)
                .unwrap_or(false)
            {
                let mut buffer = UnifiedBuffer::<i32>::new(device, 8, DType::I32)
                    .expect("construction with valid parameters should succeed");

                // Fill buffer with test data
                let test_data = vec![1, 2, 3, 4, 5, 6, 7, 8];
                buffer
                    .copy_from_host(&test_data)
                    .expect("copy from host memory should succeed");

                // Test slice access (after prefetching to host)
                buffer
                    .prefetch_to_host()
                    .expect("prefetch to host should succeed");
                unsafe {
                    let slice = buffer.as_slice();
                    assert_eq!(slice.len(), 8);
                    assert_eq!(slice[0], 1);
                    assert_eq!(slice[7], 8);
                }
            }
        }
    }

    #[test]
    fn test_unified_buffer_fill_and_zero() {
        if crate::is_available() {
            let device = Arc::new(CudaDevice::new(0).expect("Arc should succeed"));

            if device
                .supports_feature(crate::cuda::device::CudaFeature::ManagedMemory)
                .unwrap_or(false)
            {
                let mut buffer = UnifiedBuffer::<f32>::new(device, 10, DType::F32)
                    .expect("construction with valid parameters should succeed");

                // Test fill
                buffer.fill(3.14).expect("fill operation should succeed");

                let mut result = vec![0.0; 10];
                buffer
                    .copy_to_host(&mut result)
                    .expect("copy to host memory should succeed");
                for &val in &result {
                    assert_eq!(val, 3.14);
                }

                // Test zero
                buffer.set_zero().expect("zero-fill should succeed");
                buffer
                    .copy_to_host(&mut result)
                    .expect("copy to host memory should succeed");
                for &val in &result {
                    assert_eq!(val, 0.0);
                }
            }
        }
    }
}