aboutsummaryrefslogtreecommitdiffhomepage
path: root/zluda/src/impl/pointer.rs
blob: 1eef540e8ba8a97fc66e1e95d8e071b3f816e2a9 (plain)
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
use cuda_types::*;
use hip_runtime_sys::*;
use std::{ffi::c_void, ptr};

pub(crate) unsafe fn get_attribute(
    data: *mut c_void,
    attribute: hipPointer_attribute,
    ptr: hipDeviceptr_t,
) -> CUresult {
    if data == ptr::null_mut() {
        return CUresult::ERROR_INVALID_VALUE;
    }
    // TODO: implement by getting device ordinal & allocation start,
    // then go through every context for that device
    if attribute == hipPointer_attribute::HIP_POINTER_ATTRIBUTE_CONTEXT {
        return CUresult::ERROR_NOT_SUPPORTED;
    }
    if attribute == hipPointer_attribute::HIP_POINTER_ATTRIBUTE_MEMORY_TYPE {
        let mut hip_result = hipMemoryType(0);
        hipPointerGetAttribute(
            (&mut hip_result as *mut hipMemoryType).cast::<c_void>(),
            attribute,
            ptr,
        )?;
        let cuda_result = memory_type(hip_result)?;
        *(data as _) = cuda_result;
    } else {
        hipPointerGetAttribute(data, attribute, ptr)?;
    }
    Ok(())
}

fn memory_type(cu: hipMemoryType) -> Result<CUmemorytype, hipErrorCode_t> {
    match cu {
        hipMemoryType::hipMemoryTypeHost => Ok(CUmemorytype::CU_MEMORYTYPE_HOST),
        hipMemoryType::hipMemoryTypeDevice => Ok(CUmemorytype::CU_MEMORYTYPE_DEVICE),
        hipMemoryType::hipMemoryTypeArray => Ok(CUmemorytype::CU_MEMORYTYPE_ARRAY),
        hipMemoryType::hipMemoryTypeUnified => Ok(CUmemorytype::CU_MEMORYTYPE_UNIFIED),
        _ => Err(hipErrorCode_t::hipErrorInvalidValue),
    }
}