IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Mojo function

foreach

def foreach[dtype: DType, rank: Int, ctx_target: CompilationTarget, //, FuncType: def[width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, *, simd_width: Int = get_kernel_simd_width[dtype, ctx_target](), _trace_name: StaticString = "mogg.for_each"](var func: FuncType, tensor: ManagedTensorSlice[static_spec=tensor.static_spec], ctx: DeviceContext[ctx_target])

Apply a RegisterPassable body to each element of the tensor slice.

Value-argument twin of the foreach overload above: the body is a runtime closure passed by value rather than a comptime parameter, so callers write a unified closure instead of a capturing one. The body receives the element index as a Coord; use coord_to_index_list to convert it to an IndexList if integer index arithmetic is needed.

The wrapper captures both func and tensor and routes the store through tensor._fused_store, which preserves the fused store path that FusedOutputTensor depends on.

Parameters:

  • ​dtype (DType): The data type of the elements in the tensor slice.
  • ​rank (Int): The rank of the tensor slice.
  • ​ctx_target (CompilationTarget): The compilation target of ctx (inferred).
  • ​FuncType (def[width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of the per-element body closure.
  • ​simd_width (Int): The SIMD width for the target (usually leave this as its default value).
  • ​_trace_name (StaticString): Name of the executed operation displayed in the trace_description.

Args:

  • ​func (FuncType): The function to apply to each element of the tensor slice.
  • ​tensor (ManagedTensorSlice[static_spec=tensor.static_spec]): The output tensor slice which receives the return values from func.
  • ​ctx (DeviceContext[ctx_target]): The call context (forward this from the custom operation).

def foreach[Z: Output & Tensor, ctx_target: CompilationTarget, FuncType: def[width: Int](Coord[**?]) -> SIMD[Z.dtype, width] & RegisterPassable & ImplicitlyCopyable, //, *, simd_width: Int = get_kernel_simd_width[Z.dtype, ctx_target](), _trace_name: StaticString = "mogg.for_each"](var func: FuncType, z: Z, ctx: DeviceContext[ctx_target])

Apply a RegisterPassable body to each element of a tensor-trait output.

Tensor-trait twin of the ManagedTensorSlice overload above. A dense output stores through its TileTensor projection. A fused output stores through Storable.store, which runs the fused consumer epilogue.

Constraints:

Z conforms to TileTensorable, or to Storable and TrivialRegisterPassable. A fused output runs only on the CPU, because its host proxy cannot store on a device.

Parameters:

  • ​Z (Output & Tensor): The type of the output tensor argument.
  • ​ctx_target (CompilationTarget): The compilation target of ctx (inferred).
  • ​FuncType (def[width: Int](Coord[**?]) -> SIMD[Z.dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of the per-element body closure.
  • ​simd_width (Int): The SIMD width for the target (usually leave this as its default value).
  • ​_trace_name (StaticString): Name of the executed operation displayed in the trace_description.

Args:

  • ​func (FuncType): The function to apply to each element of the output.
  • ​z (Z): The output tensor argument which receives the return values from func.
  • ​ctx (DeviceContext[ctx_target]): The call context (forward this from the custom operation).

Was this page helpful?