Changes in / [8224d24:1ddb794] in sasmodels
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/autoc.py
r1ddb794 r1ddb794 92 92 # not special: add function to translate stack 93 93 translate.append((name, obj)) 94 elif isinstance(obj, (int, float, list, tuple, np.ndarray)):94 elif isinstance(obj, float): 95 95 constants[name] = obj 96 # Claim all constants are declared on line 1 97 snippets.append('#line 1 "%s"'%escaped_filename) 98 snippets.append(define_constant(name, obj)) 96 snippets.append('#line 1 "%s"' % escaped_filename) 97 snippets.append("const double %s = %.15g;"%(name, obj)) 98 elif isinstance(obj, int): 99 constants[name] = obj 100 snippets.append('#line 1 "%s"' % escaped_filename) 101 snippets.append("const int %s = %d;"%(name, obj)) 102 elif isinstance(obj, (list, tuple, np.ndarray)): 103 constants[name] = obj 104 # extend constant arrays to a multiple of 4; not sure if this 105 # is necessary, but some OpenCL targets broke if the number 106 # of parameters in the parameter table was not a multiple of 4, 107 # so do it for all constant arrays to be safe. 108 if len(obj)%4 != 0: 109 obj = list(obj) + [0.]*(4-len(obj)) 110 vals = ", ".join("%.15g"%v for v in obj) 111 snippets.append('#line 1 "%s"' % escaped_filename) 112 snippets.append("const double %s[] = {%s};" %(name, vals)) 99 113 elif isinstance(obj, special.Gauss): 100 #constants["GAUSS_N"] = obj.n101 #constants["GAUSS_Z"] = obj.z102 #constants["GAUSS_W"] = obj.w114 constants["GAUSS_N"] = obj.n 115 constants["GAUSS_Z"] = obj.z 116 constants["GAUSS_W"] = obj.w 103 117 libs.append('lib/gauss%d.c'%obj.n) 104 118 source = (source.replace(name+'.n', 'GAUSS_N') … … 128 142 info.Iq = info.Iqxy = info.form_volume = None 129 143 130 def define_constant(name, value):131 if isinstance(value, int):132 parts = ["int ", name, " = ", "%d"%value, ";"]133 elif isinstance(value, float):134 parts = ["double ", name, " = ", "%.15g"%value, ";"]135 else:136 # extend constant arrays to a multiple of 4; not sure if this137 # is necessary, but some OpenCL targets broke if the number138 # of parameters in the parameter table was not a multiple of 4,139 # so do it for all constant arrays to be safe.140 if len(value)%4 != 0:141 value = list(value) + [0.]*(4 - len(value)%4)142 elements = ["%.15g"%v for v in value]143 parts = ["double ", name, "[]", " = ",144 "{\n ", ", ".join(elements), "\n};"]145 return "".join(parts)146 147 144 148 145 # Modified from the following: -
sasmodels/py2c.py
rddfdb16 r1ddb794 57 57 12/15/2017, OE: Precedence maintained by writing opening and closing 58 58 parenthesesm '(',')', in procedure 'visit_BinOp'. 59 12/18/2017, OE: Added call to 'add_current_line()' at the beginning60 of visit_Return61 62 59 """ 63 60 import ast … … 102 99 103 100 104 def to_source(node, func_name, constants=None): 101 #def to_source(node, indent_with=' ' * 4, add_line_information=False): 102 def to_source(node, func_name): 105 103 """This function can convert a node tree back into python sourcecode. 106 104 This is useful for debugging purposes, especially if you're dealing with … … 120 118 number information of statement nodes. 121 119 """ 122 generator = SourceGenerator(' ' * 4, False, constants) 120 generator = SourceGenerator(' ' * 4, False) 121 # generator.required_functions = func_name 123 122 generator.visit(node) 124 123 124 # return ''.join(generator.result) 125 125 return ''.join(generator.c_proc) 126 126 … … 138 138 """ 139 139 140 def __init__(self, indent_with, add_line_information=False , constants=None):140 def __init__(self, indent_with, add_line_information=False): 141 141 self.result = [] 142 142 self.indent_with = indent_with … … 160 160 self.C_Functions = [] 161 161 self.C_Vectors = [] 162 self.C_Constants = constants163 162 self.SubRef = False 164 163 self.InSubscript = False … … 263 262 % arg_name, str(default.n)) 264 263 self.warnings.append(w_str) 264 # self.write_python('=') 265 # self.visit(default) 265 266 266 267 def decorators(self, node): … … 320 321 self.visit(node.value) 321 322 self.add_semi_colon() 323 # self.write_c(';') 322 324 self.add_current_line() 323 325 for n, item in enumerate(self.Tuples): … … 345 347 self.visit(node.value) 346 348 self.add_semi_colon() 349 # self.write_c(';') 347 350 self.add_current_line() 348 351 … … 405 408 fLine = True 406 409 start_var += 1 410 # if(len(self.C_IntVars) > 0): 411 # s = self.listToDeclare(self.C_IntVars) 412 # self.c_proc.insert(start_var, " int " + s + ";\n") 413 # fLine = True 414 # start_var += 1 407 415 if(len(self.C_Vectors) > 0): 408 416 s = self.listToDeclare(self.C_Vectors) … … 438 446 439 447 def getMethodSignature(self): 448 # args_str = ListToString(self.arguments) 440 449 args_str = '' 441 450 for n in range(len(self.arguments)): … … 444 453 args_str += ", " 445 454 return(args_str) 455 # self.strMethodSignature = 'double ' + self.name + '(' + args_str + ")" 446 456 447 457 def InsertSignature(self): … … 463 473 self.arguments = [] 464 474 self.name = node.name 475 # if self.name not in self.required_functions[0]: 476 # return 465 477 print("Parsing '" + self.name + "'") 466 478 args_str = "" … … 468 480 self.visit(node.args) 469 481 self.getMethodSignature() 482 # for C 470 483 self.signature_line = len(self.c_proc) 484 # self.add_c_line(self.strMethodSignature) 471 485 self.add_c_line("\n{") 472 486 start_vars = len(self.c_proc) + 1 … … 543 557 if(hasattr(node,'value')): 544 558 line_number = node.value.lineno 545 elif hasattr(node, 546 if hasattr(node.iter, 559 elif hasattr(node,'iter'): 560 if hasattr(node.iter,'lineno'): 547 561 line_number = node.iter.lineno 548 562 return(line_number) … … 585 599 # Iterator name is in node.target.id. 586 600 self.add_current_line() 601 # if(len(self.current_statement) > 0): 602 # self.add_c_line(self.current_statement) 603 # self.current_statement = '' 587 604 fForDone = False 588 605 self.current_statement = '' … … 684 701 685 702 def visit_Return(self, node): 686 self. add_current_line()703 self.newline(node) 687 704 if node.value is None: 688 705 self.write_c('return') … … 785 802 name = node.id 786 803 # add variable to C_Vars if it ins't there yet, not an argument and not a number 787 if 804 if((name not in self.C_Functions) and (name not in self.C_Vars) and \ 788 805 (name not in self.C_IntVars) and (name not in self.arguments) and \ 789 (name not in self.C_Constants) and (name.isdigit() == False)):806 (name.isdigit() == False)): 790 807 if(self.InSubscript): 791 808 self.C_IntVars.append(node.id) … … 930 947 931 948 def visit_Subscript(self, node): 932 if (node.value.id not in self.C_Constants): 933 if(node.value.id not in self.C_Pointers): 934 self.C_Pointers.append(node.value.id) 949 if(node.value.id not in self.C_Pointers): 950 self.C_Pointers.append(node.value.id) 935 951 self.SubRef = True 936 952 self.visit(node.value) … … 1032 1048 self.visit(if_) 1033 1049 1050 # def visit_excepthandler(self, node): 1051 # self.newline(node) 1052 # self.write_python('except') 1053 # if node.type is not None: 1054 # self.write_python(' ') 1055 # self.visit(node.type) 1056 # if node.name is not None: 1057 # self.write_python(' as ') 1058 # self.visit(node.name) 1059 # self.body(node.body) 1060 1034 1061 def visit_arguments(self, node): 1035 1062 self.signature(node) 1063 1064 def Iq1(q, porod_scale, porod_exp, lorentz_scale, lorentz_length, peak_pos, lorentz_exp=17): 1065 z1 = z2 = z = abs(q - peak_pos) * lorentz_length 1066 if(q > p): 1067 q = p + 17 1068 p = q - 5 1069 z3 = -8 1070 inten = (porod_scale / q ** porod_exp 1071 + lorentz_scale /(1 + z ** lorentz_exp)) 1072 return inten 1073 1074 def Iq(q, porod_scale, porod_exp, lorentz_scale, lorentz_length, peak_pos, lorentz_exp=17): 1075 z1 = z2 = z = abs(q - peak_pos) * lorentz_length 1076 if(q > p): 1077 q = p + 17 1078 p = q - 5 1079 elif(q == p): 1080 q = p * q 1081 q *= z1 1082 p = z1 1083 elif(q == 17): 1084 q = p * q - 17 1085 else: 1086 q += 7 1087 z3 = -8 1088 inten = (porod_scale / q ** porod_exp 1089 + lorentz_scale /(1 + z ** lorentz_exp)) 1090 return inten 1036 1091 1037 1092 def print_function(f=None): … … 1049 1104 def translate(functions, constants=0): 1050 1105 snippets = [] 1051 #snippets.append("#include <math.h>")1052 #snippets.append("")1053 1106 for source, fname, line_no in functions: 1054 line_directive = '#line %d "%s"'%(line_no, fname.replace('\\', 1107 line_directive = '#line %d "%s"'%(line_no, fname.replace('\\','\\\\')) 1055 1108 snippets.append(line_directive) 1056 1109 tree = ast.parse(source) 1057 1110 # in the future add filename, offset, constants 1058 c_code = to_source(tree, functions , constants)1111 c_code = to_source(tree, functions) 1059 1112 snippets.append(c_code) 1060 1113 return snippets
Note: See TracChangeset
for help on using the changeset viewer.