6
from numpy.testing import *
8
from weave import blitz_tools
9
from weave.ast_tools import harvest_variables
13
from weave_test_utils import *
17
class test_ast_to_blitz_expr(ScipyTestCase):
19
def generic_test(self,expr,desired):
21
ast = parser.suite(expr)
22
ast_list = ast.tolist()
23
actual = blitz_tools.ast_to_blitz_expr(ast_list)
24
actual = remove_whitespace(actual)
25
desired = remove_whitespace(desired)
26
print_assert_equal(expr,actual,desired)
28
def check_simple_expr(self):
29
"""convert simple expr to blitz
33
expr = "a[:1:2] = b[:1+i+2:]"
34
desired = "a(blitz::Range(_beg,1-1,2))="\
35
"b(blitz::Range(_beg,1+i+2-1));"
36
self.generic_test(expr,desired)
38
def check_fdtd_expr(self):
39
""" convert fdtd equation to blitz.
40
ex[:,1:,1:] = ca_x[:,1:,1:] * ex[:,1:,1:]
41
+ cb_y_x[:,1:,1:] * (hz[:,1:,1:] - hz[:,:-1,:])
42
- cb_z_x[:,1:,1:] * (hy[:,1:,1:] - hy[:,1:,:-1]);
43
Note: This really should have "\" at the end of each line
44
to indicate continuation.
46
expr = "ex[:,1:,1:] = ca_x[:,1:,1:] * ex[:,1:,1:]" \
47
"+ cb_y_x[:,1:,1:] * (hz[:,1:,1:] - hz[:,:-1,:])"\
48
"- cb_z_x[:,1:,1:] * (hy[:,1:,1:] - hy[:,1:,:-1])"
49
desired = 'ex(_all,blitz::Range(1,_end),blitz::Range(1,_end))='\
50
' ca_x(_all,blitz::Range(1,_end),blitz::Range(1,_end))'\
51
' *ex(_all,blitz::Range(1,_end),blitz::Range(1,_end))'\
52
'+cb_y_x(_all,blitz::Range(1,_end),blitz::Range(1,_end))'\
53
'*(hz(_all,blitz::Range(1,_end),blitz::Range(1,_end))'\
54
' -hz(_all,blitz::Range(_beg,Nhz(1)-1-1),_all))'\
55
' -cb_z_x(_all,blitz::Range(1,_end),blitz::Range(1,_end))'\
56
'*(hy(_all,blitz::Range(1,_end),blitz::Range(1,_end))'\
57
'-hy(_all,blitz::Range(1,_end),blitz::Range(_beg,Nhy(2)-1-1)));'
58
self.generic_test(expr,desired)
60
class test_blitz(ScipyTestCase):
61
"""* These are long running tests...
63
I'd like to benchmark these things somehow.
65
def generic_test(self,expr,arg_dict,type,size,mod_location):
66
clean_result = array(arg_dict['result'],copy=1)
68
exec expr in globals(),arg_dict
71
desired = arg_dict['result']
72
arg_dict['result'] = clean_result
74
old_env = os.environ.get('PYTHONCOMPILED','')
75
os.environ['PYTHONCOMPILED'] = mod_location
76
blitz_tools.blitz(expr,arg_dict,{},verbose=0) #,
77
#extra_compile_args = ['-O3','-malign-double','-funroll-loops'])
78
os.environ['PYTHONCOMPILED'] = old_env
81
actual = arg_dict['result']
82
# this really should give more info...
84
# this isn't very stringent. Need to tighten this up and
85
# learn where failures are occuring.
86
assert(allclose(abs(actual.ravel()),abs(desired.ravel()),1e-4,1e-6))
93
print sum(abs(diff.ravel()),axis=0)
95
return standard,compiled
97
def generic_2d(self,expr,typ):
98
""" The complex testing is pretty lame...
100
mod_location = empty_temp_dir()
102
ast = parser.suite(expr)
103
arg_list = harvest_variables(ast.tolist())
105
all_sizes = [(10,10), (50,50), (100,100), (500,500), (1000,1000)]
106
print '\nExpression:', expr
107
for size in all_sizes:
108
result = zeros(size,typ)
111
arg_dict[arg] = random.normal(0,1,size).astype(typ)
112
# set imag part of complex values to non-zero value
113
try: arg_dict[arg].imag = arg_dict[arg].real
115
print 'Run:', size,typ
116
standard,compiled = self.generic_test(expr,arg_dict,type,size,
119
speed_up = standard/compiled
122
print "1st run(numpy.numerix,compiled,speed up): %3.4f, %3.4f, " \
123
"%3.4f" % (standard,compiled,speed_up)
124
standard,compiled = self.generic_test(expr,arg_dict,type,size,
127
speed_up = standard/compiled
130
print "2nd run(numpy.numerix,compiled,speed up): %3.4f, %3.4f, " \
131
"%3.4f" % (standard,compiled,speed_up)
132
cleanup_temp_dir(mod_location)
133
#def check_simple_2d(self):
134
# """ result = a + b"""
135
# expr = "result = a + b"
136
# self.generic_2d(expr)
137
def check_5point_avg_2d_float(self,level=10):
138
""" result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]
139
+ b[1:-1,2:] + b[1:-1,:-2]) / 5.
141
expr = "result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
142
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
143
self.generic_2d(expr,float32)
144
def check_5point_avg_2d_double(self,level=10):
145
""" result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]
146
+ b[1:-1,2:] + b[1:-1,:-2]) / 5.
148
expr = "result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
149
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
150
self.generic_2d(expr,float64)
151
def _check_5point_avg_2d_complex_float(self,level=10):
152
""" Note: THIS TEST is KNOWN TO FAIL ON GCC 3.x. It will not adversely affect 99.99 percent of weave
154
result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]
155
+ b[1:-1,2:] + b[1:-1,:-2]) / 5.
157
Note: THIS TEST is KNOWN TO FAIL ON GCC 3.x. The reason is that
158
5. is a double and b is a complex32. blitz doesn't know
159
how to handle complex32/double. See:
160
http://www.oonumerics.org/MailArchives/blitz-support/msg00541.php
161
Unfortunately, the fix isn't trivial. Instead of fixing it, I
162
prefer to wait until we replace blitz++ with Pat Miller's code
163
that doesn't rely on blitz..
165
expr = "result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
166
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
167
self.generic_2d(expr,complex64)
168
def check_5point_avg_2d_complex_double(self,level=10):
169
""" result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]
170
+ b[1:-1,2:] + b[1:-1,:-2]) / 5.
172
expr = "result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
173
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
174
self.generic_2d(expr,complex128)
176
if __name__ == "__main__":