-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
70 lines (58 loc) · 2.17 KB
/
Copy pathbuild.rs
File metadata and controls
70 lines (58 loc) · 2.17 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
use std::{env, fs, path::PathBuf, process::Command};
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=cuda/");
if env::var("CARGO_FEATURE_CUDA").is_ok() {
compile_cuda_kernels();
}
if env::var("CARGO_FEATURE_AVX512").is_ok() {
println!("cargo:rustc-cfg=feature=\"avx512\"");
}
if env::var("CARGO_FEATURE_NEON").is_ok() {
println!("cargo:rustc-cfg=feature=\"neon\"");
}
}
fn compile_cuda_kernels() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let cuda_dir = PathBuf::from("cuda/kernels");
let cu_files: Vec<PathBuf> = fs::read_dir(&cuda_dir)
.expect("cuda/kernels directory not found")
.filter_map(|e| {
let path = e.ok()?.path();
if path.extension()?.to_str()? == "cu" {
Some(path)
} else {
None
}
})
.collect();
let mut obj_files: Vec<PathBuf> = Vec::new();
for cu in &cu_files {
let stem = cu.file_stem().unwrap().to_str().unwrap();
let obj = out_dir.join(format!("{}.o", stem));
let arch = env::var("CUDA_ARCH").unwrap_or_else(|_| "sm_80".to_string());
let status = Command::new("nvcc")
.args(["-O3", &format!("-arch={}", arch)])
.args(["--use_fast_math", "-Xptxas=-v"])
.arg("-Icuda/include")
.args(["-c", cu.to_str().unwrap()])
.arg("-o")
.arg(&obj)
.status()
.expect("nvcc not found — install CUDA toolkit and ensure nvcc is on PATH");
assert!(status.success(), "nvcc failed for {:?}", cu);
obj_files.push(obj);
}
let lib = out_dir.join("libbitnet_cuda.a");
let mut ar = Command::new("ar");
ar.arg("crs").arg(&lib);
for obj in &obj_files {
ar.arg(obj);
}
ar.status().expect("ar command failed");
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static=bitnet_cuda");
println!("cargo:rustc-link-lib=dylib=cuda");
println!("cargo:rustc-link-lib=dylib=cudart");
println!("cargo:rustc-link-lib=dylib=stdc++");
}