Skip to main content

High-performance, scalable Reservoir Computing framework in C++ with Python bindings.

Project description

rclib: Reservoir Computing Library

rclib is a high-performance, scalable, and general-purpose reservoir computing framework implemented in C++ with Python bindings. It is designed to handle both small-scale networks and very large-scale (40,000+ neurons) architectures, supporting deep (stacked) and parallel reservoir configurations.

Project Goals

  • Performance: Core logic in C++17 using Eigen for linear algebra.
  • Scalability: Efficient handling of large sparse reservoirs and complex architectures.
  • Flexibility: Modular design separating Reservoirs and Readouts.
  • Ease of Use: Pythonic interface via pybind11 and scikit-learn style API.
  • Reproducibility: Deterministic results via explicit seeding of random reservoirs.

Getting Started

Prerequisites

  • C++ Compiler: GCC, Clang, or MSVC supporting C++17.
  • CMake: Version 3.15 or higher.
  • Python: Version 3.10 or higher (for Python bindings).
  • Build Tool: uv is recommended for managing the Python environment, but standard pip works too.
  • OpenMP: Required for parallelization.
    • Ubuntu/Debian: sudo apt install libomp-dev

Building from Source

  1. Clone the repository:

    git clone --recursive https://github.com/hrshtst/rclib.git
    cd rclib
    

    Note: The --recursive flag is crucial to fetch dependencies (Eigen, Catch2, pybind11) located in cpp_core/third_party. If you cloned without --recursive, run:

    git submodule update --init --recursive
    
  2. Build C++ Core and Examples:

    # Build with examples enabled (defaults: Release type, Export Compile Commands ON)
    cmake -S . -B build -DBUILD_EXAMPLES=ON
    cmake --build build --config Release -j $(nproc)
    
  3. Run a C++ Example:

    # Run the Mackey-Glass time series prediction example (if built with -DBUILD_EXAMPLES=ON)
    ./build/examples/cpp/mackey_glass
    

Using the Python Interface

This project provides Python bindings for the core C++ code, leveraging uv, scikit-build-core, and pybind11.

To enable fast incremental builds and automatic rebuilding when C++ source files change (see astral-sh/uv#13998), use the following two-step installation process:

# 1. Install build dependencies without installing the project
uv sync --no-install-project --only-group build

# 2. Install the project and remaining dependencies
uv sync

# Run the quick start example
uv run python examples/python/quick_start.py

With this configuration, any changes to the C++ source code in cpp_core will automatically trigger a rebuild of the Python extension module upon the next import, ensuring your Python environment always uses the latest C++ logic without manual recompilation.

Integrating rclib_core into Your C++ Project (CMake)

If you wish to use rclib_core as a static library in your own C++ project, the recommended approach is to add it as a Git submodule.

  1. Add rclib as a Git Submodule: Navigate to your project's root directory and add rclib as a submodule:

    git submodule add https://github.com/hrshtst/rclib.git third_party/rclib
    git submodule update --init --recursive third_party/rclib
    

    (Adjust third_party/rclib to your desired path.)

  2. Integrate with Your CMakeLists.txt: In your project's CMakeLists.txt file, add rclib as a subdirectory and link against its rclib_core target. Ensure you propagate relevant build options like OpenMP if needed.

    # Add rclib as a subdirectory
    add_subdirectory(third_party/rclib)
    
    # Example of how to link rclib_core to your target executable or library
    add_executable(my_rc_app main.cpp)
    target_link_libraries(my_rc_app PRIVATE rclib_core)
    
    # Note: rclib_core internally handles its Eigen dependency.
    # If your project directly uses Eigen, ensure it's properly configured in your CMakeLists.txt.
    
  3. Configure Parallelization (Optional): If your project also uses OpenMP or needs to control rclib's parallelization, you can set the RCLIB_USE_OPENMP and RCLIB_ENABLE_EIGEN_PARALLELIZATION CMake options before calling add_subdirectory(third_party/rclib).

    set(RCLIB_USE_OPENMP ON CACHE BOOL "Enable OpenMP support in rclib_core")
    set(RCLIB_ENABLE_EIGEN_PARALLELIZATION OFF CACHE BOOL "Enable Eigen's internal parallelization in rclib_core")
    add_subdirectory(third_party/rclib)
    # ... rest of your project's CMakeLists.txt
    

Running Tests

Using Nox (Recommended)

nox automates environment setup and execution for both Python and C++.

# Run all default sessions (Lint, Type Check, Python Tests)
uv run nox

# Run Python tests only
uv run nox -s tests

# Run C++ tests only
uv run nox -s tests_cpp

Manual Execution (Step-by-Step)

If you prefer to run tests manually without nox:

C++ Tests

# 1. Configure and build
cmake -S . -B build -DBUILD_TESTING=ON
cmake --build build --config Release -j $(nproc)

# 2. Run tests
ctest --test-dir build --output-on-failure

Python Tests

# 1. Build and install the C++ extension in the current environment
cmake -S . -B build
cmake --build build --config Release -j $(nproc) --target _rclib

# 2. Run pytest
uv run pytest

Documentation

The project documentation is built using mkdocs and the Material theme. It includes theoretical background, user guides, and API references.

Using Nox (Recommended)

# Build the documentation
uv run nox -s docs

Manual Execution

If you prefer to run mkdocs directly:

# 1. Install documentation dependencies
uv sync --group docs

# 2. Build the documentation
uv run mkdocs build

# 3. Serve the documentation locally with live-reloading
uv run mkdocs serve

The documentation is automatically deployed to https://hrshtst.github.io/rclib/ on every push to the main branch.

Parallelization Configuration

rclib provides flexible options to control parallelization strategies, allowing you to optimize for your specific workload and hardware. This is managed via CMake options.

Options

Option Default Description
RCLIB_USE_OPENMP ON Enables OpenMP support. Required for any multi-threading.
RCLIB_ENABLE_EIGEN_PARALLELIZATION OFF Enables Eigen's internal parallelization (using OpenMP).

Recommended Configurations

1. User-Level Parallelism (Default)

Best for: Training multiple reservoirs, batch processing, or typical workloads.

  • Configuration:
    cmake -S . -B build -DRCLIB_USE_OPENMP=ON -DRCLIB_ENABLE_EIGEN_PARALLELIZATION=OFF
    
  • How it works: rclib uses OpenMP #pragma omp parallel for loops to parallelize high-level operations (e.g., updating state for multiple reservoirs in a parallel architecture, or processing batches). Eigen is forced to run in single-threaded mode to avoid oversubscription (too many threads competing for resources).

2. Eigen-Level Parallelism

Best for: Very large single networks or dense matrix operations where linear algebra is the bottleneck.

  • Configuration:
    cmake -S . -B build -DRCLIB_USE_OPENMP=ON -DRCLIB_ENABLE_EIGEN_PARALLELIZATION=ON
    
  • How it works: rclib disables its own OpenMP loops. Instead, it lets Eigen use the OpenMP thread pool to parallelize internal matrix operations (like large dense matrix multiplications). This is useful when the reservoir state size is huge.

3. Serial (Single-Threaded)

Best for: Debugging or systems without OpenMP.

  • Configuration:
    cmake -S . -B build -DRCLIB_USE_OPENMP=OFF
    

Performance Benchmarking

The benchmarks/ directory contains scripts to evaluate performance across different thread counts and parallelization modes.

  1. Run the Benchmark Suite:

    ./benchmarks/benchmark_parallel_comparison.sh
    

    This script compiles the project in different modes (Serial, User OMP, Eigen OMP) and runs the performance_benchmark executable multiple times.

  2. Visualize Results:

    uv run python benchmarks/plot_parallel_comparison.py
    

    This generates plots comparing execution time and MSE for different methods and configurations.

Development

Code Quality Tools

The project uses several tools to ensure code quality, all of which are integrated into pre-commit and nox:

  • Ruff: For Python linting and formatting.
  • Basedpyright: For static type checking.
  • clang-format: For C++ formatting (LLVM style).
  • shellcheck: For shell script linting.
  • cmake-format / cmake-lint: For CMake formatting and linting.
  • pre-commit: To enforce checks before committing.

Automation with Nox

nox is used to automate various development tasks:

  • uv run nox -s lint: Run linters.
  • uv run nox -s type_check: Run type checkers.
  • uv run nox -s tests: Run Python tests.
  • uv run nox -s tests_cpp: Run C++ tests.
  • uv run nox -s docs: Build documentation.

Setting up pre-commit

To ensure all code follows the project's style and quality standards, it is recommended to set up pre-commit:

uv run pre-commit install

AI Assistance & Development Workflow

This project is developed with the assistance of an AI coding agent using the Gemini CLI tool. The AI is also used to generate commit messages and parts of the documentation, including API and theoretical reference sections.

Workflow:

  1. Context & Theory (Human): The maintainer, Hiroshi Atsuta, establishes the project roadmap in GEMINI.md and writes the theoretical background implemented as documentation in docs/theory/.
  2. Implementation (AI): The AI assistant uses these documents and the constraints defined in GEMINI.md to implement code scaffolding, core logic, tests, and documentation.
  3. Review & Revision (Human): The maintainer reviews, tests, and revises the generated code to ensure quality and correctness. This iterative cycle ensures high standards while leveraging AI efficiency.

Responsibility: All responsibilities for the code hosted in this repository lie with the maintainer. The AI serves strictly as an implementation assistant; final architectural decisions and code quality are human-led.

Feedback: If you identify problems, or find code that appears to be unoriginal or rights-protected, please notify the maintainer immediately by filing an issue.

Contributor Policy: External contributors are welcome to use AI tools for assistance, provided they adhere to the same standard of review and responsibility. If you use AI to generate code for a Pull Request, please disclose it in the PR description and ensure you have thoroughly reviewed and tested the code.

Acknowledgments

IPA Logo     MITOU Target Logo

This project is supported by the MITOU Target Program (Reservoir Computing field) of the Information-technology Promotion Agency, Japan (IPA). Details of the supported project can be found in the official summary (Japanese).

License

This project is licensed under the Apache License 2.0 - see the 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

rclib-0.0.4.tar.gz (4.7 MB view details)

Uploaded Source

Built Distributions

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

rclib-0.0.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

rclib-0.0.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

rclib-0.0.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file rclib-0.0.4.tar.gz.

File metadata

  • Download URL: rclib-0.0.4.tar.gz
  • Upload date:
  • Size: 4.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rclib-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f026b7cb449f3def658e1a9ef075f19d685aace0042faf17a92e6b72fcf31622
MD5 909000ade3d47d2c9a78cbd61f645477
BLAKE2b-256 1d138f770df12a4efc5c8cb79bd03f730fff8aa372f5aa22fe7a1545335d2ddb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rclib-0.0.4.tar.gz:

Publisher: release.yml on hrshtst/rclib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rclib-0.0.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for rclib-0.0.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bae3e874649fb9e63f64d06db5621b4638451901ac8964491e26c58c7e921708
MD5 567c48ad1298f74d108ed431e0f9769e
BLAKE2b-256 2ac8b237907432b86180e3e01e00e6edbe6061b933c78b3c9cb837d867dfb94c

See more details on using hashes here.

Provenance

The following attestation bundles were made for rclib-0.0.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on hrshtst/rclib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rclib-0.0.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for rclib-0.0.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5ca75679cafeb43d3576b804992fa79002d565da3aa72a1243d94546f85bd7f3
MD5 a87e6372e0f3aec54210c5996386c252
BLAKE2b-256 44a4284d1b3b9d64fd38eaadb3965f28a650e4389f86014422ce5c5dee29a589

See more details on using hashes here.

Provenance

The following attestation bundles were made for rclib-0.0.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on hrshtst/rclib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rclib-0.0.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for rclib-0.0.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a2d689e9bdfed4666bb5ef19ff15d847f594761b89e71833b027a66e52656f0d
MD5 14884e18a7725753bc0cefead5006b92
BLAKE2b-256 022936978fe71926041f45ea501748e8f6824925b6020c5d089e81f6676fa970

See more details on using hashes here.

Provenance

The following attestation bundles were made for rclib-0.0.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on hrshtst/rclib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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