This document is relevant for: Trn2, Trn3

nki.lnc.core_barrier#

nki.lnc.core_barrier(data: NkiTensor, cores, engine=engine.gpsimd, name=None)[source]#

Synchronize execution across physical NeuronCores sharing an HBM stack.

Note

Available only on NeuronCore-v3 or newer.

This instruction creates a synchronization point where all specified NeuronCores must reach before any can proceed. The barrier is implemented using a semaphore-based protocol where each NeuronCore writes a semaphore to each other core (remote semaphore update) and then waits for the other cores’ semaphores before continuing execution (local semaphore wait).

The use case is when two NeuronCores both need to write to disjoint portions of a shared HBM tensor (data) and they both need to consume the tensor after both cores have finished writing into the tensor. In this case, both cores can perform the write to data in HBM using nki.isa.dma_copy, and then signal to each other when the write operation is complete using nki.lnc.core_barrier.

Currently only cores=(0, 1) is supported, synchronizing exactly two physical NeuronCores that share the same HBM stack. This applies at LNC degree 1 and 2. At LNC degree 1, each physical core forms a separate Logical NeuronCore, but the pair can still access shared HBM and use the barrier to wait for each other’s writes.

The data parameter represents the shared data that all cores need to synchronize on. This must be data in shared HBM that multiple cores are accessing.

The engine parameter allows specifying which engine inside the NeuronCores should execute the barrier instruction (that is, the remote semaphore update and local semaphore wait). The barrier will block execution on this engine, other engines will not be blocked. The DMA Engine (nki.isa.engine.dma) cannot execute the barrier instruction and is rejected.

Parameters:
  • data – the shared data that all cores need to synchronize on; must be data in shared HBM

  • cores – a tuple of core indices to synchronize; only (0, 1) is supported

  • engine – the engine to execute the barrier instruction on; defaults to GpSimd Engine

  • name – (optional) name for the instruction.

Example:

Run the kernel with LNC degree 2 and input shape (2, 128, 64).

import nki
import nki.isa as nisa
import nki.language as nl
import nki.lnc as lnc

@nki.jit
def core_barrier_kernel(src):
    rank = nl.program_id(0)
    tile = nl.ndarray(src.shape[1:], dtype=src.dtype, buffer=nl.sbuf)
    shared = nl.ndarray(src.shape, dtype=src.dtype, buffer=nl.shared_hbm)
    out = nl.ndarray(src.shape, dtype=src.dtype, buffer=nl.shared_hbm)
    nisa.dma_copy(dst=tile, src=src[rank])
    nisa.dma_copy(dst=shared[rank], src=tile)
    lnc.core_barrier(shared, (0, 1))
    nisa.dma_copy(dst=tile, src=shared[1 - rank])
    nisa.dma_copy(dst=out[rank], src=tile)
    return out

This document is relevant for: Trn2, Trn3