typestar

SIMD reductions in Mojo

Reductions collapse a whole vector down to one scalar in log-depth steps.

comptime I8 = SIMD[DType.int32, 8]


def main():
    var samples = I8(4, 8, 15, 16, 23, 42, 7, 3)
    print(samples.reduce_add())
    print(samples.reduce_max(), samples.reduce_min())
    print(samples.reduce_mul())

How it works

  1. reduce_add sums all eight lanes.
  2. reduce_max and reduce_min find the extremes.
  3. reduce_mul multiplies the lanes together.

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
199
Tokens
57
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 49 seconds.

Type this snippet

Step 3 of 6 in SIMD, step 8 of 22 in Parameters & SIMD.

← Previous Next →