Giter Club home page Giter Club logo

Comments (2)

johnyf avatar johnyf commented on May 29, 2024

The segmentation fault that causes the exit from the Python program (after increasing the recursion limit) is caused by the upper bound on the size of the call stack. The Python program can change the size of the call stack for new threads, and in this way avoid this error. An example of how to increase the call stack size, and handle long expressions using the module dd.autoref, is the following (using the function threading.stack_size):

"""How to configure CPython for long `dd` expressions."""
import faulthandler
import sys
import threading
import time

import dd.autoref as _bdd


N = 10**5


def runner():
    """Configure CPython, then run."""
    sys.setrecursionlimit(10**3 * N)
    faulthandler.enable()
    threading.stack_size(N * 4096)
    thread = threading.Thread(
        target=test_long_expressions)
    thread.start()
    thread.join()


def test_long_expressions():
    """Create BDDs from two long expressions."""
    bdd = _bdd.BDD()
    bdd.declare('x')
    expr = 'TRUE'
    # requires increasing the recursion limit
    u = add_long_expression(expr, bdd)
    if u != bdd.true:
        raise AssertionError(u)
    print(
        'success with first expression, '
        f'result: {u!r}')
    expr = 'x'
    # requires both:
    #
    # - increasing the recursion limit
    # - increasing the stack size
    #
    # (otherwise the CPython interpreter
    #  exits with a segmentation fault)
    u = add_long_expression(expr, bdd)
    if u != bdd.var('x'):
        raise AssertionError(u)
    print(
        'success with second expression, '
        f'result: {u!r}')


def add_long_expression(expr, bdd):
    """Add BDD to `bdd`, from `expr`."""
    k = round(N / len(expr))
    long_expr = rf'{expr} /\ ' * k + expr
    print(
        'attempting to add expression that '
        f'contains {len(long_expr)} characters')
    t_before = time.time()
    u = bdd.add_expr(long_expr)
    t_after = time.time()
    dt = t_after - t_before
    print(
        'expression converted '
        f'to BDD in {dt:.2f} seconds')
    return u


if __name__ == '__main__':
    runner()

from dd.

grgeorgiev avatar grgeorgiev commented on May 29, 2024

Thanks for the reply. This is a very elegant solution.
Meanwhile, I just divided the long expression into a set of shorter ones which actually works better in my case.
I think this can be closed now.

from dd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.