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

Using --gpu-architecture native with nvcc #474

Merged
merged 4 commits into from
Feb 22, 2023
Merged
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
57 changes: 37 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
mod cuda {
pub fn build_ptx() {
let out_dir = std::env::var("OUT_DIR").unwrap();
let mut kernel_paths: Vec<std::path::PathBuf> = glob::glob("src/**/*.cu")
let kernel_paths: Vec<std::path::PathBuf> = glob::glob("src/**/*.cu")
.unwrap()
.map(|p| p.unwrap())
.collect();
Expand All @@ -30,23 +30,21 @@ mod cuda {

for path in &mut include_directories {
println!("cargo:rerun-if-changed={}", path.display());
// remove the filename from the path so it's just the directory
path.pop();
}

include_directories.sort();
include_directories.dedup();

println!("cargo:warning=Found kernels {kernel_paths:?}");
println!("cargo:warning=Found include directories {include_directories:?}");

#[allow(unused)]
let include_options: Vec<String> = include_directories
.into_iter()
.map(|s| "-I".to_string() + &s.into_os_string().into_string().unwrap())
.collect::<Vec<_>>();

#[cfg(feature = "ci-check")]
for mut kernel_path in kernel_paths.drain(..) {
for mut kernel_path in kernel_paths.into_iter() {
kernel_path.set_extension("ptx");

let mut ptx_path: std::path::PathBuf = out_dir.clone().into();
Expand All @@ -55,21 +53,40 @@ mod cuda {
}

#[cfg(not(feature = "ci-check"))]
for kernel_path in kernel_paths.drain(..) {
println!("cargo:rerun-if-changed={}", kernel_path.display());

let output = std::process::Command::new("nvcc")
.args(["--gpu-architecture", "compute_60"])
.arg("--ptx")
.args(["--output-directory", &out_dir])
.args(&include_options)
.arg(&kernel_path)
.output()
.unwrap();

assert!(
output.status.success(),
"nvcc error while compiling {kernel_path:?}: {output:?}",
{
let start = std::time::Instant::now();

kernel_paths
.iter()
.for_each(|p| println!("cargo:rerun-if-changed={}", p.display()));

let children = kernel_paths
.iter()
.map(|p| {
std::process::Command::new("nvcc")
.args(["--gpu-architecture", "native"])
.arg("--ptx")
.args(["--default-stream", "per-thread"])
.args(["--output-directory", &out_dir])
.args(&include_options)
.arg(p)
.spawn()
.unwrap()
})
.collect::<Vec<_>>();

for (kernel_path, child) in kernel_paths.iter().zip(children.into_iter()) {
let output = child.wait_with_output().unwrap();
assert!(
output.status.success(),
"nvcc error while compiling {kernel_path:?}: {output:?}",
);
}

println!(
"cargo:warning=Compiled {:?} cuda kernels in {:?}",
kernel_paths.len(),
start.elapsed()
);
}
}
Expand Down