Changeset 50b5464 in sasmodels


Ignore:
Timestamp:
Dec 4, 2017 8:12:20 AM (6 years ago)
Author:
Paul Kienzle <pkienzle@…>
Children:
7a40b08
Parents:
bf88ef1
Message:

rearrange autoc to allow py2c translation to do whole program analysis

Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/autoc.py

    rbf88ef1 r50b5464  
    6565 
    6666    libs = []  # type: List[str] 
     67    snippets = []  # type: List[str] 
     68    constants = {} # type: Dict[str, Any] 
    6769    code = {}  # type: Dict[str, str] 
    6870    depends = {}  # type: Dict[str, List[str]] 
     
    9092                    translate.append((name, obj)) 
    9193            elif isinstance(obj, float): 
    92                 code[name] = "const double %s = %.15g\n"%(name, obj) 
     94                constants[name] = obj 
     95                snippets.append("const double %s = %.15g;"%(name, obj)) 
    9396            elif isinstance(obj, int): 
    94                 code[name] = "const int %s = %d\n"%(name, obj) 
     97                constants[name] = obj 
     98                snippets.append("const int %s = %d;"%(name, obj)) 
    9599            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)) 
    96107                vals = ", ".join("%.15g"%v for v in obj) 
    97                 code[name] = "const double %s[] = {%s};\n" %(name, vals) 
     108                snippets.append("const double %s[] = {%s};" %(name, vals)) 
    98109            elif isinstance(obj, special.Gauss): 
     110                constants["GAUSS_N"] = obj.n 
     111                constants["GAUSS_Z"] = obj.z 
     112                constants["GAUSS_W"] = obj.w 
    99113                libs.append('lib/gauss%d.c'%obj.n) 
    100114                source = (source.replace(name+'.n', 'GAUSS_N') 
     
    105119                                % (name, str(type(obj)))) 
    106120 
    107         tree = ast.parse(source) 
    108         snippet = codegen.to_source(tree) #, filename, offset) 
    109         code[function_name] = snippet 
     121        # add (possibly modified) source to set of functions to compile 
     122        code[function_name] = (source, filename, offset) 
    110123 
    111124    # remove duplicates from the dependecy list 
     
    115128            unique_libs.append(filename) 
    116129 
     130    # translate source 
     131    functions = codegen.translate( 
     132        [code[name] for name in ordered_dag(depends) if name in code], 
     133        constants) 
     134 
     135    # update model info 
    117136    info.source = unique_libs 
    118     info.c_code = "\n".join(code[k] for k in ordered_dag(depends) if k in code) 
    119  
     137    info.c_code = "\n".join(snippets) +  functions 
    120138    info.Iq = info.Iqxy = info.form_volume = None 
    121139 
  • sasmodels/codegen.py

    rdb03406 r50b5464  
    4949UNARYOP_SYMBOLS[ast.USub] = '-' 
    5050 
     51 
     52def translate(functions, constants): 
     53    # type: (List[Tuple[str, str, int]], Dict[str, Any]) -> str 
     54    snippets = [] 
     55    for source, filename, offset in functions: 
     56        tree = ast.parse(source) 
     57        snippet = to_source(tree) #, filename, offset) 
     58        snippets.append(snippet) 
     59    return "\n".join(snippets) 
    5160 
    5261def to_source(node, indent_with=' ' * 4, add_line_information=False): 
Note: See TracChangeset for help on using the changeset viewer.