Skip to content

A header-only library for implementing dynamically sized conceptual tensor data structure in C++20

License

Notifications You must be signed in to change notification settings

pooriayousefi/dynasor

Repository files navigation

Dynasor - Dynamic Tensor Library

Build Status License: MIT C++20 CMake

A modern C++20 header-only library for implementing dynamically sized conceptual tensor data structures. Dynasor provides efficient, type-safe multi-dimensional arrays with support for parallel execution policies and modern C++ concepts.

🚀 Features

  • Header-Only: Single header include for easy integration
  • C++20 Concepts: Type-safe interfaces with concept constraints
  • Parallel Execution: Support for STL execution policies (sequential, parallel)
  • Dynamic Dimensions: Runtime-configurable tensor dimensions
  • Type Safety: Template constraints for arithmetic types
  • Memory Efficient: Contiguous memory layout for optimal cache performance
  • Cross-Platform: Works on Linux (g++), macOS (clang++), and Windows (MSVC)

🎯 Quick Start

#include "dynasor.h"
#include <initializer_list>
#include <execution>

int main() {
    // Create a 2x3x4 tensor with sequential execution
    std::initializer_list<size_t> dimensions{2, 3, 4};
    dynasor<float> tensor(std::execution::seq, 
                         dimensions.begin(), 
                         dimensions.end());
    
    // Create tensor with parallel uniform random initialization
    std::initializer_list<size_t> dims{3, 3};
    auto random_tensor = dynasor<int>::uniform_random(
        std::execution::par,
        dims.begin(), dims.end(),
        42,    // seed
        -10,   // min value
        10     // max value
    );
    
    return 0;
}

📚 API Reference

Core Constructor

template<execution_policy ExPo, integral_value_iterator DimIter>
dynasor(ExPo execution_policy, DimIter dim_begin, DimIter dim_end, T init_value = T{})

Creates a tensor with specified dimensions and optional initialization value.

Random Initialization

template<execution_policy ExPo, integral_value_iterator DimIter>
static dynasor uniform_random(ExPo execution_policy, 
                             DimIter dim_begin, DimIter dim_end,
                             unsigned seed, T min_val, T max_val)

Creates a tensor filled with uniformly distributed random values.

Data Initialization

template<execution_policy ExPo, integral_value_iterator DimIter, arithmetic_value_iterator ValIter>
dynasor(ExPo execution_policy, 
        DimIter dim_begin, DimIter dim_end,
        ValIter val_begin, ValIter val_end)

Creates a tensor from existing data with specified dimensions.

🔧 Concepts and Type Safety

Dynasor uses C++20 concepts for compile-time type validation:

  • arithmetic: Accepts integral or floating-point types
  • integral_value_iterator: Iterators over integral types
  • floating_point_value_iterator: Iterators over floating-point types
  • arithmetic_value_iterator: Iterators over arithmetic types
  • execution_policy: STL execution policy concepts

🚀 Performance Features

  • Parallel Execution: Leverage std::execution::par for multi-core operations
  • Contiguous Memory: Single vector storage for optimal cache locality
  • SIMD-Friendly: Memory layout optimized for vectorization
  • Compile-Time Optimization: Template specialization and concept constraints

🏗️ Building from Source

# Clone repository
git clone https://github.com/pooriayousefi/dynasor.git
cd dynasor

# Build with CMake
cmake --preset=default
cmake --build build/default

# Run example
./build/default/dynasor_example

📊 Use Cases

  • Machine Learning: Neural network weight tensors and activations
  • Scientific Computing: Multi-dimensional data analysis
  • Image Processing: Multi-channel image representations
  • Numerical Simulations: Grid-based computations
  • Computer Graphics: Volumetric data and textures

🔧 Requirements

  • C++20 compatible compiler (GCC 11+, Clang 13+, MSVC 2022+)
  • CMake 3.20 or later
  • Standard library with execution policy support

📄 License

This project is licensed under the MIT License - see the LICENSE.txt file for details.


Author: Pooria Yousefi
Repository: https://github.com/pooriayousefi/dynasor

About

A header-only library for implementing dynamically sized conceptual tensor data structure in C++20

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published