Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better CUDA detection #61

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
31 changes: 22 additions & 9 deletions crates/cudart-sys/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ 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()
{
let path = Path::new(path_name);
if path.exists() {
return Some(path);
}
}
None
}
#[cfg(target_os = "windows")]
{
Expand Down Expand Up @@ -44,10 +48,19 @@ pub fn get_cuda_version() -> Option<String> {
if let Some(version) = option_env!("CUDA_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())
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();
Some(version)
} else {
None
}
Expand Down
Loading