Skip to content

Commit

Permalink
Merge pull request #51 from spacemeshos/use-c_char_in_provider_name
Browse files Browse the repository at this point in the history
Use c char in provider name
  • Loading branch information
poszu authored May 13, 2023
2 parents 957d91f + 0c7ffff commit c081bcf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 10 additions & 9 deletions ffi/src/initialization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::Debug;
use std::{ffi::c_char, fmt::Debug};

use post::{
initialize::{CpuInitializer, Initialize},
Expand Down Expand Up @@ -35,7 +35,7 @@ impl From<scrypt_ocl::ocl::Error> for InitializeResult {
#[repr(C)]
#[derive(Clone, PartialEq, Eq)]
pub struct Provider {
name: [u8; 64],
name: [c_char; 64],
id: u32,
class: DeviceClass,
}
Expand All @@ -53,10 +53,10 @@ pub enum DeviceClass {
impl Debug for Provider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Provider")
.field(
"name",
&std::ffi::CStr::from_bytes_until_nul(&self.name).unwrap(),
)
.field("name", unsafe {
// SAFETY: The name is always null terminated.
&std::ffi::CStr::from_ptr(self.name.as_ptr())
})
.finish()
}
}
Expand Down Expand Up @@ -88,6 +88,7 @@ pub extern "C" fn get_providers(out: *mut Provider, out_len: usize) -> Initializ
// Copy over the first out.name.len() - 1 bytes, and then add a null terminator.
let name = format!("{provider}")
.bytes()
.map(|b| b as c_char)
.take(out.name.len() - 1)
.chain(std::iter::once(0))
.collect::<Vec<_>>();
Expand All @@ -98,12 +99,12 @@ pub extern "C" fn get_providers(out: *mut Provider, out_len: usize) -> Initializ
}
if id < out.len() {
out[id] = Provider {
name: [0u8; 64],
name: [0; 64],
id: CPU_PROVIDER_ID,
class: DeviceClass::CPU,
};
let name = b"[CPU] scrypt-jane\0";
out[id].name[..name.len()].copy_from_slice(name);
out[id].name[..name.len()].copy_from_slice(&name.map(|b| b as c_char));
}

InitializeResult::InitializeOk
Expand Down Expand Up @@ -318,7 +319,7 @@ mod tests {
let count = super::get_providers_count();
let mut providers = vec![
super::Provider {
name: [0u8; 64],
name: [0; 64],
id: 0,
class: super::DeviceClass::CPU
};
Expand Down
4 changes: 2 additions & 2 deletions initializer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ fn initialize(args: InitializeArgs) -> eyre::Result<()> {
let elapsed = now.elapsed();
let labels_initialized = args.labels_per_unit * args.units;
println!(
"Initializing {labels_initialized} labels took {} seconds. Speed: {:.0} labels/sec ({:.2} MB/sec, vrf_nonce: {:?})",
elapsed.as_secs(),
"Initializing {labels_initialized} labels took {:.2} seconds. Speed: {:.0} labels/sec ({:.2} MB/sec), vrf_nonce: {:?}",
elapsed.as_secs_f64(),
labels_initialized as f64 / elapsed.as_secs_f64(),
labels_initialized as f64 * 16.0 / elapsed.as_secs_f64() / 1024.0 / 1024.0,
metadata.nonce,
Expand Down

0 comments on commit c081bcf

Please sign in to comment.