pub(crate) use std::collections::BTreeMap;
mod compiler;
mod error;
pub(crate) mod generate;
pub mod manifest;
mod package;
pub use compiler::Compiler;
pub use error::Error;
pub use generate::{Config, Generate, OCaml, Rust};
pub use manifest::Manifest;
pub use package::Package;
#[derive(Debug, serde::Deserialize, PartialEq, Eq, Clone, Copy)]
pub enum Backend {
#[serde(rename = "c")]
C,
#[serde(rename = "cuda")]
CUDA,
#[serde(rename = "opencl")]
OpenCL,
#[serde(rename = "multicore")]
Multicore,
#[serde(rename = "ispc")]
ISPC,
#[serde(rename = "hip")]
HIP,
}
impl Backend {
pub fn to_str(&self) -> &'static str {
match self {
Backend::C => "c",
Backend::CUDA => "cuda",
Backend::OpenCL => "opencl",
Backend::Multicore => "multicore",
Backend::ISPC => "ispc",
Backend::HIP => "hip",
}
}
pub fn from_name(name: &str) -> Option<Backend> {
match name.to_ascii_lowercase().as_str() {
"c" => Some(Backend::C),
"cuda" => Some(Backend::CUDA),
"opencl" => Some(Backend::OpenCL),
"multicore" => Some(Backend::Multicore),
"ispc" => Some(Backend::ISPC),
_ => None,
}
}
pub fn from_env() -> Option<Backend> {
match std::env::var("FUTHARK_BACKEND") {
Ok(name) => Backend::from_name(&name),
Err(_) => None,
}
}
pub fn required_c_libs(&self) -> &'static [&'static str] {
match self {
Backend::CUDA => &["cuda", "cudart", "nvrtc", "m"],
Backend::OpenCL => &["OpenCL", "m"],
Backend::Multicore | Backend::ISPC => &["pthread", "m"],
Backend::HIP => &["hiprtc", "amdhip64"],
_ => &[],
}
}
}
#[cfg(feature = "build")]
pub fn build(
backend: Backend,
src: impl AsRef<std::path::Path>,
dest: impl AsRef<std::path::Path>,
) {
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
let dest = std::path::PathBuf::from(&out).join(dest);
let lib = Compiler::new(backend, src)
.with_output_dir(out)
.compile()
.expect("Compilation failed");
let mut config = Config::new(&dest).expect("Unable to configure codegen");
let mut gen = config.detect().expect("Invalid output language");
gen.generate(&lib, &mut config)
.expect("Code generation failed");
lib.link();
}