~ubuntu-branches/ubuntu/raring/python-scipy/raring-proposed

« back to all changes in this revision

Viewing changes to Lib/weave/tests/test_blitz_tools.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import time
 
3
 
 
4
from numpy import *
 
5
 
 
6
from numpy.testing import *
 
7
set_package_path()
 
8
from weave import blitz_tools
 
9
from weave.ast_tools import harvest_variables
 
10
restore_path()
 
11
 
 
12
set_local_path()
 
13
from weave_test_utils import *
 
14
restore_path()
 
15
 
 
16
 
 
17
class test_ast_to_blitz_expr(ScipyTestCase):
 
18
 
 
19
    def generic_test(self,expr,desired):
 
20
        import parser
 
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)
 
27
 
 
28
    def check_simple_expr(self):
 
29
        """convert simple expr to blitz
 
30
 
 
31
           a[:1:2] = b[:1+i+2:]
 
32
        """
 
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)
 
37
 
 
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.
 
45
        """
 
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)
 
59
 
 
60
class test_blitz(ScipyTestCase):
 
61
    """* These are long running tests...
 
62
 
 
63
         I'd like to benchmark these things somehow.
 
64
    *"""
 
65
    def generic_test(self,expr,arg_dict,type,size,mod_location):
 
66
        clean_result = array(arg_dict['result'],copy=1)
 
67
        t1 = time.time()
 
68
        exec expr in globals(),arg_dict
 
69
        t2 = time.time()
 
70
        standard = t2 - t1
 
71
        desired = arg_dict['result']
 
72
        arg_dict['result'] = clean_result
 
73
        t1 = time.time()
 
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
 
79
        t2 = time.time()
 
80
        compiled = t2 - t1
 
81
        actual = arg_dict['result']
 
82
        # this really should give more info...
 
83
        try:
 
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))
 
87
        except:
 
88
            diff = actual-desired
 
89
            print diff[:4,:4]
 
90
            print diff[:4,-4:]
 
91
            print diff[-4:,:4]
 
92
            print diff[-4:,-4:]
 
93
            print sum(abs(diff.ravel()),axis=0)
 
94
            raise AssertionError
 
95
        return standard,compiled
 
96
 
 
97
    def generic_2d(self,expr,typ):
 
98
        """ The complex testing is pretty lame...
 
99
        """
 
100
        mod_location = empty_temp_dir()
 
101
        import parser
 
102
        ast = parser.suite(expr)
 
103
        arg_list = harvest_variables(ast.tolist())
 
104
        #print arg_list
 
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)
 
109
            arg_dict = {}
 
110
            for arg in arg_list:
 
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
 
114
                except:  pass
 
115
            print 'Run:', size,typ
 
116
            standard,compiled = self.generic_test(expr,arg_dict,type,size,
 
117
                                                  mod_location)
 
118
            try:
 
119
                speed_up = standard/compiled
 
120
            except:
 
121
                speed_up = -1.
 
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,
 
125
                                                  mod_location)
 
126
            try:
 
127
                speed_up = standard/compiled
 
128
            except:
 
129
                speed_up = -1.
 
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.
 
140
        """
 
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.
 
147
        """
 
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
 
153
 
 
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.
 
156
 
 
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..
 
164
        """
 
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.
 
171
        """
 
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)
 
175
 
 
176
if __name__ == "__main__":
 
177
    ScipyTest().run()