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
use crate::{channel::ComputeChannel, client::ComputeClient, server::ComputeServer};
use core::ops::DerefMut;
use hashbrown::HashMap;

/// The compute type has the responsibility to retrieve the correct compute client based on the
/// given device.
pub struct ComputeRuntime<Device, Server: ComputeServer, Channel> {
    clients: spin::Mutex<Option<HashMap<Device, ComputeClient<Server, Channel>>>>,
}

/// The kind of execution to be performed.
#[derive(Default, Hash, PartialEq, Eq, Clone, Debug, Copy)]
pub enum ExecutionMode {
    /// Checked kernels are safe.
    #[default]
    Checked,
    /// Unchecked kernels are unsafe.
    Unchecked,
}

impl<Device, Server, Channel> Default for ComputeRuntime<Device, Server, Channel>
where
    Device: core::hash::Hash + PartialEq + Eq + Clone + core::fmt::Debug,
    Server: ComputeServer,
    Channel: ComputeChannel<Server>,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<Device, Server, Channel> ComputeRuntime<Device, Server, Channel>
where
    Device: core::hash::Hash + PartialEq + Eq + Clone + core::fmt::Debug,
    Server: ComputeServer,
    Channel: ComputeChannel<Server>,
{
    /// Create a new compute.
    pub const fn new() -> Self {
        Self {
            clients: spin::Mutex::new(None),
        }
    }

    /// Get the compute client for the given device.
    ///
    /// Provide the init function to create a new client if it isn't already initialized.
    pub fn client<Init>(&self, device: &Device, init: Init) -> ComputeClient<Server, Channel>
    where
        Init: Fn() -> ComputeClient<Server, Channel>,
    {
        let mut clients = self.clients.lock();

        if clients.is_none() {
            Self::register_inner(device, init(), &mut clients);
        }

        match clients.deref_mut() {
            Some(clients) => match clients.get(device) {
                Some(client) => client.clone(),
                None => {
                    let client = init();
                    clients.insert(device.clone(), client.clone());
                    client
                }
            },
            _ => unreachable!(),
        }
    }

    /// Register the compute client for the given device.
    ///
    /// # Note
    ///
    /// This function is mostly useful when the creation of the compute client can't be done
    /// synchronously and require special context.
    ///
    /// # Panics
    ///
    /// If a client is already registered for the given device.
    pub fn register(&self, device: &Device, client: ComputeClient<Server, Channel>) {
        let mut clients = self.clients.lock();

        Self::register_inner(device, client, &mut clients);
    }

    fn register_inner(
        device: &Device,
        client: ComputeClient<Server, Channel>,
        clients: &mut Option<HashMap<Device, ComputeClient<Server, Channel>>>,
    ) {
        if clients.is_none() {
            *clients = Some(HashMap::new());
        }

        if let Some(clients) = clients {
            if clients.contains_key(device) {
                panic!("Client already created for device {:?}", device);
            }

            clients.insert(device.clone(), client);
        }
    }
}