-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuild.rs
More file actions
302 lines (270 loc) · 11.7 KB
/
Copy pathbuild.rs
File metadata and controls
302 lines (270 loc) · 11.7 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
fn main() {
println!("cargo:rerun-if-changed=build.rs");
#[cfg(all(feature = "cuda", feature = "hip"))]
compile_error!("Both 'cuda' and 'hip' features are enabled; only one can be used.");
#[cfg(all(not(feature = "cuda"), not(feature = "hip"), feature = "gpu-single"))]
compile_error!(
"The 'gpu-single' feature must be used with either of the 'cuda' or 'hip' features."
);
built::write_built_file().expect("Failed to acquire build-time information");
#[cfg(any(feature = "cuda", feature = "hip"))]
gpu::build_and_link();
}
#[cfg(any(feature = "cuda", feature = "hip"))]
mod gpu {
use std::{env, path::PathBuf};
/// Search for any C/C++/CUDA/HIP files, populate the provided buffer with
/// them, and have rerun-if-changed on all of them.
#[cfg(any(feature = "cuda", feature = "hip"))]
fn get_gpu_files<P: AsRef<std::path::Path>>(dir: P, files: &mut Vec<PathBuf>) {
for path in std::fs::read_dir(dir).expect("dir exists") {
let path = path.expect("is readable").path();
if path.is_dir() {
get_gpu_files(&path, files)
}
match path.extension().and_then(|os_str| os_str.to_str()) {
Some("cu") => {
println!("cargo:rerun-if-changed={}", path.display());
files.push(path);
}
Some("h" | "cuh") => println!("cargo:rerun-if-changed={}", path.display()),
_ => (),
}
}
}
/// Queries nvcc for its supported SM targets by parsing `nvcc --help`.
/// Returns None if nvcc is not found or produces no recognisable output.
#[cfg(feature = "cuda")]
fn get_nvcc_supported_sms() -> Option<std::collections::HashSet<u16>> {
let output = std::process::Command::new("nvcc")
.arg("--list-gpu-code")
.output()
.ok()?;
let text = String::from_utf8_lossy(&output.stdout);
let mut sms = std::collections::HashSet::new();
for line in text.lines() {
let line = line.trim();
if let Some(num) = line.strip_prefix("sm_") {
if let Ok(sm) = num.parse::<u16>() {
sms.insert(sm);
}
}
}
if sms.is_empty() {
None
} else {
Some(sms)
}
}
pub(super) fn build_and_link() {
let mut gpu_files = vec![];
get_gpu_files("src/gpu", &mut gpu_files);
#[cfg(feature = "cuda")]
let mut gpu_target = {
fn parse_and_validate_compute(c: &str, var: &str) -> Vec<u16> {
let mut out = vec![];
for compute in c.trim().split(',') {
// Check that there are only two or three numeric characters
// (e.g. 86 for Ampere, 120 for Blackwell).
if compute.len() < 2 || compute.len() > 3 {
panic!("When parsing {var}, found '{compute}', which is not a two- or three-digit number!")
}
match compute.parse() {
Ok(p) => out.push(p),
Err(_) => {
panic!("'{compute}', part of {var}, couldn't be parsed into a number!")
}
}
}
out
}
// Attempt to read HYPERDRIVE_CUDA_COMPUTE. HYPERBEAM_CUDA_COMPUTE
// can be used instead, too.
println!("cargo:rerun-if-env-changed=HYPERDRIVE_CUDA_COMPUTE");
println!("cargo:rerun-if-env-changed=HYPERBEAM_CUDA_COMPUTE");
let nvcc_sms = get_nvcc_supported_sms();
let targets: Vec<u16> = match (
env::var("HYPERDRIVE_CUDA_COMPUTE"),
env::var("HYPERBEAM_CUDA_COMPUTE"),
) {
// When a user-supplied variable exists, validate its values
// against what the installed nvcc actually supports.
(Ok(c), _) | (Err(_), Ok(c)) => {
let requested = parse_and_validate_compute(&c, "HYPERDRIVE_CUDA_COMPUTE");
if let Some(ref supported) = nvcc_sms {
requested
.into_iter()
.filter(|&sm| {
if supported.contains(&sm) {
true
} else {
println!("cargo:warning=Skipping sm={sm}: not supported by installed nvcc");
false
}
})
.collect()
} else {
requested
}
}
// By default, target everything the installed nvcc supports.
(Err(_), Err(_)) => {
let mut sms = nvcc_sms
.expect(
"Could not query nvcc for supported SMs; \
install nvcc or set HYPERDRIVE_CUDA_COMPUTE",
)
.into_iter()
.collect::<Vec<_>>();
sms.sort_unstable();
println!("cargo:warning=No HYPERDRIVE_CUDA_COMPUTE; targeting nvcc-supported sm_{sms:?}");
sms
}
};
if targets.is_empty() {
panic!(
"No CUDA targets remain. \
Check HYPERDRIVE_CUDA_COMPUTE or your nvcc installation."
);
}
let mut cuda_target = cc::Build::new();
cuda_target.cuda(true).cudart("shared"); // We handle linking cudart statically
// If $CXX is not set but $CUDA_PATH is, search for
// $CUDA_PATH/bin/g++ and if it exists, set that as $CXX.
if env::var_os("CXX").is_none() {
// Unlike above, we care about $CUDA_PATH being unicode.
if let Ok(cuda_path) = env::var("CUDA_PATH") {
// Look for the g++ that CUDA wants.
let compiler = std::path::PathBuf::from(cuda_path).join("bin/g++");
if compiler.exists() {
println!("cargo:warning=Setting $CXX to {}", compiler.display());
env::set_var("CXX", compiler.into_os_string());
}
}
}
// One matching-pair cubin per target SM.
for &sm in &targets {
cuda_target.flag("-gencode");
cuda_target.flag(format!("arch=compute_{sm},code=sm_{sm}"));
}
// PTX fallback for the highest target: JIT-compilable on future GPUs.
if let Some(&max) = targets.iter().max() {
cuda_target.flag("-gencode");
cuda_target.flag(format!("arch=compute_{max},code=compute_{max}"));
}
match env::var("DEBUG").as_deref() {
Ok("false") => (),
_ => {
cuda_target.flag("-G");
}
};
cuda_target
};
#[cfg(feature = "hip")]
let mut gpu_target = {
println!("cargo:rerun-if-env-changed=HIP_PATH");
let mut hip_path = match env::var_os("HIP_PATH") {
Some(p) => {
println!(
"cargo:warning=HIP_PATH set from env {}",
p.to_string_lossy()
);
std::path::PathBuf::from(p)
}
None => {
let hip_path = hip_sys::hiprt::get_hip_path();
println!(
"cargo:warning=HIP_PATH set from hip_sys {}",
hip_path.display()
);
hip_path
}
};
// It seems that various ROCm releases change where hipcc is...
let mut compiler = hip_path.join("bin/hipcc");
if !compiler.exists() {
// Try the dir above, which might be the ROCm dir.
hip_path = hip_path.parent().unwrap().into();
compiler = hip_path.join("bin/hipcc");
if !compiler.exists() {
panic!(
"Couldn't find hipcc in either {} or {}",
hip_sys::hiprt::get_hip_path().display(),
hip_path.parent().unwrap().display()
);
}
}
if !hip_path.join("include/hip/hip_runtime_api.h").exists() {
panic!(
"Couldn't find include/hip/hip_runtime_api.h in {}",
hip_path.display()
);
}
let mut hip_target = cc::Build::new();
println!("cargo:warning={compiler:?}");
hip_target.compiler(compiler);
println!("cargo:rerun-if-env-changed=HIP_FLAGS");
if let Some(p) = env::var_os("HIP_FLAGS") {
let s: String = p.to_string_lossy().into();
println!("cargo:warning=HIP_FLAGS set from env {s}",);
hip_target.flag(&s);
}
println!("cargo:rerun-if-env-changed=ROCM_VER");
println!("cargo:rerun-if-env-changed=ROCM_PATH");
println!("cargo:rerun-if-env-changed=HYPERBEAM_HIP_ARCH");
println!("cargo:rerun-if-env-changed=HYPERDRIVE_HIP_ARCH");
let arches: Vec<String> = match (
env::var("HYPERBEAM_HIP_ARCH"),
env::var("HYPERDRIVE_HIP_ARCH"),
) {
(Ok(c), _) | (Err(_), Ok(c)) => {
vec![c]
}
_ => {
println!("cargo:warning=No offload arch found, try HYPERBEAM_HIP_ARCH");
vec![]
}
};
for arch in arches {
hip_target.flag(&format!("--offload-arch={arch}"));
}
match env::var("DEBUG").as_deref() {
Ok("false") => (),
_ => {
hip_target
.flag("-ggdb")
.flag("-O1") // <- don't use -O0 https://github.com/ROCm/HIP/issues/3183
.flag("-gmodules");
}
};
hip_target
};
// The DEBUG env. variable is set by cargo. If running "cargo build
// --release", DEBUG is "false", otherwise "true". C/C++/CUDA like
// the compile option "NDEBUG" to be defined when using assert.h, so
// if appropriate, define that here. We also define "DEBUG" so that
// can be used.
match env::var("DEBUG").as_deref() {
Ok("false") | Ok("0") => {
gpu_target.define("NDEBUG", "");
}
_ => {
gpu_target.define("DEBUG", "").flag("-v");
println!("cargo:warning={gpu_target:?}");
}
};
// If we're told to, use single-precision floats. The default in the GPU
// code is to use double-precision.
#[cfg(feature = "gpu-single")]
gpu_target.define("SINGLE", None);
// Break in case of emergency.
// gpu_target.debug(true);
for f in gpu_files {
gpu_target.file(f);
}
gpu_target.compile("hyperdrive_gpu");
}
}