shunwang / numexpr

Automatically exported from code.google.com/p/numexpr
MIT License
0 stars 0 forks source link

get_optimization() is called twice #3

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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('a**3', optimization="none")
        print "result-->", numexpr.evaluate('a**3', 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 reported on code.google.com by fal...@gmail.com on 14 Apr 2008 at 12:26

GoogleCodeExporter commented 9 years ago
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.

Original comment by gdemen...@gmail.com on 2 May 2011 at 3:14