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
use std::ffi::c_void;
use std::ptr::null_mut;
use std::sync::Arc;

use crate::bindings::{cudaFree, cudaMalloc, cudaMemcpy, cudaMemcpyAsync, cudaMemcpyKind};
use crate::wrapper::handle::{CudaDevice, CudaStream};
use crate::wrapper::mem::pinned::PinnedMem;
use crate::wrapper::status::Status;

/// A reference-counted pointer into a [DeviceBuffer]. The buffer cannot be constructed directly,
/// instead it can only be created by allocating a new [DevicePtr] with [DevicePtr::alloc].
///
/// The inner [DeviceBuffer] is automatically freed when there are no [DevicePtr] any more that refer to it.
/// Since the memory may be shared all accessor methods are marked unsafe.
///
/// Cloning this type does not copy the underlying memory, but only increases the reference count.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct DevicePtr {
    buffer: Arc<DeviceBuffer>,
    offset: isize,
}

// TODO it's a bit weird that this is public without being able to construct it,
// but it's useful to let the user know it exists in docs.
/// A device allocation as returned by cudaMalloc.
#[derive(Debug, Eq, PartialEq, Hash)]
pub struct DeviceBuffer {
    device: CudaDevice,
    base_ptr: *mut c_void,
    len_bytes: isize,
}

// TODO is this correct? We've don't even attempt device-side memory safety, but can this cause cpu-side issues?
// TODO should we implement Sync? It's probably never a good idea to actually share pointers between threads...
unsafe impl Send for DeviceBuffer {}

unsafe impl Sync for DeviceBuffer {}

impl Drop for DeviceBuffer {
    fn drop(&mut self) {
        unsafe {
            self.device.switch_to();
            cudaFree(self.base_ptr).unwrap_in_drop()
        }
    }
}

impl DevicePtr {
    pub fn alloc(device: CudaDevice, len_bytes: usize) -> Self {
        unsafe {
            let mut device_ptr = null_mut();

            device.switch_to();
            cudaMalloc(&mut device_ptr as *mut _, len_bytes).unwrap();

            let inner = DeviceBuffer {
                device,
                base_ptr: device_ptr,
                len_bytes: len_bytes as isize,
            };
            DevicePtr {
                buffer: Arc::new(inner),
                offset: 0,
            }
        }
    }

    pub fn device(&self) -> CudaDevice {
        self.buffer.device
    }

    pub fn offset_bytes(self, offset: isize) -> DevicePtr {
        let new_offset = self.offset + offset;

        if self.buffer.len_bytes == 0 {
            assert_eq!(offset, 0, "Non-zero offset not allowed on empty buffer");
        } else {
            assert!(
                (0..self.buffer.len_bytes).contains(&new_offset),
                "Offset {} is out of range on {:?}",
                offset,
                self
            );
        }

        DevicePtr {
            buffer: self.buffer,
            offset: new_offset,
        }
    }

    pub unsafe fn ptr(&self) -> *mut c_void {
        self.buffer.base_ptr.offset(self.offset)
    }

    /// The number of `DevicePtr` sharing the underlying buffer that are still alive.
    pub fn shared_count(&self) -> usize {
        Arc::strong_count(&self.buffer)
    }

    pub unsafe fn copy_linear_from_host(&self, buffer: &[u8]) {
        self.assert_linear_in_bounds(buffer.len());

        self.device().switch_to();
        cudaMemcpy(
            self.ptr(),
            buffer as *const _ as *const _,
            buffer.len(),
            cudaMemcpyKind::cudaMemcpyHostToDevice,
        )
        .unwrap();
    }

    pub unsafe fn copy_linear_from_host_async(&self, buffer: &PinnedMem, stream: &CudaStream) {
        self.assert_linear_in_bounds(buffer.len_bytes());

        self.device().switch_to();
        cudaMemcpyAsync(
            self.ptr(),
            buffer.ptr(),
            buffer.len_bytes(),
            cudaMemcpyKind::cudaMemcpyDeviceToHost,
            stream.inner(),
        )
        .unwrap();
    }

    pub unsafe fn copy_linear_to_host(&self, buffer: &mut [u8]) {
        self.assert_linear_in_bounds(buffer.len());

        self.device().switch_to();
        cudaMemcpy(
            buffer as *mut _ as *mut _,
            self.ptr(),
            buffer.len(),
            cudaMemcpyKind::cudaMemcpyDeviceToHost,
        )
        .unwrap();
    }

    pub unsafe fn copy_linear_to_host_async(&self, buffer: &mut PinnedMem, stream: &CudaStream) {
        self.assert_linear_in_bounds(buffer.len_bytes());

        self.device().switch_to();
        cudaMemcpyAsync(
            buffer.ptr(),
            self.ptr(),
            buffer.len_bytes(),
            cudaMemcpyKind::cudaMemcpyDeviceToHost,
            stream.inner(),
        )
        .unwrap();
    }

    pub unsafe fn copy_linear_from_device(&self, other: &DevicePtr, len_bytes: usize) {
        assert_eq!(
            self.device(),
            other.device(),
            "Can only copy between tensors on the same device"
        );

        self.assert_linear_in_bounds(len_bytes);
        other.assert_linear_in_bounds(len_bytes);

        self.device().switch_to();
        cudaMemcpy(
            self.ptr(),
            other.ptr(),
            len_bytes,
            cudaMemcpyKind::cudaMemcpyDeviceToDevice,
        )
        .unwrap();
    }

    fn assert_linear_in_bounds(&self, len: usize) {
        assert!(
            (self.offset + len as isize) <= self.buffer.len_bytes,
            "Linear slice with length {} out of bounds for {:?}",
            len,
            self
        );
    }
}