Skip to main content

strided_basic/
reduce_view.rs

1//! Reduce operations on dynamic-rank strided views.
2
3use crate::kernel::{
4    build_plan_fused, ensure_same_shape, for_each_inner_block_preordered, same_contiguous_layout,
5    sequential_contiguous_layout, total_len,
6};
7use crate::simd;
8use crate::view::{col_major_strides, StridedArray, StridedView, StridedViewMut};
9use crate::{Exec, Result, StridedError};
10use core::num::NonZeroUsize;
11use std::ops::Range;
12use strided_view::ElementOp;
13
14use crate::cost::KernelClass;
15use crate::fuse::compute_costs;
16use crate::inline::InlineVec;
17use crate::threading::{
18    for_each_inner_block_with_offsets, is_parallel, mapreduce_threaded_fold, SendPtr,
19};
20
21/// Number of independent accumulators of [`fold_contiguous`].
22const FOLD_LANES: usize = 16;
23
24/// Minimum outputs along a unit-stride kept axis for the column sweep of
25/// [`reduce_axis`].
26const AXIS_SWEEP_MIN_LEN: usize = 16;
27
28/// Byte budget of one output block of the column sweep of [`reduce_axis`].
29const AXIS_SWEEP_BLOCK_BYTES: usize = 64 * 1024;
30
31/// Combines the lanes of [`fold_contiguous`] left to right, after `init`.
32///
33/// Not inlined on purpose: the lane array crosses the call by memory, which
34/// keeps the lanes of the surrounding loop in vector registers. Inlined, the
35/// final fold makes the loop half-extract and re-insert every lane (on x86-64
36/// AVX2 `f64` max: 18 shuffles per 16 elements) although it runs once.
37#[inline(never)]
38fn fold_lanes<U, R>(lanes: [U; FOLD_LANES], init: U, reduce_fn: &R) -> U
39where
40    R: Fn(U, U) -> U,
41{
42    lanes.into_iter().fold(init, reduce_fn)
43}
44
45/// Folds a contiguous run with [`FOLD_LANES`] independent accumulators.
46///
47/// Lane `i` starts from element `i` and folds elements `i + FOLD_LANES`,
48/// `i + 2 * FOLD_LANES`, ... in order; the remainder is folded into the first
49/// lanes. `init` is combined exactly once, before lane 0, and the lanes are
50/// then combined left to right by [`fold_lanes`]. Independent lanes let the
51/// compiler keep several vector accumulators in flight instead of one serial
52/// dependency chain, which is what makes a closure-based sum or max approach
53/// the speed of a hand written lane loop.
54#[inline(always)]
55fn fold_contiguous<T, Op, M, R, U>(src: &[T], init: U, map_fn: &M, reduce_fn: &R) -> U
56where
57    T: Copy,
58    Op: ElementOp<T>,
59    M: Fn(T) -> U,
60    R: Fn(U, U) -> U,
61    U: Clone,
62{
63    if src.len() < 2 * FOLD_LANES {
64        let mut acc = init;
65        for &value in src {
66            acc = reduce_fn(acc, map_fn(Op::apply(value)));
67        }
68        return acc;
69    }
70    let (head, rest) = src.split_at(FOLD_LANES);
71    let mut lanes: [U; FOLD_LANES] = core::array::from_fn(|lane| map_fn(Op::apply(head[lane])));
72    let (chunks, remainder) = rest.as_chunks::<FOLD_LANES>();
73    for chunk in chunks {
74        for (lane, &value) in lanes.iter_mut().zip(chunk) {
75            *lane = reduce_fn(lane.clone(), map_fn(Op::apply(value)));
76        }
77    }
78    for (lane, &value) in lanes.iter_mut().zip(remainder) {
79        *lane = reduce_fn(lane.clone(), map_fn(Op::apply(value)));
80    }
81    fold_lanes(lanes, init, reduce_fn)
82}
83
84/// Folds `len` elements starting at `ptr` with step `stride`.
85///
86/// # Safety
87/// `ptr.offset(i * stride)` must be a readable element for every `i < len`.
88#[inline(always)]
89unsafe fn fold_run<T, Op, M, R, U>(
90    ptr: *const T,
91    stride: isize,
92    len: usize,
93    init: U,
94    map_fn: &M,
95    reduce_fn: &R,
96) -> U
97where
98    T: Copy,
99    Op: ElementOp<T>,
100    M: Fn(T) -> U,
101    R: Fn(U, U) -> U,
102    U: Clone,
103{
104    if stride == 1 {
105        // SAFETY: the caller proves `len` readable unit-stride elements.
106        let run = unsafe { std::slice::from_raw_parts(ptr, len) };
107        return fold_contiguous::<T, Op, M, R, U>(run, init, map_fn, reduce_fn);
108    }
109    let mut acc = init;
110    let mut cursor = ptr;
111    for index in 0..len {
112        // SAFETY: the caller proves every element of the run is readable.
113        acc = reduce_fn(acc, map_fn(Op::apply(unsafe { *cursor })));
114        if index + 1 < len {
115            cursor = cursor.wrapping_offset(stride);
116        }
117    }
118    acc
119}
120
121/// Full reduction with map function: `reduce(init, op, map.(src))`.
122///
123/// # Evaluation order
124///
125/// The result is independent of the pool when the call runs with a serial
126/// token ([`Exec::serial`]) or a budget of one: only serial branches run, and
127/// their fold order is fixed by the source layout. With a larger budget the
128/// fold is split over the budget, so the result is repeatable for a fixed
129/// shape, layout and budget but can differ between budgets.
130///
131/// In neither case is this a strict left to right fold: a contiguous source is
132/// folded with sixteen independent lanes (see the crate source for the exact
133/// lane assignment), non-contiguous sources are traversed in a cache-friendly
134/// loop order, and parallel execution folds per-task chunks independently
135/// and combines them in a tree. Floating point results can therefore differ
136/// from a sequential left to right sum in the last bits, and no bitwise
137/// equality across machines or target features is guaranteed.
138///
139/// Because of that, `reduce_fn` must be associative and commutative, and
140/// `init` must be an identity of `reduce_fn` whenever the reduction can run on
141/// several threads.
142///
143/// # Allocation
144///
145/// At rank ≤ [`INLINE_RANK`](strided_view::INLINE_RANK) the view metadata and
146/// the traversal plan are stored inline, so a call performs no heap
147/// allocation. Above that bound the planner may spill to the heap.
148///
149/// Because `map_fn` and `reduce_fn` are opaque closures this entry cannot
150/// select the dtype-specific SIMD kernels of [`crate::ErasedReducePlan`];
151/// callers that reduce with a known operation (sum, product, max, min) on a
152/// supported dtype can use that plan for the fastest path.
153///
154/// # Errors
155///
156/// Returns [`StridedError::OffsetOverflow`] when the element count of `src`
157/// does not fit `usize`.
158pub fn reduce<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
159    src: &StridedView<T, Op>,
160    map_fn: M,
161    reduce_fn: R,
162    init: U,
163    exec: Exec<'_>,
164) -> Result<U>
165where
166    M: Fn(T) -> U + Sync,
167    R: Fn(U, U) -> U + Sync,
168    U: Clone + Send + Sync,
169{
170    reduce_impl(src, map_fn, reduce_fn, init, exec)
171}
172
173fn reduce_impl<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
174    src: &StridedView<T, Op>,
175    map_fn: M,
176    reduce_fn: R,
177    init: U,
178    exec: Exec<'_>,
179) -> Result<U>
180where
181    M: Fn(T) -> U + Sync,
182    R: Fn(U, U) -> U + Sync,
183    U: Clone + Send + Sync,
184{
185    let src_ptr = src.ptr();
186    let src_dims = src.dims();
187    let src_strides = src.strides();
188
189    // A zero-element reduction is exactly `init`. Return before planning: an
190    // empty view is allowed to carry an arbitrarily large stride, which the
191    // block-size arithmetic of the planner cannot represent.
192    if total_len(src_dims)? == 0 {
193        return Ok(init);
194    }
195
196    let contiguous =
197        sequential_contiguous_layout(KernelClass::Reduce, src_dims, &[src_strides], &exec)?;
198    if contiguous.is_some() {
199        let len = total_len(src_dims)?;
200        let src = unsafe { std::slice::from_raw_parts(src_ptr, len) };
201        return Ok(simd::dispatch_if_large(len, || {
202            fold_contiguous::<T, Op, M, R, U>(src, init, &map_fn, &reduce_fn)
203        }));
204    }
205
206    // Parallel contiguous fast path: split into scheduler chunks with slice-based iteration.
207    // This enables LLVM auto-vectorization on each chunk, unlike the general threaded path
208    // which uses scalar pointer-offset loops.
209    let total = total_len(src_dims)?;
210    if is_parallel(KernelClass::Reduce, total, &exec)
211        && same_contiguous_layout(src_dims, &[src_strides]).is_some()
212    {
213        let src_slice = unsafe { std::slice::from_raw_parts(src_ptr, total) };
214        let result = crate::threading::parallel_map_reduce(
215            0..total,
216            crate::cost::for_len_tree(exec, KernelClass::Reduce, total),
217            &|range| {
218                simd::dispatch_if_large(range.len(), || {
219                    fold_contiguous::<T, Op, M, R, U>(
220                        &src_slice[range],
221                        init.clone(),
222                        &map_fn,
223                        &reduce_fn,
224                    )
225                })
226            },
227            &|a, b| reduce_fn(a, b),
228        );
229        return Ok(result);
230    }
231
232    let strides_list: [&[isize]; 1] = [src_strides];
233
234    let (fused_dims, ordered_strides, plan) =
235        build_plan_fused(src_dims, &strides_list, None, std::mem::size_of::<T>());
236
237    let total = total_len(&fused_dims)?;
238    if is_parallel(KernelClass::Reduce, total, &exec) {
239        let src_send = SendPtr(src_ptr as *mut T);
240        let costs = compute_costs(&ordered_strides);
241        let initial_offsets: crate::inline::OperandVec<isize> =
242            ordered_strides.iter().map(|_| 0isize).collect();
243        let map_fn_ref = &map_fn;
244        let reduce_fn_ref = &reduce_fn;
245        let init_ref = &init;
246        // The reduction tree combines each split's partial result, so no
247        // worker-count-sized output-slot vector is needed (#306 scope).
248        let result = mapreduce_threaded_fold(
249            &fused_dims,
250            &plan.block,
251            &ordered_strides,
252            &initial_offsets,
253            &costs,
254            KernelClass::Reduce,
255            exec,
256            &|dims, blocks, strides_list, offsets| {
257                let mut acc = init_ref.clone();
258                for_each_inner_block_with_offsets(
259                    dims,
260                    blocks,
261                    strides_list,
262                    offsets,
263                    |offsets, len, strides| {
264                        let mut ptr = unsafe { src_send.as_const().offset(offsets[0]) };
265                        let stride = strides[0];
266                        for _ in 0..len {
267                            let val = Op::apply(unsafe { *ptr });
268                            let mapped = map_fn_ref(val);
269                            acc = reduce_fn_ref(acc.clone(), mapped);
270                            unsafe {
271                                ptr = ptr.offset(stride);
272                            }
273                        }
274                        Ok(())
275                    },
276                )?;
277                Ok(acc)
278            },
279            &|a, b| reduce_fn_ref(a, b),
280        )?;
281        return Ok(result);
282    }
283
284    let mut acc = init;
285    let initial_offsets: crate::inline::OperandVec<isize> =
286        ordered_strides.iter().map(|_| 0isize).collect();
287    for_each_inner_block_preordered(
288        &fused_dims,
289        &plan.block,
290        &ordered_strides,
291        &initial_offsets,
292        |offsets, len, strides| {
293            let mut ptr = unsafe { src_ptr.offset(offsets[0]) };
294            let stride = strides[0];
295            for _ in 0..len {
296                let val = Op::apply(unsafe { *ptr });
297                let mapped = map_fn(val);
298                acc = reduce_fn(acc.clone(), mapped);
299                unsafe {
300                    ptr = ptr.offset(stride);
301                }
302            }
303            Ok(())
304        },
305    )?;
306
307    Ok(acc)
308}
309
310/// Folds a contiguous run of two operands with [`FOLD_LANES`] independent
311/// accumulators.
312///
313/// Same lane scheme as [`fold_contiguous`]: lane `i` starts from the pair at
314/// index `i` and folds the pairs at `i + FOLD_LANES`, `i + 2 * FOLD_LANES`,
315/// ... in order, the remainder goes to the first lanes, and `init` is combined
316/// once before the lanes are combined left to right.
317///
318/// The scheduling below deliberately mirrors [`fold_contiguous`] instead of
319/// sharing a generic "map index to value" lane helper: routing the existing
320/// single-operand fold through such an accessor was measured at 0.34 ns/element
321/// against 0.11 ns/element on a contiguous `f64` `reduce` (1 MiB, release,
322/// `--release -j 4`), because the `as_chunks` + `zip` form is what lets the
323/// compiler drop the bound checks and vectorize the 16 lanes. Keep the two
324/// bodies in step by hand; both must use the lane assignment documented here.
325#[inline(always)]
326fn fold_contiguous2<A, B, OpA, OpB, M, R, U>(
327    a: &[A],
328    b: &[B],
329    init: U,
330    map_fn: &M,
331    reduce_fn: &R,
332) -> U
333where
334    A: Copy,
335    B: Copy,
336    OpA: ElementOp<A>,
337    OpB: ElementOp<B>,
338    M: Fn(A, B) -> U,
339    R: Fn(U, U) -> U,
340    U: Clone,
341{
342    debug_assert_eq!(a.len(), b.len());
343    if a.len() < 2 * FOLD_LANES {
344        let mut acc = init;
345        for (&value_a, &value_b) in a.iter().zip(b) {
346            acc = reduce_fn(acc, map_fn(OpA::apply(value_a), OpB::apply(value_b)));
347        }
348        return acc;
349    }
350    let (head_a, rest_a) = a.split_at(FOLD_LANES);
351    let (head_b, rest_b) = b.split_at(FOLD_LANES);
352    let mut lanes: [U; FOLD_LANES] =
353        core::array::from_fn(|lane| map_fn(OpA::apply(head_a[lane]), OpB::apply(head_b[lane])));
354    let (chunks_a, remainder_a) = rest_a.as_chunks::<FOLD_LANES>();
355    let (chunks_b, remainder_b) = rest_b.as_chunks::<FOLD_LANES>();
356    for (chunk_a, chunk_b) in chunks_a.iter().zip(chunks_b) {
357        for ((lane, &value_a), &value_b) in lanes.iter_mut().zip(chunk_a).zip(chunk_b) {
358            *lane = reduce_fn(
359                lane.clone(),
360                map_fn(OpA::apply(value_a), OpB::apply(value_b)),
361            );
362        }
363    }
364    for ((lane, &value_a), &value_b) in lanes.iter_mut().zip(remainder_a).zip(remainder_b) {
365        *lane = reduce_fn(
366            lane.clone(),
367            map_fn(OpA::apply(value_a), OpB::apply(value_b)),
368        );
369    }
370    lanes.into_iter().fold(init, reduce_fn)
371}
372
373/// Folds `len` operand pairs starting at `a_ptr`/`b_ptr` with the given
374/// strides.
375///
376/// # Safety
377/// `a_ptr.offset(i * a_stride)` and `b_ptr.offset(i * b_stride)` must be
378/// readable elements for every `i < len`.
379#[inline(always)]
380// Run leaf over two strided inputs: both pointers, their strides, the length, the
381// initial value and the two folds.
382#[allow(clippy::too_many_arguments)]
383unsafe fn fold_run2<A, B, OpA, OpB, M, R, U>(
384    a_ptr: *const A,
385    a_stride: isize,
386    b_ptr: *const B,
387    b_stride: isize,
388    len: usize,
389    init: &U,
390    map_fn: &M,
391    reduce_fn: &R,
392) -> U
393where
394    A: Copy,
395    B: Copy,
396    OpA: ElementOp<A>,
397    OpB: ElementOp<B>,
398    M: Fn(A, B) -> U,
399    R: Fn(U, U) -> U,
400    U: Clone,
401{
402    if a_stride == 1 && b_stride == 1 {
403        // SAFETY: the caller proves `len` readable unit-stride elements of
404        // both operands.
405        let a = unsafe { std::slice::from_raw_parts(a_ptr, len) };
406        let b = unsafe { std::slice::from_raw_parts(b_ptr, len) };
407        return fold_contiguous2::<A, B, OpA, OpB, M, R, U>(a, b, init.clone(), map_fn, reduce_fn);
408    }
409    let mut acc = init.clone();
410    let mut cursor_a = a_ptr;
411    let mut cursor_b = b_ptr;
412    for index in 0..len {
413        // SAFETY: the caller proves every pair of the run is readable.
414        let mapped = map_fn(
415            OpA::apply(unsafe { *cursor_a }),
416            OpB::apply(unsafe { *cursor_b }),
417        );
418        acc = reduce_fn(acc, mapped);
419        if index + 1 < len {
420            cursor_a = cursor_a.wrapping_offset(a_stride);
421            cursor_b = cursor_b.wrapping_offset(b_stride);
422        }
423    }
424    acc
425}
426
427/// Two-input mapped reduction: `reduce(init, op, map.(a[i], b[i]))`.
428///
429/// This is the two-operand sibling of [`reduce`]. It exists so that the
430/// accumulator type `U` is chosen by the caller instead of being tied to the
431/// operand types by `Mul`, which is what makes a widening accumulation
432/// possible — for example an `f32` dot accumulated in `f64`:
433///
434/// ```
435/// use strided_basic::{zip_reduce, Exec, StridedArray};
436///
437/// let a = StridedArray::<f32>::from_fn_col_major(&[2, 3], |i| (1 + i[0] + 2 * i[1]) as f32);
438/// let b = StridedArray::<f32>::from_fn_col_major(&[2, 3], |i| (1 + i[0]) as f32 * 0.5);
439/// let dot: f64 = zip_reduce(
440///     &a.view(),
441///     &b.view(),
442///     |x: f32, y: f32| x as f64 * y as f64,
443///     |x, y| x + y,
444///     0.0,
445///     Exec::serial(),
446/// )
447/// .unwrap();
448/// assert_eq!(dot, 16.5);
449/// ```
450///
451/// Both operands must have the same shape. Each operand's [`ElementOp`] is
452/// applied before `map_fn`, so conjugation and transposition flags are honoured
453/// without materializing a conjugated copy. Permuted, gapped, negative and
454/// stride-0 (broadcast) operands are supported: the traversal is the fused
455/// plan of [`reduce`], extended to two stride lists.
456///
457/// # Evaluation order
458///
459/// The result is independent of the pool when the call runs with a serial
460/// token ([`Exec::serial`]) or a budget of one: only serial branches run, and
461/// their fold order is fixed by the operand layouts. With a larger budget the
462/// fold is split over the budget, so the result is repeatable for a fixed
463/// shape, layout and budget but can differ between budgets.
464///
465/// In neither case is this a strict left to right fold: a contiguous operand
466/// pair is folded with the same sixteen independent lanes as [`reduce`], the
467/// general plan is traversed in a cache-friendly loop order, and parallel
468/// execution folds per-worker chunks independently and combines them in a
469/// tree. Floating point results can therefore differ from a sequential left to
470/// right sum in the last bits, and no bitwise equality across machines or
471/// target features is guaranteed.
472///
473/// Because of that, `reduce_fn` must be associative and commutative, and
474/// `init` must be an identity of `reduce_fn` whenever the reduction can run on
475/// several threads.
476///
477/// # Allocation
478///
479/// At rank ≤ [`INLINE_RANK`](strided_view::INLINE_RANK) the view metadata and
480/// the two-operand traversal plan are stored inline, so a call performs no heap
481/// allocation. Above that bound the planner may spill to the heap.
482///
483/// # Errors
484///
485/// Returns [`StridedError::RankMismatch`] / [`StridedError::ShapeMismatch`] when
486/// the operand shapes differ, and [`StridedError::OffsetOverflow`] for
487/// unrepresentable element counts.
488pub fn zip_reduce<A, B, OpA, OpB, M, R, U>(
489    a: &StridedView<A, OpA>,
490    b: &StridedView<B, OpB>,
491    map_fn: M,
492    reduce_fn: R,
493    init: U,
494    exec: Exec<'_>,
495) -> Result<U>
496where
497    A: Copy + Send + Sync,
498    B: Copy + Send + Sync,
499    OpA: ElementOp<A>,
500    OpB: ElementOp<B>,
501    M: Fn(A, B) -> U + Sync,
502    R: Fn(U, U) -> U + Sync,
503    U: Clone + Send + Sync,
504{
505    ensure_same_shape(a.dims(), b.dims())?;
506
507    let a_ptr = a.ptr();
508    let b_ptr = b.ptr();
509    let dims = a.dims();
510    let a_strides = a.strides();
511    let b_strides = b.strides();
512
513    // A zero-element reduction is exactly `init`. Return before planning: an
514    // empty view is allowed to carry an arbitrarily large stride, which the
515    // block-size arithmetic of the planner cannot represent.
516    let total = total_len(dims)?;
517    if total == 0 {
518        return Ok(init);
519    }
520
521    if sequential_contiguous_layout(KernelClass::Reduce, dims, &[a_strides, b_strides], &exec)?
522        .is_some()
523    {
524        return Ok(simd::dispatch_if_large(total, || {
525            // SAFETY: both operands prove `total` readable elements.
526            let sa = unsafe { std::slice::from_raw_parts(a_ptr, total) };
527            let sb = unsafe { std::slice::from_raw_parts(b_ptr, total) };
528            fold_contiguous2::<A, B, OpA, OpB, M, R, U>(sa, sb, init, &map_fn, &reduce_fn)
529        }));
530    }
531
532    // Parallel contiguous fast path: split into scheduler chunks so each chunk
533    // folds two contiguous slices, matching the serial contiguous kernel.
534    if is_parallel(KernelClass::Reduce, total, &exec)
535        && same_contiguous_layout(dims, &[a_strides, b_strides]).is_some()
536    {
537        // SAFETY: both operands prove `total` readable elements.
538        let sa = unsafe { std::slice::from_raw_parts(a_ptr, total) };
539        let sb = unsafe { std::slice::from_raw_parts(b_ptr, total) };
540        let result = crate::threading::parallel_map_reduce(
541            0..total,
542            crate::cost::for_len_tree(exec, KernelClass::Reduce, total),
543            &|range| {
544                let len = range.len();
545                simd::dispatch_if_large(len, || {
546                    fold_contiguous2::<A, B, OpA, OpB, M, R, U>(
547                        &sa[range.clone()],
548                        &sb[range],
549                        init.clone(),
550                        &map_fn,
551                        &reduce_fn,
552                    )
553                })
554            },
555            &|left, right| reduce_fn(left, right),
556        );
557        return Ok(result);
558    }
559
560    let strides_list: [&[isize]; 2] = [a_strides, b_strides];
561    let elem_size = std::mem::size_of::<A>().max(std::mem::size_of::<B>());
562    let (fused_dims, ordered_strides, plan) =
563        build_plan_fused(dims, &strides_list, None, elem_size);
564
565    let total = total_len(&fused_dims)?;
566    if is_parallel(KernelClass::Reduce, total, &exec) {
567        let a_send = SendPtr(a_ptr as *mut A);
568        let b_send = SendPtr(b_ptr as *mut B);
569        let costs = compute_costs(&ordered_strides);
570        let initial_offsets: crate::inline::OperandVec<isize> =
571            ordered_strides.iter().map(|_| 0isize).collect();
572        let map_fn_ref = &map_fn;
573        let reduce_fn_ref = &reduce_fn;
574        let init_ref = &init;
575        // The reduction tree combines each split's partial result, so no
576        // worker-count-sized output-slot vector is needed (#306 scope).
577        let result = mapreduce_threaded_fold(
578            &fused_dims,
579            &plan.block,
580            &ordered_strides,
581            &initial_offsets,
582            &costs,
583            KernelClass::Reduce,
584            exec,
585            &|dims, blocks, strides_list, offsets| {
586                let mut acc = init_ref.clone();
587                for_each_inner_block_with_offsets(
588                    dims,
589                    blocks,
590                    strides_list,
591                    offsets,
592                    |offsets, len, strides| {
593                        // SAFETY: the plan proves every pair of this block
594                        // is a readable element of both operands.
595                        acc = unsafe {
596                            fold_run2::<A, B, OpA, OpB, M, R, U>(
597                                a_send.as_const().offset(offsets[0]),
598                                strides[0],
599                                b_send.as_const().offset(offsets[1]),
600                                strides[1],
601                                len,
602                                &acc,
603                                map_fn_ref,
604                                reduce_fn_ref,
605                            )
606                        };
607                        Ok(())
608                    },
609                )?;
610                Ok(acc)
611            },
612            &|left, right| reduce_fn_ref(left, right),
613        )?;
614        return Ok(result);
615    }
616
617    let mut acc = init;
618    let initial_offsets: crate::inline::OperandVec<isize> =
619        ordered_strides.iter().map(|_| 0isize).collect();
620    for_each_inner_block_preordered(
621        &fused_dims,
622        &plan.block,
623        &ordered_strides,
624        &initial_offsets,
625        |offsets, len, strides| {
626            // SAFETY: the plan proves every pair of this block is a readable
627            // element of both operands.
628            acc = unsafe {
629                fold_run2::<A, B, OpA, OpB, M, R, U>(
630                    a_ptr.offset(offsets[0]),
631                    strides[0],
632                    b_ptr.offset(offsets[1]),
633                    strides[1],
634                    len,
635                    &acc,
636                    &map_fn,
637                    &reduce_fn,
638                )
639            };
640            Ok(())
641        },
642    )?;
643
644    Ok(acc)
645}
646
647/// Reduce along a single axis, returning a new `StridedArray`.
648///
649/// This allocates a column-major output and delegates to
650/// [`reduce_axis_into`].
651///
652/// Every output folds `init` once and then its reduced elements. When the
653/// reduced axis has unit stride those elements are folded with the lane
654/// scheme of [`reduce`], so `reduce_fn` must be associative and commutative.
655/// When the leading kept axis has unit stride, a block of adjacent outputs
656/// is accumulated one contiguous source column at a time, which keeps the
657/// left to right order per output. Outputs are split across the budget of
658/// `exec` once the number of source elements read exceeds the threading
659/// threshold; each output is computed by one task, so the result does not
660/// depend on the budget.
661///
662/// # Errors
663///
664/// Returns [`StridedError::InvalidAxis`] when `axis >= src.ndim()`, and
665/// [`StridedError::OffsetOverflow`] when the source element count cannot be
666/// represented in `usize`. The destination layout is built here, so the
667/// destination-shape errors of [`reduce_axis_into`] cannot occur.
668///
669/// # Panics
670///
671/// Panics when the kept source extents cannot be described in `isize`, which
672/// an empty source extent hides from the element-count check: reducing axis 0
673/// of a `[0, usize::MAX]` source builds the output strides with
674/// [`col_major_strides`], which panics instead of reporting an error.
675pub fn reduce_axis<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
676    src: &StridedView<T, Op>,
677    axis: usize,
678    map_fn: M,
679    reduce_fn: R,
680    init: U,
681    exec: Exec<'_>,
682) -> Result<StridedArray<U>>
683where
684    M: Fn(T) -> U + Sync,
685    R: Fn(U, U) -> U + Sync,
686    U: Clone + Send + Sync,
687{
688    let rank = src.ndim();
689    if axis >= rank {
690        return Err(StridedError::InvalidAxis { axis, rank });
691    }
692    // Reject an element count beyond `usize` before allocating the output.
693    total_len(src.dims())?;
694    let out_dims: InlineVec<usize> = src
695        .dims()
696        .iter()
697        .enumerate()
698        .filter(|(i, _)| *i != axis)
699        .map(|(_, &d)| d)
700        .collect();
701    // A full reduction keeps the historical rank-1 shape-[1] output; the
702    // `_into` form accepts any single-element destination for that case.
703    let (out_dims, out_strides) = if out_dims.is_empty() {
704        let mut dims = InlineVec::new();
705        dims.push(1usize);
706        let strides = col_major_strides(&dims);
707        (dims, strides)
708    } else {
709        let strides = col_major_strides(&out_dims);
710        (out_dims, strides)
711    };
712    let total_out = total_len(&out_dims)?;
713    let mut out =
714        StridedArray::<U>::from_parts(vec![init.clone(); total_out], &out_dims, &out_strides, 0)?;
715    {
716        let mut dest = out.view_mut();
717        // The fresh column-major destination needs no validation.
718        reduce_axis_into_impl(&mut dest, src, axis, map_fn, reduce_fn, init, exec)?;
719    }
720    Ok(out)
721}
722
723/// Reduce along a single axis into caller-owned storage.
724///
725/// `dest` must have the source's shape with `axis` removed, or any
726/// single-element destination for a full reduction, and an injective layout.
727/// Every destination element is written: an empty reduced axis fills each
728/// output with `init`.
729///
730/// # Errors
731///
732/// Returns [`StridedError::InvalidAxis`] for `axis >= src.ndim()`,
733/// [`StridedError::RankMismatch`] / [`StridedError::ShapeMismatch`] for a
734/// destination shape that is not the reduced source shape,
735/// [`StridedError::NonInjectiveOutputLayout`] for an overlapping destination,
736/// and [`StridedError::OffsetOverflow`] for unrepresentable sizes.
737///
738/// # Examples
739///
740/// ```
741/// use strided_basic::{reduce_axis_into, Exec, StridedArray};
742/// # let src = StridedArray::<f64>::from_fn_col_major(&[2, 3], |i| (i[0] + 2 * i[1]) as f64);
743/// let mut out = StridedArray::<f64>::col_major(&[3]);
744/// reduce_axis_into(&mut out.view_mut(), &src.view(), 0, |x| x, |a, b| a + b, 0.0, Exec::serial()).unwrap();
745/// assert_eq!(out.get(&[0]), 1.0);
746/// ```
747///
748/// Validation runs here; `reduce_axis_into_impl` is the shared kernel. The
749/// array-returning wrapper owns a fresh column-major destination and calls the
750/// kernel directly, so the common path does not re-validate.
751pub fn reduce_axis_into<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
752    dest: &mut StridedViewMut<U>,
753    src: &StridedView<T, Op>,
754    axis: usize,
755    map_fn: M,
756    reduce_fn: R,
757    init: U,
758    exec: Exec<'_>,
759) -> Result<()>
760where
761    M: Fn(T) -> U + Sync,
762    R: Fn(U, U) -> U + Sync,
763    U: Clone + Send + Sync,
764{
765    let rank = src.ndim();
766    if axis >= rank {
767        return Err(StridedError::InvalidAxis { axis, rank });
768    }
769    let src_dims = src.dims();
770    // Reject an element count beyond usize (a huge stride-0 broadcast) up
771    // front instead of looping over it.
772    total_len(src_dims)?;
773    let kept_len = rank - 1;
774    let dest_len = total_len(dest.dims())?;
775    if kept_len == 0 {
776        if dest_len != 1 {
777            return Err(StridedError::ShapeMismatch(vec![dest_len], vec![1]));
778        }
779    } else {
780        if dest.ndim() != kept_len {
781            return Err(StridedError::RankMismatch(dest.ndim(), kept_len));
782        }
783        let dest_dims = dest.dims();
784        let mut d = 0usize;
785        for (i, &src_dim) in src_dims.iter().enumerate() {
786            if i == axis {
787                continue;
788            }
789            if src_dim != dest_dims[d] {
790                let expected: Vec<usize> = src_dims
791                    .iter()
792                    .enumerate()
793                    .filter(|(i, _)| *i != axis)
794                    .map(|(_, &d)| d)
795                    .collect();
796                return Err(StridedError::ShapeMismatch(dest_dims.to_vec(), expected));
797            }
798            d += 1;
799        }
800        if !crate::layout_check::is_injective_layout(dest.dims(), dest.strides()) {
801            return Err(StridedError::NonInjectiveOutputLayout);
802        }
803    }
804    reduce_axis_into_impl(dest, src, axis, map_fn, reduce_fn, init, exec)
805}
806
807/// Shared kernel of [`reduce_axis_into`] and [`reduce_axis`].
808///
809/// Assumes the destination has the reduced source shape (or one element for a
810/// full reduction) and an injective layout; the public entry validates that,
811/// while the array-returning wrapper owns a freshly allocated column-major
812/// destination and can skip the re-validation.
813fn reduce_axis_into_impl<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
814    dest: &mut StridedViewMut<U>,
815    src: &StridedView<T, Op>,
816    axis: usize,
817    map_fn: M,
818    reduce_fn: R,
819    init: U,
820    exec: Exec<'_>,
821) -> Result<()>
822where
823    M: Fn(T) -> U + Sync,
824    R: Fn(U, U) -> U + Sync,
825    U: Clone + Send + Sync,
826{
827    let rank = src.ndim();
828    let src_dims = src.dims();
829    let src_strides = src.strides();
830    let src_ptr = src.ptr();
831    let dest_len = total_len(dest.dims())?;
832    let axis_len = src_dims[axis];
833    let axis_stride = src_strides[axis];
834
835    if axis_len == 0 {
836        // No source elements to fold: every output is `init`. Fill the
837        // destination directly rather than traversing the source, because an
838        // empty source view carries a dangling base pointer and `offset` on it
839        // is UB even when the fold reads zero elements.
840        fill_outputs_with(dest, &init);
841        return Ok(());
842    }
843
844    if rank == 1 {
845        // Full reduction into a single destination element.
846        // SAFETY: the view proves `axis_len` readable elements along `axis`.
847        let acc = unsafe {
848            fold_run::<T, Op, M, R, U>(src_ptr, axis_stride, axis_len, init, &map_fn, &reduce_fn)
849        };
850        unsafe { *dest.as_mut_ptr() = acc };
851        return Ok(());
852    }
853    if dest_len == 0 {
854        return Ok(());
855    }
856
857    // Build the kept-axis metadata in one pass. A column-major destination is
858    // the common case (the array-returning wrapper always allocates one); its
859    // offset equals the linear output index, so its stride list is only
860    // materialized for a strided destination.
861    let dest_stride_view = dest.strides();
862    let mut kept: InlineVec<(usize, isize)> = InlineVec::new();
863    let mut dest_linear = true;
864    let mut prefix = 1usize;
865    for (i, (&dim, &src_stride)) in src_dims.iter().zip(src_strides).enumerate() {
866        if i == axis {
867            continue;
868        }
869        let d = kept.len();
870        kept.push((dim, src_stride));
871        if dest_linear && dest_stride_view[d] != prefix as isize {
872            dest_linear = false;
873        }
874        prefix = prefix.saturating_mul(dim);
875    }
876    let dest_strides: InlineVec<isize> = if dest_linear {
877        InlineVec::new()
878    } else {
879        dest_stride_view.iter().copied().collect()
880    };
881    let out_ptr = dest.as_mut_ptr();
882    let parts = AxisParts {
883        kept: &kept,
884        dest_strides: &dest_strides,
885        axis_len,
886        axis_stride,
887        map_fn: &map_fn,
888        reduce_fn: &reduce_fn,
889        init: &init,
890    };
891
892    let work = dest_len.saturating_mul(axis_len);
893    let exec = crate::cost::for_len(exec, KernelClass::Reduce, work)
894        .limit(NonZeroUsize::new(dest_len).unwrap_or(NonZeroUsize::MIN));
895    if !exec.is_serial() {
896        let src_send = SendPtr(src_ptr as *mut T);
897        let out_send = SendPtr(out_ptr);
898        let parts = &parts;
899        if dest_linear {
900            crate::threading::parallel_for_each(0..dest_len, exec, &|range| {
901                // SAFETY: worker ranges are disjoint, the destination is
902                // injective, and the view proves every source offset
903                // reachable from its kept coordinates.
904                unsafe {
905                    reduce_axis_range_linear::<T, Op, M, R, U>(
906                        src_send.as_const(),
907                        out_send.as_ptr(),
908                        range,
909                        parts,
910                    )
911                }
912            });
913        } else {
914            crate::threading::parallel_for_each(0..dest_len, exec, &|range| {
915                // SAFETY: as for the linear path.
916                unsafe {
917                    reduce_axis_range_strided::<T, Op, M, R, U>(
918                        src_send.as_const(),
919                        out_send.as_ptr(),
920                        range,
921                        parts,
922                    )
923                }
924            });
925        }
926        return Ok(());
927    }
928
929    // SAFETY: as for the parallel ranges, with one range covering all outputs.
930    if dest_linear {
931        unsafe {
932            reduce_axis_range_linear::<T, Op, M, R, U>(src_ptr, out_ptr, 0..dest_len, &parts)
933        };
934    } else {
935        unsafe {
936            reduce_axis_range_strided::<T, Op, M, R, U>(src_ptr, out_ptr, 0..dest_len, &parts)
937        };
938    }
939    Ok(())
940}
941
942/// Writes `value` into every logical destination element.
943///
944/// Used when the reduced axis is empty: the fold would read zero source
945/// elements, so the destination is exactly `init` everywhere and no source
946/// pointer may be formed. The odometer keeps the fill allocation-free within
947/// [`strided_view::INLINE_RANK`] and spills above it.
948fn fill_outputs_with<U: Clone>(dest: &mut StridedViewMut<U>, value: &U) {
949    let dims = dest.dims();
950    if dims.contains(&0) {
951        return;
952    }
953    let strides = dest.strides();
954    let rank = dims.len();
955    let total: usize = dims.iter().product();
956    let base = dest.as_mut_ptr();
957    if rank == 0 {
958        // SAFETY: a rank-0 destination is one writable element.
959        unsafe { *base = value.clone() };
960        return;
961    }
962    let mut coords_inline = [0usize; strided_view::INLINE_RANK];
963    let mut coords_heap: Vec<usize> = Vec::new();
964    let coords: &mut [usize] = if rank <= strided_view::INLINE_RANK {
965        &mut coords_inline[..rank]
966    } else {
967        coords_heap.resize(rank, 0);
968        &mut coords_heap[..]
969    };
970    let mut offset = 0isize;
971    for _ in 0..total {
972        // SAFETY: the destination is an injective view validated by the
973        // caller, and the cursor visits each logical element exactly once, so
974        // every written offset is in bounds.
975        unsafe { *base.offset(offset) = value.clone() };
976        for axis in 0..rank {
977            coords[axis] += 1;
978            offset += strides[axis];
979            if coords[axis] < dims[axis] {
980                break;
981            }
982            coords[axis] = 0;
983            offset -= dims[axis] as isize * strides[axis];
984        }
985    }
986}
987
988/// Shared inputs of [`reduce_axis_range_linear`] and
989/// [`reduce_axis_range_strided`].
990struct AxisParts<'a, M, R, U> {
991    /// `(extent, source stride)` of every kept axis, in output order.
992    kept: &'a [(usize, isize)],
993    /// Destination stride of every kept axis, in the same order.
994    dest_strides: &'a [isize],
995    axis_len: usize,
996    axis_stride: isize,
997    map_fn: &'a M,
998    reduce_fn: &'a R,
999    init: &'a U,
1000}
1001
1002/// Column-major destination cursor: the destination offset is the linear output
1003/// index, so no destination stride bookkeeping is needed.
1004///
1005/// Kept out of line so the common path stays the same single-cursor loop as the
1006/// pre-split kernel.
1007///
1008/// # Safety
1009/// `out` must be a column-major destination of `parts.kept` extents, `range` a
1010/// subrange of its logical output indices that no other thread writes, and
1011/// every source offset reachable from a kept coordinate plus
1012/// `k * parts.axis_stride` for `k < parts.axis_len` must be a readable element
1013/// of `src`.
1014#[inline(never)]
1015unsafe fn reduce_axis_range_linear<T, Op, M, R, U>(
1016    src: *const T,
1017    out: *mut U,
1018    range: Range<usize>,
1019    parts: &AxisParts<'_, M, R, U>,
1020) where
1021    T: Copy,
1022    Op: ElementOp<T>,
1023    M: Fn(T) -> U,
1024    R: Fn(U, U) -> U,
1025    U: Clone,
1026{
1027    let kept = parts.kept;
1028    let (lead_extent, lead_stride) = kept[0];
1029    let sweep = lead_stride == 1 && lead_extent >= AXIS_SWEEP_MIN_LEN && parts.axis_len > 1;
1030    let block = (AXIS_SWEEP_BLOCK_BYTES / std::mem::size_of::<U>().max(1)).max(AXIS_SWEEP_MIN_LEN);
1031
1032    // Decode the first output once, then advance incrementally. A fixed stack
1033    // array with a spill covers every inline rank without the per-access enum
1034    // match a `TinyVec` cursor would add to the hot loop, and allocates only
1035    // above `INLINE_RANK` (#306 P1).
1036    let coords_len = kept.len();
1037    let mut coords_inline = [0usize; strided_view::INLINE_RANK];
1038    let mut coords_heap: Vec<usize> = Vec::new();
1039    let coords: &mut [usize] = if coords_len <= strided_view::INLINE_RANK {
1040        &mut coords_inline[..coords_len]
1041    } else {
1042        coords_heap.resize(coords_len, 0);
1043        &mut coords_heap[..]
1044    };
1045    let mut rest = range.start;
1046    let mut src_off = 0isize;
1047    for (coord, &(extent, stride)) in coords.iter_mut().zip(kept) {
1048        *coord = rest % extent;
1049        rest /= extent;
1050        src_off += *coord as isize * stride;
1051    }
1052
1053    let mut output = range.start;
1054    while output < range.end {
1055        let len = if sweep {
1056            (lead_extent - coords[0]).min(range.end - output).min(block)
1057        } else {
1058            1
1059        };
1060        if sweep {
1061            // SAFETY: the `len` outputs are inside the caller's range, their
1062            // unit-stride destination elements are contiguous, and their
1063            // unit-stride source columns are readable.
1064            unsafe { sweep_block::<T, Op, M, R, U>(src, src_off, out.add(output), len, parts) };
1065        } else {
1066            // SAFETY: one reachable output and its reduced run.
1067            unsafe {
1068                let acc = fold_run::<T, Op, M, R, U>(
1069                    src.offset(src_off),
1070                    parts.axis_stride,
1071                    parts.axis_len,
1072                    parts.init.clone(),
1073                    parts.map_fn,
1074                    parts.reduce_fn,
1075                );
1076                *out.add(output) = acc;
1077            }
1078        }
1079        output += len;
1080        if output >= range.end {
1081            break;
1082        }
1083        // Advance the kept coordinates by `len` along the leading axis; a
1084        // block never crosses the end of the leading axis.
1085        coords[0] += len;
1086        src_off += len as isize * lead_stride;
1087        let mut axis = 0;
1088        while coords[axis] == kept[axis].0 {
1089            src_off -= kept[axis].0 as isize * kept[axis].1;
1090            coords[axis] = 0;
1091            axis += 1;
1092            coords[axis] += 1;
1093            src_off += kept[axis].1;
1094        }
1095    }
1096}
1097
1098/// General strided destination cursor, tracking a destination offset next to
1099/// the source offset.
1100///
1101/// # Safety
1102/// `out` must be a destination of `parts.kept` extents with injective layout,
1103/// `range` a subrange of its logical output indices that no other thread
1104/// writes, and every source offset reachable from a kept coordinate plus
1105/// `k * parts.axis_stride` for `k < parts.axis_len` must be a readable element
1106/// of `src`.
1107#[inline(never)]
1108unsafe fn reduce_axis_range_strided<T, Op, M, R, U>(
1109    src: *const T,
1110    out: *mut U,
1111    range: Range<usize>,
1112    parts: &AxisParts<'_, M, R, U>,
1113) where
1114    T: Copy,
1115    Op: ElementOp<T>,
1116    M: Fn(T) -> U,
1117    R: Fn(U, U) -> U,
1118    U: Clone,
1119{
1120    let kept = parts.kept;
1121    let dest_strides = parts.dest_strides;
1122    let (lead_extent, lead_stride) = kept[0];
1123    let lead_dest_stride = dest_strides[0];
1124    let sweep = lead_stride == 1
1125        && lead_dest_stride == 1
1126        && lead_extent >= AXIS_SWEEP_MIN_LEN
1127        && parts.axis_len > 1;
1128    let block = (AXIS_SWEEP_BLOCK_BYTES / std::mem::size_of::<U>().max(1)).max(AXIS_SWEEP_MIN_LEN);
1129
1130    // Same fixed-array-plus-spill cursor as `reduce_axis_range_linear`: the
1131    // `TinyVec` enum match is not paid per output access in the hot loop and
1132    // only ranks above `INLINE_RANK` allocate (#306 P1).
1133    let coords_len = kept.len();
1134    let mut coords_inline = [0usize; strided_view::INLINE_RANK];
1135    let mut coords_heap: Vec<usize> = Vec::new();
1136    let coords: &mut [usize] = if coords_len <= strided_view::INLINE_RANK {
1137        &mut coords_inline[..coords_len]
1138    } else {
1139        coords_heap.resize(coords_len, 0);
1140        &mut coords_heap[..]
1141    };
1142    let mut rest = range.start;
1143    let mut src_off = 0isize;
1144    let mut dest_off = 0isize;
1145    for (coord, (&(extent, stride), &dest_stride)) in
1146        coords.iter_mut().zip(kept.iter().zip(dest_strides))
1147    {
1148        *coord = rest % extent;
1149        rest /= extent;
1150        src_off += *coord as isize * stride;
1151        dest_off += *coord as isize * dest_stride;
1152    }
1153
1154    let mut output = range.start;
1155    while output < range.end {
1156        let len = if sweep {
1157            (lead_extent - coords[0]).min(range.end - output).min(block)
1158        } else {
1159            1
1160        };
1161        if sweep {
1162            // SAFETY: as for the linear cursor; `lead_dest_stride == 1` makes
1163            // the `len` destinations contiguous.
1164            unsafe {
1165                sweep_block::<T, Op, M, R, U>(src, src_off, out.offset(dest_off), len, parts)
1166            };
1167        } else {
1168            // SAFETY: one reachable output and its reduced run.
1169            unsafe {
1170                let acc = fold_run::<T, Op, M, R, U>(
1171                    src.offset(src_off),
1172                    parts.axis_stride,
1173                    parts.axis_len,
1174                    parts.init.clone(),
1175                    parts.map_fn,
1176                    parts.reduce_fn,
1177                );
1178                *out.offset(dest_off) = acc;
1179            }
1180        }
1181        output += len;
1182        if output >= range.end {
1183            break;
1184        }
1185        coords[0] += len;
1186        src_off += len as isize * lead_stride;
1187        dest_off += len as isize * lead_dest_stride;
1188        let mut axis = 0;
1189        while coords[axis] == kept[axis].0 {
1190            src_off -= kept[axis].0 as isize * kept[axis].1;
1191            dest_off -= kept[axis].0 as isize * dest_strides[axis];
1192            coords[axis] = 0;
1193            axis += 1;
1194            coords[axis] += 1;
1195            src_off += kept[axis].1;
1196            dest_off += dest_strides[axis];
1197        }
1198    }
1199}
1200
1201/// Accumulates `len` adjacent outputs, one contiguous source column per
1202/// reduced index, keeping the left to right order per output.
1203///
1204/// # Safety
1205/// `out` must be `len` writable initialized outputs and `src + src_off +
1206/// k * parts.axis_stride` must start `len` readable elements for every
1207/// `k < parts.axis_len`.
1208#[inline(always)]
1209unsafe fn sweep_block<T, Op, M, R, U>(
1210    src: *const T,
1211    src_off: isize,
1212    out: *mut U,
1213    len: usize,
1214    parts: &AxisParts<'_, M, R, U>,
1215) where
1216    T: Copy,
1217    Op: ElementOp<T>,
1218    M: Fn(T) -> U,
1219    R: Fn(U, U) -> U,
1220    U: Clone,
1221{
1222    let map_fn = parts.map_fn;
1223    let reduce_fn = parts.reduce_fn;
1224    // SAFETY: forwarded caller contract.
1225    let out = unsafe { std::slice::from_raw_parts_mut(out, len) };
1226    let column = |k: usize| {
1227        // SAFETY: forwarded caller contract for reduced index `k`.
1228        unsafe {
1229            std::slice::from_raw_parts(src.offset(src_off + k as isize * parts.axis_stride), len)
1230        }
1231    };
1232    let first = column(0);
1233    for (slot, &value) in out.iter_mut().zip(first) {
1234        *slot = reduce_fn(parts.init.clone(), map_fn(Op::apply(value)));
1235    }
1236    let mut k = 1;
1237    while k + 4 <= parts.axis_len {
1238        let (c0, c1, c2, c3) = (column(k), column(k + 1), column(k + 2), column(k + 3));
1239        for (index, slot) in out.iter_mut().enumerate() {
1240            let mut acc = reduce_fn(slot.clone(), map_fn(Op::apply(c0[index])));
1241            acc = reduce_fn(acc, map_fn(Op::apply(c1[index])));
1242            acc = reduce_fn(acc, map_fn(Op::apply(c2[index])));
1243            acc = reduce_fn(acc, map_fn(Op::apply(c3[index])));
1244            *slot = acc;
1245        }
1246        k += 4;
1247    }
1248    while k < parts.axis_len {
1249        let values = column(k);
1250        for (slot, &value) in out.iter_mut().zip(values) {
1251            *slot = reduce_fn(slot.clone(), map_fn(Op::apply(value)));
1252        }
1253        k += 1;
1254    }
1255}