numexpr icon indicating copy to clipboard operation
numexpr copied to clipboard

get_optimization() is called twice

Open FrancescAlted opened this issue 11 years ago • 1 comments

From [email protected] on April 14, 2008 14:26:04

If you apply the next patch to numexpr sources:

{{{ --- numexpr/expressions.py ( revision 14 ) +++ numexpr/expressions.py (working copy) @@ -180,6 +192,7 @@

@ophelper def pow_op(a, b):

  • print "get_optimization()-->", get_optimization() if allConstantNodes([a,b]): return ConstantNode(a**b) if isinstance(b, ConstantNode):

}}}

and run the next code:

{{{ import numpy import threading import numexpr

a = numpy.arange(3)

class ThreadTest(threading.Thread): def run(self): print "result-->", numexpr.evaluate('a3', optimization="none") print "result-->", numexpr.evaluate('a3', optimization="moderate") print "result-->", numexpr.evaluate('a**3', optimization="aggressive")

test = ThreadTest() test.start()

}}}

you will get the next result:

{{{ result--> get_optimization()--> none get_optimization()--> aggressive [0 1 8] result--> get_optimization()--> moderate get_optimization()--> aggressive [0 1 8] result--> get_optimization()--> aggressive get_optimization()--> aggressive [0 1 8] }}}

There are two problems here:

  1. I don't know why get_optimization() is called twice
  2. It seems to me that the optimization flag is used in 'aggressive' mode always.

Please notice that this is probably not related with threading, but the example includes threads so as to see if the contexts work as they should.

Original issue: http://code.google.com/p/numexpr/issues/detail?id=3

FrancescAlted avatar Jan 22 '14 10:01 FrancescAlted

From [email protected] on May 02, 2011 08:14:12

FWIW, I stumbled upon a different symptom for the same problem: all "op" functions are also called twice. And the cause is getExprNames which compiles & evaluates the string (and even builds an AST). Then that expression tree & ast are just discarded, instead of being reused and type-annotated in a second pass.

This only has an impact on the first time evaluate is called but it's quite wasteful for one-time expressions on moderately small arrays.

FrancescAlted avatar Jan 22 '14 10:01 FrancescAlted

Closing as stale due to inactivity. Feel free to reopen.

AbelHutten avatar Feb 16 '24 08:02 AbelHutten