#[cfg(feature = "cuda")]
use trueno_gpu::driver::CudaContext;
#[cfg(feature = "cuda")]
use trueno_gpu::memory::resident::{
reset_transfer_counters, total_d2h_transfers, total_h2d_transfers, GpuResidentTensor,
};
#[test]
#[cfg(feature = "cuda")]
fn test_gpu_tensor_created_on_device() {
let ctx = match CudaContext::new(0) {
Ok(ctx) => ctx,
Err(_) => return, };
reset_transfer_counters();
let data = vec![1.0f32, 2.0, 3.0, 4.0];
let tensor = GpuResidentTensor::from_host(&ctx, &data).expect("Upload failed");
assert!(tensor.is_device_resident());
assert_eq!(tensor.host_to_device_transfers(), 1);
assert_eq!(tensor.device_to_host_transfers(), 0);
assert_eq!(total_h2d_transfers(), 1);
assert_eq!(total_d2h_transfers(), 0);
}
#[test]
#[cfg(not(feature = "cuda"))]
fn test_gpu_tensor_created_on_device() {
}
#[test]
#[cfg(feature = "cuda")]
fn test_gpu_to_host_transfers() {
let ctx = match CudaContext::new(0) {
Ok(ctx) => ctx,
Err(_) => return, };
reset_transfer_counters();
let data = vec![1.0f32, 2.0, 3.0, 4.0];
let mut tensor = GpuResidentTensor::from_host(&ctx, &data).expect("Upload failed");
assert_eq!(tensor.device_to_host_transfers(), 0);
let result = tensor.to_host().expect("Download failed");
assert_eq!(result, data);
assert_eq!(tensor.device_to_host_transfers(), 1);
assert_eq!(total_d2h_transfers(), 1);
}
#[test]
#[cfg(not(feature = "cuda"))]
fn test_gpu_to_host_transfers() {}
#[test]
#[cfg(feature = "cuda")]
fn test_gpu_operations_stay_on_device() {
let ctx = match CudaContext::new(0) {
Ok(ctx) => ctx,
Err(_) => return, };
reset_transfer_counters();
let a = GpuResidentTensor::from_host(&ctx, &vec![1.0f32; 64]).expect("Upload A");
let b = GpuResidentTensor::from_host(&ctx, &vec![2.0f32; 64]).expect("Upload B");
assert_eq!(total_h2d_transfers(), 2);
assert_eq!(total_d2h_transfers(), 0);
let c = a.add(&ctx, &b).expect("Add failed");
assert!(c.is_device_resident());
assert_eq!(c.host_to_device_transfers(), 0); assert_eq!(c.device_to_host_transfers(), 0);
assert_eq!(total_h2d_transfers(), 2);
assert_eq!(total_d2h_transfers(), 0);
}
#[test]
#[cfg(not(feature = "cuda"))]
fn test_gpu_operations_stay_on_device() {}
#[test]
#[ignore = "TDD: Implementation pending - operation chaining not yet implemented"]
fn test_operation_chain_no_intermediate_transfers() {
}