Skip to main content

GPU Accelerated masked arrays with automatic handling of CPU and GPU arrays.

Project description

XuPy

logo

XuPy is a comprehensive Python package that provides GPU-accelerated masked arrays and NumPy-compatible functionality using CuPy. It automatically handles GPU/CPU fallback and offers an intuitive interface for scientific computing with masked data.

Features

  • GPU Acceleration: Automatic GPU detection with CuPy fallback to NumPy
  • Masked Arrays: Full support for masked arrays with GPU acceleration
  • Statistical Functions: Comprehensive statistical operations (mean, std, var, min, max, etc.)
  • Array Manipulation: Reshape, transpose, squeeze, expand_dims, and more
  • Mathematical Functions: Trigonometric, exponential, logarithmic, and rounding functions
  • Random Generation: Various random number generators (normal, uniform, etc.)
  • Universal Functions: Support for applying any CuPy/NumPy ufunc with mask preservation
  • Performance: Optimized for large-scale data processing on GPU

Installation

pip install xupy

Quick Start

import xupy as xp

# Create arrays with automatic GPU detection
a = xp.random.normal(0, 1, (1000, 1000))
b = xp.random.normal(0, 1, (1000, 1000))

# Create masks
mask = xp.random.random((1000, 1000)) > 0.1

# Create masked arrays
am = xp.ma.masked_array(a, mask)
bm = xp.ma.masked_array(b, mask)

# Perform operations (masks are automatically handled)
result = am + bm
mean_val = am.mean()
std_val = am.std()

Performance Benefits

XuPy automatically detects GPU availability and provides significant speedup for large arrays:

  • Small arrays (< 1000 elements): CPU (NumPy) may be faster due to GPU overhead
  • Medium arrays (1000-10000 elements): GPU provides 2-5x speedup
  • Large arrays (> 10000 elements): GPU provides 5-20x speedup depending on operation complexity

GPU Requirements

  • CUDA-compatible GPU with compute capability 3.0+
  • CuPy package installed (pip install cupy-cuda12x for CUDA 12.x)
  • Automatic fallback to NumPy if GPU is unavailable

API Compatibility

XuPy maintains high compatibility with NumPy's masked array interface while leveraging CuPy's optimized operations:

  • All standard properties (shape, dtype, size, ndim, T)
  • Comprehensive arithmetic operations with mask propagation
  • Memory-optimized statistical methods (mean, std, var, min, max) using CuPy's native operations
  • Array manipulation methods (reshape, transpose, squeeze)
  • Universal function support through apply_ufunc
  • Conversion to NumPy masked arrays via asmarray()
  • GPU memory management through MemoryContext

GPU Memory Management

XuPy includes an advanced MemoryContext class for efficient GPU memory management:

import xupy as xp

# Basic usage with automatic cleanup
with xp.MemoryContext() as ctx:
    # GPU operations
    data = xp.random.normal(0, 1, (10000, 10000))
    result = data.mean()
# Memory automatically cleaned up on exit

# Advanced features
with xp.MemoryContext(memory_threshold=0.8, auto_cleanup=True) as ctx:
    # Monitor memory usage
    mem_info = ctx.get_memory_info()
    print(f"GPU Memory: {mem_info['used'] / (1024**3):.2f} GB")
    
    # Aggressive cleanup when needed
    if ctx.check_memory_pressure():
        ctx.aggressive_cleanup()
    
    # Emergency cleanup for critical situations
    ctx.emergency_cleanup()

MemoryContext Features

  • Automatic Cleanup: Memory freed automatically when exiting context
  • Memory Monitoring: Real-time tracking of GPU memory usage
  • Pressure Detection: Automatic cleanup when memory usage is high
  • Aggressive Cleanup: Force garbage collection and cache clearing
  • Emergency Cleanup: Nuclear option for out-of-memory situations
  • Object Tracking: Track GPU objects for proper cleanup
  • Memory History: Keep history of memory usage over time

Documentation

For detailed documentation, including comprehensive API reference and advanced usage examples, see docs.md.

License

See LICENSE.

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

xupy-1.5.1.tar.gz (46.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xupy-1.5.1-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file xupy-1.5.1.tar.gz.

File metadata

  • Download URL: xupy-1.5.1.tar.gz
  • Upload date:
  • Size: 46.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for xupy-1.5.1.tar.gz
Algorithm Hash digest
SHA256 f7fd95881bf958195cec2947f828607e6bb9c554b1dc9ebfca5530baba5436e5
MD5 84c6f6d231befd673a236bbd9b89a997
BLAKE2b-256 6dad3e4ed098f1ed26807f16e5fe632c5f9b586e7efac8623735fc1ab089cb89

See more details on using hashes here.

File details

Details for the file xupy-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: xupy-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 36.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for xupy-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5a3893a2b2c9f991b66854feed9a79f47be8cbd80eadac6bcdc78c3b92d0d67b
MD5 7d3be7fe9bc8d2947a4bca60e46ab8d9
BLAKE2b-256 c0cfcc70b6015821bed56149af784b02768faf39815f8a68a0dccb5d0e1f6ef0

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