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
//! Device memory allocation and management
use crate::{Result, runtime_error};
use crate::runtime::{Device, BackendType};
use std::marker::PhantomData;
use std::sync::Arc;
use std::alloc::{alloc, dealloc, Layout};
/// Raw device memory pointer
pub struct DevicePtr {
raw: *mut u8,
size: usize,
backend: BackendType,
}
impl DevicePtr {
/// Allocate raw device memory
pub fn allocate(size: usize, device: &Arc<Device>) -> Result<Self> {
if size == 0 {
return Err(runtime_error!("Cannot allocate zero-sized buffer"));
}
let backend = device.backend();
let raw = match backend {
BackendType::Native => {
// Host-memory allocation serves as the compute target for CPU and
// fallback backends. When a native GPU backend is active, the
// Backend::allocate_memory path handles device-side allocation.
unsafe {
let layout = Layout::from_size_align(size, 8)
.map_err(|e| runtime_error!("Invalid layout: {}", e))?;
alloc(layout)
}
}
BackendType::WebGPU => {
// Host-memory allocation for the runtime abstraction layer.
// The high-level WebGPU backend manages its own device-side
// buffer objects; DevicePtr provides the host-side mirror.
unsafe {
let layout = Layout::from_size_align(size, 8)
.map_err(|e| runtime_error!("Invalid layout: {}", e))?;
alloc(layout)
}
}
BackendType::CPU => {
// CPU backend uses regular heap allocation
unsafe {
let layout = Layout::from_size_align(size, 8)
.map_err(|e| runtime_error!("Invalid layout: {}", e))?;
alloc(layout)
}
}
};
if raw.is_null() {
return Err(runtime_error!("Failed to allocate {} bytes of device memory", size));
}
Ok(Self { raw, size, backend })
}
/// Get raw pointer
pub fn as_ptr(&self) -> *const u8 {
self.raw
}
/// Get mutable raw pointer
pub fn as_mut_ptr(&mut self) -> *mut u8 {
self.raw
}
/// Get allocation size
pub fn size(&self) -> usize {
self.size
}
}
impl Drop for DevicePtr {
fn drop(&mut self) {
if !self.raw.is_null() {
match self.backend {
BackendType::Native => {
// Host-side deallocation for the runtime abstraction.
// Native GPU backends handle their own device-side frees.
unsafe {
if let Ok(layout) = Layout::from_size_align(self.size, 8) {
dealloc(self.raw, layout);
}
}
}
BackendType::WebGPU => {
// Host-side deallocation for the runtime abstraction.
// WebGPU device buffers are released by the backend layer.
unsafe {
if let Ok(layout) = Layout::from_size_align(self.size, 8) {
dealloc(self.raw, layout);
}
}
}
BackendType::CPU => {
unsafe {
if let Ok(layout) = Layout::from_size_align(self.size, 8) {
dealloc(self.raw, layout);
}
}
}
}
}
}
}
/// Device memory buffer
pub struct DeviceBuffer<T> {
ptr: DevicePtr,
len: usize,
device: Arc<Device>,
phantom: PhantomData<T>,
}
impl<T: Copy> DeviceBuffer<T> {
/// Allocate a new device buffer
pub fn new(len: usize, device: Arc<Device>) -> Result<Self> {
if len == 0 {
return Err(runtime_error!("Cannot allocate zero-length buffer"));
}
let size = len * std::mem::size_of::<T>();
let ptr = DevicePtr::allocate(size, &device)?;
Ok(Self {
ptr,
len,
device,
phantom: PhantomData,
})
}
/// Get buffer length
pub fn len(&self) -> usize {
self.len
}
/// Check if buffer is empty
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Get the device this buffer is allocated on
pub fn device(&self) -> &Arc<Device> {
&self.device
}
/// Get raw device pointer
///
/// # Safety
/// The caller must ensure that the returned pointer is not used after the `DeviceBuffer` is dropped.
/// The caller must also ensure that the memory is not accessed concurrently.
pub unsafe fn as_ptr(&self) -> *const T {
self.ptr.as_ptr() as *const T
}
/// Get mutable raw device pointer
///
/// # Safety
/// The caller must ensure that the returned pointer is not used after the `DeviceBuffer` is dropped.
/// The caller must also ensure that the memory is not accessed concurrently.
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
self.ptr.as_mut_ptr() as *mut T
}
/// Copy from host memory
pub fn copy_from_host(&mut self, data: &[T]) -> Result<()> {
if data.len() != self.len {
return Err(runtime_error!(
"Host buffer length {} doesn't match device buffer length {}",
data.len(),
self.len
));
}
let size = self.len * std::mem::size_of::<T>();
match self.device.backend() {
BackendType::Native => {
// Runtime-level host-to-host copy. The native GPU backend
// performs its own host-to-device transfers when dispatching
// kernels; this path keeps the host mirror in sync.
unsafe {
std::ptr::copy_nonoverlapping(
data.as_ptr() as *const u8,
self.ptr.as_mut_ptr(),
size
);
}
}
BackendType::WebGPU => {
// Runtime-level host copy. The WebGPU backend writes to its
// own device buffers independently; this maintains the host mirror.
unsafe {
std::ptr::copy_nonoverlapping(
data.as_ptr() as *const u8,
self.ptr.as_mut_ptr(),
size
);
}
}
BackendType::CPU => {
unsafe {
std::ptr::copy_nonoverlapping(
data.as_ptr() as *const u8,
self.ptr.as_mut_ptr(),
size
);
}
}
}
Ok(())
}
/// Copy to host memory
pub fn copy_to_host(&self, data: &mut [T]) -> Result<()> {
if data.len() != self.len {
return Err(runtime_error!(
"Host buffer length {} doesn't match device buffer length {}",
data.len(),
self.len
));
}
let size = self.len * std::mem::size_of::<T>();
match self.device.backend() {
BackendType::Native => {
// Runtime-level host-to-host copy. The native GPU backend
// performs its own device-to-host transfers after kernel
// execution; this path reads from the host mirror.
unsafe {
std::ptr::copy_nonoverlapping(
self.ptr.as_ptr(),
data.as_mut_ptr() as *mut u8,
size
);
}
}
BackendType::WebGPU => {
// Runtime-level host copy. The WebGPU backend reads from its
// own device buffers independently; this reads the host mirror.
unsafe {
std::ptr::copy_nonoverlapping(
self.ptr.as_ptr(),
data.as_mut_ptr() as *mut u8,
size
);
}
}
BackendType::CPU => {
unsafe {
std::ptr::copy_nonoverlapping(
self.ptr.as_ptr(),
data.as_mut_ptr() as *mut u8,
size
);
}
}
}
Ok(())
}
/// Fill buffer with a value
pub fn fill(&mut self, value: T) -> Result<()> {
// For now, copy to host, fill, and copy back
// TODO: Optimize with kernel-based fill
let host_data = vec![value; self.len];
self.copy_from_host(&host_data)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::runtime::Device;
#[test]
fn test_device_buffer_allocation() {
let device = Device::get_default().unwrap();
let buffer = DeviceBuffer::<f32>::new(1024, device).unwrap();
assert_eq!(buffer.len(), 1024);
assert!(!buffer.is_empty());
}
#[test]
fn test_host_device_copy() {
let device = Device::get_default().unwrap();
let mut buffer = DeviceBuffer::<f32>::new(100, device).unwrap();
// Create test data
let host_data: Vec<f32> = (0..100).map(|i| i as f32).collect();
// Copy to device
buffer.copy_from_host(&host_data).unwrap();
// Copy back
let mut result = vec![0.0; 100];
buffer.copy_to_host(&mut result).unwrap();
// Verify
assert_eq!(host_data, result);
}
}