diff options
author | Andrzej Janik <[email protected]> | 2021-02-28 00:46:50 +0100 |
---|---|---|
committer | Andrzej Janik <[email protected]> | 2021-02-28 00:46:50 +0100 |
commit | b7ee6d66c3cfac9922addc6c362ae437cfa8fee5 (patch) | |
tree | 9666c7d357e1d15ed9c516a673074fd64d4abfc3 /zluda_ml/src/impl.rs | |
parent | 871b8d1befab63a1cd7f25a9bb26c9ecaebb1cd1 (diff) | |
download | ZLUDA-b7ee6d66c3cfac9922addc6c362ae437cfa8fee5.tar.gz ZLUDA-b7ee6d66c3cfac9922addc6c362ae437cfa8fee5.zip |
Implement enough nvml to make GeekBench happy
Diffstat (limited to 'zluda_ml/src/impl.rs')
-rw-r--r-- | zluda_ml/src/impl.rs | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/zluda_ml/src/impl.rs b/zluda_ml/src/impl.rs new file mode 100644 index 0000000..0fd0de1 --- /dev/null +++ b/zluda_ml/src/impl.rs @@ -0,0 +1,138 @@ +use level_zero as l0;
+use std::{io::Write, ops::Add};
+use std::{
+ os::raw::{c_char, c_uint},
+ ptr,
+};
+
+use crate::nvml::nvmlReturn_t;
+
+macro_rules! stringify_nmvlreturn_t {
+ ($x:ident => [ $($variant:ident),+ ]) => {
+ match $x {
+ $(
+ nvmlReturn_t::$variant => Some(concat!(stringify!($variant), "\0")),
+ )+
+ _ => None
+ }
+ }
+}
+
+#[cfg(debug_assertions)]
+pub(crate) fn unimplemented() -> nvmlReturn_t {
+ unimplemented!()
+}
+
+#[cfg(not(debug_assertions))]
+pub(crate) fn unimplemented() -> nvmlReturn_t {
+ nvmlReturn_t::NVML_ERROR_NOT_SUPPORTED
+}
+
+pub(crate) fn error_string(result: nvmlReturn_t) -> *const ::std::os::raw::c_char {
+ let text = stringify_nmvlreturn_t!(
+ result => [
+ NVML_SUCCESS,
+ NVML_ERROR_UNINITIALIZED,
+ NVML_ERROR_INVALID_ARGUMENT,
+ NVML_ERROR_NOT_SUPPORTED,
+ NVML_ERROR_NO_PERMISSION,
+ NVML_ERROR_ALREADY_INITIALIZED,
+ NVML_ERROR_NOT_FOUND,
+ NVML_ERROR_INSUFFICIENT_SIZE,
+ NVML_ERROR_INSUFFICIENT_POWER,
+ NVML_ERROR_DRIVER_NOT_LOADED,
+ NVML_ERROR_TIMEOUT,
+ NVML_ERROR_IRQ_ISSUE,
+ NVML_ERROR_LIBRARY_NOT_FOUND,
+ NVML_ERROR_FUNCTION_NOT_FOUND,
+ NVML_ERROR_CORRUPTED_INFOROM,
+ NVML_ERROR_GPU_IS_LOST,
+ NVML_ERROR_RESET_REQUIRED,
+ NVML_ERROR_OPERATING_SYSTEM,
+ NVML_ERROR_LIB_RM_VERSION_MISMATCH,
+ NVML_ERROR_IN_USE,
+ NVML_ERROR_MEMORY,
+ NVML_ERROR_NO_DATA,
+ NVML_ERROR_VGPU_ECC_NOT_SUPPORTED,
+ NVML_ERROR_INSUFFICIENT_RESOURCES,
+ NVML_ERROR_UNKNOWN
+ ]
+ );
+ match text {
+ Some(text) => text.as_ptr() as *const _,
+ None => ptr::null(),
+ }
+}
+
+pub(crate) fn shutdown() -> nvmlReturn_t {
+ nvmlReturn_t::NVML_SUCCESS
+}
+
+pub(crate) fn init_v2() -> Result<(), nvmlReturn_t> {
+ Ok(l0::init()?)
+}
+
+pub(crate) fn init_with_flags() -> Result<(), nvmlReturn_t> {
+ init_v2()
+}
+
+impl From<l0::sys::ze_result_t> for nvmlReturn_t {
+ fn from(l0_err: l0::sys::ze_result_t) -> Self {
+ match l0_err {
+ l0::sys::ze_result_t::ZE_RESULT_ERROR_UNINITIALIZED => {
+ nvmlReturn_t::NVML_ERROR_UNINITIALIZED
+ }
+ _ => nvmlReturn_t::NVML_ERROR_UNKNOWN,
+ }
+ }
+}
+
+impl From<Result<(), nvmlReturn_t>> for nvmlReturn_t {
+ fn from(result: Result<(), nvmlReturn_t>) -> Self {
+ match result {
+ Ok(()) => nvmlReturn_t::NVML_SUCCESS,
+ Err(e) => e,
+ }
+ }
+}
+
+struct CountingWriter<T: std::io::Write> {
+ pub base: T,
+ pub len: usize,
+}
+
+impl<T: std::io::Write> std::io::Write for CountingWriter<T> {
+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
+ self.len += buf.len();
+ self.base.write(buf)
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ self.base.flush()
+ }
+}
+
+pub(crate) fn system_get_driver_version(
+ version_ptr: *mut c_char,
+ length: c_uint,
+) -> Result<(), nvmlReturn_t> {
+ if version_ptr == ptr::null_mut() {
+ return Err(nvmlReturn_t::NVML_ERROR_INVALID_ARGUMENT);
+ }
+ let drivers = l0::Driver::get()?;
+ let output_slice =
+ unsafe { std::slice::from_raw_parts_mut(version_ptr as *mut u8, (length - 1) as usize) };
+ let mut output_write = CountingWriter {
+ base: output_slice,
+ len: 0,
+ };
+ for d in drivers {
+ let props = d.get_properties()?;
+ let driver_version = props.driverVersion;
+ write!(&mut output_write, "{}", driver_version)
+ .map_err(|_| nvmlReturn_t::NVML_ERROR_UNKNOWN)?;
+ unsafe { *(version_ptr.add(output_write.len)) = 0 };
+ return Ok(());
+ }
+ Err(nvmlReturn_t::NVML_ERROR_UNKNOWN)
+}
|