Changes in sasmodels/autoc.py [2badeca:67cc0ff] in sasmodels


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/autoc.py

    r2badeca r67cc0ff  
    4949        return 
    5050 
    51     public_methods = "Iq", "Iqxy", "form_volume" 
     51    public_methods = "Iq", "Iqac", "Iqabc", "Iqxy", "form_volume" 
    5252 
    5353    tagged = [] # type: List[str] 
     
    7272        function_name, function = translate.pop(0) 
    7373        filename = function.__code__.co_filename 
     74        escaped_filename = filename.replace('\\', '\\\\') 
    7475        offset = function.__code__.co_firstlineno 
    7576        refs = function.__code__.co_names 
     
    9192                    # not special: add function to translate stack 
    9293                    translate.append((name, obj)) 
    93             elif isinstance(obj, float): 
     94            elif isinstance(obj, (int, float, list, tuple, np.ndarray)): 
    9495                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)) 
    10999            elif isinstance(obj, special.Gauss): 
    110                 constants["GAUSS_N"] = obj.n 
    111                 constants["GAUSS_Z"] = obj.z 
    112                 constants["GAUSS_W"] = obj.w 
    113                 libs.append('lib/gauss%d.c'%obj.n) 
     100                for var, value in zip(("N", "Z", "W"), (obj.n, obj.z, obj.w)): 
     101                    var = "GAUSS_"+var 
     102                    constants[var] = value 
     103                    snippets.append('#line 1 "%s"'%escaped_filename) 
     104                    snippets.append(define_constant(var, value)) 
     105                #libs.append('lib/gauss%d.c'%obj.n) 
    114106                source = (source.replace(name+'.n', 'GAUSS_N') 
    115107                          .replace(name+'.z', 'GAUSS_Z') 
     
    129121 
    130122    # 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...") 
     123    ordered_code = [code[name] for name in ordered_dag(depends) if name in code] 
     124    functions = py2c.translate(ordered_code, constants) 
     125    snippets.extend(functions) 
    148126 
    149127    # update model info 
    150128    info.source = unique_libs 
    151129    info.c_code = "\n".join(snippets) 
    152     info.Iq = info.Iqxy = info.form_volume = None 
     130    info.Iq = info.Iqac = info.Iqabc = info.Iqxy = info.form_volume = None 
     131 
     132def define_constant(name, value): 
     133    if isinstance(value, int): 
     134        parts = ["int ", name, " = ", "%d"%value, ";"] 
     135    elif isinstance(value, float): 
     136        parts = ["double ", name, " = ", "%.15g"%value, ";"] 
     137    else: 
     138        # extend constant arrays to a multiple of 4; not sure if this 
     139        # is necessary, but some OpenCL targets broke if the number 
     140        # of parameters in the parameter table was not a multiple of 4, 
     141        # so do it for all constant arrays to be safe. 
     142        if len(value)%4 != 0: 
     143            value = list(value) + [0.]*(4 - len(value)%4) 
     144        elements = ["%.15g"%v for v in value] 
     145        parts = ["double ", name, "[]", " = ", 
     146                 "{\n   ", ", ".join(elements), "\n};"] 
     147    return "".join(parts) 
    153148 
    154149 
Note: See TracChangeset for help on using the changeset viewer.