Skip to main content

Mojo struct

ThreadScope

struct ThreadScope

Represents the scope of thread operations in GPU programming.

This struct defines the scope at which thread operations are performed, particularly for operations like tensor distribution and synchronization. It provides two main scopes: BLOCK and WARP, which correspond to different levels of thread grouping in GPU programming models.

Example:

from layout.layout_tensor import copy_dram_to_sram, ThreadScope

# Distribute tensor at block level (all threads in block participate)
copy_dram_to_sram[layout, thread_scope=ThreadScope.BLOCK](dst, src)

# Distribute tensor at warp level (only threads in same warp participate)
copy_dram_to_sram[layout, thread_scope=ThreadScope.WARP](dst, src)

Performance:

  • WARP scope operations typically have lower synchronization overhead than BLOCK scope operations.
  • BLOCK scope operations allow coordination across all threads in a block, which is necessary for certain algorithms.
  • The choice of scope can significantly impact performance and correctness of parallel algorithms.

Notes:

  • The appropriate scope depends on the specific algorithm and hardware.
  • WARP scope operations may be more efficient for operations that only require coordination within a warp.
  • BLOCK scope operations are necessary when threads from different warps need to coordinate.
  • The actual size of a warp or block is hardware-dependent.

Implemented traits

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable, TrivialRegisterPassable, Writable

comptime members

BLOCK

comptime BLOCK = ThreadScope(0)

Represents operations at the thread block level, where all threads in a block participate.

WARP

comptime WARP = ThreadScope(1)

Represents operations at the warp level, where only threads within the same warp participate.

Methods

__init__

__init__(value: Int) -> Self

Initialize a ThreadScope with the given integer value.

Args:

  • value (Int): An integer representing the thread scope (0 for BLOCK, 1 for WARP).

__eq__

__eq__(self, other: Self) -> Bool

Compare two ThreadScope objects for equality.

Args:

  • other (Self): Another ThreadScope object to compare with.

Returns:

Bool: True if the thread scopes are equal, False otherwise.

__ne__

__ne__(self, other: Self) -> Bool

Compare two ThreadScope objects for inequality.

Args:

  • other (Self): Another ThreadScope object to compare with.

Returns:

Bool: True if the thread scopes are not equal, False otherwise.

write_to

write_to(self, mut writer: T)

Write the ThreadScope as a human-readable string representation.

Aborts: If the thread scope has an invalid value.

Args:

  • writer (T): The writer to write the string representation to.

__int__

__int__(self) -> Int

Convert the ThreadScope to an integer value.

Returns:

Int: The integer value of the thread scope (0 for BLOCK, 1 for WARP).

Was this page helpful?