1use std::sync::Arc;
4
5use cudarc::driver::{
6 CudaContext, CudaFunction, CudaModule, CudaStream, LaunchConfig, PushKernelArg,
7};
8use cudarc::nvrtc::Ptx;
9
10use crate::error::{AtlasError, Result};
11
12pub struct KernelModule {
17 module: Arc<CudaModule>,
18}
19
20impl KernelModule {
21 pub fn from_ptx_src(ctx: &Arc<CudaContext>, ptx_src: &str) -> Result<Self> {
26 let ptx = Ptx::from_src(ptx_src);
27 let module = ctx
28 .load_module(ptx)
29 .map_err(|e| AtlasError::ModuleLoad(format!("PTX load failed: {e}")))?;
30 Ok(Self { module })
31 }
32
33 pub fn get_function(&self, name: &str) -> Result<CudaFunction> {
35 self.module
36 .load_function(name)
37 .map_err(|e| AtlasError::ModuleLoad(format!("Function '{name}' not found: {e}")))
38 }
39}
40
41pub fn launch_config(n: u32, block_size: u32) -> LaunchConfig {
43 LaunchConfig {
44 grid_dim: (n.div_ceil(block_size), 1, 1),
45 block_dim: (block_size, 1, 1),
46 shared_mem_bytes: 0,
47 }
48}
49
50pub unsafe fn launch_vector_add(
59 stream: &Arc<CudaStream>,
60 func: &CudaFunction,
61 a_ptr: u64,
62 b_ptr: u64,
63 c_ptr: u64,
64 n: u32,
65) -> Result<()> {
66 let cfg = launch_config(n, 256);
67 unsafe {
68 stream
69 .launch_builder(func)
70 .arg(&a_ptr)
71 .arg(&b_ptr)
72 .arg(&c_ptr)
73 .arg(&n)
74 .launch(cfg)
75 .map_err(|e| AtlasError::KernelLaunch(format!("vector_add launch failed: {e}")))?;
76 }
77 Ok(())
78}