Cyclops Tensor Framework (CTF)

repository·master·Indexed 42 days ago

https://github.com/cyclops-community/ctf

A parallel, distributed-memory numerical library for multidimensional arrays (tensors) supporting C++ and Python. Designed for high-performance scientific computing using MPI, it provides a domain-specific language for tensor contractions, index-based notation, and sparse matrix primitives. It includes support for ScaLAPACK integration, SVD, and einsum, making it suitable for computational chemistry, physics, and algebraic graph algorithms.

Tokens
16.8K
Snippets
61
Records
83
Agent score
53%

What's inside CTF

  1. Use CTF for Numerical Methods and Tensor Contractions

    master

    CTF provides a domain-specific language optimized for computational chemistry and physics involving higher-order tensors (order 3+). It is designed for workloads such as coupled-cluster computations, lattice QCD, and quantum informatics.

    Key application examples include:

    • CCSD: Coupled-cluster computations (examples/ccsd.cxx).
    • Sparse MP3: Sparse tensor operations (examples/sparse_mp3.cxx).
    • Aquarius: A specialized application built on CTF.
  2. Use Tensor indexing and contractions in C++

    master

    CTF uses operator overloading and string-based indexing to build expression trees for parallel execution.

    Indexing Semantics

    • Contraction (Summation): If an index appears in the operands but NOT in the output, it is contracted (summed over). Example: y["i"] = R["ij"] * b["j"] (index j is contracted).
    • Hadamard Product: If an index appears in both operands and the output, it is an element-wise operation. Example: x["i"] = d["i"] * y["i"].
    • General Form: CTF can execute any operation of the form C["..."] += A["..."] * B["..."] provided the string lengths match the tensor orders.
    // Matrix-vector multiplication with contraction on 'j'
    y["i"] = R["ij"] * b["j"];
    
    // Hadamard product
    x["i"] = d["i"] * y["i"];
  3. Prototype Parallel Numerical Algorithms

    master

    CTF's high-level abstractions for vectors, matrices, and higher-order tensors make it suitable for prototyping various numerical algorithms:

    • Algebraic Multigrid: Requires sparse matrix multiplication (examples/algebraic_multigrid.cxx).
    • Recursive Algorithms: Higher-order tensors can express algorithms like parallel scans (examples/scan.cxx) and FFT (examples/fft.cxx).
    • Distributed-Memory Coordination: CTF provides interface hook-ups for ScaLAPACK, making it easy to coordinate with distributed-memory solvers.
  4. Implement Algebraic Graph Algorithms with Sparse Primitives

    master

    CTF provides sparse matrix primitives that allow for the development of parallel graph algorithms. A key feature is that matrices and vectors can be defined on user-defined semirings or monoids. Multiplication of sparse matrices with sparse vectors or other sparse matrices is automatically parallelized.

    Example implementations in the repository:

    • Bellman-Ford (Single Source Shortest Path): examples/sssp.cxx
    • Betweenness Centrality: examples/btwn_central.cxx
  5. Build the CTF Python documentation

    master

    To build the documentation for the CTF Python library, ensure the library is installed system-wide or that its paths are correctly configured in your environment. You must install the necessary dependencies first, then use sphinx-apidoc to generate the API documentation from the source, and finally use make to compile the HTML files.

    Follow these steps in the documentation directory:

    1. Install requirements using pip.
    2. Generate API documentation using sphinx-apidoc pointing to the ../ctf source directory.
    3. Build the HTML output using make html.
    pip install -r requirements.txt
    sphinx-apidoc -f -o . ../ctf && make html
  6. Test the CTF libraries

    master

    Use the following commands to run test suites for C++ and Python:

    C++ Testing

    • Sequential tests: make test
    • 2-processor tests: make test2
    • N-processor tests: make testN (replace N with the desired number of processors).

    Python Testing

    • Sequential tests: make python_test
    • N-processor tests: make python_testN (replace N with the desired number of processors).
    # Example: Run Python tests with 4 processors
    make python_test4
  7. Configure the CTF build

    master

    The ./configure script is responsible for obtaining dependencies and setting the build type. It generates config.mk (C++), setup.py (Python), and a how-did-i-configure file.

    To see all available configuration options, run ./configure --help.

    ./configure --help
  8. Build and install C++ and Python libraries

    master

    After configuring, use make to build the libraries. Parallel make is supported.

    C++ Libraries

    • Build static library: make libctf
    • Build shared library: make shared
    • Install C++ libraries: make install (requires sudo if installing to system directories like /usr/local/).

    Python Library

    • Build Python library: make python
    • Install Python library via pip: make python_install (requires sudo if not in a virtual environment).

    Uninstallation

    • Uninstall C++: make uninstall
    • Uninstall Python: make python_uninstall
    # Example: Build and install Python library
    make python
    make python_install
  9. Install the Cyclops Tensor Framework (CTF)

    master

    CTF follows the standard installation convention. You must first run the configure script to check dependencies and generate build files (config.mk for C++ and setup.py for Python), then build and install.

    Note on MPICH Bug: If you encounter segmentation faults when running with 2 or more processors using MPICH, set the following environment variable: export MPIR_CVAR_DEVICE_COLLECTIVES=none

    ./configure
    make
    make install
  10. Use Term classes to build tensor expressions

    master

    The Term class and its derivatives, Sum_Term and Contract_Term, are the primary abstractions for building and manipulating tensor expressions in CTF. You can compose complex expressions using standard arithmetic operators (+, -, *) which return new Term objects.

    • Term: The base abstract class for all expressions.
    • Sum_Term: Represents the addition or subtraction of terms.
    • Contract_Term: Represents the multiplication (contraction) of terms.

    Expressions can be evaluated using .execute() or have their computational cost estimated using .estimate_time().

    // Example of composing a tensor expression
    // Result = (A * B) + (C * 0.5)
    CTF_int::Term* expression = (A * B) + (C * 0.5);
    
    // Execute the expression into an output tensor
    expression->execute(output_tensor);
  11. Understand the TensorOperation scheduling model

    master

    CTF uses a Directed Acyclic Graph (DAG) model to schedule tensor operations for parallel execution.

    DAG Construction:

    • The scheduler maintains a latest_write map that tracks the most recent TensorOperation that writes to a specific tensor pointer.
    • When a new operation is added, the scheduler checks this map for all required input tensors.
    • If a dependency exists in the map, the new operation is added as a successor to that existing operation.
    • If no entry exists, the dependency is considered satisfied.

    Execution Logic:

    • Each operation tracks dependency_count (total dependencies) and dependency_left (remaining dependencies).
    • When an operation completes, it calls schedule_op_successors(), which decrements the dependency_left of all its successors.
    • Once an operation's dependency_left reaches zero, it is moved to the ready_tasks queue and becomes eligible for execution.
  12. Configure dependencies for ScaLAPACK and HPTT

    master

    CTF has strict dependencies on MPI and BLAS. You can automate the building of supplemental packages during the ./configure step:

    • ScaLAPACK: Use --build-scalapack (requires cmake). Alternatively, provide a manual library path.
    • HPTT (for faster transposition): Use --build-hptt.