Changes in / [ddfdb16:8224d24] in sasmodels
- Location:
- sasmodels
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/autoc.py
r2badeca r1ddb794 72 72 function_name, function = translate.pop(0) 73 73 filename = function.__code__.co_filename 74 escaped_filename = filename.replace('\\', '\\\\') 74 75 offset = function.__code__.co_firstlineno 75 76 refs = function.__code__.co_names … … 91 92 # not special: add function to translate stack 92 93 translate.append((name, obj)) 93 elif isinstance(obj, float):94 elif isinstance(obj, (int, float, list, tuple, np.ndarray)): 94 95 constants[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)) 96 # Claim all constants are declared on line 1 97 snippets.append('#line 1 "%s"'%escaped_filename) 98 snippets.append(define_constant(name, obj)) 109 99 elif isinstance(obj, special.Gauss): 110 constants["GAUSS_N"] = obj.n111 constants["GAUSS_Z"] = obj.z112 constants["GAUSS_W"] = obj.w100 #constants["GAUSS_N"] = obj.n 101 #constants["GAUSS_Z"] = obj.z 102 #constants["GAUSS_W"] = obj.w 113 103 libs.append('lib/gauss%d.c'%obj.n) 114 104 source = (source.replace(name+'.n', 'GAUSS_N') … … 129 119 130 120 # translate source 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...") 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) 148 124 149 125 # update model info … … 151 127 info.c_code = "\n".join(snippets) 152 128 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 this 137 # is necessary, but some OpenCL targets broke if the number 138 # 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) 153 146 154 147 -
sasmodels/kerneldll.py
r2d81cfe r1ddb794 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"%(command_str, exc.output)) 187 raise RuntimeError("compile failed.\n%s\n%s" 188 % (command_str, exc.output.decode())) 188 189 if not os.path.exists(output): 189 190 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 self.arguments.append(arg.arg) 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) 247 252 248 253 padding = [None] *(len(node.args) - len(node.defaults)) 249 254 for arg, default in zip(node.args, padding + node.defaults): 250 255 if default is not None: 251 self.warnings.append("Default Parameter unknown to C") 252 w_str = "Default Parameters are unknown to C: '" + arg.arg + \ 253 " = " + str(default.n) + "'" 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)) 254 264 self.warnings.append(w_str) 255 265 … … 304 314 if(len(self.Tuples) > 0): 305 315 tplTargets = list(self.Tuples) 306 self.Tuples.clear()316 del self.Tuples[:] 307 317 self.write_c(' = ') 308 318 self.is_sequence = False … … 402 412 self.c_proc.insert(start_var, c_dcl + "\n") 403 413 start_var += 1 404 self.C_Vars.clear()405 self.C_IntVars.clear()406 self.C_Vectors.clear()407 self.C_Pointers.clear()414 del self.C_Vars[:] 415 del self.C_IntVars[:] 416 del self.C_Vectors[:] 417 del self.C_Pointers[:] 408 418 self.C_DclPointers 409 419 if(fLine == True): … … 418 428 self.c_proc.insert(start_var, " double " + s + ";\n") 419 429 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 = True426 430 427 431 def ListToString(self, strings): … … 783 787 if ((name not in self.C_Functions) and (name not in self.C_Vars) and \ 784 788 (name not in self.C_IntVars) and (name not in self.arguments) and \ 785 (name not in self.C_Constants) and (name.is numeric() == False)):789 (name not in self.C_Constants) and (name.isdigit() == False)): 786 790 if(self.InSubscript): 787 791 self.C_IntVars.append(node.id) … … 1043 1047 print(tree_source) 1044 1048 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 1058 1049 def translate(functions, constants=0): 1059 snip lets = []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)1050 snippets = [] 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) 1065 1056 tree = ast.parse(source) 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)) 1057 # in the future add filename, offset, constants 1058 c_code = to_source(tree, functions, constants) 1059 snippets.append(c_code) 1060 return snippets 1073 1061 1074 1062 def get_file_names():
Note: See TracChangeset
for help on using the changeset viewer.