From 4f6be5efb91bb8d09289489c18b225605ae65f7c Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Fri, 22 Jan 2021 14:08:14 +0400 Subject: [PATCH] Add available_providers method --- examples/info.rs | 2 ++ src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/examples/info.rs b/examples/info.rs index 56415c5..578ffae 100644 --- a/examples/info.rs +++ b/examples/info.rs @@ -25,6 +25,8 @@ fn main() -> Result<()> { let so = SessionOptions::new()?; + println!("Available Providers: {:?}", so.available_providers()); + for path in &opt.onnx { println!("model {:?}", path); let session = match Session::new(&env, path, &so) { diff --git a/src/lib.rs b/src/lib.rs index a917470..cbafd1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,6 +160,24 @@ impl SessionOptions { let raw = call!(@unsafe @ptr CreateSessionOptions)?; Ok(SessionOptions { raw }) } + + pub fn available_providers(&self) -> Vec { + let mut providers_array: *mut *mut i8 = std::ptr::null_mut(); + let mut providers_len: i32 = 0; + + call!(@unsafe @expect GetAvailableProviders, &mut providers_array, &mut providers_len); + + let slice = unsafe { std::slice::from_raw_parts(providers_array, providers_len as usize) }; + + let mut res = Vec::new(); + for i in slice { + res.push(unsafe {CStr::from_ptr(*i)}.to_string_lossy().to_string()); + } + + call!(@unsafe @expect ReleaseAvailableProviders, providers_array, providers_len); + + res + } pub fn add_cpu(&self, use_arena: bool) { let so = self.raw;