Git Product home page Git Product logo

Comments (8)

dham avatar dham commented on August 26, 2024

How do we know the LRU cache is big enough?

from ufl.

mscroggs avatar mscroggs commented on August 26, 2024

How do we know the LRU cache is big enough?

Good question. We should probably use lru_cache(maxsize=None) (which is what cache is from Python 3.9 onwards, #272)

from ufl.

dham avatar dham commented on August 26, 2024

Isn't that a memory leak?

from ufl.

mscroggs avatar mscroggs commented on August 26, 2024

I need to read up on lru_cache more. I was trying to avoid writing our own cache decorator but maybe that's unavoidable

from ufl.

dham avatar dham commented on August 26, 2024

I think there is a tough lifetimes issue going on here. UFL needs to hold onto references for the duration of a tree traversal, lest the DAG explode. On the other hand, some UFL objects will be subclassed and carry large amounts of data (Coefficient and Matrix) so it's important that stray references to those objects are not retained, otherwise quite bad memory leaks can occur (this is a current pyadjoint issue, BTW). I think that probably means that the caches have to have a life of exactly one tree traversal. I think that would mean still having something like multifunction, but delegating the implementation of the individual cases back to the objects, so that you keep extensibility.

from ufl.

mscroggs avatar mscroggs commented on August 26, 2024

I've added a hastily implemented caching function to #271.

Caching function is in https://github.com/FEniCS/ufl/blob/mscroggs/apply_restrictions/ufl/core/caching.py

Can then be called like this:

def apply_restrictions(expression: ApplyRestrictions):
"""Propagate restriction nodes to wrap differential terminals directly."""
caching.initialise_cache("apply_restrictions")
result = expression.apply_restrictions()
caching.clear_cache("apply_restrictions")
return result

from ufl.

dham avatar dham commented on August 26, 2024

That's not recursion safe. Imagine someone puts some UFL inside an ExternalOperator, and then takes a derivative. The derivative application method of the external operator might well itself call the derivative application visitor.

Another issue with this approach is that it assumes that we know in advance what the full set of DAG visitors is, because if someone wants to do something to a UFL expression that we didn't think of, they will have to go through and subclass every single UFL type to add the new method.

A further issue is that it's not at all clear how you specify traversal orders other than post-order.

I think that the right way to do this is till to have a dedicated tree visitor (i.e. a multifunction but without using the UFL type system). @wence- described how to do this well in #35. Note that this is completely compatible with having a lot of visitors built in to UFL expression as methods. That bit of this proposal isn't the issue.

from ufl.

mscroggs avatar mscroggs commented on August 26, 2024

#274 is an updated version of my PR that uses class methods with a copy of the old traversal methods

from ufl.

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.