CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyccel

Python-to-Fortran/C transpiler for scientific high-performance computing that automatically converts Python code into optimized low-level code.

Pending
Overview
Eval results
Files

embedded-acceleration.mddocs/

Embedded Acceleration

Core functions for accelerating Python code from within Python scripts, enabling interactive transpilation and immediate performance improvements without external tooling.

Capabilities

Main Acceleration Function

Accelerates Python functions, classes, or modules using Pyccel in embedded mode, generating optimized code in the specified target language.

from pyccel import epyccel

def epyccel(
    function_class_or_module,
    *,
    language='fortran',
    compiler_family=None,
    compiler_config=None,
    flags=None,
    wrapper_flags=None,
    debug=None,
    include=(),
    libdir=(),
    libs=(),
    folder=None,
    mpi=False,
    openmp=False,
    verbose=0,
    time_execution=False,
    developer_mode=False,
    conda_warnings='basic',
    context_dict=None,
    comm=None,
    root=0,
    bcast=True
):
    """
    Accelerate Python function or module using Pyccel in embedded mode.

    Parameters:
    - function_class_or_module: function | class | module | str
        Python function, class, module, or string code to accelerate
    - language: {'fortran', 'c', 'python'}
        Target language for generated code (default: 'fortran')
    - compiler_family: {'GNU', 'intel', 'PGI', 'nvidia', 'LLVM'}
        Compiler family (default: 'GNU')
    - compiler_config: pathlib.Path | str
        Path to JSON compiler configuration file
    - flags: iterable of str
        Additional compiler flags
    - wrapper_flags: iterable of str
        Compiler flags for wrapper code
    - debug: bool
        Compile in debug mode
    - include: tuple
        Additional include directories
    - libdir: tuple
        Additional library directories
    - libs: tuple
        Additional libraries to link
    - folder: str
        Output folder for compiled code
    - mpi: bool
        Enable MPI parallel execution (default: False)
    - openmp: bool
        Enable OpenMP parallel execution (default: False)
    - verbose: int
        Verbosity level (default: 0)
    - time_execution: bool
        Time Pyccel's internal stages
    - developer_mode: bool
        Enable developer error mode
    - conda_warnings: {'off', 'basic', 'verbose'}
        Conda warning level
    - context_dict: dict[str, obj]
        Additional context objects for translated code
    - comm: mpi4py.MPI.Comm
        MPI communicator (for parallel mode)
    - root: int
        MPI root process rank (default: 0)
    - bcast: bool
        Broadcast results to all processes (default: True)

    Returns:
    Accelerated function, class, or module
    """

Usage examples:

from pyccel import epyccel

# Accelerate a simple function
def add_numbers(a, b):
    return a + b

add_fast = epyccel(add_numbers, language='fortran')
result = add_fast(5.0, 3.0)

# Accelerate with compiler options
multiply_fast = epyccel(
    lambda x, y: x * y,
    language='c',
    compiler_family='GNU',
    debug=True,
    verbose=1
)

# Accelerate with MPI support
def parallel_compute(data):
    return sum(data)

parallel_fast = epyccel(parallel_compute, mpi=True, openmp=True)

SymPy Expression Acceleration

Converts SymPy mathematical expressions into Pyccel-accelerated functions for fast numerical evaluation.

from pyccel import lambdify

def lambdify(
    expr,
    args,
    *,
    result_type=None,
    use_out=False,
    **kwargs
):
    """
    Convert a SymPy expression into a Pyccel-accelerated function.

    Parameters:
    - expr: sympy.Expr
        SymPy expression to convert
    - args: dict[sympy.Symbol, str]
        Dictionary mapping symbols to type annotations
    - result_type: str
        Type annotation for function result
    - use_out: bool
        Modify 'out' parameter instead of returning (default: False)
    - **kwargs: dict
        Additional arguments passed to epyccel

    Returns:
    Pyccel-accelerated function for expression evaluation
    """

Usage examples:

import sympy as sp
from pyccel import lambdify

# Create symbolic variables
x, y = sp.symbols('x y')

# Define symbolic expression
expr = x**2 + 2*x*y + y**2

# Convert to accelerated function
args = {x: 'float', y: 'float'}
fast_func = lambdify(expr, args, result_type='float')

# Use the accelerated function
result = fast_func(3.0, 4.0)  # (3^2 + 2*3*4 + 4^2) = 49.0

# Array operations with SymPy
import numpy as np
arr_expr = sp.sin(x) + sp.cos(y)
array_args = {x: 'float[:]', y: 'float[:]'}
array_func = lambdify(arr_expr, array_args, result_type='float[:]')

x_vals = np.linspace(0, np.pi, 100)
y_vals = np.linspace(0, 2*np.pi, 100)
result = array_func(x_vals, y_vals)

Utility Functions

Internal utilities for source code extraction and context management.

from pyccel.commands.epyccel import get_source_code_and_context

def get_source_code_and_context(func_or_class):
    """
    Extract source code and context from function or class.

    Parameters:
    - func_or_class: Function | type
        Python function or class object

    Returns:
    - code: list[str]
        Source code lines
    - context_dict: dict[str, object]
        Context objects for execution

    Raises:
    - TypeError: If object is not callable
    """

Types

Type Annotations

# Common type strings for annotations
'int'           # Integer type
'float'         # Floating point type
'complex'       # Complex number type
'bool'          # Boolean type
'str'           # String type
'int[:]'        # 1D integer array
'float[:,:]'    # 2D float array
'complex[::1]'  # Contiguous 1D complex array

Exception Types

from pyccel.errors.errors import PyccelError

class PyccelError(Exception):
    """Base exception for Pyccel transpilation errors."""

Install with Tessl CLI

npx tessl i tessl/pypi-pyccel

docs

command-line.md

decorators.md

embedded-acceleration.md

index.md

type-system.md

tile.json