Skip to main content

CUDA accelerated correlation and sum reduction functions

Project description

CUDA Kernels

A Python package providing CUDA-accelerated functions for autocorrelation and sum reduction operations, with automatic CPU fallback when CUDA is not available.

Installation

From PyPI (Recommended)

pip install cuda-kernels

The package is distributed as a source distribution and compiled on your machine at install time. If the CUDA toolkit (nvcc) is detected, the GPU kernels are built and used automatically; otherwise the package installs CPU-only and runs the optimized NumPy fallback. GPU acceleration therefore requires nvcc (CUDA Toolkit) and a C/C++ host compiler to be present when you run pip install.

From GitHub

pip install git+https://github.com/AstuteFern/cuda-toolkit.git

From Source

git clone https://github.com/AstuteFern/cuda-toolkit.git
cd cuda-toolkit
pip install .

Requirements

  • Python 3.6+
  • NumPy

Optional (for CUDA acceleration)

  • NVIDIA GPU with CUDA support
  • CUDA Toolkit (version 11.0+)

Note: The package works on any system. If CUDA is not available, it automatically uses optimized CPU implementations.

Quick Start

import numpy as np
from cuda_kernels import autocorrelation, reduction_sum

# Create test data
data = np.random.randn(1000).astype(np.float32)

# Compute autocorrelation (automatically uses CUDA if available)
acf = autocorrelation(data, max_lag=50)
print(f"Autocorrelation shape: {acf.shape}")

# Compute sum reduction
total = reduction_sum(data)
print(f"Sum: {total}")

API Reference

autocorrelation(data, max_lag=None, force_cpu=False)

Compute autocorrelation of a time series.

Parameters:

  • data (numpy.ndarray): Input 1D array (converted to float32)
  • max_lag (int, optional): Maximum lag to compute. Default: len(data)-1
  • force_cpu (bool): Force CPU implementation. Default: False

Returns:

  • numpy.ndarray: Autocorrelation values for lags [0, max_lag)

reduction_sum(data, force_cpu=False)

Compute sum of array elements.

Parameters:

  • data (numpy.ndarray): Input 1D array (converted to float32)
  • force_cpu (bool): Force CPU implementation. Default: False

Returns:

  • float: Sum of all elements

Examples

Basic Usage

import numpy as np
from cuda_kernels import autocorrelation, reduction_sum

# Example 1: Autocorrelation
signal = np.sin(np.linspace(0, 4*np.pi, 1000)).astype(np.float32)
acf = autocorrelation(signal, max_lag=100)

# Example 2: Sum reduction
data = np.array([1, 2, 3, 4, 5], dtype=np.float32)
total = reduction_sum(data)  # Returns 15.0

Checking CUDA Status

import sys
autocorr_module = sys.modules['cuda_kernels.autocorrelation']
reduction_module = sys.modules['cuda_kernels.reduction']

print(f"CUDA available: {autocorr_module._cuda_available}")

Force CPU Mode

# Useful for testing or when you want consistent behavior
cpu_result = reduction_sum(data, force_cpu=True)

Performance

  • With CUDA: Significant speedup for large arrays (10K+ elements)
  • CPU Fallback: Optimized NumPy implementations, still efficient for most use cases
  • Automatic Detection: No configuration needed, works out of the box

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cuda_kernels-0.2.0.tar.gz (10.9 kB view details)

Uploaded Source

File details

Details for the file cuda_kernels-0.2.0.tar.gz.

File metadata

  • Download URL: cuda_kernels-0.2.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for cuda_kernels-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b1886bdf47e087b191bf589757fa0455cb92a5dffdc2ba909be39b1c898b2704
MD5 214611de9c61ed6aa8c7198237fb8fdd
BLAKE2b-256 628538c504613b28854a7391ae0e7f520abddf6507de5461be6d1cb203f71e11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page