forked from vortex-data/vortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
73 lines (60 loc) · 2.04 KB
/
Copy pathbuild.rs
File metadata and controls
73 lines (60 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};
use vortex_gpu_kernels::generate_unpack;
use walkdir::WalkDir;
fn main() -> anyhow::Result<()> {
let project_name = "vortex-gpu-kernels";
let manifest_dir =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("Failed to get manifest dir"));
let kernels_dir = manifest_dir.join("kernels");
let generator_dir = manifest_dir.parent().unwrap().join(project_name);
fs::create_dir_all(&kernels_dir)?;
// Generate for all bit widths and both features
generate_unpack::<u8>(&kernels_dir, 32)?;
generate_unpack::<u16>(&kernels_dir, 32)?;
generate_unpack::<u32>(&kernels_dir, 32)?;
generate_unpack::<u64>(&kernels_dir, 16)?;
if !has_nvcc() {
// Don't run cuda compilation if nvcc is not available.
return Ok(());
}
println!("cargo:rerun-if-changed={}", generator_dir.to_str().unwrap());
for entry in WalkDir::new(&kernels_dir).into_iter().flatten() {
if entry.path().extension().is_some_and(|ext| ext == "cu") {
println!("cargo:rerun-if-changed={}", entry.path().display());
nvcc_compile_ptx(kernels_dir.as_path(), entry.path())?;
}
}
Ok(())
}
fn nvcc_compile_ptx(kernel_dir: &Path, cu_path: &Path) -> anyhow::Result<()> {
let res = Command::new("nvcc")
.arg("-arch=sm_80")
.arg("--restrict")
.arg("--ptx")
.arg("--include-path")
.arg(kernel_dir)
.arg("-c")
.arg(cu_path)
.arg("-o")
.arg(cu_path.with_extension("ptx"))
.output()?;
assert!(
res.status.success(),
"Failed to compile {}: {}",
cu_path.display(),
str::from_utf8(&res.stderr)?
);
Ok(())
}
fn has_nvcc() -> bool {
Command::new("nvcc")
.arg("--version")
.output()
.is_ok_and(|o| o.status.success())
}