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

use crate::cuda::moving_averages::DeviceArrayF32;
use crate::indicators::minmax::{MinmaxBatchRange, MinmaxParams};
use cust::context::Context;
use cust::device::Device;
use cust::function::{BlockSize, GridSize};
use cust::memory::{mem_get_info, DeviceBuffer};
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;

#[inline]
fn floor_log2_usize(mut n: usize) -> usize {
    debug_assert!(n > 0);
    let mut p = 0usize;
    while n > 1 {
        n >>= 1;
        p += 1;
    }
    p
}

#[inline]
fn sparse_table_levels(n: usize) -> usize {
    if n == 0 {
        0
    } else {
        floor_log2_usize(n) + 1
    }
}

#[inline]
fn rmq_scratch_bytes(n: usize) -> usize {
    10usize
        .saturating_mul(sparse_table_levels(n))
        .saturating_mul(n)
}

#[derive(Error, Debug)]
pub enum CudaMinmaxError {
    #[error("CUDA error: {0}")]
    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("not implemented")]
    NotImplemented,
}

pub struct DeviceMinmaxQuad {
    pub is_min: DeviceBuffer<f32>,
    pub is_max: DeviceBuffer<f32>,
    pub last_min: DeviceBuffer<f32>,
    pub last_max: DeviceBuffer<f32>,
    pub rows: usize,
    pub cols: usize,
}

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

impl Default for BatchKernelPolicy {
    fn default() -> Self {
        BatchKernelPolicy::Auto
    }
}

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

impl Default for ManySeriesKernelPolicy {
    fn default() -> Self {
        ManySeriesKernelPolicy::Auto
    }
}

#[derive(Clone, Copy, Debug, Default)]
pub struct CudaMinmaxPolicy {
    pub batch: BatchKernelPolicy,
    pub many_series: ManySeriesKernelPolicy,
}

pub struct CudaMinmax {
    module: Module,
    stream: Stream,
    context: Arc<Context>,
    device_id: u32,
    policy: CudaMinmaxPolicy,
}

impl CudaMinmax {
    pub fn new(device_id: usize) -> Result<Self, CudaMinmaxError> {
        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"), "/minmax_kernel.ptx"));
        let jit_opts = &[
            ModuleJitOption::DetermineTargetFromContext,
            ModuleJitOption::OptLevel(OptLevel::O2),
        ];
        let module = match Module::from_ptx(ptx, jit_opts) {
            Ok(m) => m,
            Err(_) => {
                if let Ok(m) = Module::from_ptx(ptx, &[ModuleJitOption::DetermineTargetFromContext])
                {
                    m
                } else {
                    Module::from_ptx(ptx, &[])?
                }
            }
        };
        let stream = Stream::new(StreamFlags::NON_BLOCKING, None)?;

        Ok(Self {
            module,
            stream,
            context,
            device_id: device_id as u32,
            policy: CudaMinmaxPolicy::default(),
        })
    }

    pub fn set_policy(&mut self, p: CudaMinmaxPolicy) {
        self.policy = p;
    }
    pub fn context_arc(&self) -> Arc<Context> {
        self.context.clone()
    }
    pub fn device_id(&self) -> u32 {
        self.device_id
    }
    pub fn synchronize(&self) -> Result<(), CudaMinmaxError> {
        self.stream.synchronize().map_err(CudaMinmaxError::from)
    }

    #[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(bytes: usize, headroom: usize) -> bool {
        if !Self::mem_check_enabled() {
            return true;
        }
        if let Some((free, _)) = Self::device_mem_info() {
            bytes.saturating_add(headroom) <= free
        } else {
            true
        }
    }

    pub fn minmax_batch_dev(
        &self,
        high: &[f32],
        low: &[f32],
        sweep: &MinmaxBatchRange,
    ) -> Result<(DeviceMinmaxQuad, Vec<MinmaxParams>), CudaMinmaxError> {
        if high.is_empty() || low.is_empty() || high.len() != low.len() {
            return Err(CudaMinmaxError::InvalidInput(
                "inputs must be non-empty and same length".into(),
            ));
        }
        let len = high.len();

        let mut first_valid: Option<i32> = None;
        for (i, (&h, &l)) in high.iter().zip(low.iter()).enumerate() {
            if h.is_finite() && l.is_finite() {
                first_valid = Some(i as i32);
                break;
            }
        }
        let first_valid = first_valid
            .ok_or_else(|| CudaMinmaxError::InvalidInput("all values are NaN".into()))?;

        let d_high = DeviceBuffer::from_slice(high)?;
        let d_low = DeviceBuffer::from_slice(low)?;
        let out = self.minmax_batch_dev_from_device_inputs(
            &d_high,
            &d_low,
            len,
            first_valid as usize,
            sweep,
        )?;
        self.stream.synchronize()?;
        return Ok(out);

        let combos = expand_grid(sweep)?;

        let max_o = combos
            .iter()
            .map(|c| c.order.unwrap_or(3))
            .max()
            .unwrap_or(3);
        if len - (first_valid as usize) < max_o {
            return Err(CudaMinmaxError::InvalidInput(format!(
                "not enough valid data for max order {} (valid after first={}): {}",
                max_o,
                first_valid,
                len - (first_valid as usize)
            )));
        }

        let in_bytes = len
            .checked_mul(std::mem::size_of::<f32>())
            .and_then(|b| b.checked_mul(2))
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput("input size overflow for minmax batch".into())
            })?;
        let params_bytes = combos
            .len()
            .checked_mul(std::mem::size_of::<i32>())
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput("params size overflow for minmax batch".into())
            })?;
        let out_elems = combos
            .len()
            .checked_mul(len)
            .and_then(|n| n.checked_mul(4))
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput(
                    "output element count overflow for minmax batch".into(),
                )
            })?;
        let out_bytes = out_elems
            .checked_mul(std::mem::size_of::<f32>())
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput("output size overflow for minmax batch".into())
            })?;
        let base_required = in_bytes
            .checked_add(params_bytes)
            .and_then(|b| b.checked_add(out_bytes))
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput(
                    "total allocation size overflow for minmax batch".into(),
                )
            })?;

        if !Self::will_fit(base_required, 64 * 1024 * 1024) {
            if let Some((free, _)) = Self::device_mem_info() {
                return Err(CudaMinmaxError::OutOfMemory {
                    required: base_required,
                    free,
                    headroom: 64 * 1024 * 1024,
                });
            } else {
                return Err(CudaMinmaxError::InvalidInput(
                    "insufficient device memory for minmax batch".into(),
                ));
            }
        }

        let d_high = DeviceBuffer::from_slice(high)?;
        let d_low = DeviceBuffer::from_slice(low)?;
        let orders_i32: Vec<i32> = combos.iter().map(|p| p.order.unwrap_or(3) as i32).collect();
        let d_orders = DeviceBuffer::from_slice(&orders_i32)?;

        let elems = combos
            .len()
            .checked_mul(len)
            .ok_or_else(|| CudaMinmaxError::InvalidInput("output element count overflow".into()))?;
        let mut d_is_min: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;
        let mut d_is_max: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;
        let mut d_last_min: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;
        let mut d_last_max: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;

        let k_levels = sparse_table_levels(len);
        let rmq_bytes = rmq_scratch_bytes(len);
        let avg_k = {
            let s: f64 = combos.iter().map(|c| c.order.unwrap_or(3) as f64).sum();
            s / (combos.len() as f64)
        };

        let use_rmq_by_cost = (combos.len() as f64) * avg_k >= 16.0 * (len as f64).log2().max(1.0);

        let rmq_required = base_required.checked_add(rmq_bytes).unwrap_or(usize::MAX);
        let want_rmq = match self.policy.batch {
            BatchKernelPolicy::Plain { .. } => false,
            BatchKernelPolicy::Auto => {
                use_rmq_by_cost && Self::will_fit(rmq_required, 64 * 1024 * 1024)
            }
        };

        let mut series_len_i = len as i32;
        let mut first_valid_i = first_valid as i32;

        if want_rmq {
            let st_elems = k_levels.checked_mul(len).ok_or_else(|| {
                CudaMinmaxError::InvalidInput("sparse table size overflow in minmax batch".into())
            })?;
            let mut st_low_min: DeviceBuffer<f32> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;
            let mut st_high_max: DeviceBuffer<f32> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;
            let mut st_valid_low: DeviceBuffer<u8> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;
            let mut st_valid_high: DeviceBuffer<u8> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;

            let f_init = self
                .module
                .get_function("st_init_level0_minmax_valid_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "st_init_level0_minmax_valid_f32",
                })?;
            let f_build = self
                .module
                .get_function("st_build_level_k_minmax_valid_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "st_build_level_k_minmax_valid_f32",
                })?;
            let f_rmq = self
                .module
                .get_function("minmax_batch_rmq_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "minmax_batch_rmq_f32",
                })?;
            let f_ff = self
                .module
                .get_function("forward_fill_two_streams_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "forward_fill_two_streams_f32",
                })?;

            let block_x = 256u32;
            let grid_x = ((len as u32) + block_x - 1) / block_x;
            let grid: GridSize = (grid_x, 1, 1).into();
            let block: BlockSize = (block_x, 1, 1).into();
            unsafe {
                let mut low_ptr = d_low.as_device_ptr().as_raw();
                let mut high_ptr = d_high.as_device_ptr().as_raw();
                let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
                let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
                let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
                let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
                let args: &mut [*mut c_void] = &mut [
                    &mut low_ptr as *mut _ as *mut c_void,
                    &mut high_ptr as *mut _ as *mut c_void,
                    &mut series_len_i as *mut _ as *mut c_void,
                    &mut low_min_ptr as *mut _ as *mut c_void,
                    &mut high_max_ptr as *mut _ as *mut c_void,
                    &mut vlow_ptr as *mut _ as *mut c_void,
                    &mut vhigh_ptr as *mut _ as *mut c_void,
                ];
                self.stream.launch(&f_init, grid, block, 0, args)?;
            }

            for k in 1..k_levels {
                let span = 1usize << k;
                if len < span {
                    break;
                }
                let valid_pos = len - span + 1;
                let grid_kx = ((valid_pos as u32) + block_x - 1) / block_x;
                let gridk: GridSize = (grid_kx, 1, 1).into();
                unsafe {
                    let mut k_i = k as i32;
                    let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
                    let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
                    let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
                    let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
                    let args: &mut [*mut c_void] = &mut [
                        &mut series_len_i as *mut _ as *mut c_void,
                        &mut k_i as *mut _ as *mut c_void,
                        &mut low_min_ptr as *mut _ as *mut c_void,
                        &mut high_max_ptr as *mut _ as *mut c_void,
                        &mut vlow_ptr as *mut _ as *mut c_void,
                        &mut vhigh_ptr as *mut _ as *mut c_void,
                    ];
                    self.stream.launch(&f_build, gridk, block, 0, args)?;
                }
            }

            let block_x = match self.policy.batch {
                BatchKernelPolicy::Plain { block_x } => block_x.max(64),
                _ => 256,
            };
            let grid_x = ((len as u32) + block_x - 1) / block_x;

            const MAX_GRID_Y: usize = 65_535;
            let mut start = 0usize;
            while start < combos.len() {
                let count = (combos.len() - start).min(MAX_GRID_Y);
                let grid: GridSize = (grid_x, count as u32, 1).into();
                let block: BlockSize = (block_x, 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 orders_ptr = d_orders.as_device_ptr().add(start).as_raw();
                    let mut nrows_i = count as i32;
                    let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
                    let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
                    let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
                    let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
                    let mut is_min_ptr = d_is_min.as_device_ptr().add(start * len).as_raw();
                    let mut is_max_ptr = d_is_max.as_device_ptr().add(start * len).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 series_len_i as *mut _ as *mut c_void,
                        &mut first_valid_i as *mut _ as *mut c_void,
                        &mut orders_ptr as *mut _ as *mut c_void,
                        &mut nrows_i as *mut _ as *mut c_void,
                        &mut low_min_ptr as *mut _ as *mut c_void,
                        &mut high_max_ptr as *mut _ as *mut c_void,
                        &mut vlow_ptr as *mut _ as *mut c_void,
                        &mut vhigh_ptr as *mut _ as *mut c_void,
                        &mut is_min_ptr as *mut _ as *mut c_void,
                        &mut is_max_ptr as *mut _ as *mut c_void,
                    ];
                    self.stream.launch(&f_rmq, grid, block, 0, args)?;
                }

                let ff_block_x = 1024u32;
                let ff_grid: GridSize = (count as u32, 1, 1).into();
                let ff_block: BlockSize = (ff_block_x, 1, 1).into();
                unsafe {
                    let mut is_min_ptr = d_is_min.as_device_ptr().add(start * len).as_raw();
                    let mut is_max_ptr = d_is_max.as_device_ptr().add(start * len).as_raw();
                    let mut rows_i = count as i32;
                    let mut last_min_ptr = d_last_min.as_device_ptr().add(start * len).as_raw();
                    let mut last_max_ptr = d_last_max.as_device_ptr().add(start * len).as_raw();
                    let args: &mut [*mut c_void] = &mut [
                        &mut is_min_ptr as *mut _ as *mut c_void,
                        &mut is_max_ptr as *mut _ as *mut c_void,
                        &mut series_len_i as *mut _ as *mut c_void,
                        &mut rows_i as *mut _ as *mut c_void,
                        &mut last_min_ptr as *mut _ as *mut c_void,
                        &mut last_max_ptr as *mut _ as *mut c_void,
                    ];
                    self.stream.launch(&f_ff, ff_grid, ff_block, 0, args)?;
                }

                start += count;
            }

            self.stream.synchronize()?;
            return Ok((
                DeviceMinmaxQuad {
                    is_min: d_is_min,
                    is_max: d_is_max,
                    last_min: d_last_min,
                    last_max: d_last_max,
                    rows: combos.len(),
                    cols: len,
                },
                combos,
            ));
        }

        let block_x = match self.policy.batch {
            BatchKernelPolicy::Plain { block_x } => block_x.max(64),
            _ => 256,
        };
        let grid_x = 1u32;
        let func = self.module.get_function("minmax_batch_f32").map_err(|_| {
            CudaMinmaxError::MissingKernelSymbol {
                name: "minmax_batch_f32",
            }
        })?;

        let mut start = 0usize;
        const MAX_GRID_Y: usize = 65_535;
        while start < combos.len() {
            let count = (combos.len() - start).min(MAX_GRID_Y);
            let grid: GridSize = (grid_x, count as u32, 1).into();
            let block: BlockSize = (block_x, 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 orders_ptr = d_orders.as_device_ptr().add(start).as_raw();
                let mut nrows_i = count as i32;
                let mut is_min_ptr = d_is_min.as_device_ptr().add(start * len).as_raw();
                let mut is_max_ptr = d_is_max.as_device_ptr().add(start * len).as_raw();
                let mut last_min_ptr = d_last_min.as_device_ptr().add(start * len).as_raw();
                let mut last_max_ptr = d_last_max.as_device_ptr().add(start * len).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 series_len_i as *mut _ as *mut c_void,
                    &mut first_valid_i as *mut _ as *mut c_void,
                    &mut orders_ptr as *mut _ as *mut c_void,
                    &mut nrows_i as *mut _ as *mut c_void,
                    &mut is_min_ptr as *mut _ as *mut c_void,
                    &mut is_max_ptr as *mut _ as *mut c_void,
                    &mut last_min_ptr as *mut _ as *mut c_void,
                    &mut last_max_ptr as *mut _ as *mut c_void,
                ];
                self.stream.launch(&func, grid, block, 0, args)?;
            }
            start += count;
        }
        self.stream.synchronize()?;

        Ok((
            DeviceMinmaxQuad {
                is_min: d_is_min,
                is_max: d_is_max,
                last_min: d_last_min,
                last_max: d_last_max,
                rows: combos.len(),
                cols: len,
            },
            combos,
        ))
    }

    pub fn minmax_batch_dev_from_device_inputs(
        &self,
        d_high: &DeviceBuffer<f32>,
        d_low: &DeviceBuffer<f32>,
        len: usize,
        first_valid: usize,
        sweep: &MinmaxBatchRange,
    ) -> Result<(DeviceMinmaxQuad, Vec<MinmaxParams>), CudaMinmaxError> {
        if len == 0 || d_high.len() != len || d_low.len() != len {
            return Err(CudaMinmaxError::InvalidInput(format!(
                "device inputs must be non-empty and match len (high={}, low={}, len={})",
                d_high.len(),
                d_low.len(),
                len
            )));
        }
        if first_valid >= len {
            return Err(CudaMinmaxError::InvalidInput(
                "first_valid must be within the input length".into(),
            ));
        }

        let combos = expand_grid(sweep)?;

        let max_o = combos
            .iter()
            .map(|c| c.order.unwrap_or(3))
            .max()
            .unwrap_or(3);
        if len - first_valid < max_o {
            return Err(CudaMinmaxError::InvalidInput(format!(
                "not enough valid data for max order {} (valid after first={}): {}",
                max_o,
                first_valid,
                len - first_valid
            )));
        }

        let params_bytes = combos
            .len()
            .checked_mul(std::mem::size_of::<i32>())
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput("params size overflow for minmax batch".into())
            })?;
        let out_elems = combos
            .len()
            .checked_mul(len)
            .and_then(|n| n.checked_mul(4))
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput(
                    "output element count overflow for minmax batch".into(),
                )
            })?;
        let out_bytes = out_elems
            .checked_mul(std::mem::size_of::<f32>())
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput("output size overflow for minmax batch".into())
            })?;
        let base_required = params_bytes.checked_add(out_bytes).ok_or_else(|| {
            CudaMinmaxError::InvalidInput("total allocation size overflow for minmax batch".into())
        })?;

        if !Self::will_fit(base_required, 64 * 1024 * 1024) {
            if let Some((free, _)) = Self::device_mem_info() {
                return Err(CudaMinmaxError::OutOfMemory {
                    required: base_required,
                    free,
                    headroom: 64 * 1024 * 1024,
                });
            } else {
                return Err(CudaMinmaxError::InvalidInput(
                    "insufficient device memory for minmax batch".into(),
                ));
            }
        }

        let orders_i32: Vec<i32> = combos.iter().map(|p| p.order.unwrap_or(3) as i32).collect();
        let d_orders = DeviceBuffer::from_slice(&orders_i32)?;

        let elems = combos
            .len()
            .checked_mul(len)
            .ok_or_else(|| CudaMinmaxError::InvalidInput("output element count overflow".into()))?;
        let mut d_is_min: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;
        let mut d_is_max: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;
        let mut d_last_min: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;
        let mut d_last_max: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(elems) }?;

        let k_levels = sparse_table_levels(len);
        let rmq_bytes = rmq_scratch_bytes(len);
        let avg_k = {
            let s: f64 = combos.iter().map(|c| c.order.unwrap_or(3) as f64).sum();
            s / (combos.len() as f64)
        };

        let use_rmq_by_cost = (combos.len() as f64) * avg_k >= 16.0 * (len as f64).log2().max(1.0);

        let rmq_required = base_required.checked_add(rmq_bytes).unwrap_or(usize::MAX);
        let want_rmq = match self.policy.batch {
            BatchKernelPolicy::Plain { .. } => false,
            BatchKernelPolicy::Auto => {
                use_rmq_by_cost && Self::will_fit(rmq_required, 64 * 1024 * 1024)
            }
        };

        let mut series_len_i = len as i32;
        let mut first_valid_i = first_valid as i32;

        if want_rmq {
            let st_elems = k_levels.checked_mul(len).ok_or_else(|| {
                CudaMinmaxError::InvalidInput("sparse table size overflow in minmax batch".into())
            })?;
            let mut st_low_min: DeviceBuffer<f32> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;
            let mut st_high_max: DeviceBuffer<f32> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;
            let mut st_valid_low: DeviceBuffer<u8> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;
            let mut st_valid_high: DeviceBuffer<u8> =
                unsafe { DeviceBuffer::uninitialized(st_elems) }?;

            let f_init = self
                .module
                .get_function("st_init_level0_minmax_valid_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "st_init_level0_minmax_valid_f32",
                })?;
            let f_build = self
                .module
                .get_function("st_build_level_k_minmax_valid_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "st_build_level_k_minmax_valid_f32",
                })?;
            let f_rmq = self
                .module
                .get_function("minmax_batch_rmq_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "minmax_batch_rmq_f32",
                })?;
            let f_ff = self
                .module
                .get_function("forward_fill_two_streams_f32")
                .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                    name: "forward_fill_two_streams_f32",
                })?;

            let block_x = 256u32;
            let grid_x = ((len as u32) + block_x - 1) / block_x;
            let grid: GridSize = (grid_x, 1, 1).into();
            let block: BlockSize = (block_x, 1, 1).into();
            unsafe {
                let mut low_ptr = d_low.as_device_ptr().as_raw();
                let mut high_ptr = d_high.as_device_ptr().as_raw();
                let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
                let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
                let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
                let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
                let args: &mut [*mut c_void] = &mut [
                    &mut low_ptr as *mut _ as *mut c_void,
                    &mut high_ptr as *mut _ as *mut c_void,
                    &mut series_len_i as *mut _ as *mut c_void,
                    &mut low_min_ptr as *mut _ as *mut c_void,
                    &mut high_max_ptr as *mut _ as *mut c_void,
                    &mut vlow_ptr as *mut _ as *mut c_void,
                    &mut vhigh_ptr as *mut _ as *mut c_void,
                ];
                self.stream.launch(&f_init, grid, block, 0, args)?;
            }

            for k in 1..k_levels {
                let span = 1usize << k;
                if len < span {
                    break;
                }
                let valid_pos = len - span + 1;
                let grid_kx = ((valid_pos as u32) + block_x - 1) / block_x;
                let gridk: GridSize = (grid_kx, 1, 1).into();
                unsafe {
                    let mut k_i = k as i32;
                    let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
                    let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
                    let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
                    let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
                    let args: &mut [*mut c_void] = &mut [
                        &mut series_len_i as *mut _ as *mut c_void,
                        &mut k_i as *mut _ as *mut c_void,
                        &mut low_min_ptr as *mut _ as *mut c_void,
                        &mut high_max_ptr as *mut _ as *mut c_void,
                        &mut vlow_ptr as *mut _ as *mut c_void,
                        &mut vhigh_ptr as *mut _ as *mut c_void,
                    ];
                    self.stream.launch(&f_build, gridk, block, 0, args)?;
                }
            }

            let block_x = match self.policy.batch {
                BatchKernelPolicy::Plain { block_x } => block_x.max(64),
                _ => 256,
            };
            let grid_x = ((len as u32) + block_x - 1) / block_x;

            const MAX_GRID_Y: usize = 65_535;
            let mut start = 0usize;
            while start < combos.len() {
                let count = (combos.len() - start).min(MAX_GRID_Y);
                let grid: GridSize = (grid_x, count as u32, 1).into();
                let block: BlockSize = (block_x, 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 orders_ptr = d_orders.as_device_ptr().add(start).as_raw();
                    let mut nrows_i = count as i32;
                    let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
                    let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
                    let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
                    let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
                    let mut is_min_ptr = d_is_min.as_device_ptr().add(start * len).as_raw();
                    let mut is_max_ptr = d_is_max.as_device_ptr().add(start * len).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 series_len_i as *mut _ as *mut c_void,
                        &mut first_valid_i as *mut _ as *mut c_void,
                        &mut orders_ptr as *mut _ as *mut c_void,
                        &mut nrows_i as *mut _ as *mut c_void,
                        &mut low_min_ptr as *mut _ as *mut c_void,
                        &mut high_max_ptr as *mut _ as *mut c_void,
                        &mut vlow_ptr as *mut _ as *mut c_void,
                        &mut vhigh_ptr as *mut _ as *mut c_void,
                        &mut is_min_ptr as *mut _ as *mut c_void,
                        &mut is_max_ptr as *mut _ as *mut c_void,
                    ];
                    self.stream.launch(&f_rmq, grid, block, 0, args)?;
                }

                let ff_block_x = 1024u32;
                let ff_grid: GridSize = (count as u32, 1, 1).into();
                let ff_block: BlockSize = (ff_block_x, 1, 1).into();
                unsafe {
                    let mut is_min_ptr = d_is_min.as_device_ptr().add(start * len).as_raw();
                    let mut is_max_ptr = d_is_max.as_device_ptr().add(start * len).as_raw();
                    let mut rows_i = count as i32;
                    let mut last_min_ptr = d_last_min.as_device_ptr().add(start * len).as_raw();
                    let mut last_max_ptr = d_last_max.as_device_ptr().add(start * len).as_raw();
                    let args: &mut [*mut c_void] = &mut [
                        &mut is_min_ptr as *mut _ as *mut c_void,
                        &mut is_max_ptr as *mut _ as *mut c_void,
                        &mut series_len_i as *mut _ as *mut c_void,
                        &mut rows_i as *mut _ as *mut c_void,
                        &mut last_min_ptr as *mut _ as *mut c_void,
                        &mut last_max_ptr as *mut _ as *mut c_void,
                    ];
                    self.stream.launch(&f_ff, ff_grid, ff_block, 0, args)?;
                }

                start += count;
            }

            return Ok((
                DeviceMinmaxQuad {
                    is_min: d_is_min,
                    is_max: d_is_max,
                    last_min: d_last_min,
                    last_max: d_last_max,
                    rows: combos.len(),
                    cols: len,
                },
                combos,
            ));
        }

        let block_x = match self.policy.batch {
            BatchKernelPolicy::Plain { block_x } => block_x.max(64),
            _ => 256,
        };
        let grid_x = 1u32;
        let func = self.module.get_function("minmax_batch_f32").map_err(|_| {
            CudaMinmaxError::MissingKernelSymbol {
                name: "minmax_batch_f32",
            }
        })?;

        let mut start = 0usize;
        const MAX_GRID_Y: usize = 65_535;
        while start < combos.len() {
            let count = (combos.len() - start).min(MAX_GRID_Y);
            let grid: GridSize = (grid_x, count as u32, 1).into();
            let block: BlockSize = (block_x, 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 orders_ptr = d_orders.as_device_ptr().add(start).as_raw();
                let mut nrows_i = count as i32;
                let mut is_min_ptr = d_is_min.as_device_ptr().add(start * len).as_raw();
                let mut is_max_ptr = d_is_max.as_device_ptr().add(start * len).as_raw();
                let mut last_min_ptr = d_last_min.as_device_ptr().add(start * len).as_raw();
                let mut last_max_ptr = d_last_max.as_device_ptr().add(start * len).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 series_len_i as *mut _ as *mut c_void,
                    &mut first_valid_i as *mut _ as *mut c_void,
                    &mut orders_ptr as *mut _ as *mut c_void,
                    &mut nrows_i as *mut _ as *mut c_void,
                    &mut is_min_ptr as *mut _ as *mut c_void,
                    &mut is_max_ptr as *mut _ as *mut c_void,
                    &mut last_min_ptr as *mut _ as *mut c_void,
                    &mut last_max_ptr as *mut _ as *mut c_void,
                ];
                self.stream.launch(&func, grid, block, 0, args)?;
            }
            start += count;
        }

        Ok((
            DeviceMinmaxQuad {
                is_min: d_is_min,
                is_max: d_is_max,
                last_min: d_last_min,
                last_max: d_last_max,
                rows: combos.len(),
                cols: len,
            },
            combos,
        ))
    }

    pub fn minmax_many_series_one_param_time_major_dev(
        &self,
        high_tm: &[f32],
        low_tm: &[f32],
        cols: usize,
        rows: usize,
        params: &MinmaxParams,
    ) -> Result<DeviceMinmaxQuad, CudaMinmaxError> {
        if cols == 0 || rows == 0 {
            return Err(CudaMinmaxError::InvalidInput(
                "cols/rows must be > 0".into(),
            ));
        }
        let expected = cols
            .checked_mul(rows)
            .ok_or_else(|| CudaMinmaxError::InvalidInput("cols*rows overflow".into()))?;
        if high_tm.len() != expected || low_tm.len() != expected {
            return Err(CudaMinmaxError::InvalidInput(
                "time-major inputs wrong length".into(),
            ));
        }
        let order = params.order.unwrap_or(3);
        if order == 0 || order > rows {
            return Err(CudaMinmaxError::InvalidInput("invalid order".into()));
        }

        let mut first_valids = vec![0i32; cols];
        for s in 0..cols {
            let mut fv: Option<i32> = None;
            for t in 0..rows {
                let idx = t * cols + s;
                let h = high_tm[idx];
                let l = low_tm[idx];
                if h.is_finite() && l.is_finite() {
                    fv = Some(t as i32);
                    break;
                }
            }
            let found =
                fv.ok_or_else(|| CudaMinmaxError::InvalidInput(format!("series {} all NaN", s)))?;
            if (rows as i32 - found) < order as i32 {
                return Err(CudaMinmaxError::InvalidInput(format!(
                    "series {} lacks data: need >= {}, valid = {}",
                    s,
                    order,
                    rows as i32 - found
                )));
            }
            first_valids[s] = found;
        }

        let elem = std::mem::size_of::<f32>();
        let bytes = cols
            .checked_mul(rows)
            .and_then(|n| n.checked_mul(elem))
            .and_then(|b| b.checked_mul(2))
            .and_then(|b| {
                cols.checked_mul(std::mem::size_of::<i32>())
                    .and_then(|m| b.checked_add(m))
            })
            .and_then(|b| {
                cols.checked_mul(rows)
                    .and_then(|n| n.checked_mul(elem))
                    .and_then(|x| x.checked_mul(4))
                    .and_then(|x| b.checked_add(x))
            })
            .ok_or_else(|| {
                CudaMinmaxError::InvalidInput("size overflow for minmax many-series".into())
            })?;
        if !Self::will_fit(bytes, 64 * 1024 * 1024) {
            if let Some((free, _)) = Self::device_mem_info() {
                return Err(CudaMinmaxError::OutOfMemory {
                    required: bytes,
                    free,
                    headroom: 64 * 1024 * 1024,
                });
            } else {
                return Err(CudaMinmaxError::InvalidInput(
                    "insufficient device memory for minmax many-series".into(),
                ));
            }
        }

        let d_high = DeviceBuffer::from_slice(high_tm)?;
        let d_low = DeviceBuffer::from_slice(low_tm)?;
        let d_first = DeviceBuffer::from_slice(&first_valids)?;

        let total = cols
            .checked_mul(rows)
            .ok_or_else(|| CudaMinmaxError::InvalidInput("cols*rows overflow".into()))?;
        let mut d_is_min: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(total) }?;
        let mut d_is_max: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(total) }?;
        let mut d_last_min: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(total) }?;
        let mut d_last_max: DeviceBuffer<f32> = unsafe { DeviceBuffer::uninitialized(total) }?;

        let block_x = match self.policy.many_series {
            ManySeriesKernelPolicy::OneD { block_x } => block_x.max(64),
            _ => 256,
        };
        let grid: GridSize = (cols as u32, 1, 1).into();
        let block: BlockSize = (block_x, 1, 1).into();
        let func = self
            .module
            .get_function("minmax_many_series_one_param_time_major_f32")
            .map_err(|_| CudaMinmaxError::MissingKernelSymbol {
                name: "minmax_many_series_one_param_time_major_f32",
            })?;
        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 first_ptr = d_first.as_device_ptr().as_raw();
            let mut num_series_i = cols as i32;
            let mut series_len_i = rows as i32;
            let mut order_i = order as i32;
            let mut is_min_ptr = d_is_min.as_device_ptr().as_raw();
            let mut is_max_ptr = d_is_max.as_device_ptr().as_raw();
            let mut last_min_ptr = d_last_min.as_device_ptr().as_raw();
            let mut last_max_ptr = d_last_max.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 first_ptr as *mut _ as *mut c_void,
                &mut num_series_i as *mut _ as *mut c_void,
                &mut series_len_i as *mut _ as *mut c_void,
                &mut order_i as *mut _ as *mut c_void,
                &mut is_min_ptr as *mut _ as *mut c_void,
                &mut is_max_ptr as *mut _ as *mut c_void,
                &mut last_min_ptr as *mut _ as *mut c_void,
                &mut last_max_ptr as *mut _ as *mut c_void,
            ];
            self.stream.launch(&func, grid, block, 0, args)?;
        }
        self.stream.synchronize()?;

        Ok(DeviceMinmaxQuad {
            is_min: d_is_min,
            is_max: d_is_max,
            last_min: d_last_min,
            last_max: d_last_max,
            rows,
            cols,
        })
    }
}

#[inline]
fn expand_grid(r: &MinmaxBatchRange) -> Result<Vec<MinmaxParams>, CudaMinmaxError> {
    fn axis_usize(
        (start, end, step): (usize, usize, usize),
    ) -> Result<Vec<usize>, CudaMinmaxError> {
        if step == 0 || start == end {
            return Ok(vec![start]);
        }
        let mut out = Vec::new();
        if start < end {
            let st = step.max(1);
            let mut v = start;
            while v <= end {
                out.push(v);
                match v.checked_add(st) {
                    Some(next) => {
                        if next == v {
                            break;
                        }
                        v = next;
                    }
                    None => break,
                }
            }
        } else {
            let st = step.max(1) as isize;
            let mut v = start as isize;
            let end_i = end as isize;
            while v >= end_i {
                out.push(v as usize);
                v -= st;
            }
        }
        if out.is_empty() {
            return Err(CudaMinmaxError::InvalidInput(format!(
                "Invalid range: start={}, end={}, step={}",
                start, end, step
            )));
        }
        Ok(out)
    }
    let orders = axis_usize(r.order)?;
    let mut out = Vec::with_capacity(orders.len());
    for &o in &orders {
        out.push(MinmaxParams { order: Some(o) });
    }
    Ok(out)
}

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

    struct MinmaxBatchState {
        cuda: CudaMinmax,
        d_high: DeviceBuffer<f32>,
        d_low: DeviceBuffer<f32>,
        d_orders: DeviceBuffer<i32>,
        st_low_min: DeviceBuffer<f32>,
        st_high_max: DeviceBuffer<f32>,
        st_valid_low: DeviceBuffer<u8>,
        st_valid_high: DeviceBuffer<u8>,
        d_is_min: DeviceBuffer<f32>,
        d_is_max: DeviceBuffer<f32>,
        d_last_min: DeviceBuffer<f32>,
        d_last_max: DeviceBuffer<f32>,
        len: usize,
        rows: usize,
        first_valid: i32,
    }
    impl CudaBenchState for MinmaxBatchState {
        fn launch(&mut self) {
            let mut series_len_i = self.len as i32;
            let mut first_valid_i = self.first_valid;
            let mut rows_i = self.rows as i32;

            let f_rmq = self
                .cuda
                .module
                .get_function("minmax_batch_rmq_f32")
                .expect("minmax_batch_rmq_f32");
            let f_ff = self
                .cuda
                .module
                .get_function("forward_fill_two_streams_f32")
                .expect("forward_fill_two_streams_f32");

            let block_x = 256u32;
            let grid_x = ((self.len as u32) + block_x - 1) / block_x;
            let grid: GridSize = (grid_x.max(1), self.rows as u32, 1).into();
            let block: BlockSize = (block_x, 1, 1).into();

            unsafe {
                let mut high_ptr = self.d_high.as_device_ptr().as_raw();
                let mut low_ptr = self.d_low.as_device_ptr().as_raw();
                let mut orders_ptr = self.d_orders.as_device_ptr().as_raw();
                let mut nrows_i = rows_i;
                let mut low_min_ptr = self.st_low_min.as_device_ptr().as_raw();
                let mut high_max_ptr = self.st_high_max.as_device_ptr().as_raw();
                let mut vlow_ptr = self.st_valid_low.as_device_ptr().as_raw();
                let mut vhigh_ptr = self.st_valid_high.as_device_ptr().as_raw();
                let mut is_min_ptr = self.d_is_min.as_device_ptr().as_raw();
                let mut is_max_ptr = self.d_is_max.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 series_len_i as *mut _ as *mut c_void,
                    &mut first_valid_i as *mut _ as *mut c_void,
                    &mut orders_ptr as *mut _ as *mut c_void,
                    &mut nrows_i as *mut _ as *mut c_void,
                    &mut low_min_ptr as *mut _ as *mut c_void,
                    &mut high_max_ptr as *mut _ as *mut c_void,
                    &mut vlow_ptr as *mut _ as *mut c_void,
                    &mut vhigh_ptr as *mut _ as *mut c_void,
                    &mut is_min_ptr as *mut _ as *mut c_void,
                    &mut is_max_ptr as *mut _ as *mut c_void,
                ];
                self.cuda
                    .stream
                    .launch(&f_rmq, grid, block, 0, args)
                    .expect("minmax rmq launch");
            }

            let ff_block_x = 1024u32;
            let ff_grid: GridSize = (self.rows as u32, 1, 1).into();
            let ff_block: BlockSize = (ff_block_x, 1, 1).into();
            unsafe {
                let mut is_min_ptr = self.d_is_min.as_device_ptr().as_raw();
                let mut is_max_ptr = self.d_is_max.as_device_ptr().as_raw();
                let mut last_min_ptr = self.d_last_min.as_device_ptr().as_raw();
                let mut last_max_ptr = self.d_last_max.as_device_ptr().as_raw();
                let args: &mut [*mut c_void] = &mut [
                    &mut is_min_ptr as *mut _ as *mut c_void,
                    &mut is_max_ptr as *mut _ as *mut c_void,
                    &mut series_len_i as *mut _ as *mut c_void,
                    &mut rows_i as *mut _ as *mut c_void,
                    &mut last_min_ptr as *mut _ as *mut c_void,
                    &mut last_max_ptr as *mut _ as *mut c_void,
                ];
                self.cuda
                    .stream
                    .launch(&f_ff, ff_grid, ff_block, 0, args)
                    .expect("minmax forward fill launch");
            }

            self.cuda.synchronize().expect("minmax sync");
        }
    }

    fn prep_minmax_batch() -> Box<dyn CudaBenchState> {
        let len = 1_000_000usize;

        let mut h = vec![f32::NAN; len];
        let mut l = vec![f32::NAN; len];
        for i in 3..len {
            let x = i as f32;
            let b = (x * 0.001).sin() + 0.0001 * x;
            let s = (x * 0.0007).cos().abs() * 0.2 + 0.3;
            l[i] = b;
            h[i] = b * (1.0 + s);
        }
        let sweep = MinmaxBatchRange { order: (3, 252, 1) };
        let combos = expand_grid(&sweep).expect("minmax combos");
        let orders_i32: Vec<i32> = combos.iter().map(|c| c.order.unwrap_or(3) as i32).collect();
        let rows = orders_i32.len();
        let first_valid = 3i32;

        let cuda = CudaMinmax::new(0).expect("cuda minmax");

        let d_high = DeviceBuffer::from_slice(&h).expect("d_high");
        let d_low = DeviceBuffer::from_slice(&l).expect("d_low");
        let d_orders = DeviceBuffer::from_slice(&orders_i32).expect("d_orders");

        let k_levels = sparse_table_levels(len);
        let st_elems = k_levels * len;
        let mut st_low_min: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized(st_elems) }.expect("st_low_min");
        let mut st_high_max: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized(st_elems) }.expect("st_high_max");
        let mut st_valid_low: DeviceBuffer<u8> =
            unsafe { DeviceBuffer::uninitialized(st_elems) }.expect("st_valid_low");
        let mut st_valid_high: DeviceBuffer<u8> =
            unsafe { DeviceBuffer::uninitialized(st_elems) }.expect("st_valid_high");

        let f_init = cuda
            .module
            .get_function("st_init_level0_minmax_valid_f32")
            .expect("st_init_level0_minmax_valid_f32");
        let f_build = cuda
            .module
            .get_function("st_build_level_k_minmax_valid_f32")
            .expect("st_build_level_k_minmax_valid_f32");
        let block_x = 256u32;
        let grid_x = ((len as u32) + block_x - 1) / block_x;
        let grid: GridSize = (grid_x, 1, 1).into();
        let block: BlockSize = (block_x, 1, 1).into();
        let mut series_len_i = len as i32;
        unsafe {
            let mut low_ptr = d_low.as_device_ptr().as_raw();
            let mut high_ptr = d_high.as_device_ptr().as_raw();
            let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
            let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
            let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
            let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
            let args: &mut [*mut c_void] = &mut [
                &mut low_ptr as *mut _ as *mut c_void,
                &mut high_ptr as *mut _ as *mut c_void,
                &mut series_len_i as *mut _ as *mut c_void,
                &mut low_min_ptr as *mut _ as *mut c_void,
                &mut high_max_ptr as *mut _ as *mut c_void,
                &mut vlow_ptr as *mut _ as *mut c_void,
                &mut vhigh_ptr as *mut _ as *mut c_void,
            ];
            cuda.stream
                .launch(&f_init, grid, block, 0, args)
                .expect("st_init launch");
        }

        for k in 1..k_levels {
            let span = 1usize << k;
            if len < span {
                break;
            }
            let valid_pos = len - span + 1;
            let grid_kx = ((valid_pos as u32) + block_x - 1) / block_x;
            let gridk: GridSize = (grid_kx.max(1), 1, 1).into();
            unsafe {
                let mut k_i = k as i32;
                let mut low_min_ptr = st_low_min.as_device_ptr().as_raw();
                let mut high_max_ptr = st_high_max.as_device_ptr().as_raw();
                let mut vlow_ptr = st_valid_low.as_device_ptr().as_raw();
                let mut vhigh_ptr = st_valid_high.as_device_ptr().as_raw();
                let args: &mut [*mut c_void] = &mut [
                    &mut series_len_i as *mut _ as *mut c_void,
                    &mut k_i as *mut _ as *mut c_void,
                    &mut low_min_ptr as *mut _ as *mut c_void,
                    &mut high_max_ptr as *mut _ as *mut c_void,
                    &mut vlow_ptr as *mut _ as *mut c_void,
                    &mut vhigh_ptr as *mut _ as *mut c_void,
                ];
                cuda.stream
                    .launch(&f_build, gridk, block, 0, args)
                    .expect("st_build launch");
            }
        }
        cuda.synchronize().expect("st build sync");

        let elems = rows * len;
        let d_is_min: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized(elems) }.expect("d_is_min");
        let d_is_max: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized(elems) }.expect("d_is_max");
        let d_last_min: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized(elems) }.expect("d_last_min");
        let d_last_max: DeviceBuffer<f32> =
            unsafe { DeviceBuffer::uninitialized(elems) }.expect("d_last_max");

        Box::new(MinmaxBatchState {
            cuda,
            d_high,
            d_low,
            d_orders,
            st_low_min,
            st_high_max,
            st_valid_low,
            st_valid_high,
            d_is_min,
            d_is_max,
            d_last_min,
            d_last_max,
            len,
            rows,
            first_valid,
        })
    }

    pub fn bench_profiles() -> Vec<CudaBenchScenario> {
        vec![CudaBenchScenario::new(
            "minmax",
            "batch_dev",
            "minmax_cuda_batch_dev",
            "1m_x_250",
            prep_minmax_batch,
        )
        .with_inner_iters(4)]
    }
}
