Skip to content
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Nicolas Patry <patry.nicolas@protonmail.com>"]
name = "bindgen_cuda"
version = "0.1.5"
version = "0.1.7"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version = "0.1.7"
version = "0.1.5"

We can increment the version in a separate PR when creating a new release.

edition = "2021"
description = """
Bindgen like interface to build cuda kernels to interact with within Rust.
Expand Down
51 changes: 34 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ impl Builder {
/// let builder = bindgen_cuda::Builder::default().build_lib("libflash.a");
/// println!("cargo:rustc-link-lib=flash");
/// ```
pub fn build_lib<P>(self, out_file: P)
pub fn build_lib<P>(&self, out_file: P)
where
P: Into<PathBuf>,
{
let out_file = out_file.into();
let compute_cap = self.compute_cap.expect("Failed to get compute_cap");
let out_dir = self.out_dir;
let out_dir = self.out_dir.clone();
for path in &self.watch {
println!("cargo:rerun-if-changed={}", path.display());
}
Expand Down Expand Up @@ -237,11 +237,16 @@ impl Builder {
true
};
let ccbin_env = std::env::var("NVCC_CCBIN");
let nvcc_binary = if std::path::Path::new("/usr/local/cuda/bin/nvcc").exists() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could create a utility ala get_nvcc_binary and avoid the repetition

"/usr/local/cuda/bin/nvcc"
} else {
"nvcc"
};
if should_compile {
cu_files
.par_iter()
.map(|(cu_file, obj_file)| {
let mut command = std::process::Command::new("nvcc");
let mut command = std::process::Command::new(nvcc_binary);
command
.arg(format!("--gpu-architecture=sm_{compute_cap}"))
.arg("-c")
Expand Down Expand Up @@ -270,7 +275,7 @@ impl Builder {
})
.collect::<Result<(), std::io::Error>>().expect("compile files correctly");
let obj_files = cu_files.iter().map(|c| c.1.clone()).collect::<Vec<_>>();
let mut command = std::process::Command::new("nvcc");
let mut command = std::process::Command::new(nvcc_binary);
command
.arg("--lib")
.args([
Expand Down Expand Up @@ -302,17 +307,20 @@ impl Builder {
/// let bindings = bindgen_cuda::Builder::default().build_ptx().unwrap();
/// bindings.write("src/lib.rs").unwrap();
/// ```
pub fn build_ptx(self) -> Result<Bindings, Error> {
let cuda_root = self.cuda_root.expect("Could not find CUDA in standard locations, set it manually using Builder().set_cuda_root(...)");
pub fn build_ptx(&self) -> Result<Bindings, Error> {
let mut cuda_include_dir = PathBuf::from("/usr/local/cuda/include");
if let Some(cuda_root) = &self.cuda_root {
cuda_include_dir = cuda_root.join("include");
println!(
"cargo:rustc-env=CUDA_INCLUDE_DIR={}",
cuda_include_dir.display()
);
};
let compute_cap = self.compute_cap.expect("Could not find compute_cap");
let cuda_include_dir = cuda_root.join("include");
println!(
"cargo:rustc-env=CUDA_INCLUDE_DIR={}",
cuda_include_dir.display()
);
let out_dir = self.out_dir;

let mut include_paths = self.include_paths;
let out_dir = self.out_dir.clone();

let mut include_paths = self.include_paths.clone();
for path in &mut include_paths {
println!("cargo:rerun-if-changed={}", path.display());
let destination =
Expand All @@ -338,6 +346,11 @@ impl Builder {
include_options.push(format!("-I{}", cuda_include_dir.display()));

let ccbin_env = std::env::var("NVCC_CCBIN");
let nvcc_binary = if std::path::Path::new("/usr/local/cuda/bin/nvcc").exists() {
"/usr/local/cuda/bin/nvcc"
} else {
"nvcc"
};
println!("cargo:rerun-if-env-changed=NVCC_CCBIN");
for path in &self.watch {
println!("cargo:rerun-if-changed={}", path.display());
Expand All @@ -360,7 +373,7 @@ impl Builder {
if ignore {
None
} else {
let mut command = std::process::Command::new("nvcc");
let mut command = std::process::Command::new(nvcc_binary);
command.arg(format!("--gpu-architecture=sm_{compute_cap}"))
.arg("--ptx")
.args(["--default-stream", "per-thread"])
Expand Down Expand Up @@ -397,7 +410,7 @@ impl Builder {
}
Ok(Bindings {
write,
paths: self.kernel_paths,
paths: self.kernel_paths.clone(),
})
}
}
Expand Down Expand Up @@ -498,10 +511,14 @@ fn compute_cap() -> Result<usize, Error> {
println!("cargo:rustc-env=CUDA_COMPUTE_CAP={cap}");
cap
};

let nvcc_binary = if std::path::Path::new("/usr/local/cuda/bin/nvcc").exists() {
"/usr/local/cuda/bin/nvcc"
} else {
"nvcc"
};
// Grab available GPU codes from nvcc and select the highest one
let (supported_nvcc_codes, max_nvcc_code) = {
let out = std::process::Command::new("nvcc")
let out = std::process::Command::new(nvcc_binary)
.arg("--list-gpu-code")
.output()
.expect("`nvcc` failed. Ensure that you have CUDA installed and that `nvcc` is in your PATH.");
Expand Down