Changes in / [8224d24:ddfdb16] in sasmodels
- Location:
- sasmodels
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/autoc.py
r1ddb794 r2badeca 72 72 function_name, function = translate.pop(0) 73 73 filename = function.__code__.co_filename 74 escaped_filename = filename.replace('\\', '\\\\')75 74 offset = function.__code__.co_firstlineno 76 75 refs = function.__code__.co_names … … 92 91 # not special: add function to translate stack 93 92 translate.append((name, obj)) 94 elif isinstance(obj, (int, float, list, tuple, np.ndarray)):93 elif isinstance(obj, float): 95 94 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)) 95 snippets.append("const double %s = %.15g;"%(name, obj)) 96 elif isinstance(obj, int): 97 constants[name] = obj 98 snippets.append("const int %s = %d;"%(name, obj)) 99 elif isinstance(obj, (list, tuple, np.ndarray)): 100 constants[name] = obj 101 # extend constant arrays to a multiple of 4; not sure if this 102 # is necessary, but some OpenCL targets broke if the number 103 # of parameters in the parameter table was not a multiple of 4, 104 # so do it for all constant arrays to be safe. 105 if len(obj)%4 != 0: 106 obj = list(obj) + [0.]*(4-len(obj)) 107 vals = ", ".join("%.15g"%v for v in obj) 108 snippets.append("const double %s[] = {%s};" %(name, vals)) 99 109 elif isinstance(obj, special.Gauss): 100 #constants["GAUSS_N"] = obj.n101 #constants["GAUSS_Z"] = obj.z102 #constants["GAUSS_W"] = obj.w110 constants["GAUSS_N"] = obj.n 111 constants["GAUSS_Z"] = obj.z 112 constants["GAUSS_W"] = obj.w 103 113 libs.append('lib/gauss%d.c'%obj.n) 104 114 source = (source.replace(name+'.n', 'GAUSS_N') … … 119 129 120 130 # translate source 121 ordered_code = [code[name] for name in ordered_dag(depends) if name in code] 122 functions = py2c.translate(ordered_code, constants) 123 snippets.extend(functions) 131 functions = py2c.translate( 132 [code[name] for name in ordered_dag(depends) if name in code], 133 constants) 134 snippets.clear() 135 snippets.append(functions) 136 #print("source", info.source) 137 print("\n".join(snippets)) 138 try: 139 c_text = "\n".join(snippets) 140 translated = open ("_autoc.c", "a+") 141 translated.write (c_text) 142 translated.close() 143 except Exception as excp: 144 strErr = "Error:\n" + str(excp.args) 145 print(strErr) 146 #return 147 # raise RuntimeError("not yet converted...") 124 148 125 149 # update model info … … 127 151 info.c_code = "\n".join(snippets) 128 152 info.Iq = info.Iqxy = info.form_volume = None 129 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 153 147 154 -
sasmodels/kerneldll.py
r1ddb794 r2d81cfe 185 185 subprocess.check_output(command, shell=shell, stderr=subprocess.STDOUT) 186 186 except subprocess.CalledProcessError as exc: 187 raise RuntimeError("compile failed.\n%s\n%s" 188 % (command_str, exc.output.decode())) 187 raise RuntimeError("compile failed.\n%s\n%s"%(command_str, exc.output)) 189 188 if not os.path.exists(output): 190 189 raise RuntimeError("compile failed. File is in %r"%source) -
sasmodels/py2c.py
rddfdb16 rddfdb16 244 244 # for C 245 245 for arg in node.args: 246 # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 247 try: 248 arg_name = arg.arg 249 except AttributeError: 250 arg_name = arg.id 251 self.arguments.append(arg_name) 246 self.arguments.append(arg.arg) 252 247 253 248 padding = [None] *(len(node.args) - len(node.defaults)) 254 249 for arg, default in zip(node.args, padding + node.defaults): 255 250 if default is not None: 256 # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 257 # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 258 try: 259 arg_name = arg.arg 260 except AttributeError: 261 arg_name = arg.id 262 w_str = ("Default Parameters are unknown to C: '%s = %s" 263 % arg_name, str(default.n)) 251 self.warnings.append("Default Parameter unknown to C") 252 w_str = "Default Parameters are unknown to C: '" + arg.arg + \ 253 " = " + str(default.n) + "'" 264 254 self.warnings.append(w_str) 265 255 … … 314 304 if(len(self.Tuples) > 0): 315 305 tplTargets = list(self.Tuples) 316 del self.Tuples[:]306 self.Tuples.clear() 317 307 self.write_c(' = ') 318 308 self.is_sequence = False … … 412 402 self.c_proc.insert(start_var, c_dcl + "\n") 413 403 start_var += 1 414 del self.C_Vars[:]415 del self.C_IntVars[:]416 del self.C_Vectors[:]417 del self.C_Pointers[:]404 self.C_Vars.clear() 405 self.C_IntVars.clear() 406 self.C_Vectors.clear() 407 self.C_Pointers.clear() 418 408 self.C_DclPointers 419 409 if(fLine == True): … … 428 418 self.c_proc.insert(start_var, " double " + s + ";\n") 429 419 self.c_proc.insert(start_var + 1, "\n") 420 421 def writeInclude(self): 422 if(self.MathIncludeed == False): 423 self.add_c_line("#include <math.h>\n") 424 self.add_c_line("static double pi = 3.14159265359;\n") 425 self.MathIncludeed = True 430 426 431 427 def ListToString(self, strings): … … 787 783 if ((name not in self.C_Functions) and (name not in self.C_Vars) and \ 788 784 (name not in self.C_IntVars) and (name not in self.arguments) and \ 789 (name not in self.C_Constants) and (name.is digit() == False)):785 (name not in self.C_Constants) and (name.isnumeric() == False)): 790 786 if(self.InSubscript): 791 787 self.C_IntVars.append(node.id) … … 1047 1043 print(tree_source) 1048 1044 1045 def add_constants(sniplets, c_constants): 1046 sniplets.append("#include <math.h>") 1047 sniplets.append("") 1048 vars = c_constants.keys() 1049 for c_var in vars: 1050 c_values = c_constants[c_var] 1051 if isinstance(c_values, (int, float)): 1052 parts = ["double ", c_var, " = ", "%.15g"%c_values, ";"] 1053 else: 1054 elements = ["%.15g"%v for v in c_values] 1055 parts = ["double ", c_var, "[]", " = ", "{\n ", ", ".join(elements), "\n};"] 1056 sniplets.append("".join(parts)) 1057 1049 1058 def translate(functions, constants=0): 1050 snip pets = []1051 #snippets.append("#include <math.h>")1052 #snippets.append("")1053 for source, fname, line_no in functions:1054 line_directive = '#line %d "%s"'%(line_no, fname.replace('\\', '\\\\'))1055 snippets.append(line_directive)1059 sniplets = [] 1060 add_constants (sniplets, constants) 1061 for source,fname,line_no in functions: 1062 line_directive = '#line %d "%s"' %(line_no,fname) 1063 line_directive = line_directive.replace('\\','\\\\') 1064 # sniplets.append(line_directive) 1056 1065 tree = ast.parse(source) 1057 # in the future add filename, offset, constants 1058 c_code = to_source(tree, functions, constants) 1059 snippets.append(c_code) 1060 return snippets 1066 sniplet = to_source(tree, functions, constants) # in the future add filename, offset, constants 1067 sniplets.append(sniplet) 1068 c_code = "\n".join(sniplets) 1069 f_out = open ("xlate.c", "w+") 1070 f_out.write (c_code) 1071 f_out.close() 1072 return("\n".join(sniplets)) 1061 1073 1062 1074 def get_file_names():
Note: See TracChangeset
for help on using the changeset viewer.