#![cfg(feature = "cuda")]

use crate::cuda::moving_averages::DeviceArrayF32;
use crate::indicators::dx::{DxBatchRange, DxParams};
use cust::context::Context;
use cust::device::Device;
use cust::function::{BlockSize, GridSize};
use cust::memory::{mem_get_info, AsyncCopyDestination, DeviceBuffer, LockedBuffer};
use cust::module::{Module, ModuleJitOption, OptLevel};
use cust::prelude::*;
use cust::stream::{Stream, StreamFlags};
use std::env;
use std::ffi::c_void;
use std::fmt;
use std::sync::Arc;
use thiserror::Error;

#[derive(Clone, Copy, Debug)]
pub enum BatchKernelPolicy {
    Auto,
    Plain { block_x: u32 },
}

#[derive(Clone, Copy, Debug)]
pub enum ManySeriesKernelPolicy {
    Auto,
    OneD { block_x: u32 },
}

#[derive(Clone, Copy, Debug)]
pub struct CudaDxPolicy {
    pub batch: BatchKernelPolicy,
    pub many_series: ManySeriesKernelPolicy,
}
impl Default for CudaDxPolicy {
    fn default() -> Self {
        Self {
            batch: BatchKernelPolicy::Auto,
            many_series: ManySeriesKernelPolicy::Auto,
        }
    }
}

#[derive(Debug, Error)]
pub enum CudaDxError {
    #[error(transparent)]
    Cuda(#[from] cust::error::CudaError),
    #[error("out of memory: required={required} free={free} headroom={headroom}")]
    OutOfMemory {
        required: usize,
        free: usize,
        headroom: usize,
    },
    #[error("missing kernel symbol: {name}")]
    MissingKernelSymbol { name: &'static str },
    #[error("invalid input: {0}")]
    InvalidInput(String),
    #[error("invalid policy: {0}")]
    InvalidPolicy(&'static str),
    #[error("launch config too large: grid=({gx},{gy},{gz}) block=({bx},{by},{bz})")]
    LaunchConfigTooLarge {
        gx: u32,
        gy: u32,
        gz: u32,
        bx: u32,
        by: u32,
        bz: u32,
    },
    #[error("device mismatch: buf={buf}, current={current}")]
    DeviceMismatch { buf: u32, current: u32 },
    #[error("arithmetic overflow when computing {what}")]
    ArithmeticOverflow { what: &'static str },
    #[error("not implemented")]
    NotImplemented,
}

pub struct CudaDx {
    module: Module,
    stream: Stream,
    context: Arc<Context>,
    device_id: u32,
    policy: CudaDxPolicy,
    debug_batch_logged: bool,
    debug_many_logged: bool,
}

impl CudaDx {
    pub fn context_arc(&self) -> Arc<Context> {
        self.context.clone()
    }
    pub fn device_id(&self) -> u32 {
        self.device_id
    }
    pub fn new(device_id: usize) -> Result<Self, CudaDxError> {
        cust::init(CudaFlags::empty())?;
        let device = Device::get_device(device_id as u32)?;
        let context = Arc::new(Context::new(device)?);
        let ptx: &str = include_str!(concat!(env!("OUT_DIR"), "/dx_kernel.ptx"));
        let jit_opts = &[
            ModuleJitOption::DetermineTargetFromContext,
            ModuleJitOption::OptLevel(OptLevel::O2),
        ];
        let module = match Module::from_ptx(ptx, jit_opts) {
            Ok(m) => m,
            Err(_) => match Module::from_ptx(ptx, &[ModuleJitOption::DetermineTargetFromContext]) {
                Ok(m) => m,
                Err(_) => Module::from_ptx(ptx, &[])?,
            },
        };
        let stream = Stream::new(StreamFlags::NON_BLOCKING, None)?;
        Ok(Self {
            module,
            stream,
            context,
            device_id: device_id as u32,
            policy: CudaDxPolicy::default(),
            debug_batch_logged: false,
            debug_many_logged: false,
        })
    }

    pub fn set_policy(&mut self, policy: CudaDxPolicy) {
        self.policy = policy;
    }
    pub fn policy(&self) -> &CudaDxPolicy {
        &self.policy
    }
    pub fn synchronize(&self) -> Result<(), CudaDxError> {
        self.stream.synchronize().map_err(Into::into)
    }

    #[inline]
    fn mem_check_enabled() -> bool {
        match env::var("CUDA_MEM_CHECK") {
            Ok(v) => v != "0" && v.to_lowercase() != "false",
            Err(_) => true,
        }
    }

    #[inline]
    fn device_mem_info() -> Option<(usize, usize)> {
        mem_get_info().ok()
    }

    #[inline]
    fn will_fit(&self, required: usize, headroom: usize) -> Result<(), CudaDxError> {
        if !Self::mem_check_enabled() {
            return Ok(());
        }
        let (free, _total) = match Self::device_mem_info() {
            Some(v) => v,
            None => return Ok(()),
        };
        let need = required
            .checked_add(headroom)
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "required_bytes + headroom_bytes",
            })?;
        if need <= free {
            Ok(())
        } else {
            Err(CudaDxError::OutOfMemory {
                required,
                free,
                headroom,
            })
        }
    }

    fn expand_periods(sweep: &DxBatchRange) -> Result<Vec<usize>, CudaDxError> {
        let (start, end, step) = sweep.period;
        if step == 0 || start == end {
            return Ok(vec![start]);
        }
        let mut out = Vec::new();
        if start < end {
            let mut v = start;
            while v <= end {
                out.push(v);
                v = match v.checked_add(step) {
                    Some(next) if next != v => next,
                    _ => break,
                };
            }
        } else {
            let mut v = start;
            loop {
                out.push(v);
                if v <= end {
                    break;
                }
                let dec = v.saturating_sub(step);
                if dec == v {
                    break;
                }
                v = dec;
            }
            out.sort_unstable();
        }
        if out.is_empty() {
            return Err(CudaDxError::InvalidInput(format!(
                "invalid period range: start={} end={} step={}",
                start, end, step
            )));
        }
        Ok(out)
    }

    fn prepare_batch(
        high: &[f32],
        low: &[f32],
        close: &[f32],
        sweep: &DxBatchRange,
    ) -> Result<(Vec<DxParams>, usize, usize), CudaDxError> {
        if high.is_empty() || low.is_empty() || close.is_empty() {
            return Err(CudaDxError::InvalidInput("empty input".into()));
        }
        let len = high.len().min(low.len()).min(close.len());
        let first_valid = (0..len)
            .find(|&i| !high[i].is_nan() && !low[i].is_nan() && !close[i].is_nan())
            .ok_or_else(|| CudaDxError::InvalidInput("all values are NaN".into()))?;
        let periods = Self::expand_periods(sweep)?;
        let max_p = *periods.iter().max().unwrap();
        let _ = (periods.len())
            .checked_mul(max_p)
            .ok_or(CudaDxError::InvalidInput(
                "n_combos*max_period overflow".into(),
            ))?;
        if len - first_valid < max_p {
            return Err(CudaDxError::InvalidInput("not enough valid data".into()));
        }
        let combos: Vec<DxParams> = periods
            .iter()
            .map(|&p| DxParams { period: Some(p) })
            .collect();
        Ok((combos, first_valid, len))
    }

    fn precompute_terms(
        high: &[f32],
        low: &[f32],
        close: &[f32],
    ) -> (Vec<f64>, Vec<f64>, Vec<f64>, Vec<u8>) {
        let len = high.len().min(low.len()).min(close.len());
        let mut pdm = vec![0f64; len];
        let mut mdm = vec![0f64; len];
        let mut tr = vec![0f64; len];
        let mut carry = vec![0u8; len];
        if len >= 2 {
            for i in 1..len {
                let h = high[i] as f64;
                let l = low[i] as f64;
                let c = close[i] as f64;
                if h.is_nan() || l.is_nan() || c.is_nan() {
                    carry[i] = 1;
                    continue;
                }
                let h = high[i] as f64;
                let l = low[i] as f64;
                let c = close[i] as f64;
                if h.is_nan() || l.is_nan() || c.is_nan() {
                    carry[i] = 1;
                    continue;
                }
                let up = h - (high[i - 1] as f64);
                let dn = (low[i - 1] as f64) - l;
                pdm[i] = if up > 0.0 && up > dn { up } else { 0.0 };
                mdm[i] = if dn > 0.0 && dn > up { dn } else { 0.0 };
                let tr1 = h - l;
                let tr2 = (h - (close[i - 1] as f64)).abs();
                let tr3 = (l - (close[i - 1] as f64)).abs();
                tr[i] = tr1.max(tr2).max(tr3);
            }
        }
        (pdm, mdm, tr, carry)
    }

    fn precompute_dm_and_carry(
        high: &[f32],
        low: &[f32],
        close: &[f32],
    ) -> (Vec<f64>, Vec<f64>, Vec<u8>) {
        let len = high.len().min(low.len()).min(close.len());
        let mut pdm = vec![0f64; len];
        let mut mdm = vec![0f64; len];
        let mut carry = vec![0u8; len];
        if len >= 2 {
            for i in 1..len {
                let h = high[i] as f64;
                let l = low[i] as f64;
                let c = close[i] as f64;
                if h.is_nan() || l.is_nan() || c.is_nan() {
                    carry[i] = 1;
                    continue;
                }
                let up = h - (high[i - 1] as f64);
                let dn = (low[i - 1] as f64) - l;
                pdm[i] = if up > 0.0 && up > dn { up } else { 0.0 };
                mdm[i] = if dn > 0.0 && dn > up { dn } else { 0.0 };
            }
        }
        (pdm, mdm, carry)
    }

    pub fn dx_batch_dev(
        &self,
        high: &[f32],
        low: &[f32],
        close: &[f32],
        sweep: &DxBatchRange,
    ) -> Result<(DeviceArrayF32, Vec<DxParams>), CudaDxError> {
        let (combos, first_valid, len) = Self::prepare_batch(high, low, close, sweep)?;
        let d_high = unsafe { DeviceBuffer::from_slice_async(high, &self.stream) }?;
        let d_low = unsafe { DeviceBuffer::from_slice_async(low, &self.stream) }?;
        let d_close = unsafe { DeviceBuffer::from_slice_async(close, &self.stream) }?;
        let out = self.dx_batch_dev_from_device_inputs(
            &d_high,
            &d_low,
            &d_close,
            len,
            first_valid,
            sweep,
        )?;
        self.stream.synchronize()?;
        Ok(out)
    }

    pub fn dx_batch_dev_from_device_inputs(
        &self,
        d_high: &DeviceBuffer<f32>,
        d_low: &DeviceBuffer<f32>,
        d_close: &DeviceBuffer<f32>,
        len: usize,
        first_valid: usize,
        sweep: &DxBatchRange,
    ) -> Result<(DeviceArrayF32, Vec<DxParams>), CudaDxError> {
        if len == 0 {
            return Err(CudaDxError::InvalidInput("empty input".into()));
        }
        if d_high.len() != len || d_low.len() != len || d_close.len() != len {
            return Err(CudaDxError::InvalidInput(
                "device input length mismatch".into(),
            ));
        }
        let periods = Self::expand_periods(sweep)?;
        let max_p = *periods.iter().max().unwrap();
        let _ = (periods.len())
            .checked_mul(max_p)
            .ok_or(CudaDxError::InvalidInput(
                "n_combos*max_period overflow".into(),
            ))?;
        if len - first_valid < max_p {
            return Err(CudaDxError::InvalidInput("not enough valid data".into()));
        }
        let combos: Vec<DxParams> = periods
            .iter()
            .map(|&p| DxParams { period: Some(p) })
            .collect();
        let rows = combos.len();

        let use_fast = match self.policy.batch {
            BatchKernelPolicy::Plain { .. } => false,
            BatchKernelPolicy::Auto => {
                (len >= 131_072 && rows >= 8) || (rows >= 64 && len >= 65_536)
            }
        };

        let sz_f64 = std::mem::size_of::<f64>();
        let sz_f32 = std::mem::size_of::<f32>();
        let sz_u8 = std::mem::size_of::<u8>();
        let sz_i32 = std::mem::size_of::<i32>();
        let rows_len = rows
            .checked_mul(len)
            .ok_or(CudaDxError::ArithmeticOverflow { what: "rows * len" })?;
        let base_terms = len
            .checked_mul(sz_f64)
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "len * size_of::<f64>()",
            })?;
        let req_bytes_terms = if use_fast {
            2usize
                .checked_mul(base_terms)
                .and_then(|x| x.checked_add(len.checked_mul(sz_u8)?))
                .and_then(|x| x.checked_add(rows.checked_mul(sz_i32)?))
                .and_then(|x| x.checked_add(rows_len.checked_mul(sz_f32)?))
        } else {
            3usize
                .checked_mul(base_terms)
                .and_then(|x| x.checked_add(len.checked_mul(sz_u8)?))
                .and_then(|x| x.checked_add(rows.checked_mul(sz_i32)?))
                .and_then(|x| x.checked_add(rows_len.checked_mul(sz_f32)?))
        }
        .ok_or(CudaDxError::ArithmeticOverflow {
            what: "req_bytes batch",
        })?;
        let req_bytes = req_bytes_terms;
        self.will_fit(req_bytes, 64 * 1024 * 1024)?;
        let mut d_pdm: DeviceBuffer<f64> =
            unsafe { DeviceBuffer::uninitialized_async(len, &self.stream) }?;
        let mut d_mdm: DeviceBuffer<f64> =
            unsafe { DeviceBuffer::uninitialized_async(len, &self.stream) }?;
        let mut d_tr: DeviceBuffer<f64> =
            unsafe { DeviceBuffer::uninitialized_async(len, &self.stream) }?;
        let mut d_carry: DeviceBuffer<u8> =
            unsafe { DeviceBuffer::uninitialized_async(len, &self.stream) }?;
        self.launch_precompute_terms_raw(
            d_high,
            d_low,
            d_close,
            len,
            &mut d_pdm,
            &mut d_mdm,
            &mut d_tr,
            &mut d_carry,
        )?;
        let periods_host: Vec<i32> = combos.iter().map(|c| c.period.unwrap() as i32).collect();
        let d_periods = unsafe { DeviceBuffer::from_slice_async(&periods_host, &self.stream) }?;
        let mut d_out: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized_async(rows * len, &self.stream) }?;

        self.launch_batch_symbol(
            if use_fast {
                "dx_batch_f32_fast"
            } else {
                "dx_batch_f32"
            },
            &d_pdm,
            &d_mdm,
            &d_tr,
            &d_carry,
            &d_periods,
            len,
            rows,
            first_valid,
            &mut d_out,
        )?;
        Ok((
            DeviceArrayF32 {
                buf: d_out,
                rows,
                cols: len,
            },
            combos,
        ))
    }

    #[allow(clippy::too_many_arguments)]
    fn launch_precompute_terms_raw(
        &self,
        d_high: &DeviceBuffer<f32>,
        d_low: &DeviceBuffer<f32>,
        d_close: &DeviceBuffer<f32>,
        len: usize,
        d_pdm: &mut DeviceBuffer<f64>,
        d_mdm: &mut DeviceBuffer<f64>,
        d_tr: &mut DeviceBuffer<f64>,
        d_carry: &mut DeviceBuffer<u8>,
    ) -> Result<(), CudaDxError> {
        let func = self
            .module
            .get_function("dx_build_terms_f64")
            .map_err(|_| CudaDxError::MissingKernelSymbol {
                name: "dx_build_terms_f64",
            })?;
        let grid: GridSize = (1, 1, 1).into();
        let block: BlockSize = (1, 1, 1).into();
        unsafe {
            let mut high_ptr = d_high.as_device_ptr().as_raw();
            let mut low_ptr = d_low.as_device_ptr().as_raw();
            let mut close_ptr = d_close.as_device_ptr().as_raw();
            let mut len_i = len as i32;
            let mut pdm_ptr = d_pdm.as_device_ptr().as_raw();
            let mut mdm_ptr = d_mdm.as_device_ptr().as_raw();
            let mut tr_ptr = d_tr.as_device_ptr().as_raw();
            let mut carry_ptr = d_carry.as_device_ptr().as_raw();
            let args: &mut [*mut c_void] = &mut [
                &mut high_ptr as *mut _ as *mut c_void,
                &mut low_ptr as *mut _ as *mut c_void,
                &mut close_ptr as *mut _ as *mut c_void,
                &mut len_i as *mut _ as *mut c_void,
                &mut pdm_ptr as *mut _ as *mut c_void,
                &mut mdm_ptr as *mut _ as *mut c_void,
                &mut tr_ptr as *mut _ as *mut c_void,
                &mut carry_ptr as *mut _ as *mut c_void,
            ];
            self.stream.launch(&func, grid, block, 0, args)?;
        }
        Ok(())
    }

    pub fn dx_batch_into_host_f32(
        &self,
        high: &[f32],
        low: &[f32],
        close: &[f32],
        sweep: &DxBatchRange,
        out: &mut [f32],
    ) -> Result<(usize, usize, Vec<DxParams>), CudaDxError> {
        let (arr, combos) = self.dx_batch_dev(high, low, close, sweep)?;
        let need = arr
            .rows
            .checked_mul(arr.cols)
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "rows * cols",
            })?;
        if out.len() != need {
            return Err(CudaDxError::InvalidInput(format!(
                "output slice wrong length: got {}, need {}",
                out.len(),
                need
            )));
        }

        let mut pinned: LockedBuffer<f32> = unsafe { LockedBuffer::uninitialized(need) }?;
        unsafe { arr.buf.async_copy_to(pinned.as_mut_slice(), &self.stream) }?;
        self.stream.synchronize()?;
        out.copy_from_slice(pinned.as_slice());
        Ok((arr.rows, arr.cols, combos))
    }

    fn launch_batch_symbol(
        &self,
        symbol: &'static str,
        d_pdm: &DeviceBuffer<f64>,
        d_mdm: &DeviceBuffer<f64>,
        d_tr: &DeviceBuffer<f64>,
        d_carry: &DeviceBuffer<u8>,
        d_periods: &DeviceBuffer<i32>,
        series_len: usize,
        n_combos: usize,
        first_valid: usize,
        d_out: &mut DeviceBuffer<f32>,
    ) -> Result<(), CudaDxError> {
        let func =
            self.module
                .get_function(symbol)
                .map_err(|_| CudaDxError::MissingKernelSymbol {
                    name: match symbol {
                        s => s,
                    },
                })?;
        let block_x = match self.policy.batch {
            BatchKernelPolicy::Auto => {
                const TARGET_BLOCKS: u32 = 64;
                let mut bx = ((n_combos as u32 + TARGET_BLOCKS - 1) / TARGET_BLOCKS).max(32);
                bx = ((bx + 31) / 32) * 32;
                bx.min(256)
            }
            BatchKernelPolicy::Plain { block_x } => block_x.max(32),
        };
        if std::env::var("BENCH_DEBUG").ok().as_deref() == Some("1") && !self.debug_batch_logged {
            eprintln!(
                "[dx] batch kernel ({}): block_x={} rows={} len={}",
                symbol, block_x, n_combos, series_len
            );
            unsafe {
                (*(self as *const _ as *mut CudaDx)).debug_batch_logged = true;
            }
        }
        unsafe {
            let grid_x = ((n_combos as u32) + block_x - 1) / block_x;
            if block_x > 1024 {
                return Err(CudaDxError::LaunchConfigTooLarge {
                    gx: grid_x.max(1),
                    gy: 1,
                    gz: 1,
                    bx: block_x,
                    by: 1,
                    bz: 1,
                });
            }
            let grid: GridSize = (grid_x.max(1), 1, 1).into();
            let block: BlockSize = (block_x, 1, 1).into();
            let mut pdm = d_pdm.as_device_ptr().as_raw();
            let mut mdm = d_mdm.as_device_ptr().as_raw();
            let mut tr = d_tr.as_device_ptr().as_raw();
            let mut car = d_carry.as_device_ptr().as_raw();
            let mut per = d_periods.as_device_ptr().as_raw();
            let mut n = series_len as i32;
            let mut r = n_combos as i32;
            let mut f = first_valid as i32;
            let mut o = d_out.as_device_ptr().as_raw();
            let mut args: [*mut c_void; 9] = [
                &mut pdm as *mut _ as *mut c_void,
                &mut mdm as *mut _ as *mut c_void,
                &mut tr as *mut _ as *mut c_void,
                &mut car as *mut _ as *mut c_void,
                &mut per as *mut _ as *mut c_void,
                &mut n as *mut _ as *mut c_void,
                &mut r as *mut _ as *mut c_void,
                &mut f as *mut _ as *mut c_void,
                &mut o as *mut _ as *mut c_void,
            ];
            self.stream.launch(&func, grid, block, 0, &mut args)?;
        }
        Ok(())
    }

    pub fn dx_many_series_one_param_time_major_dev(
        &self,
        high_tm: &[f32],
        low_tm: &[f32],
        close_tm: &[f32],
        cols: usize,
        rows: usize,
        period: usize,
    ) -> Result<DeviceArrayF32, CudaDxError> {
        if cols == 0 || rows == 0 {
            return Err(CudaDxError::InvalidInput("empty matrix".into()));
        }
        let expected = cols
            .checked_mul(rows)
            .ok_or(CudaDxError::InvalidInput("rows*cols overflow".into()))?;
        if high_tm.len() != expected || low_tm.len() != expected || close_tm.len() != expected {
            return Err(CudaDxError::InvalidInput("matrix shape mismatch".into()));
        }

        let mut first_valids = vec![rows as i32; cols];

        for s in 0..cols {
            for t in 0..rows {
                let idx = t * cols + s;
                if !high_tm[idx].is_nan() && !low_tm[idx].is_nan() && !close_tm[idx].is_nan() {
                    first_valids[s] = t as i32;
                    break;
                }
            }
        }
        for &fv in &first_valids {
            if (fv as usize) + period - 1 >= rows {
                return Err(CudaDxError::InvalidInput(
                    "not enough valid data for at least one series".into(),
                ));
            }
        }

        let sz_f32 = std::mem::size_of::<f32>();
        let three_cols_rows = 3usize
            .checked_mul(cols)
            .and_then(|x| x.checked_mul(rows))
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "3 * cols * rows",
            })?;
        let cols_bytes = cols
            .checked_mul(sz_f32)
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "cols * size_of::<f32>()",
            })?;
        let cols_rows_bytes = cols
            .checked_mul(rows)
            .and_then(|x| x.checked_mul(sz_f32))
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "cols * rows * size_of::<f32>()",
            })?;
        let req = three_cols_rows
            .checked_mul(sz_f32)
            .and_then(|x| x.checked_add(cols_bytes))
            .and_then(|x| x.checked_add(cols_rows_bytes))
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "req_bytes many-series",
            })?;
        self.will_fit(req, 64 * 1024 * 1024)?;

        let d_high = unsafe { DeviceBuffer::from_slice_async(high_tm, &self.stream) }?;
        let d_low = unsafe { DeviceBuffer::from_slice_async(low_tm, &self.stream) }?;
        let d_close = unsafe { DeviceBuffer::from_slice_async(close_tm, &self.stream) }?;
        let d_first = DeviceBuffer::from_slice(&first_valids)?;
        let out_elems = cols
            .checked_mul(rows)
            .ok_or(CudaDxError::ArithmeticOverflow {
                what: "cols * rows",
            })?;
        let mut d_out: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized_async(out_elems, &self.stream) }?;

        let use_fast = match self.policy.many_series {
            ManySeriesKernelPolicy::OneD { .. } => false,
            ManySeriesKernelPolicy::Auto => rows >= 8192 && cols >= 64,
        };
        self.launch_many_series_symbol(
            if use_fast {
                "dx_many_series_one_param_time_major_f32_fast"
            } else {
                "dx_many_series_one_param_time_major_f32"
            },
            &d_high,
            &d_low,
            &d_close,
            cols,
            rows,
            period,
            &d_first,
            &mut d_out,
        )?;
        self.stream.synchronize()?;
        Ok(DeviceArrayF32 {
            buf: d_out,
            rows,
            cols,
        })
    }

    pub fn dx_many_series_one_param_time_major_into_host_f32(
        &self,
        high_tm: &[f32],
        low_tm: &[f32],
        close_tm: &[f32],
        cols: usize,
        rows: usize,
        period: usize,
        out_tm: &mut [f32],
    ) -> Result<(), CudaDxError> {
        let expected = cols
            .checked_mul(rows)
            .ok_or(CudaDxError::InvalidInput("rows*cols overflow".into()))?;
        if out_tm.len() != expected {
            return Err(CudaDxError::InvalidInput("out slice wrong length".into()));
        }
        let arr = self.dx_many_series_one_param_time_major_dev(
            high_tm, low_tm, close_tm, cols, rows, period,
        )?;
        let mut pinned: LockedBuffer<f32> = unsafe { LockedBuffer::uninitialized(arr.len()) }?;
        unsafe { arr.buf.async_copy_to(pinned.as_mut_slice(), &self.stream) }?;
        self.stream.synchronize()?;
        out_tm.copy_from_slice(pinned.as_slice());
        Ok(())
    }

    fn launch_many_series_symbol(
        &self,
        symbol: &'static str,
        d_high: &DeviceBuffer<f32>,
        d_low: &DeviceBuffer<f32>,
        d_close: &DeviceBuffer<f32>,
        cols: usize,
        rows: usize,
        period: usize,
        d_first_valids: &DeviceBuffer<i32>,
        d_out: &mut DeviceBuffer<f32>,
    ) -> Result<(), CudaDxError> {
        let func =
            self.module
                .get_function(symbol)
                .map_err(|_| CudaDxError::MissingKernelSymbol {
                    name: match symbol {
                        s => s,
                    },
                })?;
        let block_x = match self.policy.many_series {
            ManySeriesKernelPolicy::Auto => 256,
            ManySeriesKernelPolicy::OneD { block_x } => block_x.max(32),
        };
        if std::env::var("BENCH_DEBUG").ok().as_deref() == Some("1") && !self.debug_many_logged {
            eprintln!(
                "[dx] many-series kernel ({}): block_x={} cols={} rows={} period={}",
                symbol, block_x, cols, rows, period
            );
            unsafe {
                (*(self as *const _ as *mut CudaDx)).debug_many_logged = true;
            }
        }
        unsafe {
            let grid_x = ((cols as u32) + block_x - 1) / block_x;
            if block_x > 1024 {
                return Err(CudaDxError::LaunchConfigTooLarge {
                    gx: grid_x.max(1),
                    gy: 1,
                    gz: 1,
                    bx: block_x,
                    by: 1,
                    bz: 1,
                });
            }
            let grid: GridSize = (grid_x.max(1), 1, 1).into();
            let block: BlockSize = (block_x, 1, 1).into();
            let mut h = d_high.as_device_ptr().as_raw();
            let mut l = d_low.as_device_ptr().as_raw();
            let mut c = d_close.as_device_ptr().as_raw();
            let mut cols_i = cols as i32;
            let mut rows_i = rows as i32;
            let mut p = period as i32;
            let mut fv = d_first_valids.as_device_ptr().as_raw();
            let mut o = d_out.as_device_ptr().as_raw();
            let mut args: [*mut c_void; 8] = [
                &mut h as *mut _ as *mut c_void,
                &mut l as *mut _ as *mut c_void,
                &mut c as *mut _ as *mut c_void,
                &mut cols_i as *mut _ as *mut c_void,
                &mut rows_i as *mut _ as *mut c_void,
                &mut p as *mut _ as *mut c_void,
                &mut fv as *mut _ as *mut c_void,
                &mut o as *mut _ as *mut c_void,
            ];
            self.stream.launch(&func, grid, block, 0, &mut args)?;
        }
        Ok(())
    }
}

pub mod benches {
    use super::*;
    use crate::cuda::bench::helpers::gen_series;
    use crate::cuda::bench::{CudaBenchScenario, CudaBenchState};

    const LEN_1M: usize = 1_000_000;
    const COLS_512: usize = 512;
    const ROWS_16K: usize = 16_384;

    fn synth_hlc_from_close(close: &[f32]) -> (Vec<f32>, Vec<f32>) {
        let mut high = close.to_vec();
        let mut low = close.to_vec();
        for i in 0..close.len() {
            let v = close[i];
            if v.is_nan() {
                continue;
            }
            if v.is_nan() {
                continue;
            }
            let x = i as f32 * 0.0025;
            let off = (0.002 * x.sin()).abs() + 0.15;
            high[i] = v + off;
            low[i] = v - off;
        }
        (high, low)
    }

    struct BatchState {
        cuda: CudaDx,
        d_pdm: DeviceBuffer<f64>,
        d_mdm: DeviceBuffer<f64>,
        d_tr: DeviceBuffer<f64>,
        d_carry: DeviceBuffer<u8>,
        d_periods: DeviceBuffer<i32>,
        len: usize,
        first_valid: usize,
        n_combos: usize,
        d_out: DeviceBuffer<f32>,
    }
    impl CudaBenchState for BatchState {
        fn launch(&mut self) {
            self.cuda
                .launch_batch_symbol(
                    "dx_batch_f32_fast",
                    &self.d_pdm,
                    &self.d_mdm,
                    &self.d_tr,
                    &self.d_carry,
                    &self.d_periods,
                    self.len,
                    self.n_combos,
                    self.first_valid,
                    &mut self.d_out,
                )
                .expect("dx batch kernel");
            self.cuda.stream.synchronize().expect("dx sync");
        }
    }

    struct ManySeriesState {
        cuda: CudaDx,
        d_high_tm: DeviceBuffer<f32>,
        d_low_tm: DeviceBuffer<f32>,
        d_close_tm: DeviceBuffer<f32>,
        d_first_valids: DeviceBuffer<i32>,
        cols: usize,
        rows: usize,
        period: usize,
        symbol: &'static str,
        d_out_tm: DeviceBuffer<f32>,
    }
    impl CudaBenchState for ManySeriesState {
        fn launch(&mut self) {
            self.cuda
                .launch_many_series_symbol(
                    self.symbol,
                    &self.d_high_tm,
                    &self.d_low_tm,
                    &self.d_close_tm,
                    self.cols,
                    self.rows,
                    self.period,
                    &self.d_first_valids,
                    &mut self.d_out_tm,
                )
                .expect("dx many-series kernel");
            self.cuda.stream.synchronize().expect("dx sync");
        }
    }

    fn prep_batch() -> Box<dyn CudaBenchState> {
        let cuda = CudaDx::new(0).expect("cuda dx");
        let close = gen_series(LEN_1M);
        let (high, low) = synth_hlc_from_close(&close);
        let sweep = DxBatchRange { period: (8, 64, 8) };
        let (combos, first_valid, len) =
            CudaDx::prepare_batch(&high, &low, &close, &sweep).expect("prepare_batch");
        let n_combos = combos.len();
        let periods_host: Vec<i32> = combos.iter().map(|c| c.period.unwrap() as i32).collect();
        let (pdm, mdm, carry) = CudaDx::precompute_dm_and_carry(&high, &low, &close);

        let d_pdm = unsafe { DeviceBuffer::from_slice_async(&pdm, &cuda.stream) }.expect("d_pdm");
        let d_mdm = unsafe { DeviceBuffer::from_slice_async(&mdm, &cuda.stream) }.expect("d_mdm");
        let d_tr: DeviceBuffer<f64> =
            unsafe { DeviceBuffer::uninitialized_async(1, &cuda.stream) }.expect("d_tr");
        let d_carry =
            unsafe { DeviceBuffer::from_slice_async(&carry, &cuda.stream) }.expect("d_carry");
        let d_periods = unsafe { DeviceBuffer::from_slice_async(&periods_host, &cuda.stream) }
            .expect("d_periods");
        let d_out: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized_async(n_combos * len, &cuda.stream) }
                .expect("d_out");
        cuda.stream.synchronize().expect("sync after prep");

        Box::new(BatchState {
            cuda,
            d_pdm,
            d_mdm,
            d_tr,
            d_carry,
            d_periods,
            len,
            first_valid,
            n_combos,
            d_out,
        })
    }

    fn prep_many() -> Box<dyn CudaBenchState> {
        let cuda = CudaDx::new(0).expect("cuda dx");
        let cols = COLS_512;
        let rows = ROWS_16K;
        let close_tm = {
            let mut v = vec![f32::NAN; cols * rows];
            for s in 0..cols {
                for t in s..rows {
                    let x = (t as f32) + (s as f32) * 0.2;
                    v[t * cols + s] = (x * 0.002).sin() + 0.0003 * x;
                }
            }
            v
        };
        let (high_tm, low_tm) = synth_hlc_from_close(&close_tm);
        let period = 14usize;
        let mut first_valids = vec![rows as i32; cols];
        for s in 0..cols {
            for t in 0..rows {
                let idx = t * cols + s;
                if !high_tm[idx].is_nan() && !low_tm[idx].is_nan() && !close_tm[idx].is_nan() {
                    first_valids[s] = t as i32;
                    break;
                }
            }
        }
        let d_high_tm = DeviceBuffer::from_slice(&high_tm).expect("d_high_tm");
        let d_low_tm = DeviceBuffer::from_slice(&low_tm).expect("d_low_tm");
        let d_close_tm = DeviceBuffer::from_slice(&close_tm).expect("d_close_tm");
        let d_first_valids = DeviceBuffer::from_slice(&first_valids).expect("d_first_valids");
        let elems_out = cols * rows;
        let d_out_tm: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized(elems_out) }.expect("d_out_tm");
        let use_fast = match cuda.policy.many_series {
            ManySeriesKernelPolicy::OneD { .. } => false,
            ManySeriesKernelPolicy::Auto => rows >= 8192 && cols >= 64,
        };
        let symbol = if use_fast {
            "dx_many_series_one_param_time_major_f32_fast"
        } else {
            "dx_many_series_one_param_time_major_f32"
        };
        cuda.stream.synchronize().expect("sync after prep");
        Box::new(ManySeriesState {
            cuda,
            d_high_tm,
            d_low_tm,
            d_close_tm,
            d_first_valids,
            cols,
            rows,
            period,
            symbol,
            d_out_tm,
        })
    }

    fn bytes_batch() -> usize {
        (3 * LEN_1M + LEN_1M + (LEN_1M / 8) + (LEN_1M * ((64 - 8) / 8 + 1)))
            * std::mem::size_of::<f32>()
            + 64 * 1024 * 1024
    }
    fn bytes_many() -> usize {
        (3 * COLS_512 * ROWS_16K + COLS_512 + COLS_512 * ROWS_16K) * std::mem::size_of::<f32>()
            + 64 * 1024 * 1024
    }

    pub fn bench_profiles() -> Vec<CudaBenchScenario> {
        vec![
            CudaBenchScenario::new("dx", "batch", "dx_cuda_batch", "1m", prep_batch)
                .with_mem_required(bytes_batch()),
            CudaBenchScenario::new(
                "dx",
                "many_series_one_param",
                "dx_cuda_many_series",
                "16k x 512",
                prep_many,
            )
            .with_mem_required(bytes_many()),
        ]
    }
}
