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

elementwise

def elementwise[func: def[width: Int, rank: Int, alignment: Int = 1](IndexList[rank]) capturing -> None, simd_width: Int, *, target: StringSlice[StaticConstantOrigin] = StringSlice("cpu"), _trace_description: StringSlice[StaticConstantOrigin] = StringSlice("elementwise")](shape: Int, context: DeviceContext)

Executes func[width, rank](indices), possibly as sub-tasks, for a suitable combination of width and indices so as to cover shape. Returns when all sub-tasks have completed.

Parameters:

  • func (def[width: Int, rank: Int, alignment: Int = 1](IndexList[rank]) capturing -> None): The body function.
  • simd_width (Int): The SIMD vector width to use.
  • target (StringSlice[StaticConstantOrigin]): The target to run on.
  • _trace_description (StringSlice[StaticConstantOrigin]): Description of the trace.

Args:

  • shape (Int): The shape of the buffer.
  • context (DeviceContext): The device context to use.

Raises:

If the operation fails.

def elementwise[rank: Int, //, func: def[width: Int, rank: Int, alignment: Int = 1](IndexList[rank]) capturing -> None, simd_width: Int, *, target: StringSlice[StaticConstantOrigin] = StringSlice("cpu"), _trace_description: StringSlice[StaticConstantOrigin] = StringSlice("elementwise")](shape: IndexList[rank, element_type=shape.element_type], context: DeviceContext)

Executes func[width, rank](indices), possibly as sub-tasks, for a suitable combination of width and indices so as to cover shape. Returns when all sub-tasks have completed.

Parameters:

  • rank (Int): The rank of the buffer.
  • func (def[width: Int, rank: Int, alignment: Int = 1](IndexList[rank]) capturing -> None): The body function.
  • simd_width (Int): The SIMD vector width to use.
  • target (StringSlice[StaticConstantOrigin]): The target to run on.
  • _trace_description (StringSlice[StaticConstantOrigin]): Description of the trace.

Args:

Raises:

If the operation fails.

def elementwise[rank: Int, //, FuncType: ImplicitlyCopyable & RegisterPassable & def[width: Int, rank: Int, alignment: Int](IndexList[rank]) -> None, simd_width: Int, *, target: StringSlice[StaticConstantOrigin] = StringSlice("cpu"), _trace_description: StringSlice[StaticConstantOrigin] = StringSlice("elementwise")](func: FuncType, shape: IndexList[rank, element_type=shape.element_type], context: DeviceContext)

Unified-closure entry point for elementwise (DeviceContext).

Accepts a parametric body (already in unified-closure form, with explicit captures) and dispatches to _elementwise_impl. rank and FuncType are inferred from the runtime shape and func arguments, so simd_width is the only explicit positional parameter — callers can write elementwise[N](func, shape, ctx).

Parameters:

  • rank (Int): The rank of the buffer.
  • FuncType (ImplicitlyCopyable & RegisterPassable & def[width: Int, rank: Int, alignment: Int](IndexList[rank]) -> None): A parametric callable taking IndexList[rank] and template parameters width, rank, alignment.
  • simd_width (Int): The SIMD vector width to use.
  • target (StringSlice[StaticConstantOrigin]): The target to run on.
  • _trace_description (StringSlice[StaticConstantOrigin]): Description of the trace.

Args:

Raises:

If the operation fails.