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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! An `OpenCL` program.
use std;
use std::ops::{Deref, DerefMut};
use std::ffi::CString;
use std::io::Read;
use std::fs::File;
use std::path::PathBuf;
use std::collections::HashSet;
use std::convert::Into;

use core::error::{Result as OclResult, Error as OclError};
use core::{self, Program as ProgramCore, Context as ContextCore,
    ProgramInfo, ProgramInfoResult, ProgramBuildInfo, ProgramBuildInfoResult};
#[cfg(feature = "opencl_version_2_1")]
use core::ClVersions;
use standard::{Context, Device, DeviceSpecifier};


/// A program from which kernels can be created from.
///
/// To use with multiple devices, create manually with `::from_parts()`.
///
/// ## Destruction
///
/// Handled automatically. Feel free to store, clone, and share among threads
/// as you please.
///
#[derive(Clone, Debug)]
pub struct Program(ProgramCore);

impl Program {
    /// Returns a new `ProgramBuilder`.
    pub fn builder() -> ProgramBuilder {
        ProgramBuilder::new()
    }

    /// Returns a new program built from pre-created build components and device
    /// list.
    ///
    /// Prefer `::builder` to create a new `Program`.
    ///
    pub fn new(context_obj_core: &ContextCore, src_strings: Vec<CString>,
            device_ids: Option<&[Device]>, cmplr_opts: CString) -> OclResult<Program>
    {
        let obj_core = try!(core::create_build_program(context_obj_core, &src_strings, device_ids,
            &cmplr_opts));

        Ok(Program(obj_core))
    }

    /// Returns a new program built from pre-created build components and device
    /// list for programs with intermediate language byte source.
    #[cfg(feature = "opencl_version_2_1")]
    pub fn with_il(il: Vec<u8>, device_ids: Option<&[Device]>, cmplr_opts: CString,
            context_obj_core: &ContextCore) -> OclResult<Program>
    {
        let device_versions = context_obj_core.device_versions()?;

        let obj_core = core::create_program_with_il(context_obj_core, &il, Some(&device_versions))?;

        core::build_program(&obj_core, device_ids, &cmplr_opts, None, None)?;

        // let devices = context_obj_core.devices()?.into_iter().map(|d| d.into()).collect();

        Ok(Program(obj_core))
    }

    /// Returns a reference to the core pointer wrapper, usable by functions in
    /// the `core` module.
    #[deprecated(since="0.13.0", note="Use `::core` instead.")]
    pub fn core_as_ref(&self) -> &ProgramCore {
        &self.0
    }

    /// Returns a reference to the core pointer wrapper, usable by functions in
    /// the `core` module.
    #[inline]
    pub fn core(&self) -> &ProgramCore {
        &self.0
    }

    /// Returns the list of devices associated with this program.
    #[deprecated(since="0.13.0", note="Use `::info` with `ProgramInfo::Devices` instead.")]
    pub fn devices(&self) -> Vec<Device> {
        unimplemented!();
    }

    /// Returns info about this program.
    pub fn info(&self, info_kind: ProgramInfo) -> ProgramInfoResult {
        core::get_program_info(&self.0, info_kind)
    }

    /// Returns info about this program's build.
    ///
    /// * TODO: Check that device is valid.
    pub fn build_info(&self, device: Device, info_kind: ProgramBuildInfo) -> ProgramBuildInfoResult {
        core::get_program_build_info(&self.0, &device, info_kind)
    }

    fn fmt_info(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("Program")
            .field("ReferenceCount", &self.info(ProgramInfo::ReferenceCount))
            .field("Context", &self.info(ProgramInfo::Context))
            .field("NumDevices", &self.info(ProgramInfo::NumDevices))
            .field("Devices", &self.info(ProgramInfo::Devices))
            .field("Source", &self.info(ProgramInfo::Source))
            .field("BinarySizes", &self.info(ProgramInfo::BinarySizes))
            .field("Binaries", &self.info(ProgramInfo::Binaries))
            .field("NumKernels", &self.info(ProgramInfo::NumKernels))
            .field("KernelNames", &self.info(ProgramInfo::KernelNames))
            .finish()
    }
}


impl std::fmt::Display for Program {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.fmt_info(f)
    }
}

impl Deref for Program {
    type Target = ProgramCore;

    fn deref(&self) -> &ProgramCore {
        &self.0
    }
}

impl DerefMut for Program {
    fn deref_mut(&mut self) -> &mut ProgramCore {
        &mut self.0
    }
}


/// A build option used by ProgramBuilder.
///
/// Strings intended for use either by the compiler as a command line switch
/// or for inclusion in the final build source code.
///
/// A few of the often used variants have constructors for convenience.
///
/// * [FIXME] TODO: Explain how each variant is used.
///
/// * [FIXME] TODO: Examples.
#[derive(Clone, Debug)]
pub enum BuildOpt {
    CmplrDefine { ident: String, val: String },
    CmplrInclDir { path: String },
    CmplrOther(String),
    IncludeDefine { ident: String, val: String },
    IncludeRaw(String),
    IncludeRawEof(String),
}

impl BuildOpt {
    /// Returns a `BuildOpt::CmplrDefine`.
    pub fn cmplr_def<S: Into<String>>(ident: S, val: i32) -> BuildOpt {
        BuildOpt::CmplrDefine {
            ident: ident.into(),
            val: val.to_string(),
        }
    }

    /// Returns a `BuildOpt::IncludeDefine`.
    pub fn include_def<S: Into<String>>(ident: S, val: String) -> BuildOpt {
        BuildOpt::IncludeDefine {
            ident: ident.into(),
            val: val,
        }
    }
}


/// A builder for `Program`.
///
// * [SOMEDAY TODO]: Keep track of line number range for each string and print
// out during build failure.
//
#[must_use = "builders do nothing unless '::build' is called"]
#[derive(Clone, Debug)]
pub struct ProgramBuilder {
    options: Vec<BuildOpt>,
    src_files: Vec<PathBuf>,
    il: Option<Vec<u8>>,
    device_spec: Option<DeviceSpecifier>,
}

impl ProgramBuilder {
    /// Returns a new, empty, build configuration object.
    pub fn new() -> ProgramBuilder {
        ProgramBuilder {
            options: Vec::with_capacity(64),
            src_files: Vec::with_capacity(16),
            il: None,
            device_spec: None,
        }
    }

    /// Returns a newly built Program.
    ///
    ///
    ///
    ///
    /// * TODO: If the context is associated with more than one device,
    /// check that at least one of those devices has been specified. An empty
    /// device list will cause an `OpenCL` error in that case.
    ///
    /// * TODO: Check for duplicate devices in the final device list.
    #[cfg(not(feature = "opencl_version_2_1"))]
    pub fn build(self, context: &Context) -> OclResult<Program> {
        let device_list = match self.device_spec {
            Some(ref ds) => ds.to_device_list(context.platform()?)?,
            None => context.devices(),
        };

        match self.il {
            Some(_) => {
                return Err("ocl::ProgramBuilder::build: Unreachable section (IL).".into());
            },
            None => {
                Program::new(
                    context,
                    try!(self.get_src_strings().map_err(|e| e.to_string())),
                    Some(&device_list[..]),
                    try!(self.get_compiler_options().map_err(|e| e.to_string())),
                )
            },
        }
    }

    /// Returns a newly built Program.
    ///
    /// * TODO: If the context is associated with more than one device,
    /// check that at least one of those devices has been specified. An empty
    /// device list will cause an `OpenCL` error in that case.
    ///
    /// * TODO: Check for duplicate devices in the final device list.
    #[cfg(feature = "opencl_version_2_1")]
    pub fn build(mut self, context: &Context) -> OclResult<Program> {
        let device_list = match self.device_spec {
            Some(ref ds) => try!(ds.to_device_list(context.platform()?)),
            None => context.devices().to_owned(),
        };

        match self.il.take() {
            Some(il) => {
                if cfg!(feature = "opencl_version_2_1") {
                    if self.src_files.len() > 0 { return Err("ProgramBuilder::build: \
                        No source files may be set when building with IR.".into()); }

                    Program::with_il(
                        il,
                        Some(&device_list[..]),
                        self.get_compiler_options().map_err(|e| e.to_string())?,
                        context
                    )
                } else {
                    return Err("ocl::ProgramBuilder::build: Unreachable section.".into());
                }
            },
            None => {
                Program::new(
                    context,
                    try!(self.get_src_strings().map_err(|e| e.to_string())),
                    Some(&device_list[..]),
                    try!(self.get_compiler_options().map_err(|e| e.to_string())),
                )
            },
        }
    }

    /// Adds a build option containing a compiler command line definition.
    /// Formatted as `-D {name}={val}`.
    ///
    /// ## Example
    ///
    /// `...cmplr_def("MAX_ITERS", 500)...`
    ///
    pub fn cmplr_def<S: Into<String>>(mut self, name: S, val: i32) -> ProgramBuilder {
        self.options.push(BuildOpt::cmplr_def(name, val));
        self
    }

    /// Adds a build option containing a raw compiler command line parameter.
    /// Formatted as `{}` (exact text).
    ///
    /// ## Example
    ///
    /// `...cmplr_opt("-g")...`
    ///
    pub fn cmplr_opt<S: Into<String>>(mut self, co: S) -> ProgramBuilder {
        self.options.push(BuildOpt::CmplrOther(co.into()));
        self
    }

    /// Pushes pre-created build option to the list of options.
    pub fn bo(mut self, bo: BuildOpt) -> ProgramBuilder {
        self.options.push(bo);
        self
    }

    /// Adds the contents of a file to the program.
    pub fn src_file<P: Into<PathBuf>>(mut self, file_path: P) -> ProgramBuilder {
        let file_path = file_path.into();
        assert!(file_path.is_file(), "ProgramBuilder::src_file(): Source file error: \
            '{}' does not exist.", file_path.display());
        self.src_files.push(file_path);
        self
    }

    /// Adds raw text to the program source.
    pub fn src<S: Into<String>>(mut self, src: S) -> ProgramBuilder {
        self.options.push(BuildOpt::IncludeRawEof(src.into()));
        self
    }

    /// Adds SPIR-V or an implementation-defined intermediate language to this program.
    ///
    /// Any source files or source text added to this build will cause an
    /// error upon building.
    ///
    /// Use the `include_bytes!` macro to include source code from a file statically.
    ///
    /// * TODO: Future addition: Allow IL to be loaded directly from a file
    /// in the same way that text source is.
    ///
    #[cfg(feature = "opencl_version_2_1")]
    pub fn il(mut self, il: Vec<u8>) -> ProgramBuilder {
        self.il = Some(il);
        self
    }

    /// Specifies a list of devices to build this program on. The devices must
    /// be associated with the context passed to `::build` later on.
    ///
    /// Devices may be specified in any number of ways including simply
    /// passing a device or slice of devices. See the [`impl
    /// From`][device_specifier_from] section of
    /// [`DeviceSpecifier`][device_specifier] for more information.
    ///
    ///
    /// ## Panics
    ///
    /// Devices must not have already been specified.
    ///
    /// [device_specifier_from]: enum.DeviceSpecifier.html#method.from
    /// [device_specifier]: enum.DeviceSpecifier.html
    ///
    pub fn devices<D: Into<DeviceSpecifier>>(mut self, device_spec: D)
            -> ProgramBuilder
    {
        assert!(self.device_spec.is_none(), "ocl::ProgramBuilder::devices(): Devices already specified");
        self.device_spec = Some(device_spec.into());
        self
    }

    /// Returns the devices specified to be associated the program.
    pub fn get_device_spec(&self) -> &Option<DeviceSpecifier> {
        &self.device_spec
    }

    /// Returns a concatenated string of command line options to be passed to
    /// the compiler when building this program.
    pub fn get_compiler_options(&self) -> OclResult<CString> {
        let mut opts: Vec<String> = Vec::with_capacity(64);

        for option in &self.options {
            match *option {
                BuildOpt::CmplrDefine { ref ident, ref val } => {
                    opts.push(format!("-D {}={}", ident, val))
                },

                BuildOpt::CmplrInclDir { ref path } => {
                    opts.push(format!("-I {}", path))
                },

                BuildOpt::CmplrOther(ref s) => {
                    opts.push(s.clone())
                },

                _ => (),
            }
        }

        CString::new(opts.join(" ").into_bytes()).map_err(OclError::from)
    }

    /// Returns the final program source code as a list of strings.
    ///
    /// ### Order of Inclusion
    ///
    /// 1. Macro definitions and code strings specified by a
    ///    `BuildOpt::IncludeDefine` or `BuildOpt::IncludeRaw` via `::bo`
    /// 2. Contents of files specified via `::src_file`
    /// 3. Contents of strings specified via `::src` or a
    ///   `BuildOpt::IncludeRawEof` via `::bo`
    ///
    pub fn get_src_strings(&self) -> OclResult<Vec<CString>> {
        let mut src_strings: Vec<CString> = Vec::with_capacity(64);
        let mut src_file_history: HashSet<PathBuf> = HashSet::with_capacity(64);

        src_strings.extend_from_slice(&try!(self.get_includes()));

        for srcpath in &self.src_files {
            let mut src_bytes: Vec<u8> = Vec::with_capacity(100000);

            if src_file_history.contains(srcpath) { continue; }
            src_file_history.insert(srcpath.clone());

            let mut src_file_handle = try!(File::open(srcpath));

            try!(src_file_handle.read_to_end(&mut src_bytes));
            src_bytes.shrink_to_fit();
            src_strings.push(try!(CString::new(src_bytes)));
        }

        src_strings.extend_from_slice(&try!(self.get_includes_eof()));
        src_strings.shrink_to_fit();
        Ok(src_strings)
    }

    /// Parses `self.options` for options intended for inclusion at the beginning of
    /// the final program source and returns them as a list of strings.
    ///
    /// Generally used for #define directives, constants, etc. Normally called from
    /// `::get_src_strings()`.
    fn get_includes(&self) -> OclResult<Vec<CString>> {
        let mut strings = Vec::with_capacity(64);
        strings.push(try!(CString::new("\n".as_bytes())));

        for option in &self.options {
            match *option {
                BuildOpt::IncludeDefine { ref ident, ref val } => {
                    strings.push(try!(CString::new(format!("#define {}  {}\n", ident, val)
                        .into_bytes())));
                },
                BuildOpt::IncludeRaw(ref text) => {
                    strings.push(try!(CString::new(text.clone().into_bytes())));
                },
                _ => (),
            };

        }

        strings.shrink_to_fit();
        Ok(strings)
    }

    /// Parses `self.options` for options intended for inclusion at the end of
    /// the final program source and returns them as a list of strings.
    fn get_includes_eof(&self) -> OclResult<Vec<CString>> {
        let mut strings = Vec::with_capacity(64);
        strings.push(try!(CString::new("\n".as_bytes())));

        for option in &self.options {
            if let BuildOpt::IncludeRawEof(ref text) = *option {
                strings.push(try!(CString::new(text.clone().into_bytes())));
            }
        }

        strings.shrink_to_fit();
        Ok(strings)
    }
}