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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use cubecl_common::{backtrace::BackTrace, bytes::Bytes, stream_id::StreamId};
use cubecl_core::{
MemoryUsage,
future::DynFut,
server::{
Binding, CopyDescriptor, ExecutionError, ExecutionMode, Handle, IoError, ProfileError,
},
};
use cubecl_hip_sys::{
HIP_SUCCESS, hipMemcpyKind_hipMemcpyDeviceToHost, hipMemcpyKind_hipMemcpyHostToDevice,
ihipStream_t,
};
use cubecl_runtime::{
compiler::{CompilationError, CubeTask},
id::KernelId,
logging::ServerLogger,
memory_management::{MemoryAllocationMode, MemoryHandle},
stream::{GcTask, ResolvedStreams},
};
use cubecl_zspace::striding::has_pitched_row_major_strides;
use std::{ffi::c_void, sync::Arc};
use crate::{
compute::{
MB, context::HipContext, fence::Fence, gpu::GpuResource,
io::controller::PinnedMemoryManagedAllocController, stream::HipStreamBackend,
},
runtime::HipCompiler,
};
#[derive(new)]
/// The `Command` struct encapsulates a HIP context and a set of resolved HIP streams, providing an
/// interface for executing GPU-related operations such as memory allocation, data transfers, kernel
/// registration, and task execution.
pub struct Command<'a> {
ctx: &'a mut HipContext,
pub(crate) streams: ResolvedStreams<'a, HipStreamBackend>,
}
impl<'a> Command<'a> {
/// Retrieves a GPU resource associated with the provided binding.
///
/// # Parameters
///
/// * `binding` - The binding specifying the stream, memory, and offsets for the resource.
///
/// # Returns
///
/// * `Ok(GpuResource)` - The GPU resource associated with the binding.
/// * `Err(IoError::InvalidHandle)` - If the binding does not correspond to a valid resource.
pub fn resource(&mut self, binding: Binding) -> Result<GpuResource, IoError> {
self.streams
.get(&binding.stream)
.memory_management_gpu
.get_resource(binding.memory, binding.offset_start, binding.offset_end)
.ok_or(IoError::InvalidHandle {
backtrace: BackTrace::capture(),
})
}
/// Retrieves the gpu memory usage of the current stream.
///
/// # Returns
///
/// * The [MemoryUsage] struct.
pub fn memory_usage(&mut self) -> MemoryUsage {
self.streams.current().memory_management_gpu.memory_usage()
}
/// Explicitly cleanup gpu memory on the current stream.
pub fn memory_cleanup(&mut self) {
self.streams.current().memory_management_gpu.cleanup(true)
}
/// Set the [MemoryAllocationMode] for the current stream.
///
/// # Parameters
///
/// * `mode` - The allocation mode to be used.
pub fn allocation_mode(&mut self, mode: MemoryAllocationMode) {
self.streams.current().memory_management_gpu.mode(mode)
}
/// Allocates a new GPU memory buffer of the specified size.
///
/// # Parameters
///
/// * `size` - The size of the memory to allocate (in bytes).
///
/// # Returns
///
/// * `Ok(Handle)` - A handle to the newly allocated GPU memory.
/// * `Err(IoError)` - If the allocation fails.
pub fn reserve(&mut self, size: u64) -> Result<Handle, IoError> {
let handle = self.streams.current().memory_management_gpu.reserve(size)?;
Ok(Handle::new(
handle,
None,
None,
self.streams.current,
self.streams.cursor,
size,
))
}
/// Creates a [Bytes] instance from pinned memory, if suitable for the given size.
///
/// For small data transfers (<= 100 MB) or when explicitly marked as pinned, this function
/// uses pinned memory to optimize performance. For larger transfers, it falls back to regular memory.
///
/// # Arguments
///
/// * `size` - The number of bytes to allocate.
/// * `marked_pinned` - Whether to force the use of pinned memory.
///
/// # Returns
///
/// A [Bytes] instance of the correct size.
pub fn reserve_cpu(
&mut self,
size: usize,
marked_pinned: bool,
origin: Option<StreamId>,
) -> Bytes {
// Use pinned memory for small transfers (<= 100 MB) or when explicitly marked.
if !marked_pinned && size > 100 * MB {
return Bytes::from_bytes_vec(vec![0; size]);
}
self.reserve_pinned(size, origin)
.unwrap_or_else(|| Bytes::from_bytes_vec(vec![0; size]))
}
pub fn gc<T: Send + 'static>(&mut self, to_drop: T) {
let fence = Fence::new(self.streams.current().sys);
self.streams.gc(GcTask::new(to_drop, fence));
}
fn reserve_pinned(&mut self, size: usize, origin: Option<StreamId>) -> Option<Bytes> {
let stream = match origin {
Some(id) => self.streams.get(&id),
None => self.streams.current(),
};
let handle = stream.memory_management_cpu.reserve(size as u64).ok()?;
let binding = MemoryHandle::binding(handle);
let resource = stream
.memory_management_cpu
.get_resource(binding.clone(), None, None)
.ok_or(IoError::InvalidHandle {
backtrace: BackTrace::capture(),
})
.ok()?;
let controller = Box::new(PinnedMemoryManagedAllocController::init(binding, resource));
// SAFETY: The binding has initialized memory for at least `size` bytes.
Some(unsafe { Bytes::from_controller(controller, size) })
}
/// Asynchronously reads data from GPU memory to host memory based on the provided copy descriptors.
///
/// # Parameters
///
/// * `descriptors` - A vector of descriptors specifying the source GPU memory and its layout.
///
/// # Returns
///
/// * A `Future` resolving to:
/// * `Ok(Vec<Bytes>)` - The data read from the GPU as a vector of byte arrays.
/// * `Err(IoError)` - If the read operation fails.
pub fn read_async(
&mut self,
descriptors: Vec<CopyDescriptor<'_>>,
) -> impl Future<Output = Result<Vec<Bytes>, IoError>> + Send + use<> {
let descriptors_moved = descriptors
.iter()
.map(|b| b.binding.clone())
.collect::<Vec<_>>();
let result = self.copies_to_bytes(descriptors, true);
let fence = Fence::new(self.streams.current().sys);
async move {
let sync = fence.wait_sync();
// Release memory handle.
core::mem::drop(descriptors_moved);
sync?;
result
}
}
#[allow(unused)]
/// TODO: Read data using the origin stream where the data was allocated.
pub fn read_async_origin(
&mut self,
descriptors: Vec<CopyDescriptor<'_>>,
) -> impl Future<Output = Result<Vec<Bytes>, IoError>> + Send + use<> {
let results = self.copies_to_bytes_origin(descriptors, true);
async move {
let (bytes, fences) = results?;
for fence in fences {
fence.wait_sync();
}
Ok(bytes)
}
}
fn copies_to_bytes(
&mut self,
descriptors: Vec<CopyDescriptor<'_>>,
pinned: bool,
) -> Result<Vec<Bytes>, IoError> {
let mut result = Vec::with_capacity(descriptors.len());
for descriptor in descriptors {
result.push(self.copy_to_bytes(descriptor, pinned, None)?);
}
Ok(result)
}
fn copies_to_bytes_origin(
&mut self,
descriptors: Vec<CopyDescriptor<'_>>,
pinned: bool,
) -> Result<(Vec<Bytes>, Vec<Fence>), IoError> {
let mut data = Vec::with_capacity(descriptors.len());
let mut fences = Vec::with_capacity(descriptors.len());
let mut fenced = Vec::with_capacity(descriptors.len());
for descriptor in descriptors {
let stream = descriptor.binding.stream;
let bytes = self.copy_to_bytes(descriptor, pinned, Some(stream))?;
if !fenced.contains(&stream) {
let fence = Fence::new(self.streams.get(&stream).sys);
fenced.push(stream);
fences.push(fence);
}
data.push(bytes);
}
Ok((data, fences))
}
fn copy_to_bytes(
&mut self,
descriptor: CopyDescriptor<'_>,
pinned: bool,
stream_id: Option<StreamId>,
) -> Result<Bytes, IoError> {
let num_bytes = descriptor.shape.iter().product::<usize>() * descriptor.elem_size;
let mut bytes = self.reserve_cpu(num_bytes, pinned, stream_id);
self.write_to_cpu(descriptor, &mut bytes, stream_id)?;
Ok(bytes)
}
/// Writes data to the host from the GPU memory as specified by the copy descriptor.
///
/// # Parameters
///
/// * `descriptor` - Describes the source GPU memory, its shape, strides, and element size.
/// * `bytes` - The host bytes to write from the GPU.
///
/// # Returns
///
/// * `Ok(())` - If the write operation succeeds.
/// * `Err(IoError)` - If the strides are invalid or the resource cannot be accessed.
pub fn write_to_cpu(
&mut self,
descriptor: CopyDescriptor,
bytes: &mut Bytes,
stream_id: Option<StreamId>,
) -> Result<(), IoError> {
let CopyDescriptor {
binding,
shape,
strides,
elem_size,
} = descriptor;
if !has_pitched_row_major_strides(shape, strides) {
return Err(IoError::UnsupportedStrides {
backtrace: BackTrace::capture(),
});
}
let resource = self.resource(binding)?;
let stream = match stream_id {
Some(id) => self.streams.get(&id),
None => self.streams.current(),
};
unsafe { write_to_cpu(shape, strides, elem_size, bytes, resource.ptr, stream.sys) }
}
/// Writes data from the host to GPU memory as specified by the copy descriptor.
///
/// # Parameters
///
/// * `descriptor` - Describes the destination GPU memory, its shape, strides, and element size.
/// * `data` - The host data to write to the GPU.
///
/// # Returns
///
/// * `Ok(())` - If the write operation succeeds.
/// * `Err(IoError)` - If the strides are invalid or the resource cannot be accessed.
pub fn write_to_gpu(
&mut self,
descriptor: CopyDescriptor,
bytes: &Bytes,
) -> Result<(), IoError> {
let CopyDescriptor {
binding,
shape,
strides,
elem_size,
} = descriptor;
if !has_pitched_row_major_strides(shape, strides) {
return Err(IoError::UnsupportedStrides {
backtrace: BackTrace::capture(),
});
}
let resource = self.resource(binding)?;
let current = self.streams.current();
unsafe {
write_to_gpu(resource, shape, strides, elem_size, bytes, current.sys)?;
};
Ok(())
}
/// Allocates a new GPU memory buffer and immediately copies contiguous host data into it.
///
/// # Parameters
///
/// * `data` - The host data to copy to the GPU.
///
/// # Returns
///
/// * `Ok(Handle)` - A handle to the newly allocated and populated GPU memory.
/// * `Err(IoError)` - If the allocation or data copy fails.
pub fn create_with_data(&mut self, data: &[u8]) -> Result<Handle, IoError> {
let handle = self.reserve(data.len() as u64)?;
let shape = [data.len()];
let desc = CopyDescriptor::new(handle.clone().binding(), &shape, &[1], 1);
let shape1 = desc.shape;
let strides = desc.strides;
if !has_pitched_row_major_strides(shape1, strides) {
return Err(IoError::UnsupportedStrides {
backtrace: BackTrace::capture(),
});
}
let resource = self.resource(desc.binding)?;
let current = self.streams.current();
unsafe {
write_to_gpu(
resource,
desc.shape,
desc.strides,
desc.elem_size,
data,
current.sys,
)?;
}
Ok(handle)
}
/// Synchronizes the current stream, ensuring all pending operations are complete.
///
/// # Returns
///
/// * A `DynFut<()>` future that resolves when the stream is synchronized.
pub fn sync(&mut self) -> DynFut<Result<(), ExecutionError>> {
let fence = Fence::new(self.streams.current().sys);
Box::pin(async { fence.wait_sync() })
}
/// Executes a registered CUDA kernel with the specified parameters.
///
/// # Parameters
///
/// * `kernel_id` - The identifier of the kernel to execute.
/// * `kernel` - The cube task to compile if not cached.
/// * `mode` - The execution mode for the current kernel.
/// * `dispatch_count` - The number of thread blocks in the x, y, and z dimensions.
/// * `resources` - GPU resources (e.g., buffers) used by the kernel.
/// * `logger` - The logger to use to write compilation & runtime info.
///
/// # Panics
///
/// * If the execution fails, with an error message or profiling error.
pub fn kernel(
&mut self,
kernel_id: KernelId,
kernel: Box<dyn CubeTask<HipCompiler>>,
mode: ExecutionMode,
dispatch_count: (u32, u32, u32),
resources: &[GpuResource],
logger: Arc<ServerLogger>,
) -> Result<(), CompilationError> {
if !self.ctx.module_names.contains_key(&kernel_id) {
self.ctx.compile_kernel(&kernel_id, kernel, mode, logger)?;
}
let stream = self.streams.current();
let result = self
.ctx
.execute_task(stream, kernel_id, dispatch_count, resources);
if let Err(err) = result {
match self.ctx.timestamps.is_empty() {
true => panic!("{err:?}"),
false => self.ctx.timestamps.error(ProfileError::Unknown {
reason: format!("{err:?}"),
backtrace: BackTrace::capture(),
}),
}
};
Ok(())
}
}
pub(crate) unsafe fn write_to_cpu(
shape: &[usize],
strides: &[usize],
elem_size: usize,
bytes: &mut Bytes,
resource_ptr: *mut c_void,
stream: *mut ihipStream_t,
) -> Result<(), IoError> {
let rank = shape.len();
if rank <= 1 {
let status = unsafe {
cubecl_hip_sys::hipMemcpyDtoHAsync(
bytes.as_mut_ptr() as *mut _,
resource_ptr,
bytes.len(),
stream,
)
};
if status != HIP_SUCCESS {
return Err(IoError::Unknown {
description: format!("HIP memcpy failed: {status}"),
backtrace: BackTrace::capture(),
});
}
return Ok(());
}
let dim_x = shape[rank - 1];
let width_bytes = dim_x * elem_size;
let dim_y: usize = shape.iter().rev().skip(1).product();
let pitch = strides[rank - 2] * elem_size;
unsafe {
let status = cubecl_hip_sys::hipMemcpy2DAsync(
bytes.as_mut_ptr() as *mut _,
width_bytes,
resource_ptr,
pitch,
width_bytes,
dim_y,
hipMemcpyKind_hipMemcpyDeviceToHost,
stream,
);
// Fallback, sometimes the copy doesn't work.
if status != HIP_SUCCESS {
let status = cubecl_hip_sys::hipMemcpyDtoHAsync(
bytes.as_mut_ptr() as *mut _,
resource_ptr,
bytes.len(),
stream,
);
assert_eq!(status, HIP_SUCCESS, "Should send data to device");
}
}
Ok(())
}
unsafe fn write_to_gpu(
resource: GpuResource,
shape: &[usize],
strides: &[usize],
elem_size: usize,
data: &[u8],
stream: *mut ihipStream_t,
) -> Result<(), IoError> {
let rank = shape.len();
if !has_pitched_row_major_strides(shape, strides) {
return Err(IoError::UnsupportedStrides {
backtrace: BackTrace::capture(),
});
}
if rank > 1 {
let stride = strides[rank - 2];
let width = *shape.last().unwrap_or(&1);
let height: usize = shape.iter().rev().skip(1).product();
let width_bytes = width * elem_size;
let stride_bytes = stride * elem_size;
unsafe {
let status = cubecl_hip_sys::hipMemcpy2DAsync(
resource.ptr,
stride_bytes,
data as *const _ as *mut _,
width_bytes,
width_bytes,
height.max(1),
hipMemcpyKind_hipMemcpyHostToDevice,
stream,
);
assert_eq!(status, HIP_SUCCESS, "Should send data to device");
}
} else {
unsafe {
let status = cubecl_hip_sys::hipMemcpyHtoDAsync(
resource.ptr,
data as *const _ as *mut _,
data.len(),
stream,
);
assert_eq!(status, HIP_SUCCESS, "Should send data to device");
}
};
Ok(())
}