|
| 1 | +use std::env; |
| 2 | +use std::path; |
| 3 | +use std::process::{Command, Stdio}; |
| 4 | + |
| 5 | +use cuda_builder::CudaBuilder; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + println!("cargo::rerun-if-changed=build.rs"); |
| 9 | + println!("cargo::rerun-if-changed=kernels"); |
| 10 | + |
| 11 | + let out_path = path::PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 12 | + let kernels_path = path::PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("kernels"); |
| 13 | + |
| 14 | + CudaBuilder::new(kernels_path.as_path()) |
| 15 | + .copy_to(out_path.join("kernels.ptx")) |
| 16 | + .build() |
| 17 | + .unwrap(); |
| 18 | + |
| 19 | + // Generate PTX from native CUDA kernels |
| 20 | + let cuda_kernel_path = kernels_path.join("cuda/sdot.cu"); |
| 21 | + |
| 22 | + println!("cargo::rerun-if-changed={}", cuda_kernel_path.display()); |
| 23 | + |
| 24 | + let cuda_ptx = out_path.join("kernels_cuda_mangles.ptx"); |
| 25 | + let mut nvcc = Command::new("nvcc"); |
| 26 | + nvcc.arg("--ptx") |
| 27 | + .args(["--Werror", "all-warnings"]) |
| 28 | + .args(["--output-directory", out_path.as_os_str().to_str().unwrap()]) |
| 29 | + .args(["-o", cuda_ptx.as_os_str().to_str().unwrap()]) |
| 30 | + .arg(cuda_kernel_path.as_path()); |
| 31 | + |
| 32 | + let build = nvcc |
| 33 | + .stderr(Stdio::inherit()) |
| 34 | + .output() |
| 35 | + .expect("failed to execute nvcc kernel build"); |
| 36 | + |
| 37 | + assert!(build.status.success()); |
| 38 | + |
| 39 | + // Decodes (demangles) low-level identifiers |
| 40 | + let cat_out = Command::new("cat") |
| 41 | + .arg(cuda_ptx) |
| 42 | + .stdout(Stdio::piped()) |
| 43 | + .stderr(Stdio::inherit()) |
| 44 | + .spawn() |
| 45 | + .expect("Failed to start cat process") |
| 46 | + .stdout |
| 47 | + .expect("Failed to open cat stdout"); |
| 48 | + |
| 49 | + let outputs = std::fs::File::create(out_path.join("kernels_cuda.ptx")) |
| 50 | + .expect("Can not open output ptc kernel file"); |
| 51 | + |
| 52 | + let filt_out = Command::new("cu++filt") |
| 53 | + .arg("-p") |
| 54 | + .stdin(Stdio::from(cat_out)) |
| 55 | + .stdout(Stdio::from(outputs)) |
| 56 | + .stderr(Stdio::inherit()) |
| 57 | + .spawn() |
| 58 | + .expect("Failed to start cu++filt process") |
| 59 | + .wait_with_output() |
| 60 | + .expect("Failed to wait on cu++filt"); |
| 61 | + |
| 62 | + assert!(filt_out.status.success()); |
| 63 | +} |
0 commit comments