Python: Make algebra faster by using pool of vectors

Bug #747318 reported by Joachim Haga
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
DOLFIN
Invalid
Wishlist
Unassigned

Bug Description

In Python, the natural way to write matrix algebra involves a lot of temporary vector creation. By implementing a pool of vectors, I see a 15-20% performance improvement in a matrix-algebra heavy workload. The basic idea is to add a method to the GenericMatrix interface (Python only, probably). Then Matrix.__mul__ (and everywhere else a vector is needed) can use this method whenever they need to create a vector, and they are transparently re-used whenever they go out of scope.

    @vec_pool
    def create_vec(self, dim=1):
        vec = dolfin.Vector()
        self.resize(vec, dim)
        return vec

The implementation of the vec_pool decorator is found here: http://bazaar.launchpad.net/~jobh/cbc.block/trunk/download/head:/block/object__pool.py/object_pool.py
(code copyright me, LGPL or whatever you prefer.)

Revision history for this message
Johan Hake (johan-hake) wrote :

Looks like a nice thing to have but I am not sure it is appropriate to expand DOLFIN with this functionality. It would be more natural to extend libraries using this, such as cbc.block, for this functionality.

Also, using:
  D = A.copy()
  A += B

instead of:

  D = A+B

In this particular case it does not matter but in general it will.

Johan

Revision history for this message
Garth Wells (garth-wells) wrote :

This isn't a bug. It should be turned into a Blueprint (for discussion).

Revision history for this message
Joachim Haga (jobh) wrote : Re: [Bug 747318] Re: Python: Make algebra faster by using pool of vectors

Sure. It's a wishlist "bug". Feel free to reject if these are unwanted. I
have only very limited internet connection here, but can do the blueprint
thing later in the summer.

-j.
Den 5. apr. 2011 23.50 skrev "Garth Wells" <email address hidden>
følgende:
> This isn't a bug. It should be turned into a Blueprint (for discussion).
>
> --
> You received this bug notification because you are a direct subscriber
> of the bug.
> https://bugs.launchpad.net/bugs/747318
>
> Title:
> Python: Make algebra faster by using pool of vectors
>
> Status in DOLFIN:
> New
>
> Bug description:
> In Python, the natural way to write matrix algebra involves a lot of
> temporary vector creation. By implementing a pool of vectors, I see a
> 15-20% performance improvement in a matrix-algebra heavy workload. The
> basic idea is to add a method to the GenericMatrix interface (Python
> only, probably). Then Matrix.__mul__ (and everywhere else a vector is
> needed) can use this method whenever they need to create a vector, and
> they are transparently re-used whenever they go out of scope.
>
> @vec_pool
> def create_vec(self, dim=1):
> vec = dolfin.Vector()
> self.resize(vec, dim)
> return vec
>
> The implementation of the vec_pool decorator is found here:
http://bazaar.launchpad.net/~jobh/cbc.block/trunk/download/head:/block/object__pool.py/object_pool.py
> (code copyright me, LGPL or whatever you prefer.)
>
> To unsubscribe from this bug, go to:
> https://bugs.launchpad.net/dolfin/+bug/747318/+subscribe

Revision history for this message
Joachim Haga (jobh) wrote :

This code is already in cbc.block via code injection (aka monkey patching).
Just trying to move the good stuff upstream. I don't see how your examples
differ (except for cache behaviour) - either way a new vector must be
created, no?
Den 5. apr. 2011 23.41 skrev "Johan Hake" <email address hidden>
følgende:
> Looks like a nice thing to have but I am not sure it is appropriate to
> expand DOLFIN with this functionality. It would be more natural to
> extend libraries using this, such as cbc.block, for this functionality.
>
> Also, using:
> D = A.copy()
> A += B
>
> instead of:
>
> D = A+B
>
> In this particular case it does not matter but in general it will.
>
> Johan
>
> --
> You received this bug notification because you are a direct subscriber
> of the bug.
> https://bugs.launchpad.net/bugs/747318
>
> Title:
> Python: Make algebra faster by using pool of vectors
>
> Status in DOLFIN:
> New
>
> Bug description:
> In Python, the natural way to write matrix algebra involves a lot of
> temporary vector creation. By implementing a pool of vectors, I see a
> 15-20% performance improvement in a matrix-algebra heavy workload. The
> basic idea is to add a method to the GenericMatrix interface (Python
> only, probably). Then Matrix.__mul__ (and everywhere else a vector is
> needed) can use this method whenever they need to create a vector, and
> they are transparently re-used whenever they go out of scope.
>
> @vec_pool
> def create_vec(self, dim=1):
> vec = dolfin.Vector()
> self.resize(vec, dim)
> return vec
>
> The implementation of the vec_pool decorator is found here:
http://bazaar.launchpad.net/~jobh/cbc.block/trunk/download/head:/block/object__pool.py/object_pool.py
> (code copyright me, LGPL or whatever you prefer.)
>
> To unsubscribe from this bug, go to:
> https://bugs.launchpad.net/dolfin/+bug/747318/+subscribe

Revision history for this message
Johan Hake (johan-hake) wrote :

As I said, this example won't make a difference as it start of with a copy. But for a longer algorithm it might be worth while instantiating some tmp vectors and use imul. Imul does not generate additional vectors.

Johan

On Apr 7, 2011, at 12:07, Joachim Haga <email address hidden> wrote:

> This code is already in cbc.block via code injection (aka monkey patching)

Changed in dolfin:
importance: Undecided → Wishlist
Changed in dolfin:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.