Inferensys

Glossary

Gather-Scatter Operations

Gather-Scatter operations are fundamental parallel computing primitives where 'gather' collects data from non-contiguous memory addresses into a contiguous vector, and 'scatter' writes a contiguous vector to non-contiguous addresses.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
SPARSE MODEL INFERENCE

What is Gather-Scatter Operations?

Gather-scatter operations are fundamental parallel computing primitives essential for the efficient execution of sparse neural networks and other irregular data computations.

Gather-scatter operations are low-level parallel computing instructions that manage data movement between contiguous and non-contiguous memory addresses. The gather operation reads a vector of data from a list of scattered memory locations into a contiguous register or buffer. Conversely, the scatter operation writes a contiguous vector of data out to a list of scattered memory addresses. These operations are the foundational mechanism for implementing sparse linear algebra kernels, such as sparse matrix-vector multiplication (SpMV), by efficiently collecting non-zero operands and distributing results.

In the context of sparse model inference, these operations are critical for performance. After pruning creates a network with many zero-valued weights, a sparse inference kernel uses gather to collect the active weights and corresponding activations from memory. It then performs the necessary multiplications and uses scatter to write the results to the correct locations in the output tensor. The efficiency of these operations directly impacts the sparse efficiency gap, as poor memory access patterns and load imbalance can negate the theoretical speedup from zero-skipping.

PARALLEL COMPUTING PRIMITIVES

Core Characteristics of Gather-Scatter

Gather and scatter are fundamental data movement operations that enable efficient computation on sparse, non-contiguous data by transforming between contiguous and indexed memory layouts.

01

Gather Operation

A gather operation collects data elements from a source array using a list of indices, assembling them into a contiguous destination vector. This is essential for loading the non-zero weights and corresponding activations required for a sparse matrix multiplication.

  • Mechanism: dest[i] = src[indices[i]]
  • Use Case: Loading a dense vector of values from a sparse matrix's non-zero entries, as defined by its index arrays (e.g., CSR column indices).
  • Performance Impact: Efficiency is dominated by the random read access pattern from the source memory, which can cause cache thrashing.
02

Scatter Operation

A scatter operation writes elements from a contiguous source vector to a destination array at specified indices. This is used to accumulate partial results, such as output activations, to their correct locations after a sparse computation.

  • Mechanism: dest[indices[i]] = src[i]
  • Use Case: Writing the computed results of a sparse matrix-vector multiplication to the correct rows of the output tensor.
  • Performance Impact: Random write access can be even more costly than gathers due to write-back policies and potential for write conflicts, requiring atomic operations in parallel contexts.
03

Indexed vs. Contiguous Data

The core purpose of gather-scatter is to bridge the gap between indexed data (sparse, irregular) and contiguous data (dense, regular).

  • Sparse Tensor Formats (like CSR, COO) store data as (value, index) pairs.
  • Compute Kernels (like SpMM) require dense vectors of operands for efficient SIMD/vector processing.
  • Gather-Scatter Cycle: 1) Gather non-zero operands into dense buffers. 2) Perform dense compute on buffers. 3) Scatter results back via indices. This cycle is the heart of sparse inference.
04

Hardware Instruction Support

Modern CPU and GPU ISAs include explicit instructions to accelerate these patterns, reducing overhead versus software-managed loops.

  • CPU: Intel AVX-512 provides VGATHERDPS and VSCATTERDPS instructions.
  • GPU: NVIDIA GPUs execute gather-scatter via their load/store units and the ld.global.nc/st.global.wb instructions, often coalesced within a warp.
  • Specialized Accelerators: NPUs and TPUs often integrate dedicated gather-scatter units into their memory hierarchies to minimize latency for sparse workloads.
05

Performance Bottlenecks

The primary challenge for gather-scatter is memory bandwidth utilization and latency, not compute.

  • Irregular Access: Non-sequential memory reads/writes defeat spatial prefetchers and reduce cache line utilization.
  • Metadata Overhead: Processing index arrays consumes additional memory bandwidth and instruction cycles.
  • Load Imbalance: In parallel execution, threads may have varying numbers of indices to process, causing some cores to stall waiting for others.
  • The Sparse Efficiency Gap: The theoretical FLOP reduction from sparsity is often not fully realized due to these memory system overheads.
06

Relationship to Sparse Kernels

Gather-scatter operations are the enabling primitives for higher-level sparse kernels like Sparse Matrix-Matrix Multiplication (SpMM) and Sparse Convolution.

  • SpMM Kernel Phases: 1) Gather input feature map slices. 2) Perform dense sub-matrix multiplications. 3) Scatter-add results to the output matrix.
  • Blocked Sparsity (e.g., N:M): Uses structured indices to make gather-scatter patterns more regular and efficient, improving cache locality and enabling use of Sparse Tensor Cores.
  • Compiler Role: Advanced compilers fuse operations to minimize intermediate gather-scatter steps, a key optimization called sparse operator fusion.
FUNDAMENTAL DATA MOVEMENT PRIMITIVES

Gather vs. Scatter: Operation Comparison

A technical comparison of the gather and scatter operations, which are foundational for efficient sparse tensor computation in neural network inference.

Feature / CharacteristicGather OperationScatter Operation

Primary Function

Collects data from non-contiguous source addresses into a contiguous destination vector.

Distributes data from a contiguous source vector to non-contiguous destination addresses.

Data Flow Direction

Read from multiple, scattered memory locations.

Write to multiple, scattered memory locations.

Memory Access Pattern

Irregular reads. Performance is often memory-bandwidth bound.

Irregular writes. Performance is often limited by write serialization and cache policies.

Typical Use Case in Sparse Inference

Fetching non-zero weights or input activations based on an index list for a sparse matrix multiplication (SpMM).

Accumulating partial results from a sparse computation back to a dense output tensor at computed index locations.

Hardware Instruction Support

Common (e.g., vgather in AVX2/AVX-512, GPU instructions). Often optimized for read coalescing.

Less common. May be implemented via atomic operations or simulated with read-modify-write sequences. More challenging to optimize.

Atomicity Requirement

Not required. Concurrent reads are safe.

Often required for correctness in parallel execution to prevent race conditions when multiple threads target the same address.

Performance Bottleneck

Cache misses and memory latency due to random reads. Efficiency depends on index coherence.

Store buffer saturation, cache line contention, and atomic operation overhead. Often slower than gather.

Representative Pseudocode

for i in indices: dest[i] = src[indices[i]]

for i in indices: dest[indices[i]] += src[i] (requires atomic add for parallelism)

SPARSE MODEL INFERENCE

Frequently Asked Questions

Essential questions about gather-scatter operations, the fundamental memory access primitives that enable efficient execution of sparse neural networks on parallel hardware.

A gather-scatter operation is a pair of parallel computing primitives for efficient, non-contiguous memory access. Gather reads data from a set of specified (often irregular) memory addresses into a contiguous vector register. Scatter performs the inverse, writing data from a contiguous vector to a set of specified non-contiguous addresses. These operations are critical for executing sparse linear algebra, where data (like non-zero matrix values) is not stored contiguously in memory.

In the context of sparse model inference, a gather operation collects the active (non-zero) weights and their corresponding activations from memory based on a pruning mask or sparse index list. After the computation (e.g., a multiply-accumulate), a scatter operation may write the results back to specific locations in an output tensor. These operations replace inefficient, serialized pointer-chasing loops with parallelized, vectorized memory instructions, though they introduce overhead from index processing.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.