furiosa_device/config/
builder.rs

1use super::inner::{Config, Count, DeviceConfigInner};
2use crate::arch::Arch;
3use crate::device::DeviceMode;
4use crate::{DeviceConfig, DeviceError};
5
6#[derive(Clone)]
7pub struct NotDetermined {
8    pub(crate) _priv: (),
9}
10
11impl TryInto<DeviceConfig> for NotDetermined {
12    type Error = DeviceError;
13
14    fn try_into(self) -> Result<DeviceConfig, Self::Error> {
15        Err(DeviceError::parse_error(
16            "",
17            "fallback device config is not set",
18        ))
19    }
20}
21
22impl From<NotDetermined> for Arch {
23    fn from(_: NotDetermined) -> Self {
24        Arch::WarboyB0
25    }
26}
27
28impl From<NotDetermined> for DeviceMode {
29    fn from(_: NotDetermined) -> Self {
30        DeviceMode::Fusion
31    }
32}
33
34impl From<NotDetermined> for Count {
35    fn from(_: NotDetermined) -> Self {
36        Count::Finite(1)
37    }
38}
39
40#[derive(Clone)]
41pub struct All {
42    pub(crate) _priv: (),
43}
44
45impl From<All> for Count {
46    fn from(_: All) -> Self {
47        Count::All
48    }
49}
50
51/// A builder struct for `DeviceConfig`.
52#[derive(Clone)]
53pub struct DeviceConfigBuilder<A, M, C> {
54    pub(crate) arch: A,
55    pub(crate) mode: M,
56    pub(crate) count: C,
57}
58
59impl<A, C> DeviceConfigBuilder<A, NotDetermined, C> {
60    pub fn multicore(self) -> DeviceConfigBuilder<A, DeviceMode, C> {
61        DeviceConfigBuilder {
62            arch: self.arch,
63            mode: DeviceMode::MultiCore,
64            count: self.count,
65        }
66    }
67
68    pub fn single(self) -> DeviceConfigBuilder<A, DeviceMode, C> {
69        DeviceConfigBuilder {
70            arch: self.arch,
71            mode: DeviceMode::Single,
72            count: self.count,
73        }
74    }
75
76    pub fn fused(self) -> DeviceConfigBuilder<A, DeviceMode, C> {
77        DeviceConfigBuilder {
78            arch: self.arch,
79            mode: DeviceMode::Fusion,
80            count: self.count,
81        }
82    }
83}
84
85impl<A, M, C> DeviceConfigBuilder<A, M, C>
86where
87    Arch: From<A>,
88    DeviceMode: From<M>,
89    Count: From<C>,
90{
91    pub fn count(self, count: u8) -> DeviceConfig {
92        let builder = DeviceConfigBuilder {
93            arch: self.arch,
94            mode: self.mode,
95            count: Count::Finite(count),
96        };
97        builder.build()
98    }
99
100    pub fn all(self) -> DeviceConfig {
101        let builder = DeviceConfigBuilder {
102            arch: self.arch,
103            mode: self.mode,
104            count: All { _priv: () },
105        };
106
107        builder.build()
108    }
109
110    pub fn build(self) -> DeviceConfig {
111        let mode = DeviceMode::from(self.mode);
112        let core_num = match mode {
113            DeviceMode::MultiCore => 0,
114            DeviceMode::Single => 1,
115            DeviceMode::Fusion => 2,
116        };
117
118        DeviceConfig {
119            inner: DeviceConfigInner {
120                cfgs: vec![Config::Unnamed {
121                    arch: Arch::from(self.arch),
122                    core_num,
123                    mode,
124                    count: Count::from(self.count),
125                }],
126            },
127        }
128    }
129}