def generic_2d(self,expr,typ): # The complex testing is pretty lame... ast = parser.suite(expr) arg_list = harvest_variables(ast.tolist()) all_sizes = [(10,10), (50,50), (100,100), (500,500), (1000,1000)] debug_print('\nExpression:', expr) with TempdirBlitz(): for size in all_sizes: arg_dict = {} for arg in arg_list: arg_dict[arg] = random.normal(0,1,size).astype(typ) # set imag part of complex values to non-zero value try: arg_dict[arg].imag = arg_dict[arg].real except: pass debug_print('Run:', size,typ) standard,compiled = self.generic_check(expr,arg_dict,typ,size) try: speed_up = standard/compiled except: speed_up = -1. debug_print("1st run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard,compiled,speed_up)) standard,compiled = self.generic_check(expr,arg_dict,typ,size) try: speed_up = standard/compiled except: speed_up = -1. debug_print("2nd run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard,compiled,speed_up))
def generic_2d(self,expr,typ): # The complex testing is pretty lame... ast = parser.suite(expr) arg_list = harvest_variables(ast.tolist()) all_sizes = [(10,10), (50,50), (100,100), (500,500), (1000,1000)] debug_print('\nExpression:', expr) with TempdirBlitz(): for size in all_sizes: arg_dict = {} for arg in arg_list: arg_dict[arg] = random.normal(0,1,size).astype(typ) # set imag part of complex values to non-zero value try: arg_dict[arg].imag = arg_dict[arg].real except: pass debug_print('Run:', size,typ) standard,compiled = self.generic_check(expr,arg_dict,type,size) try: speed_up = standard/compiled except: speed_up = -1. debug_print("1st run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard,compiled,speed_up)) standard,compiled = self.generic_check(expr,arg_dict,type,size) try: speed_up = standard/compiled except: speed_up = -1. debug_print("2nd run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard,compiled,speed_up))
def blitz(expr,local_dict=None, global_dict=None,check_size=1,verbose=0,**kw): # this could call inline, but making a copy of the # code here is more efficient for several reasons. global function_catalog # this grabs the local variables from the *previous* call # frame -- that is the locals from the function that called # inline. call_frame = sys._getframe().f_back if local_dict is None: local_dict = call_frame.f_locals if global_dict is None: global_dict = call_frame.f_globals # 1. Check the sizes of the arrays and make sure they are compatible. # This is expensive, so unsetting the check_size flag can save a lot # of time. It also can cause core-dumps if the sizes of the inputs # aren't compatible. if check_size and not size_check.check_expr(expr,local_dict,global_dict): raise ValueError("inputs failed to pass size check.") # 2. try local cache try: results = function_cache[expr](*(local_dict,global_dict)) return results except: pass try: results = attempt_function_call(expr,local_dict,global_dict) # 3. build the function except ValueError: # This section is pretty much the only difference # between blitz and inline ast = parser.suite(expr) ast_list = ast.tolist() expr_code = ast_to_blitz_expr(ast_list) arg_names = ast_tools.harvest_variables(ast_list) module_dir = global_dict.get('__file__',None) func = inline_tools.compile_function(expr_code,arg_names,local_dict, global_dict,module_dir, compiler='gcc',auto_downcast=1, verbose=verbose, type_converters=converters.blitz, **kw) function_catalog.add_function(expr,func,module_dir) try: results = attempt_function_call(expr,local_dict,global_dict) except ValueError: warnings.warn('compilation failed. Executing as python code', BlitzWarning) exec(expr, global_dict, local_dict)
def generic_check(self, expr, desired, **kw): ast_list = parser.expr(expr).tolist() args = harvest_variables(ast_list) loc = locals().update(kw) for var in args: s = '%s = size_check.dummy_array(%s)' % (var, var) exec(s, loc) try: actual = eval(expr, locals()).shape except: actual = 'failed' if actual is 'failed' and desired is 'failed': return assert_array_equal(actual, desired, expr)
def generic_check(self,expr,desired,**kw): ast_list = parser.expr(expr).tolist() args = harvest_variables(ast_list) loc = locals().update(kw) for var in args: s = '%s = size_check.dummy_array(%s)' % (var,var) exec(s,loc) try: actual = eval(expr,locals()).shape except: actual = 'failed' if actual is 'failed' and desired is 'failed': return assert_array_equal(actual,desired, expr)
def generic_2d(self,expr,typ): """ The complex testing is pretty lame... """ mod_location = empty_temp_dir() import parser ast = parser.suite(expr) arg_list = harvest_variables(ast.tolist()) #print arg_list all_sizes = [(10,10), (50,50), (100,100), (500,500), (1000,1000)] print '\nExpression:', expr for size in all_sizes: result = zeros(size,typ) arg_dict = {} for arg in arg_list: arg_dict[arg] = random.normal(0,1,size).astype(typ) # set imag part of complex values to non-zero value try: arg_dict[arg].imag = arg_dict[arg].real except: pass print 'Run:', size,typ standard,compiled = self.generic_test(expr,arg_dict,type,size, mod_location) try: speed_up = standard/compiled except: speed_up = -1. print "1st run(numpy.numerix,compiled,speed up): %3.4f, %3.4f, " \ "%3.4f" % (standard,compiled,speed_up) standard,compiled = self.generic_test(expr,arg_dict,type,size, mod_location) try: speed_up = standard/compiled except: speed_up = -1. print "2nd run(numpy.numerix,compiled,speed up): %3.4f, %3.4f, " \ "%3.4f" % (standard,compiled,speed_up) cleanup_temp_dir(mod_location)
def generic_check(self,expr,desired): import parser ast_list = parser.suite(expr).tolist() actual = ast_tools.harvest_variables(ast_list) assert_equal(actual,desired,expr)
def generic_check(self, expr, desired): import parser ast_list = parser.suite(expr).tolist() actual = ast_tools.harvest_variables(ast_list) assert_equal(actual, desired, expr)