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
reduce_addsums all eight lanes.reduce_maxandreduce_minfind the extremes.reduce_mulmultiplies the lanes together.
Keywords and builtins used here
DTypeSIMDdefmainprintvar
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.
Step 3 of 6 in SIMD, step 8 of 22 in Parameters & SIMD.