use cuda_core::{ContextLimit, CudaContext};
const ALL: [ContextLimit; 7] = [
ContextLimit::StackSize,
ContextLimit::PrintfFifoSize,
ContextLimit::MallocHeapSize,
ContextLimit::DevRuntimeSyncDepth,
ContextLimit::DevRuntimePendingLaunchCount,
ContextLimit::MaxL2FetchGranularity,
ContextLimit::PersistingL2CacheSize,
];
const UNSUPPORTED: cuda_core::sys::CUresult =
cuda_core::sys::cudaError_enum_CUDA_ERROR_UNSUPPORTED_LIMIT;
#[test]
fn every_limit_reads_and_the_writable_ones_round_trip() {
let ctx = CudaContext::new(0).expect("failed to create CUDA context");
for limit in ALL {
match ctx.limit(limit) {
Ok(_) => {}
Err(e) if e.0 == UNSUPPORTED => {}
Err(e) => panic!("limit({limit:?}) failed with an unexpected status: {e:?}"),
}
}
let original = ctx
.stack_size()
.expect("stack_size() must be readable on any device");
let raised = original + 4096;
ctx.set_limit(ContextLimit::StackSize, raised)
.expect("set_limit(StackSize) failed");
assert_eq!(
ctx.limit(ContextLimit::StackSize).unwrap(),
raised,
"set_limit(StackSize, {raised}) must be observable through limit(StackSize)"
);
assert_eq!(
ctx.stack_size().unwrap(),
ctx.limit(ContextLimit::StackSize).unwrap(),
"stack_size() must agree with limit(StackSize)"
);
ctx.set_stack_size(original)
.expect("set_stack_size failed to restore the original");
assert_eq!(
ctx.stack_size().unwrap(),
original,
"set_stack_size() must agree with set_limit(StackSize, ..)"
);
let heap_before = ctx.limit(ContextLimit::MallocHeapSize).unwrap();
let fifo_before = ctx.limit(ContextLimit::PrintfFifoSize).unwrap();
let heap_target = heap_before + (1 << 20);
ctx.set_limit(ContextLimit::MallocHeapSize, heap_target)
.expect("set_limit(MallocHeapSize) failed before any kernel launch");
assert_eq!(
ctx.limit(ContextLimit::MallocHeapSize).unwrap(),
heap_target,
"the malloc heap must hold the size just written"
);
assert_eq!(
ctx.limit(ContextLimit::PrintfFifoSize).unwrap(),
fifo_before,
"writing MallocHeapSize must leave PrintfFifoSize untouched"
);
ctx.set_limit(ContextLimit::MallocHeapSize, heap_before)
.expect("failed to restore the malloc heap size");
}