---
title: Optimized Parallel GPU Merge Kernel
url: https://www.emergentmind.com/topics/parallel-gpu-merge-kernel
type: topic
---

# Optimized Parallel GPU Merge Kernel

A parallel GPU merge kernel is a specialized algorithmic component designed to perform k-way merging of sorted sequences on Graphics Processing Units (GPUs), exploiting their massive parallelism and unique compute and memory hierarchy characteristics. At its core, this kernel facilitates the merging of $K$ input runs into a single sorted sequence, mapping the problem efficiently onto the GPU architecture to maximize memory bandwidth and parallel throughput while minimizing synchronization and avoiding shared memory bank conflicts. The GPU Multiway Mergesort (MMS) algorithm exemplifies the state of the art in this area, offering an asymptotically optimal implementation in terms of global memory accesses and achieving practical performance advantages over previous comparison-based GPU sorting algorithms [1702.07961].

## 1. Problem Formulation: Parallel k-way Merge on GPU

Given $K$ sorted runs $A^1, \dots, A^K$ of total length $N$, the objective is to produce a single output run by merging all input runs. In the GPU setting, $P$ warps are launched, each responsible for a contiguous segment (termed a "tile") of the output, of size roughly $N/P$. Each warp operates in lockstep, issuing coalesced global-memory accesses of width $B$ (corresponding to the warp width). This design partitions the k-way merge into $P$ independent sub-merges, each of size $N/P$, promoting perfect load balance and maximizing the efficiency of memory coalescing.

## 2. Partitioning: Discovering Merge Tile Boundaries

Partitioning is accomplished by mapping each output tile to precise indices in each input run. For warp $t$ ($0 \le t < P$) and per-run starting indices $p_{t,1}, ..., p_{t,K}$, the requirement is:
$$
\sum_{j=1}^K p_{t,j} = t \cdot R,\quad R = \lceil N/P \rceil
$$
where $rank(x) = \sum_{j=1}^K |\{y\in A^j : y < x\}|$ denotes the global rank. This boundary discovery utilizes a generalization of the merge-path technique with binary search: for each run $j$, a binary search on $A^j$ finds $p_{t,j}$, relying on K–1 binary searches or SIMD comparisons in other runs to determine global rank. Each warp’s total partitioning cost is $O(K \log N)$ global memory transfers, which is asymptotically negligible provided $R \gg B\log N$.

## 3. The minBlockHeap Merge Kernel Architecture

Upon determining per-run boundaries, each warp merges its assigned tile using the minBlockHeap, a binary heap structure where each node contains $B$ sorted elements and has $K$ leaves (each representing a block from an input run). The construction proceeds as follows:

- **Build Phase**: Every leaf node reads a block of $B$ elements from its run using coalesced accesses. The heap is built bottom-up: at every internal node $v$ (with children $u$, $w$), the fillEmptyNode procedure merges the $2B$ child elements, propagating the $B$ smallest into $v$ and recursing to refill the emptied child until a leaf is reached.
- **Steady-State Output**: The root’s $B$ elements—always the smallest available—are written back to global memory as a coalesced store. The root is then marked empty, and fillEmptyNode is invoked recursively to refill from the appropriate input, continuing until all $R$ elements are emitted.

Critical to performance, all merge steps within fillEmptyNode are realized using a parallel bitonic network across registers, leveraging warp-shuffle instructions to avoid any dependence on shared memory. This design totally eliminates shared memory bank conflicts and synchronization primitives such as __syncthreads() within warps.

## 4. I/O and Work Complexity Analysis

For each merge round, every warp reads and writes $R \approx N/P$ elements, and the total global I/O cost per merge round is therefore $O(N/(P B))$. The algorithm conducts $T = O(\log_K(N/M))$ rounds, with $M$ representing device memory size. The total global I/O complexity is thus:
$$
Q(N) = O\left(\frac{N}{P B} \log_K\frac{N}{M}\right)
$$
Choosing $K = M/B$ yields:
$$
Q(N) = O\left(\frac{N}{P B}\log_{M/B}(N/B)\right)
$$
This matches the Parallel External Memory (PEM) model’s lower bound for sorting [1702.07961]. Per output block, the only additional computation is the $2\log_2 B$ bitonic warp-shuffle steps in fillEmptyNode, giving total parallel work of
$$
O\left(\frac{N}{P W}\log_K\frac{N}{M}\log_2 B\right)
$$
with $W = B$ (warp width).

## 5. Hardware Constraints and GPU-Specific Optimizations

The choice of $K$ is governed by shared memory availability. For NVIDIA Kepler (48 KiB per SM), $K = 4$ is optimal; for Maxwell (96 KiB per SM), $K = 8$. The active warps $P$ are selected to saturate all SMs, typically 512–1024 warps. By employing warp-shuffle instructions for all intra-tile communication, all shared-memory bank conflicts are averted. The base-case merge employs a warp-level shearsort for $W^2$ elements, using a bank-conflict-free transpose. The base-case size is tuned so that $N/M$ avoids problematic powers of $K$, circumventing wasteful final merges.

## 6. Empirical Performance and Comparative Evaluation

MMS is benchmarked against Thrust and modernGPU comparison-based mergesorts, as well as CUB’s radix sort, on three platforms: GTX 770 Kepler, K40m Kepler, and M4000 Maxwell. Throughputs of 400–600 M keys/s (Kepler) and ~700 M keys/s (Maxwell) are attained. For small $N$, performance matches that of MGPU/Thrust; for large $N$, MMS achieves 5–15% higher performance due to lower I/O costs. While MMS, being comparison-based, is within 30–40% of CUB’s radix sort (1.2 G keys/s), it excels on workloads that induce worst-case bank conflicts in competitors: in such scenarios, MMS delivers a 30–45% speedup and is unaffected by the performance collapses that degrade MGPU/Thrust by 2–3x in shared-memory merges.

## 7. Theoretical and Practical Significance

The MMS parallel GPU merge kernel establishes an asymptotically optimal solution both in global memory accesses and in practical throughput for comparison-based sorting on GPUs. By eliminating shared memory bank conflicts and achieving near-perfect coalescing via partitioned tiles and register-only merges, it overcomes performance bottlenecks in existing algorithms. This positions the MMS merge kernel as a foundational primitive in high-throughput, memory-efficient sorting frameworks on GPU architectures [1702.07961].

Source: https://www.emergentmind.com/topics/parallel-gpu-merge-kernel