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 mut chunks = rest.chunks_exact(FOLD_LANES);
73    for chunk in chunks.by_ref() {
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(chunks.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 `chunks_exact` + `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 mut chunks_a = rest_a.chunks_exact(FOLD_LANES);
355    let mut chunks_b = rest_b.chunks_exact(FOLD_LANES);
356    for (chunk_a, chunk_b) in chunks_a.by_ref().zip(chunks_b.by_ref()) {
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
365        .iter_mut()
366        .zip(chunks_a.remainder())
367        .zip(chunks_b.remainder())
368    {
369        *lane = reduce_fn(
370            lane.clone(),
371            map_fn(OpA::apply(value_a), OpB::apply(value_b)),
372        );
373    }
374    lanes.into_iter().fold(init, reduce_fn)
375}
376
377/// Folds `len` operand pairs starting at `a_ptr`/`b_ptr` with the given
378/// strides.
379///
380/// # Safety
381/// `a_ptr.offset(i * a_stride)` and `b_ptr.offset(i * b_stride)` must be
382/// readable elements for every `i < len`.
383#[inline(always)]
384#[allow(clippy::too_many_arguments)]
385unsafe fn fold_run2<A, B, OpA, OpB, M, R, U>(
386    a_ptr: *const A,
387    a_stride: isize,
388    b_ptr: *const B,
389    b_stride: isize,
390    len: usize,
391    init: &U,
392    map_fn: &M,
393    reduce_fn: &R,
394) -> U
395where
396    A: Copy,
397    B: Copy,
398    OpA: ElementOp<A>,
399    OpB: ElementOp<B>,
400    M: Fn(A, B) -> U,
401    R: Fn(U, U) -> U,
402    U: Clone,
403{
404    if a_stride == 1 && b_stride == 1 {
405        // SAFETY: the caller proves `len` readable unit-stride elements of
406        // both operands.
407        let a = unsafe { std::slice::from_raw_parts(a_ptr, len) };
408        let b = unsafe { std::slice::from_raw_parts(b_ptr, len) };
409        return fold_contiguous2::<A, B, OpA, OpB, M, R, U>(a, b, init.clone(), map_fn, reduce_fn);
410    }
411    let mut acc = init.clone();
412    let mut cursor_a = a_ptr;
413    let mut cursor_b = b_ptr;
414    for index in 0..len {
415        // SAFETY: the caller proves every pair of the run is readable.
416        let mapped = map_fn(
417            OpA::apply(unsafe { *cursor_a }),
418            OpB::apply(unsafe { *cursor_b }),
419        );
420        acc = reduce_fn(acc, mapped);
421        if index + 1 < len {
422            cursor_a = cursor_a.wrapping_offset(a_stride);
423            cursor_b = cursor_b.wrapping_offset(b_stride);
424        }
425    }
426    acc
427}
428
429/// Two-input mapped reduction: `reduce(init, op, map.(a[i], b[i]))`.
430///
431/// This is the two-operand sibling of [`reduce`]. It exists so that the
432/// accumulator type `U` is chosen by the caller instead of being tied to the
433/// operand types by `Mul`, which is what makes a widening accumulation
434/// possible — for example an `f32` dot accumulated in `f64`:
435///
436/// ```
437/// use strided_basic::{zip_reduce, Exec, StridedArray};
438///
439/// let a = StridedArray::<f32>::from_fn_col_major(&[2, 3], |i| (1 + i[0] + 2 * i[1]) as f32);
440/// let b = StridedArray::<f32>::from_fn_col_major(&[2, 3], |i| (1 + i[0]) as f32 * 0.5);
441/// let dot: f64 = zip_reduce(
442///     &a.view(),
443///     &b.view(),
444///     |x: f32, y: f32| x as f64 * y as f64,
445///     |x, y| x + y,
446///     0.0,
447///     Exec::serial(),
448/// )
449/// .unwrap();
450/// assert_eq!(dot, 16.5);
451/// ```
452///
453/// Both operands must have the same shape. Each operand's [`ElementOp`] is
454/// applied before `map_fn`, so conjugation and transposition flags are honoured
455/// without materializing a conjugated copy. Permuted, gapped, negative and
456/// stride-0 (broadcast) operands are supported: the traversal is the fused
457/// plan of [`reduce`], extended to two stride lists.
458///
459/// # Evaluation order
460///
461/// The result is independent of the pool when the call runs with a serial
462/// token ([`Exec::serial`]) or a budget of one: only serial branches run, and
463/// their fold order is fixed by the operand layouts. With a larger budget the
464/// fold is split over the budget, so the result is repeatable for a fixed
465/// shape, layout and budget but can differ between budgets.
466///
467/// In neither case is this a strict left to right fold: a contiguous operand
468/// pair is folded with the same sixteen independent lanes as [`reduce`], the
469/// general plan is traversed in a cache-friendly loop order, and parallel
470/// execution folds per-worker chunks independently and combines them in a
471/// tree. Floating point results can therefore differ from a sequential left to
472/// right sum in the last bits, and no bitwise equality across machines or
473/// target features is guaranteed.
474///
475/// Because of that, `reduce_fn` must be associative and commutative, and
476/// `init` must be an identity of `reduce_fn` whenever the reduction can run on
477/// several threads.
478///
479/// # Allocation
480///
481/// At rank ≤ [`INLINE_RANK`](strided_view::INLINE_RANK) the view metadata and
482/// the two-operand traversal plan are stored inline, so a call performs no heap
483/// allocation. Above that bound the planner may spill to the heap.
484///
485/// # Errors
486///
487/// Returns [`StridedError::RankMismatch`] / [`StridedError::ShapeMismatch`] when
488/// the operand shapes differ, and [`StridedError::OffsetOverflow`] for
489/// unrepresentable element counts.
490pub fn zip_reduce<A, B, OpA, OpB, M, R, U>(
491    a: &StridedView<A, OpA>,
492    b: &StridedView<B, OpB>,
493    map_fn: M,
494    reduce_fn: R,
495    init: U,
496    exec: Exec<'_>,
497) -> Result<U>
498where
499    A: Copy + Send + Sync,
500    B: Copy + Send + Sync,
501    OpA: ElementOp<A>,
502    OpB: ElementOp<B>,
503    M: Fn(A, B) -> U + Sync,
504    R: Fn(U, U) -> U + Sync,
505    U: Clone + Send + Sync,
506{
507    ensure_same_shape(a.dims(), b.dims())?;
508
509    let a_ptr = a.ptr();
510    let b_ptr = b.ptr();
511    let dims = a.dims();
512    let a_strides = a.strides();
513    let b_strides = b.strides();
514
515    // A zero-element reduction is exactly `init`. Return before planning: an
516    // empty view is allowed to carry an arbitrarily large stride, which the
517    // block-size arithmetic of the planner cannot represent.
518    let total = total_len(dims)?;
519    if total == 0 {
520        return Ok(init);
521    }
522
523    if sequential_contiguous_layout(KernelClass::Reduce, dims, &[a_strides, b_strides], &exec)?
524        .is_some()
525    {
526        return Ok(simd::dispatch_if_large(total, || {
527            // SAFETY: both operands prove `total` readable elements.
528            let sa = unsafe { std::slice::from_raw_parts(a_ptr, total) };
529            let sb = unsafe { std::slice::from_raw_parts(b_ptr, total) };
530            fold_contiguous2::<A, B, OpA, OpB, M, R, U>(sa, sb, init, &map_fn, &reduce_fn)
531        }));
532    }
533
534    // Parallel contiguous fast path: split into scheduler chunks so each chunk
535    // folds two contiguous slices, matching the serial contiguous kernel.
536    if is_parallel(KernelClass::Reduce, total, &exec)
537        && same_contiguous_layout(dims, &[a_strides, b_strides]).is_some()
538    {
539        // SAFETY: both operands prove `total` readable elements.
540        let sa = unsafe { std::slice::from_raw_parts(a_ptr, total) };
541        let sb = unsafe { std::slice::from_raw_parts(b_ptr, total) };
542        let result = crate::threading::parallel_map_reduce(
543            0..total,
544            crate::cost::for_len_tree(exec, KernelClass::Reduce, total),
545            &|range| {
546                let len = range.len();
547                simd::dispatch_if_large(len, || {
548                    fold_contiguous2::<A, B, OpA, OpB, M, R, U>(
549                        &sa[range.clone()],
550                        &sb[range],
551                        init.clone(),
552                        &map_fn,
553                        &reduce_fn,
554                    )
555                })
556            },
557            &|left, right| reduce_fn(left, right),
558        );
559        return Ok(result);
560    }
561
562    let strides_list: [&[isize]; 2] = [a_strides, b_strides];
563    let elem_size = std::mem::size_of::<A>().max(std::mem::size_of::<B>());
564    let (fused_dims, ordered_strides, plan) =
565        build_plan_fused(dims, &strides_list, None, elem_size);
566
567    let total = total_len(&fused_dims)?;
568    if is_parallel(KernelClass::Reduce, total, &exec) {
569        let a_send = SendPtr(a_ptr as *mut A);
570        let b_send = SendPtr(b_ptr as *mut B);
571        let costs = compute_costs(&ordered_strides);
572        let initial_offsets: crate::inline::OperandVec<isize> =
573            ordered_strides.iter().map(|_| 0isize).collect();
574        let map_fn_ref = &map_fn;
575        let reduce_fn_ref = &reduce_fn;
576        let init_ref = &init;
577        // The reduction tree combines each split's partial result, so no
578        // worker-count-sized output-slot vector is needed (#306 scope).
579        let result = mapreduce_threaded_fold(
580            &fused_dims,
581            &plan.block,
582            &ordered_strides,
583            &initial_offsets,
584            &costs,
585            KernelClass::Reduce,
586            exec,
587            &|dims, blocks, strides_list, offsets| {
588                let mut acc = init_ref.clone();
589                for_each_inner_block_with_offsets(
590                    dims,
591                    blocks,
592                    strides_list,
593                    offsets,
594                    |offsets, len, strides| {
595                        // SAFETY: the plan proves every pair of this block
596                        // is a readable element of both operands.
597                        acc = unsafe {
598                            fold_run2::<A, B, OpA, OpB, M, R, U>(
599                                a_send.as_const().offset(offsets[0]),
600                                strides[0],
601                                b_send.as_const().offset(offsets[1]),
602                                strides[1],
603                                len,
604                                &acc,
605                                map_fn_ref,
606                                reduce_fn_ref,
607                            )
608                        };
609                        Ok(())
610                    },
611                )?;
612                Ok(acc)
613            },
614            &|left, right| reduce_fn_ref(left, right),
615        )?;
616        return Ok(result);
617    }
618
619    let mut acc = init;
620    let initial_offsets: crate::inline::OperandVec<isize> =
621        ordered_strides.iter().map(|_| 0isize).collect();
622    for_each_inner_block_preordered(
623        &fused_dims,
624        &plan.block,
625        &ordered_strides,
626        &initial_offsets,
627        |offsets, len, strides| {
628            // SAFETY: the plan proves every pair of this block is a readable
629            // element of both operands.
630            acc = unsafe {
631                fold_run2::<A, B, OpA, OpB, M, R, U>(
632                    a_ptr.offset(offsets[0]),
633                    strides[0],
634                    b_ptr.offset(offsets[1]),
635                    strides[1],
636                    len,
637                    &acc,
638                    &map_fn,
639                    &reduce_fn,
640                )
641            };
642            Ok(())
643        },
644    )?;
645
646    Ok(acc)
647}
648
649/// Reduce along a single axis, returning a new `StridedArray`.
650///
651/// This allocates a column-major output and delegates to
652/// [`reduce_axis_into`].
653///
654/// Every output folds `init` once and then its reduced elements. When the
655/// reduced axis has unit stride those elements are folded with the lane
656/// scheme of [`reduce`], so `reduce_fn` must be associative and commutative.
657/// When the leading kept axis has unit stride, a block of adjacent outputs
658/// is accumulated one contiguous source column at a time, which keeps the
659/// left to right order per output. Outputs are split across the budget of
660/// `exec` once the number of source elements read exceeds the threading
661/// threshold; each output is computed by one task, so the result does not
662/// depend on the budget.
663///
664/// # Errors
665///
666/// Returns [`StridedError::InvalidAxis`] when `axis >= src.ndim()`, and
667/// [`StridedError::OffsetOverflow`] when the source element count cannot be
668/// represented in `usize`. The destination layout is built here, so the
669/// destination-shape errors of [`reduce_axis_into`] cannot occur.
670///
671/// # Panics
672///
673/// Panics when the kept source extents cannot be described in `isize`, which
674/// an empty source extent hides from the element-count check: reducing axis 0
675/// of a `[0, usize::MAX]` source builds the output strides with
676/// [`col_major_strides`], which panics instead of reporting an error.
677pub fn reduce_axis<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
678    src: &StridedView<T, Op>,
679    axis: usize,
680    map_fn: M,
681    reduce_fn: R,
682    init: U,
683    exec: Exec<'_>,
684) -> Result<StridedArray<U>>
685where
686    M: Fn(T) -> U + Sync,
687    R: Fn(U, U) -> U + Sync,
688    U: Clone + Send + Sync,
689{
690    let rank = src.ndim();
691    if axis >= rank {
692        return Err(StridedError::InvalidAxis { axis, rank });
693    }
694    // Reject an element count beyond `usize` before allocating the output.
695    total_len(src.dims())?;
696    let out_dims: InlineVec<usize> = src
697        .dims()
698        .iter()
699        .enumerate()
700        .filter(|(i, _)| *i != axis)
701        .map(|(_, &d)| d)
702        .collect();
703    // A full reduction keeps the historical rank-1 shape-[1] output; the
704    // `_into` form accepts any single-element destination for that case.
705    let (out_dims, out_strides) = if out_dims.is_empty() {
706        let mut dims = InlineVec::new();
707        dims.push(1usize);
708        let strides = col_major_strides(&dims);
709        (dims, strides)
710    } else {
711        let strides = col_major_strides(&out_dims);
712        (out_dims, strides)
713    };
714    let total_out = total_len(&out_dims)?;
715    let mut out =
716        StridedArray::<U>::from_parts(vec![init.clone(); total_out], &out_dims, &out_strides, 0)?;
717    {
718        let mut dest = out.view_mut();
719        // The fresh column-major destination needs no validation.
720        reduce_axis_into_impl(&mut dest, src, axis, map_fn, reduce_fn, init, exec)?;
721    }
722    Ok(out)
723}
724
725/// Reduce along a single axis into caller-owned storage.
726///
727/// `dest` must have the source's shape with `axis` removed, or any
728/// single-element destination for a full reduction, and an injective layout.
729/// Every destination element is written: an empty reduced axis fills each
730/// output with `init`.
731///
732/// # Errors
733///
734/// Returns [`StridedError::InvalidAxis`] for `axis >= src.ndim()`,
735/// [`StridedError::RankMismatch`] / [`StridedError::ShapeMismatch`] for a
736/// destination shape that is not the reduced source shape,
737/// [`StridedError::NonInjectiveOutputLayout`] for an overlapping destination,
738/// and [`StridedError::OffsetOverflow`] for unrepresentable sizes.
739///
740/// # Examples
741///
742/// ```
743/// use strided_basic::{reduce_axis_into, Exec, StridedArray};
744/// # let src = StridedArray::<f64>::from_fn_col_major(&[2, 3], |i| (i[0] + 2 * i[1]) as f64);
745/// let mut out = StridedArray::<f64>::col_major(&[3]);
746/// reduce_axis_into(&mut out.view_mut(), &src.view(), 0, |x| x, |a, b| a + b, 0.0, Exec::serial()).unwrap();
747/// assert_eq!(out.get(&[0]), 1.0);
748/// ```
749///
750/// Validation runs here; `reduce_axis_into_impl` is the shared kernel. The
751/// array-returning wrapper owns a fresh column-major destination and calls the
752/// kernel directly, so the common path does not re-validate.
753pub fn reduce_axis_into<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
754    dest: &mut StridedViewMut<U>,
755    src: &StridedView<T, Op>,
756    axis: usize,
757    map_fn: M,
758    reduce_fn: R,
759    init: U,
760    exec: Exec<'_>,
761) -> Result<()>
762where
763    M: Fn(T) -> U + Sync,
764    R: Fn(U, U) -> U + Sync,
765    U: Clone + Send + Sync,
766{
767    let rank = src.ndim();
768    if axis >= rank {
769        return Err(StridedError::InvalidAxis { axis, rank });
770    }
771    let src_dims = src.dims();
772    // Reject an element count beyond usize (a huge stride-0 broadcast) up
773    // front instead of looping over it.
774    total_len(src_dims)?;
775    let kept_len = rank - 1;
776    let dest_len = total_len(dest.dims())?;
777    if kept_len == 0 {
778        if dest_len != 1 {
779            return Err(StridedError::ShapeMismatch(vec![dest_len], vec![1]));
780        }
781    } else {
782        if dest.ndim() != kept_len {
783            return Err(StridedError::RankMismatch(dest.ndim(), kept_len));
784        }
785        let dest_dims = dest.dims();
786        let mut d = 0usize;
787        for (i, &src_dim) in src_dims.iter().enumerate() {
788            if i == axis {
789                continue;
790            }
791            if src_dim != dest_dims[d] {
792                let expected: Vec<usize> = src_dims
793                    .iter()
794                    .enumerate()
795                    .filter(|(i, _)| *i != axis)
796                    .map(|(_, &d)| d)
797                    .collect();
798                return Err(StridedError::ShapeMismatch(dest_dims.to_vec(), expected));
799            }
800            d += 1;
801        }
802        if !crate::layout_check::is_injective_layout(dest.dims(), dest.strides()) {
803            return Err(StridedError::NonInjectiveOutputLayout);
804        }
805    }
806    reduce_axis_into_impl(dest, src, axis, map_fn, reduce_fn, init, exec)
807}
808
809/// Shared kernel of [`reduce_axis_into`] and [`reduce_axis`].
810///
811/// Assumes the destination has the reduced source shape (or one element for a
812/// full reduction) and an injective layout; the public entry validates that,
813/// while the array-returning wrapper owns a freshly allocated column-major
814/// destination and can skip the re-validation.
815fn reduce_axis_into_impl<T: Copy + Send + Sync, Op: ElementOp<T>, M, R, U>(
816    dest: &mut StridedViewMut<U>,
817    src: &StridedView<T, Op>,
818    axis: usize,
819    map_fn: M,
820    reduce_fn: R,
821    init: U,
822    exec: Exec<'_>,
823) -> Result<()>
824where
825    M: Fn(T) -> U + Sync,
826    R: Fn(U, U) -> U + Sync,
827    U: Clone + Send + Sync,
828{
829    let rank = src.ndim();
830    let src_dims = src.dims();
831    let src_strides = src.strides();
832    let src_ptr = src.ptr();
833    let dest_len = total_len(dest.dims())?;
834    let axis_len = src_dims[axis];
835    let axis_stride = src_strides[axis];
836
837    if axis_len == 0 {
838        // No source elements to fold: every output is `init`. Fill the
839        // destination directly rather than traversing the source, because an
840        // empty source view carries a dangling base pointer and `offset` on it
841        // is UB even when the fold reads zero elements.
842        fill_outputs_with(dest, &init);
843        return Ok(());
844    }
845
846    if rank == 1 {
847        // Full reduction into a single destination element.
848        // SAFETY: the view proves `axis_len` readable elements along `axis`.
849        let acc = unsafe {
850            fold_run::<T, Op, M, R, U>(src_ptr, axis_stride, axis_len, init, &map_fn, &reduce_fn)
851        };
852        unsafe { *dest.as_mut_ptr() = acc };
853        return Ok(());
854    }
855    if dest_len == 0 {
856        return Ok(());
857    }
858
859    // Build the kept-axis metadata in one pass. A column-major destination is
860    // the common case (the array-returning wrapper always allocates one); its
861    // offset equals the linear output index, so its stride list is only
862    // materialized for a strided destination.
863    let dest_stride_view = dest.strides();
864    let mut kept: InlineVec<(usize, isize)> = InlineVec::new();
865    let mut dest_linear = true;
866    let mut prefix = 1usize;
867    for (i, (&dim, &src_stride)) in src_dims.iter().zip(src_strides).enumerate() {
868        if i == axis {
869            continue;
870        }
871        let d = kept.len();
872        kept.push((dim, src_stride));
873        if dest_linear && dest_stride_view[d] != prefix as isize {
874            dest_linear = false;
875        }
876        prefix = prefix.saturating_mul(dim);
877    }
878    let dest_strides: InlineVec<isize> = if dest_linear {
879        InlineVec::new()
880    } else {
881        dest_stride_view.iter().copied().collect()
882    };
883    let out_ptr = dest.as_mut_ptr();
884    let parts = AxisParts {
885        kept: &kept,
886        dest_strides: &dest_strides,
887        axis_len,
888        axis_stride,
889        map_fn: &map_fn,
890        reduce_fn: &reduce_fn,
891        init: &init,
892    };
893
894    let work = dest_len.saturating_mul(axis_len);
895    let exec = crate::cost::for_len(exec, KernelClass::Reduce, work)
896        .limit(NonZeroUsize::new(dest_len).unwrap_or(NonZeroUsize::MIN));
897    if !exec.is_serial() {
898        let src_send = SendPtr(src_ptr as *mut T);
899        let out_send = SendPtr(out_ptr);
900        let parts = &parts;
901        if dest_linear {
902            crate::threading::parallel_for_each(0..dest_len, exec, &|range| {
903                // SAFETY: worker ranges are disjoint, the destination is
904                // injective, and the view proves every source offset
905                // reachable from its kept coordinates.
906                unsafe {
907                    reduce_axis_range_linear::<T, Op, M, R, U>(
908                        src_send.as_const(),
909                        out_send.as_ptr(),
910                        range,
911                        parts,
912                    )
913                }
914            });
915        } else {
916            crate::threading::parallel_for_each(0..dest_len, exec, &|range| {
917                // SAFETY: as for the linear path.
918                unsafe {
919                    reduce_axis_range_strided::<T, Op, M, R, U>(
920                        src_send.as_const(),
921                        out_send.as_ptr(),
922                        range,
923                        parts,
924                    )
925                }
926            });
927        }
928        return Ok(());
929    }
930
931    // SAFETY: as for the parallel ranges, with one range covering all outputs.
932    if dest_linear {
933        unsafe {
934            reduce_axis_range_linear::<T, Op, M, R, U>(src_ptr, out_ptr, 0..dest_len, &parts)
935        };
936    } else {
937        unsafe {
938            reduce_axis_range_strided::<T, Op, M, R, U>(src_ptr, out_ptr, 0..dest_len, &parts)
939        };
940    }
941    Ok(())
942}
943
944/// Writes `value` into every logical destination element.
945///
946/// Used when the reduced axis is empty: the fold would read zero source
947/// elements, so the destination is exactly `init` everywhere and no source
948/// pointer may be formed. The odometer keeps the fill allocation-free within
949/// [`strided_view::INLINE_RANK`] and spills above it.
950fn fill_outputs_with<U: Clone>(dest: &mut StridedViewMut<U>, value: &U) {
951    let dims = dest.dims();
952    if dims.iter().any(|&dim| dim == 0) {
953        return;
954    }
955    let strides = dest.strides();
956    let rank = dims.len();
957    let total: usize = dims.iter().product();
958    let base = dest.as_mut_ptr();
959    if rank == 0 {
960        // SAFETY: a rank-0 destination is one writable element.
961        unsafe { *base = value.clone() };
962        return;
963    }
964    let mut coords_inline = [0usize; strided_view::INLINE_RANK];
965    let mut coords_heap: Vec<usize> = Vec::new();
966    let coords: &mut [usize] = if rank <= strided_view::INLINE_RANK {
967        &mut coords_inline[..rank]
968    } else {
969        coords_heap.resize(rank, 0);
970        &mut coords_heap[..]
971    };
972    let mut offset = 0isize;
973    for _ in 0..total {
974        // SAFETY: the destination is an injective view validated by the
975        // caller, and the cursor visits each logical element exactly once, so
976        // every written offset is in bounds.
977        unsafe { *base.offset(offset) = value.clone() };
978        for axis in 0..rank {
979            coords[axis] += 1;
980            offset += strides[axis];
981            if coords[axis] < dims[axis] {
982                break;
983            }
984            coords[axis] = 0;
985            offset -= dims[axis] as isize * strides[axis];
986        }
987    }
988}
989
990/// Shared inputs of [`reduce_axis_range_linear`] and
991/// [`reduce_axis_range_strided`].
992struct AxisParts<'a, M, R, U> {
993    /// `(extent, source stride)` of every kept axis, in output order.
994    kept: &'a [(usize, isize)],
995    /// Destination stride of every kept axis, in the same order.
996    dest_strides: &'a [isize],
997    axis_len: usize,
998    axis_stride: isize,
999    map_fn: &'a M,
1000    reduce_fn: &'a R,
1001    init: &'a U,
1002}
1003
1004/// Column-major destination cursor: the destination offset is the linear output
1005/// index, so no destination stride bookkeeping is needed.
1006///
1007/// Kept out of line so the common path stays the same single-cursor loop as the
1008/// pre-split kernel.
1009///
1010/// # Safety
1011/// `out` must be a column-major destination of `parts.kept` extents, `range` a
1012/// subrange of its logical output indices that no other thread writes, and
1013/// every source offset reachable from a kept coordinate plus
1014/// `k * parts.axis_stride` for `k < parts.axis_len` must be a readable element
1015/// of `src`.
1016#[inline(never)]
1017unsafe fn reduce_axis_range_linear<T, Op, M, R, U>(
1018    src: *const T,
1019    out: *mut U,
1020    range: Range<usize>,
1021    parts: &AxisParts<'_, M, R, U>,
1022) where
1023    T: Copy,
1024    Op: ElementOp<T>,
1025    M: Fn(T) -> U,
1026    R: Fn(U, U) -> U,
1027    U: Clone,
1028{
1029    let kept = parts.kept;
1030    let (lead_extent, lead_stride) = kept[0];
1031    let sweep = lead_stride == 1 && lead_extent >= AXIS_SWEEP_MIN_LEN && parts.axis_len > 1;
1032    let block = (AXIS_SWEEP_BLOCK_BYTES / std::mem::size_of::<U>().max(1)).max(AXIS_SWEEP_MIN_LEN);
1033
1034    // Decode the first output once, then advance incrementally. A fixed stack
1035    // array with a spill covers every inline rank without the per-access enum
1036    // match a `TinyVec` cursor would add to the hot loop, and allocates only
1037    // above `INLINE_RANK` (#306 P1).
1038    let coords_len = kept.len();
1039    let mut coords_inline = [0usize; strided_view::INLINE_RANK];
1040    let mut coords_heap: Vec<usize> = Vec::new();
1041    let coords: &mut [usize] = if coords_len <= strided_view::INLINE_RANK {
1042        &mut coords_inline[..coords_len]
1043    } else {
1044        coords_heap.resize(coords_len, 0);
1045        &mut coords_heap[..]
1046    };
1047    let mut rest = range.start;
1048    let mut src_off = 0isize;
1049    for (coord, &(extent, stride)) in coords.iter_mut().zip(kept) {
1050        *coord = rest % extent;
1051        rest /= extent;
1052        src_off += *coord as isize * stride;
1053    }
1054
1055    let mut output = range.start;
1056    while output < range.end {
1057        let len = if sweep {
1058            (lead_extent - coords[0]).min(range.end - output).min(block)
1059        } else {
1060            1
1061        };
1062        if sweep {
1063            // SAFETY: the `len` outputs are inside the caller's range, their
1064            // unit-stride destination elements are contiguous, and their
1065            // unit-stride source columns are readable.
1066            unsafe { sweep_block::<T, Op, M, R, U>(src, src_off, out.add(output), len, parts) };
1067        } else {
1068            // SAFETY: one reachable output and its reduced run.
1069            unsafe {
1070                let acc = fold_run::<T, Op, M, R, U>(
1071                    src.offset(src_off),
1072                    parts.axis_stride,
1073                    parts.axis_len,
1074                    parts.init.clone(),
1075                    parts.map_fn,
1076                    parts.reduce_fn,
1077                );
1078                *out.add(output) = acc;
1079            }
1080        }
1081        output += len;
1082        if output >= range.end {
1083            break;
1084        }
1085        // Advance the kept coordinates by `len` along the leading axis; a
1086        // block never crosses the end of the leading axis.
1087        coords[0] += len;
1088        src_off += len as isize * lead_stride;
1089        let mut axis = 0;
1090        while coords[axis] == kept[axis].0 {
1091            src_off -= kept[axis].0 as isize * kept[axis].1;
1092            coords[axis] = 0;
1093            axis += 1;
1094            coords[axis] += 1;
1095            src_off += kept[axis].1;
1096        }
1097    }
1098}
1099
1100/// General strided destination cursor, tracking a destination offset next to
1101/// the source offset.
1102///
1103/// # Safety
1104/// `out` must be a destination of `parts.kept` extents with injective layout,
1105/// `range` a subrange of its logical output indices that no other thread
1106/// writes, and every source offset reachable from a kept coordinate plus
1107/// `k * parts.axis_stride` for `k < parts.axis_len` must be a readable element
1108/// of `src`.
1109#[inline(never)]
1110unsafe fn reduce_axis_range_strided<T, Op, M, R, U>(
1111    src: *const T,
1112    out: *mut U,
1113    range: Range<usize>,
1114    parts: &AxisParts<'_, M, R, U>,
1115) where
1116    T: Copy,
1117    Op: ElementOp<T>,
1118    M: Fn(T) -> U,
1119    R: Fn(U, U) -> U,
1120    U: Clone,
1121{
1122    let kept = parts.kept;
1123    let dest_strides = parts.dest_strides;
1124    let (lead_extent, lead_stride) = kept[0];
1125    let lead_dest_stride = dest_strides[0];
1126    let sweep = lead_stride == 1
1127        && lead_dest_stride == 1
1128        && lead_extent >= AXIS_SWEEP_MIN_LEN
1129        && parts.axis_len > 1;
1130    let block = (AXIS_SWEEP_BLOCK_BYTES / std::mem::size_of::<U>().max(1)).max(AXIS_SWEEP_MIN_LEN);
1131
1132    // Same fixed-array-plus-spill cursor as `reduce_axis_range_linear`: the
1133    // `TinyVec` enum match is not paid per output access in the hot loop and
1134    // only ranks above `INLINE_RANK` allocate (#306 P1).
1135    let coords_len = kept.len();
1136    let mut coords_inline = [0usize; strided_view::INLINE_RANK];
1137    let mut coords_heap: Vec<usize> = Vec::new();
1138    let coords: &mut [usize] = if coords_len <= strided_view::INLINE_RANK {
1139        &mut coords_inline[..coords_len]
1140    } else {
1141        coords_heap.resize(coords_len, 0);
1142        &mut coords_heap[..]
1143    };
1144    let mut rest = range.start;
1145    let mut src_off = 0isize;
1146    let mut dest_off = 0isize;
1147    for (coord, (&(extent, stride), &dest_stride)) in
1148        coords.iter_mut().zip(kept.iter().zip(dest_strides))
1149    {
1150        *coord = rest % extent;
1151        rest /= extent;
1152        src_off += *coord as isize * stride;
1153        dest_off += *coord as isize * dest_stride;
1154    }
1155
1156    let mut output = range.start;
1157    while output < range.end {
1158        let len = if sweep {
1159            (lead_extent - coords[0]).min(range.end - output).min(block)
1160        } else {
1161            1
1162        };
1163        if sweep {
1164            // SAFETY: as for the linear cursor; `lead_dest_stride == 1` makes
1165            // the `len` destinations contiguous.
1166            unsafe {
1167                sweep_block::<T, Op, M, R, U>(src, src_off, out.offset(dest_off), len, parts)
1168            };
1169        } else {
1170            // SAFETY: one reachable output and its reduced run.
1171            unsafe {
1172                let acc = fold_run::<T, Op, M, R, U>(
1173                    src.offset(src_off),
1174                    parts.axis_stride,
1175                    parts.axis_len,
1176                    parts.init.clone(),
1177                    parts.map_fn,
1178                    parts.reduce_fn,
1179                );
1180                *out.offset(dest_off) = acc;
1181            }
1182        }
1183        output += len;
1184        if output >= range.end {
1185            break;
1186        }
1187        coords[0] += len;
1188        src_off += len as isize * lead_stride;
1189        dest_off += len as isize * lead_dest_stride;
1190        let mut axis = 0;
1191        while coords[axis] == kept[axis].0 {
1192            src_off -= kept[axis].0 as isize * kept[axis].1;
1193            dest_off -= kept[axis].0 as isize * dest_strides[axis];
1194            coords[axis] = 0;
1195            axis += 1;
1196            coords[axis] += 1;
1197            src_off += kept[axis].1;
1198            dest_off += dest_strides[axis];
1199        }
1200    }
1201}
1202
1203/// Accumulates `len` adjacent outputs, one contiguous source column per
1204/// reduced index, keeping the left to right order per output.
1205///
1206/// # Safety
1207/// `out` must be `len` writable initialized outputs and `src + src_off +
1208/// k * parts.axis_stride` must start `len` readable elements for every
1209/// `k < parts.axis_len`.
1210#[inline(always)]
1211unsafe fn sweep_block<T, Op, M, R, U>(
1212    src: *const T,
1213    src_off: isize,
1214    out: *mut U,
1215    len: usize,
1216    parts: &AxisParts<'_, M, R, U>,
1217) where
1218    T: Copy,
1219    Op: ElementOp<T>,
1220    M: Fn(T) -> U,
1221    R: Fn(U, U) -> U,
1222    U: Clone,
1223{
1224    let map_fn = parts.map_fn;
1225    let reduce_fn = parts.reduce_fn;
1226    // SAFETY: forwarded caller contract.
1227    let out = unsafe { std::slice::from_raw_parts_mut(out, len) };
1228    let column = |k: usize| {
1229        // SAFETY: forwarded caller contract for reduced index `k`.
1230        unsafe {
1231            std::slice::from_raw_parts(src.offset(src_off + k as isize * parts.axis_stride), len)
1232        }
1233    };
1234    let first = column(0);
1235    for (slot, &value) in out.iter_mut().zip(first) {
1236        *slot = reduce_fn(parts.init.clone(), map_fn(Op::apply(value)));
1237    }
1238    let mut k = 1;
1239    while k + 4 <= parts.axis_len {
1240        let (c0, c1, c2, c3) = (column(k), column(k + 1), column(k + 2), column(k + 3));
1241        for (index, slot) in out.iter_mut().enumerate() {
1242            let mut acc = reduce_fn(slot.clone(), map_fn(Op::apply(c0[index])));
1243            acc = reduce_fn(acc, map_fn(Op::apply(c1[index])));
1244            acc = reduce_fn(acc, map_fn(Op::apply(c2[index])));
1245            acc = reduce_fn(acc, map_fn(Op::apply(c3[index])));
1246            *slot = acc;
1247        }
1248        k += 4;
1249    }
1250    while k < parts.axis_len {
1251        let values = column(k);
1252        for (slot, &value) in out.iter_mut().zip(values) {
1253            *slot = reduce_fn(slot.clone(), map_fn(Op::apply(value)));
1254        }
1255        k += 1;
1256    }
1257}