"""Global constants and configuration for diffct package.
This module defines core constants used throughout the diffct package,
including data types, CUDA thread block configurations, and numerical
precision parameters.
"""
import numpy as np
# ---------------------------------------------------------------------------
# Data Types and Numerical Constants
# ---------------------------------------------------------------------------
_DTYPE = np.float32
"""Default data type for numerical computations (numpy.float32)."""
def _native_spacing(spacing):
"""Stage tensor-order pitches while retaining the scalar launch type."""
if isinstance(spacing, (tuple, list)):
pitches = tuple(float(value) for value in spacing)
if any(not 1e-6 <= value <= 1e6 for value in pitches):
raise ValueError("native voxel_spacing practical range is [1e-6, 1e6] on every axis")
if max(pitches) / min(pitches) > 1e4:
raise ValueError("native voxel_spacing pitch ratio must not exceed 1e4")
return tuple(_DTYPE(value) for value in pitches)
return _DTYPE(spacing)
_INF = _DTYPE(np.inf)
"""Floating-point infinity in default data type."""
_NEG_INF = _DTYPE(-np.inf)
"""Negative floating-point infinity in default data type."""
_BIG = _DTYPE(1e30)
"""Finite stand-in for infinity in fastmath kernels, where LLVM may assume no inf occurs."""
_TINY = _DTYPE(1e-20)
"""Smallest ray direction component that the Siddon kernels divide by; ray parameters then stay below _BIG."""
_ZERO = _DTYPE(0.0)
"""Floating-point zero in default data type."""
_ONE = _DTYPE(1.0)
"""Floating-point one in default data type."""
_HALF = _DTYPE(0.5)
"""Floating-point one half in default data type."""
_EPSILON = _DTYPE(1e-6)
"""Small epsilon value for numerical comparisons to avoid division by zero."""
# ---------------------------------------------------------------------------
# CUDA Thread Block Configurations
# ---------------------------------------------------------------------------
# CUDA thread block configurations optimized for different dimensionalities
# 2D blocks: 16x16 = 256 threads per block, optimal for 2D ray-tracing kernels
# Balances occupancy with shared memory usage for parallel/fan beam projections
_TPB_2D = (16, 16)
"""CUDA threads-per-block for 2D kernels (parallel/fan beam): (16, 16) = 256 threads."""
# 3D blocks: 8x8x8 = 512 threads per block, optimal for 3D cone beam kernels
# Smaller per-dimension size accommodates higher register usage in 3D algorithms
_TPB_3D = (8, 8, 8)
"""CUDA threads-per-block for 3D kernels (cone beam): (8, 8, 8) = 512 threads."""
# ---------------------------------------------------------------------------
# CUDA JIT Decorators
# ---------------------------------------------------------------------------
# CUDA fastmath optimization: enables aggressive floating-point optimizations
# Trades numerical precision for performance in ray-tracing calculations
# Safe for CT reconstruction where slight precision loss is acceptable for speed gains
[docs]
def _FASTMATH_DECORATOR(function):
"""Create a fastmath CUDA dispatcher when a native kernel is defined."""
from numba import cuda
return cuda.jit(cache=True, fastmath=True)(function)
# Analytical reconstruction kernels (FBP / FDK voxel-driven gather) run at
# the tightest available precision - the Fourier-convention constant and
# the (sid/U)^2 weight amplify rounding errors, and the kernels are only
# called once per reconstruction so the speed penalty is negligible.
def _FDK_ACCURACY_DECORATOR(function):
"""Create an accurate CUDA dispatcher when a native kernel is defined."""
from numba import cuda
return cuda.jit(cache=True, fastmath=False)(function)