Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
fix no_std and nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kobigurk committed Nov 7, 2020
1 parent 44ac6d9 commit 0e5f2c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion algebra-core/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ mod test {
fn test_macro_empty() {
let array: Vec<u8> = vec![];
let bytes: Vec<u8> = to_bytes![array].unwrap();
assert_eq!(&bytes, &[]);
assert_eq!(bytes, Vec::<u8>::new());
assert_eq!(bytes.len(), 0);
}

Expand Down
43 changes: 29 additions & 14 deletions algebra-core/src/curves/cuda/scalar_mul/cpu_gpu_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#[macro_export]
macro_rules! impl_gpu_cpu_run_kernel {
() => {
fn init_gpu_cache_dir() -> Result<std::path::PathBuf, crate::CudaScalarMulError> {
#[allow(unused_qualifications)]
fn init_gpu_cache_dir() -> Result<crate::String, crate::CudaScalarMulError> {
#[cfg(feature = "cuda")]
{
let dir = dirs::cache_dir()
Expand All @@ -11,33 +12,47 @@ macro_rules! impl_gpu_cpu_run_kernel {
.join("cuda-scalar-mul-profiler")
.join(P::namespace());
std::fs::create_dir_all(&dir)?;
Ok(dir)
Ok(dir.to_str().to_string())
}
#[cfg(not(feature = "cuda"))]
Err(crate::CudaScalarMulError::CudaDisabledError)
}

fn read_profile_data() -> Result<String, crate::CudaScalarMulError> {
let dir = Self::init_gpu_cache_dir()?;
let data = std::fs::read_to_string(&dir.join("profile_data.txt"))?;
Ok(data)
#[allow(unused_qualifications)]
fn read_profile_data() -> Result<crate::String, crate::CudaScalarMulError> {
#[cfg(feature = "cuda")]
{
let dir = Self::init_gpu_cache_dir()?;
let data = std::fs::read_to_string(&dir.join("profile_data.txt"))?;
Ok(data)
}
#[cfg(not(feature = "cuda"))]
Err(crate::CudaScalarMulError::CudaDisabledError)
}

fn clear_gpu_profiling_data() -> Result<(), crate::CudaScalarMulError> {
#[cfg(feature = "cuda")]
{
let dir = Self::init_gpu_cache_dir()?;
let dir = std::path::PathBuf::from(Self::init_gpu_cache_dir()?);
std::fs::File::create(&dir.join("profile_data.txt"))?;
Ok(())
}
Ok(())
#[cfg(not(feature = "cuda"))]
Err(crate::CudaScalarMulError::CudaDisabledError)
}

#[allow(unused_variables)]
fn write_profile_data(profile_data: &str) -> Result<(), crate::CudaScalarMulError> {
let dir = Self::init_gpu_cache_dir()?;
let mut file = std::fs::File::create(&dir.join("profile_data.txt"))?;
file.write_all(profile_data.as_bytes())?;
file.sync_all()?;
Ok(())
#[cfg(feature = "cuda")]
{
let dir = std::path::PathBuf::from(Self::init_gpu_cache_dir()?);
let mut file = std::fs::File::create(&dir.join("profile_data.txt"))?;
file.write_all(profile_data.as_bytes())?;
file.sync_all()?;
Ok(())
}
#[cfg(not(feature = "cuda"))]
Err(crate::CudaScalarMulError::CudaDisabledError)
}

/// We split up the job statically between the CPU and GPUs
Expand Down Expand Up @@ -67,7 +82,7 @@ macro_rules! impl_gpu_cpu_run_kernel {

let _now = timer!();
// Get data for proportion of total throughput achieved by each device
let dir = Self::init_gpu_cache_dir()?;
let _ = Self::init_gpu_cache_dir()?;

let arc_mutex = P::scalar_mul_static_profiler();
let mut profile_data = arc_mutex.lock().unwrap();
Expand Down
13 changes: 8 additions & 5 deletions algebra-core/src/curves/cuda/scalar_mul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub const MAX_GROUP_ELEM_BYTES: usize = 400;
#[derive(Debug)]
pub enum CudaScalarMulError {
CudaDisabledError,
IoError(std::io::Error),
IoError,
KernelFailedError,
ProfilingSerializationError,
ProfilingDeserializationError,
Expand All @@ -46,17 +46,18 @@ impl std::error::Error for CudaScalarMulError {
}
}

#[cfg(feature = "std")]
impl From<std::io::Error> for CudaScalarMulError {
fn from(e: std::io::Error) -> Self {
CudaScalarMulError::IoError(e)
fn from(_: std::io::Error) -> Self {
CudaScalarMulError::IoError
}
}

impl fmt::Display for CudaScalarMulError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
CudaScalarMulError::CudaDisabledError => write!(f, "CUDA is disabled"),
CudaScalarMulError::IoError(e) => write!(f, "Got IO error: {}", e),
CudaScalarMulError::IoError=> write!(f, "IO error"),
CudaScalarMulError::KernelFailedError => write!(f, "Failed running kernel"),
CudaScalarMulError::ProfilingSerializationError => {
write!(f, "Failed serlializing profiling data")
Expand Down Expand Up @@ -127,6 +128,8 @@ pub(crate) mod internal {

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
use alloc::string::String;

use crate::{curves::AffineCurve, fields::PrimeField, CudaScalarMulError};

Expand All @@ -141,7 +144,7 @@ pub(crate) mod internal {

fn num_u8() -> usize;

fn init_gpu_cache_dir() -> Result<std::path::PathBuf, CudaScalarMulError>;
fn init_gpu_cache_dir() -> Result<String, CudaScalarMulError>;
fn read_profile_data() -> Result<String, CudaScalarMulError>;
fn write_profile_data(profile_data: &str) -> Result<(), CudaScalarMulError>;
fn clear_gpu_profiling_data() -> Result<(), CudaScalarMulError>;
Expand Down

0 comments on commit 0e5f2c4

Please sign in to comment.