Skip to content

Commit

Permalink
fix: better CUDA detection
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Dec 19, 2024
1 parent 612e4b4 commit 3c409f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions crates/cudart-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name = "era_cudart_sys"
description = "Raw CUDA bindings for ZKsync"

[dependencies]
serde_json = "1.0"
regex-lite = "0.1"

[build-dependencies]
serde_json = "1.0"
regex-lite = "0.1"
33 changes: 24 additions & 9 deletions crates/cudart-sys/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use std::path::{Path, PathBuf};
pub fn get_cuda_path() -> Option<&'static Path> {
#[cfg(target_os = "linux")]
{
let path = Path::new("/usr/local/cuda");
if path.exists() {
Some(path)
} else {
None
for path_name in [option_env!("CUDA_PATH"), Some("/usr/local/cuda")].iter().flatten() {
println!("trying {path_name}...");
let path = Path::new(path_name);
if path.exists() {
println!("CUDA installation found at `{}`", path.display());
return Some(path)
}
}
None
}
#[cfg(target_os = "windows")]
{
Expand Down Expand Up @@ -42,12 +45,24 @@ pub fn get_cuda_lib_path() -> Option<PathBuf> {

pub fn get_cuda_version() -> Option<String> {
if let Some(version) = option_env!("CUDA_VERSION") {
println!("CUDA version defined in CUDA_VERSION as `{}`", version);
Some(version.to_string())
} else if let Some(path) = get_cuda_path() {
let file = File::open(path.join("version.json")).expect("CUDA Toolkit should be installed");
let reader = std::io::BufReader::new(file);
let value: serde_json::Value = serde_json::from_reader(reader).unwrap();
Some(value["cuda"]["version"].as_str().unwrap().to_string())
println!("inferring CUDA version from nvcc output...");
let re = regex_lite::Regex::new(r"V(?<version>\d{2}\.\d+\.\d+)").unwrap();
let nvcc_out = std::process::Command::new("nvcc")
.arg("--version")
.output()
.expect("failed to start `nvcc`");
let nvcc_str = std::str::from_utf8(&nvcc_out.stdout).expect("`nvcc` output is not UTF8");
let captures = re.captures(&nvcc_str).unwrap();
let version = captures
.get(0)
.expect("unable to find nvcc version in the form VMM.mm.pp in the output of `nvcc --version`:\n{nvcc_str}")
.as_str()
.to_string();
println!("CUDA version inferred to be `{version}`.");
Some(version)
} else {
None
}
Expand Down

0 comments on commit 3c409f7

Please sign in to comment.