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
use crate::prelude::Result;
use std::{rc::Rc, sync::Arc};

flat_mod!(scope, raw, flags, global, single, queue);

/// An object that can be used as a Blaze context, with a similar syntax to Rust allocators.\
/// Blaze contexts are similar to OpenCL contexts, except they're also in charge of administrating and supplying
/// their various command queues. This allows Blaze contexts to manage the load between the various devices in an
/// OpenCL context.
pub trait Context {
    /// Returns a reference to the underlying [`RawContext`]
    fn as_raw(&self) -> &RawContext;
    /// Returns a slice with all of the [`Context`]'s command queues
    fn queues(&self) -> &[CommandQueue];
    /// Returns the next [`CommandQueue`], as per context implementation
    fn next_queue(&self) -> &CommandQueue;

    /// Flushes all the [`CommandQueue`]s in the context.
    #[inline(always)]
    fn flush_all(&self) -> Result<()> {
        for queue in self.queues() {
            queue.flush()?
        }
        Ok(())
    }

    /// Finishes all the [`CommandQueue`]s in the context.
    #[inline(always)]
    fn finish_all(&self) -> Result<()> {
        for queue in self.queues() {
            queue.finish()?
        }
        Ok(())
    }
}

impl<T: ?Sized + Context> Context for &T {
    #[inline(always)]
    fn as_raw(&self) -> &RawContext {
        T::as_raw(self)
    }

    #[inline(always)]
    fn queues(&self) -> &[CommandQueue] {
        T::queues(self)
    }

    #[inline(always)]
    fn next_queue(&self) -> &CommandQueue {
        T::next_queue(self)
    }
}

impl<T: ?Sized + Context> Context for Rc<T> {
    #[inline(always)]
    fn as_raw(&self) -> &RawContext {
        T::as_raw(self)
    }

    #[inline(always)]
    fn queues(&self) -> &[CommandQueue] {
        T::queues(self)
    }

    #[inline(always)]
    fn next_queue(&self) -> &CommandQueue {
        T::next_queue(self)
    }
}

impl<T: ?Sized + Context> Context for Arc<T> {
    #[inline(always)]
    fn as_raw(&self) -> &RawContext {
        T::as_raw(self)
    }

    #[inline(always)]
    fn queues(&self) -> &[CommandQueue] {
        T::queues(self)
    }

    #[inline(always)]
    fn next_queue(&self) -> &CommandQueue {
        T::next_queue(self)
    }
}