mcl_rs/
prog.rs

1#![cfg_attr(all(doc, CHANNEL_NIGHTLY), feature(doc_auto_cfg))]
2use crate::low_level;
3
4/// The list of possible program types
5#[derive(Clone, serde::Serialize, serde::Deserialize)]
6pub enum PrgType {
7    // None,
8    /// An OpenCL source file
9    Src,
10    /// An IR(Intermediate Representation) file typically specific to a given architecture
11    Ir,
12    /// A Bitstream, typically to be loaded to an FPGA
13    Bin,
14    /// A Bistream, typically to be loaded to a Dataflow or CGRA architecture
15    Graph,
16}
17
18/// An abstration for container with the computational kernels we want to execute.
19///
20/// A program must be one of [PrgType] types.
21///
22/// In some cases (e.g. [PrgType::Src]), the kernels contained in a program will be compiled at runtime,
23/// we provide the option to pass along additional compiler options.
24///
25/// Programs must be loaded into the MCL environment usingthe[Prog::load] function, or created and loaded simultaneously via the [Mcl::load_prog()][crate::mcl::Mcl::load_prog()] api.
26/// # Example
27///```
28/// use mcl_rs::{MclEnvBuilder,PrgType};
29///
30/// let mcl = MclEnvBuilder::new().num_workers(10).initialize();
31///
32/// mcl.create_prog("my_path",PrgType::Src)
33///     .with_compile_args("-D MYDEF")
34///     .load();
35/// ```
36pub struct Prog {
37    prog_path: String,
38    compile_args: String,
39    program_type: PrgType,
40}
41
42impl Prog {
43    pub(crate) fn from(prog_path: &str, prog_type: PrgType) -> Self {
44        Prog {
45            prog_path: prog_path.to_string(),
46            compile_args: "".to_string(),
47            program_type: prog_type,
48        }
49    }
50
51    /// Loads the program into the current MCL environment
52    ///
53    /// # Example
54    ///```
55    /// use mcl_rs::{MclEnvBuilder,PrgType};
56    ///
57    /// let mcl = MclEnvBuilder::new().num_workers(10).initialize();
58    /// let prog = mcl.create_prog("my_path",PrgType::Src);
59    /// prog.load();
60    ///
61    /// // alternatively it is common to call 'load' directly on 'create_prog'
62    ///  mcl.create_prog("my_path2",PrgType::Src).load();
63    /// ```
64    pub fn load(self) {
65        low_level::prg_load(&self.prog_path, &self.compile_args, self.program_type);
66    }
67
68    /// Allows one to specifiy arguments to pass to the compiler when compiling the kernels within this program
69    ///
70    /// # Example
71    ///```
72    /// use mcl_rs::{MclEnvBuilder,PrgType};
73    ///
74    /// let mcl = MclEnvBuilder::new().num_workers(10).initialize();
75    /// let prog = mcl.create_prog("my_path",PrgType::Src);
76    /// prog.with_compile_args("-D MYDEF").load();
77    ///
78    /// // alternatively it is common to create/load a prog in one line
79    ///  mcl.create_prog("my_path2",PrgType::Src)
80    ///     .with_compile_args("-D MYDEF")
81    ///     .load();
82    /// ```
83    pub fn with_compile_args(mut self, compile_args: &str) -> Self {
84        self.compile_args = compile_args.to_string();
85        self
86    }
87}