Math Operations & Logic Blocks
Relevant source files
The following files were used as context for generating this wiki page:
The Math Operations & Logic Blocks in gr-cuda provide high-performance, GPU-accelerated signal processing primitives for GNU Radio. These blocks leverage NVIDIA GPUs to perform common mathematical transformations such as addition, constant multiplication, magnitude calculation, and logarithmic scaling. The system supports two primary implementation paths: C++/CUDA kernels for maximum performance and Python/CuPy-based blocks for rapid development with minimal overhead.
These blocks utilize a zero-copy GPU pipeline where cuda_buffer manages host-device transfers and event synchronization, allowing GPU kernels to execute sequentially without returning data to the CPU between operations.
Sources: README.md:10-25, python/cuda/qa_cupy_math.py:12-20
Arithmetic and Scalar Operations
Addition Blocks
The addition blocks support multiple input streams and various data types (float, complex, and integer). These blocks are implemented as synchronous CUDA blocks that perform element-wise addition across all input ports.
The diagram shows the fan-in architecture of the addition block where N streams are summed on the GPU.
| Block Name | Data Type | Implementation |
|---|---|---|
add_ff | float32 | C++/CUDA |
add_cc | complex64 | C++/CUDA |
add_ii | int32 | C++/CUDA |
add_cupy | Dynamic | Python/CuPy |
Sources: include/gnuradio/cuda/add.h:23-45, python/cuda/qa_add.py:20-80
Constant Multiplication
The multiply_const blocks perform scalar multiplication on each element of an input stream. These are available in both C++ and Python (CuPy) flavors, supporting both single samples and vector lengths (vlen).
# Example of CuPy-based multiply_const usage
dut = multiply_const_cupy(k=2.5, dtype=np.float32, vlen=4)
Sources: include/gnuradio/cuda/multiply_const.h:20-40, python/cuda/qa_multiply_const_cupy.py:65-75
Type Conversion and Mathematical Functions
Magnitude Operations
Magnitude blocks convert complex-valued signals into real-valued representations. These blocks are frequently used in power estimation and spectral analysis pipelines.
- Complex to Mag (
complex_to_mag_cupy): Calculates $y = |x| = \sqrt{Re(x)^2 + Im(x)^2}$. - Complex to Mag Squared (
complex_to_mag_squared_cupy): Calculates $y = |x|^2 = Re(x)^2 + Im(x)^2$.
Sources: python/cuda/complex_to_mag_cupy.py:10-25, python/cuda/qa_cupy_math.py:15-80
Logarithmic Scaling (nlog10_cupy)
The nlog10_cupy block performs the operation $y = n \cdot \log_{10}(x) + k$. It is commonly used for converting power levels to Decibels (dB).
| Parameter | Type | Description |
|---|---|---|
n | float | Multiplier (typically 10 or 20) |
k | float | Offset constant |
vlen | int | Vector length of the input |
This block includes a safety mechanism to clamp zero-value inputs, preventing the generation of -inf results during GPU processing.
Sources: python/cuda/nlog10_cupy.py:10-35, python/cuda/qa_cupy_math.py:130-160
Implementation Architecture
The math blocks inherit from two main base classes depending on the language:
- C++ Blocks: Inherit from
cuda_block. Developers manage a CUDA stream and launch kernels. Explicit synchronization usingwait_for_workandmark_work_doneis required. - Python Blocks: Inherit from
cuda.sync_block,cuda.decim_block, orcuda.interp_block. Input and output items are provided as CuPy arrays already residing on the GPU.
The class hierarchy showing how Math blocks derive from base CUDA-aware block classes.
Sources: README.md:50-95, python/cuda/complex_to_mag_cupy.py:12-15
Summary
The Math Operations & Logic Blocks form the computational core of gr-cuda. By providing standard signal processing operations implemented in both CUDA C++ and Python/CuPy, the module enables developers to maintain high throughput for arithmetic-heavy tasks while benefiting from the GNU Radio scheduler's automated data movement between CPU and GPU.