Changeset 91bd550 in sasmodels


Ignore:
Timestamp:
Sep 17, 2018 6:10:44 PM (5 weeks ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, beta_approx, cuda-test, py3, ticket-1074-gammainc, ticket-1142-plugin-reload, ticket-1155BE_PolyElectrolyte, ticket-1157
Children:
ca77fc1
Parents:
839fd68
Message:

handle nested dependencies

Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/custom/__init__.py

    r839fd68 r91bd550  
    3838 
    3939_MODULE_CACHE = {} 
     40_MODULE_DEPENDS = {} 
     41_MODULE_DEPENDS_STACK = [] 
    4042def load_custom_kernel_module(path): 
    4143    """load SAS kernel from *path* as *sasmodels.custom.modelname*""" 
     
    4345    name = basename(splitext(path)[0]) 
    4446    path = os.path.expanduser(path) 
    45     timestamp = os.path.getmtime(path) 
    46     module, cache_time = _MODULE_CACHE.get(path, (None, -1)) 
    47     if cache_time < timestamp: 
    48         # Placing the model in the 'sasmodels.custom' name space. 
     47 
     48    # reload module if necessary 
     49    if need_reload(path): 
     50        # Push to the next dependency level 
     51        _MODULE_DEPENDS_STACK.append(path) 
     52        _MODULE_DEPENDS[path] = set([path]) 
     53 
     54        # Load module into the 'sasmodels.custom' name space. 
     55        # If this triggers any submodule loads then they will be added 
     56        # as dependencies below when _MODULE_DEPENDS_STACK is not empty. 
    4957        module = load_module_from_path('sasmodels.custom.'+name, path) 
     58 
     59        # Pop the dependency level 
     60        _MODULE_DEPENDS_STACK.pop() 
     61 
     62        # TODO: include external C code in the dependencies 
     63        # If we had the model info structure we could do the following: 
     64        #    _MODEL_DEPENDS[path].extend(generate.model_sources(info)) 
     65        # but at this point all we have is the module.  Don't want to 
     66        # repeat the logic in modelinfo.make_model_info. 
     67 
     68        # Cache the module with the newest timestamp 
     69        timestamp = max(os.path.getmtime(f) for f in _MODULE_DEPENDS[path]) 
    5070        _MODULE_CACHE[path] = module, timestamp 
    5171 
    52     return module 
     72        #print("loading", os.path.basename(path), _MODULE_CACHE[path][1], 
     73        #    [os.path.basename(p) for p in _MODULE_DEPENDS[path]]) 
     74 
     75    if _MODULE_DEPENDS_STACK: 
     76        # Add child and all its dependence to the parent module 
     77        working_on = _MODULE_DEPENDS_STACK[-1] 
     78        _MODULE_DEPENDS[working_on].update(_MODULE_DEPENDS[path]) 
     79 
     80    return _MODULE_CACHE[path][0] 
     81 
     82def need_reload(path): 
     83    # TODO: fails if a dependency has a modification time in the future 
     84    # If the newest dependency has a time stamp in the future, then this 
     85    # will be recorded as the cached time.  When a second dependency 
     86    # is updated to the current time stamp, it will still be considered 
     87    # older than the current build and the reload will not be triggered. 
     88    # Could instead treat all future times as 0 here and in the code above 
     89    # which records the newest timestamp.  This will force a reload when 
     90    # the future time is reached, but other than that should perform 
     91    # correctly.  Probably not worth the extra code... 
     92    _, cache_time = _MODULE_CACHE.get(path, (None, -1)) 
     93    depends = _MODULE_DEPENDS.get(path, [path]) 
     94    return any(cache_time < os.path.getmtime(p) for p in depends) 
  • sasmodels/kernelpy.py

    r108e70e r91bd550  
    3737        self.info = model_info 
    3838        self.dtype = np.dtype('d') 
    39         logger.info("load python model " + self.info.name) 
    4039 
    4140    def make_kernel(self, q_vectors): 
Note: See TracChangeset for help on using the changeset viewer.