tenflowers-core 0.1.1

Core tensor operations and execution engine for TenfloweRS
Documentation
//! Binary Operations - Modular Architecture
//!
//! This module has been refactored into a modular architecture for better maintainability,
//! performance, and code organization. The functionality is now distributed across
//! specialized submodules while maintaining full backward compatibility.
//!
//! ## Refactoring Summary (Phase 13 - September 27, 2025)
//!
//! **Original**: 1972-line monolithic file with all binary operations in one module
//! **Refactored**: Modular architecture with specialized modules:
//!
//! - **core.rs**: BinaryOp trait, OpComplexity, BinaryOpRegistry, and analytics (144 lines)
//! - **operations.rs**: Concrete operation structs (AddOp, SubOp, MulOp, etc.) (268 lines)
//! - **implementation.rs**: Generic binary_op function and broadcasting logic (180 lines)
//! - **simd.rs**: SIMD-accelerated implementations for f32 operations (154 lines)
//! - **fused.rs**: Fused operations for maximum performance (104 lines)
//! - **convenience.rs**: Public API convenience functions and ultra-performance variants (264 lines)
//! - **tests.rs**: Comprehensive test suite for all operations (186 lines)
//! - **mod.rs**: Module organization and re-exports (77 lines)
//!
//! **Total**: 1377 lines across 8 specialized modules
//! **Reduction**: 595 lines (30% reduction) while adding comprehensive documentation
//!
//! ## Benefits
//!
//! 1. **Improved Maintainability**: Clear separation of concerns makes code easier to maintain
//! 2. **Enhanced Performance**: Specialized SIMD and fused operation modules
//! 3. **Better Testing**: Isolated test modules enable targeted testing strategies
//! 4. **Code Reusability**: Modular components can be reused across the framework
//! 5. **Future Extensibility**: Easy to add new operations and optimizations
//!
//! ## Backward Compatibility
//!
//! All public APIs remain unchanged. Existing code will continue to work without modifications.
//!
//! ## Architecture
//!
//! The module is organized into specialized submodules:
//!
//! - **core**: Fundamental traits and registry for binary operations
//! - **operations**: Concrete operation implementations (AddOp, SubOp, MulOp, etc.)
//! - **implementation**: Generic binary operation implementation with broadcasting
//! - **simd**: SIMD-accelerated implementations for supported platforms
//! - **fused**: Fused operations for maximum performance
//! - **convenience**: High-level convenience functions (add, sub, mul, div, etc.)
//! - **tests**: Comprehensive test suite
//!
//! ## Usage
//!
//! ```rust
//! use tenflowers_core::ops::binary::{add, mul, sub, div};
//! use tenflowers_core::Tensor;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let a = Tensor::from_vec(vec![1.0, 2.0, 3.0], &[3])?;
//! let b = Tensor::from_vec(vec![4.0, 5.0, 6.0], &[3])?;
//!
//! let sum = add(&a, &b)?;      // Element-wise addition
//! let product = mul(&a, &b)?;  // Element-wise multiplication
//! # Ok(())
//! # }
//! ```
//!
//! ## Broadcasting
//!
//! All operations support NumPy-style broadcasting:
//!
//! ```rust
//! use tenflowers_core::ops::binary::add;
//! use tenflowers_core::Tensor;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let matrix = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2])?;
//! let scalar = Tensor::from_vec(vec![10.0], &[1])?;
//! let result = add(&matrix, &scalar)?; // Broadcasts scalar to matrix shape
//! # Ok(())
//! # }
//! ```
//!
//! ## Performance Features
//!
//! - **SIMD Acceleration**: Automatic SIMD optimization for f32 operations on supported platforms
//! - **Parallel Processing**: Multi-threaded execution for large tensors
//! - **GPU Support**: Hardware acceleration where available
//! - **Performance Monitoring**: Built-in analytics and metrics collection
//! - **Fused Operations**: Combined operations to reduce memory bandwidth

pub mod convenience;
pub mod core;
pub mod fused;
pub mod implementation;
pub mod operations;
pub mod simd;
pub mod tests;

// Re-export the public API for convenient access
pub use convenience::{
    add, clamp, div, get_binary_op_performance_report, max, min, mul, pow,
    reset_binary_op_counters, scalar_add, sub, ultra_add, ultra_div, ultra_mul, ultra_sub,
};
pub use core::{get_binary_op_registry, BinaryOp, BinaryOpAnalytics, OpComplexity};
pub use fused::fused_ops::{FusedAddMulOp, FusedMulAddOp};
pub use implementation::binary_op;
pub use operations::{AddOp, DivOp, MaxOp, MinOp, MulOp, PReLUOp, PowOp, SubOp};

// Re-export SIMD functions when available
#[cfg(feature = "simd")]
pub use simd::simd_f32_ops;

/// Broadcast an array to a target shape with memory optimization
use crate::{Result, Shape, TensorError};
use scirs2_core::ndarray::{ArrayD, IxDyn};

#[allow(dead_code)]
pub fn broadcast_array<T: Clone>(array: &ArrayD<T>, target_shape: &Shape) -> Result<ArrayD<T>> {
    let target_dims = IxDyn(target_shape.dims());

    // If shapes match, just clone
    if array.shape() == target_shape.dims() {
        return Ok(array.clone());
    }

    // Use ndarray's broadcast functionality
    array
        .broadcast(target_dims)
        .ok_or_else(|| {
            TensorError::invalid_argument(format!(
                "Cannot broadcast from {:?} to {:?}",
                array.shape(),
                target_shape
            ))
        })
        .map(|view| view.to_owned())
}