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
303
304
305
306
307
308
309
310
311
312
313
use crate::{Config, LinkType, Threading};
use anyhow::{bail, ensure, Context, Result};
use std::{
fs,
io::{self, BufRead},
path::{Path, PathBuf},
process::Command,
};
pub const STATIC_EXTENSION: &str = if cfg!(any(target_os = "linux", target_os = "macos")) {
"a"
} else {
"lib"
};
#[derive(Debug, Clone)]
pub struct Library {
pub config: Config,
pub include_dir: PathBuf,
pub library_dir: PathBuf,
pub iomp5_dir: Option<PathBuf>,
}
impl Library {
pub fn pkg_config(config: Config) -> Result<Option<Self>> {
if let Ok(out) = Command::new("pkg-config")
.arg("--variable=prefix")
.arg(config.to_string())
.output()
{
if out.status.success() {
let path = String::from_utf8(out.stdout).context("Non-UTF8 MKL prefix")?;
let prefix = Path::new(path.trim());
Self::seek_directory(config, prefix)
} else {
Ok(None)
}
} else {
Ok(None)
}
}
pub fn seek_directory(config: Config, root_dir: impl AsRef<Path>) -> Result<Option<Self>> {
let root_dir = root_dir.as_ref();
if !root_dir.is_dir() {
return Ok(None);
}
let mut library_dir = None;
let mut include_dir = None;
let mut iomp5_dir = None;
for entry in walkdir::WalkDir::new(root_dir) {
let entry = entry.unwrap();
if entry.path_is_symlink() {
continue;
}
let path = entry.into_path();
if path.is_dir() {
continue;
}
let (stem, ext) = match (path.file_stem(), path.extension()) {
(Some(stem), Some(ext)) => (
stem.to_str().context("Non UTF8 filename")?,
ext.to_str().context("Non UTF8 filename")?,
),
_ => continue,
};
if path.components().any(|c| {
if let std::path::Component::Normal(c) = c {
if let Some(c) = c.to_str() {
if c.starts_with("ia32") {
return true;
}
}
}
false
}) {
continue;
}
let dir = path
.parent()
.expect("parent must exist here since this is under `root_dir`")
.to_owned();
if stem == "mkl" && ext == "h" {
include_dir = Some(dir);
continue;
}
let name = if let Some(name) = stem.strip_prefix(std::env::consts::DLL_PREFIX) {
name
} else {
continue;
};
match (config.link, ext) {
(LinkType::Static, STATIC_EXTENSION) => match name {
"mkl_core" => {
ensure!(
library_dir.replace(dir).is_none(),
"Two or more MKL found in {}",
root_dir.display()
)
}
"iomp5" => {
ensure!(
iomp5_dir.replace(dir).is_none(),
"Two or more MKL found in {}",
root_dir.display()
)
}
_ => {}
},
(LinkType::Dynamic, std::env::consts::DLL_EXTENSION) => match name {
"mkl_core" => {
ensure!(
library_dir.replace(dir).is_none(),
"Two or more MKL found in {}",
root_dir.display()
)
}
"iomp5" => {
ensure!(
iomp5_dir.replace(dir).is_none(),
"Two or more MKL found in {}",
root_dir.display()
)
}
_ => {}
},
_ => {}
}
}
if config.parallel == Threading::OpenMP && iomp5_dir.is_none() {
return Ok(None);
}
Ok(match (library_dir, include_dir) {
(Some(library_dir), Some(include_dir)) => Some(Library {
config,
include_dir,
library_dir,
iomp5_dir,
}),
_ => None,
})
}
pub fn new(config: Config) -> Result<Self> {
if let Some(lib) = Self::pkg_config(config)? {
return Ok(lib);
}
if let Ok(mklroot) = std::env::var("MKLROOT") {
if let Some(lib) = Self::seek_directory(config, mklroot)? {
return Ok(lib);
}
}
for path in ["/opt/intel", "C:/Program Files (x86)/IntelSWTools/"] {
let path = Path::new(path);
if let Some(lib) = Self::seek_directory(config, path)? {
return Ok(lib);
}
}
bail!("Intel MKL not found in system");
}
pub fn available() -> Vec<Self> {
Config::possibles()
.into_iter()
.flat_map(|cfg| Self::new(cfg).ok())
.collect()
}
pub fn version(&self) -> Result<(u32, u32, u32)> {
let version_h = self.include_dir.join("mkl_version.h");
let f = fs::File::open(version_h).context("Failed to open mkl_version.h")?;
let f = io::BufReader::new(f);
let mut year = None;
let mut minor = None;
let mut update = None;
for line in f.lines().flatten() {
if !line.starts_with("#define") {
continue;
}
let ss: Vec<&str> = line.split(' ').collect();
match ss[1] {
"__INTEL_MKL__" => year = Some(ss[2].parse()?),
"__INTEL_MKL_MINOR__" => minor = Some(ss[2].parse()?),
"__INTEL_MKL_UPDATE__" => update = Some(ss[2].parse()?),
_ => continue,
}
}
match (year, minor, update) {
(Some(year), Some(minor), Some(update)) => Ok((year, minor, update)),
_ => bail!("Invalid mkl_version.h"),
}
}
pub fn print_cargo_metadata(&self) -> Result<()> {
println!("cargo:rustc-link-search={}", self.library_dir.display());
if let Some(iomp5_dir) = &self.iomp5_dir {
if iomp5_dir != &self.library_dir {
println!("cargo:rustc-link-search={}", iomp5_dir.display());
}
}
for lib in self.config.libs() {
match self.config.link {
LinkType::Static => {
println!("cargo:rustc-link-lib=static={}", lib);
}
LinkType::Dynamic => {
println!("cargo:rustc-link-lib=dylib={}", lib);
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[ignore]
#[test]
fn seek_opt_intel() {
for cfg in Config::possibles() {
let lib = Library::seek_directory(cfg, "/opt/intel").unwrap().unwrap();
dbg!(lib.version().unwrap());
}
}
#[ignore]
#[test]
fn pkg_config() {
for cfg in Config::possibles() {
if cfg.parallel == Threading::OpenMP {
continue;
}
let lib = Library::pkg_config(cfg).unwrap().unwrap();
dbg!(lib.version().unwrap());
}
}
}