use alloc::string::{String, ToString};
use pliron::derive::format;
use crate::AddressType;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, serde::Serialize, serde::Deserialize)]
#[allow(missing_docs)]
#[format("`(` $x `, ` $y `, ` $z `)`")]
pub struct Dim3 {
pub x: u32,
pub y: u32,
pub z: u32,
}
impl Dim3 {
pub const fn new_single() -> Self {
Self { x: 1, y: 1, z: 1 }
}
pub const fn new_1d(x: u32) -> Self {
Self { x, y: 1, z: 1 }
}
pub const fn new_2d(x: u32, y: u32) -> Self {
Self { x, y, z: 1 }
}
pub const fn new_3d(x: u32, y: u32, z: u32) -> Self {
Self { x, y, z }
}
pub const fn num_elems(&self) -> u32 {
self.x * self.y * self.z
}
pub const fn can_contain(&self, other: Dim3) -> bool {
self.x >= other.x && self.y >= other.y && self.z >= other.z
}
}
impl From<(u32, u32, u32)> for Dim3 {
fn from(value: (u32, u32, u32)) -> Self {
Dim3::new_3d(value.0, value.1, value.2)
}
}
impl From<Dim3> for (u32, u32, u32) {
fn from(val: Dim3) -> Self {
(val.x, val.y, val.z)
}
}
#[derive(
Default, Hash, PartialEq, Eq, Clone, Debug, Copy, serde::Serialize, serde::Deserialize,
)]
pub enum ExecutionMode {
#[default]
Checked,
Validate,
Unchecked,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct KernelSettings {
pub cube_dim: Dim3,
pub address_type: AddressType,
pub kernel_name: String,
pub debug_symbols: bool,
pub cluster_dim: Option<Dim3>,
pub execution_mode: ExecutionMode,
}
impl KernelSettings {
pub fn new(cube_dim: Dim3, execution_mode: ExecutionMode, address_type: AddressType) -> Self {
Self {
cube_dim,
address_type,
kernel_name: String::new(),
debug_symbols: false,
cluster_dim: None,
execution_mode,
}
}
}
impl KernelSettings {
pub fn cube_dim(mut self, cube_dim: Dim3) -> Self {
self.cube_dim = cube_dim;
self
}
pub fn address_type(mut self, ty: AddressType) -> Self {
self.address_type = ty;
self
}
pub fn kernel_name<S: AsRef<str>>(mut self, name: S) -> Self {
self.kernel_name = name.as_ref().to_string();
self
}
pub fn debug_symbols(mut self) -> Self {
self.debug_symbols = true;
self
}
pub fn cluster_dim(mut self, cluster_dim: Dim3) -> Self {
self.cluster_dim = Some(cluster_dim);
self
}
}