reduction
实现 SIMD reductions。
您可以从包中导入这些 API。例如:algorithm
from algorithm import map_reduce
map_reduce
map_reduce[
simd_width: Int,
size: Dim,
type: DType,
acc_type: DType,
input_gen_fn: fn[DType, Int](Int, /) capturing -> SIMD[$0, $1],
reduce_vec_to_vec_fn: fn[DType, DType, Int](SIMD[$0, $2], SIMD[$1, $2], /) capturing -> SIMD[$0, $2],
reduce_vec_to_scalar_fn: fn[DType, Int](SIMD[$0, $1], /) -> SIMD[$0, 1]
]
(dst: Buffer[type, size, 0], init: SIMD[acc_type, 1]) -> SIMD[acc_type, 1]
将调用input_gen_fn的结果存储在 dst 中,同时使用自定义归并函数归并结果。
参数:
- simd_width :计算的向量宽度。
Int
- size :缓冲区大小。
Dim
- type :缓冲区元素 dtype。
DType
- acc_type :归并累加器的 dtype。
DType
- input_gen_fn :生成要reduction的输入的函数。
fn[DType, Int](Int, /) capturing -> SIMD[$0, $1]
- reduce_vec_to_vec_fn :映射函数。此函数用于组合累积两个输入数据块:例如,我们加载两个元素向量,并需要将它们简化为单个向量。
fn[DType, DType, Int](SIMD[$0, $2], SIMD[$1, $2], /) capturing -> SIMD[$0, $2]``8xfloat32``8xfloat32
- reduce_vec_to_scalar_fn :归并函数。此函数用于将向量简化为标量。例如,当我们得到向量并希望将其简化为标量时。
fn[DType, Int](SIMD[$0, $1], /) -> SIMD[$0, 1]``8xfloat32``float32
参数:
- dst :输出缓冲区。
Buffer[type, size, 0]
- init :要在累加器中使用的初始值。
SIMD[acc_type, 1]
返回:
计算出的归并值。
reduce
reduce[
reduce_fn: fn[DType, DType, Int](SIMD[$0, $2], SIMD[$1, $2], /) capturing -> SIMD[$0, $2