teeny_core/device/mod.rs
1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::{
18 device::{
19 buffer::Buffer,
20 program::{Kernel, Program},
21 },
22 dtype::Num,
23 errors::Result,
24};
25
26/// Device-side memory buffers.
27pub mod buffer;
28/// Device/context management.
29pub mod context;
30/// Compiled kernel programs.
31pub mod program;
32
33/// A device-specific kernel launch configuration (grid/block dimensions, etc).
34pub trait LaunchConfig: Sized {}
35
36/// A device capable of allocating buffers and launching kernels.
37pub trait Device<'a>: Sized {
38 /// This device's buffer type for elements of dtype `N`.
39 type Buffer<N: Num>: Buffer<'a, N>;
40 /// This device's compiled-program type for kernel `K`.
41 type Program<K: Kernel>: Program<'a, K>;
42 /// This device's launch configuration type.
43 type LaunchConfig: LaunchConfig;
44
45 /// Allocates a buffer for `count` elements of `N`.
46 fn buffer<N: Num>(&self, count: usize) -> Result<Self::Buffer<N>>;
47
48 /// Launches `program` with the given launch `cfg` and `args`.
49 fn launch<K: Kernel>(
50 &self,
51 program: &Self::Program<K>,
52 cfg: &Self::LaunchConfig,
53 args: K::Args<'a>,
54 ) -> Result<()>;
55}