From e8935f24f1395edd8b101a8d83b7b5af0cee2cbf Mon Sep 17 00:00:00 2001 From: Paul Kienzle Date: Fri, 23 Jun 2023 19:35:00 -0400 Subject: [PATCH] Add flag for whether model is compiled or not. --- sasmodels/compare.py | 2 +- sasmodels/core.py | 6 +++--- sasmodels/generate.py | 3 +-- sasmodels/model_test.py | 4 ++-- sasmodels/modelinfo.py | 15 +++++++++------ 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/sasmodels/compare.py b/sasmodels/compare.py index f5228c9bd..bf49e8f37 100755 --- a/sasmodels/compare.py +++ b/sasmodels/compare.py @@ -551,7 +551,7 @@ def constrain_pars(model_info, pars): name = name.split('*')[0] # Suppress magnetism for python models (not yet implemented) - if callable(model_info.Iq): + if not model_info.compiled: pars.update(suppress_magnetism(pars)) if name == 'barbell': diff --git a/sasmodels/core.py b/sasmodels/core.py index 5a35ade9c..46cfad68f 100644 --- a/sasmodels/core.py +++ b/sasmodels/core.py @@ -96,7 +96,7 @@ def _matches(name, kind): info = load_model_info(name) pars = info.parameters.kernel_parameters # TODO: may be adding Fq to the list at some point - is_pure_py = callable(info.Iq) + is_pure_py = not info.compiled if kind == "py": return is_pure_py elif kind == "c": @@ -327,7 +327,7 @@ def build_model(model_info, dtype=None, platform="ocl"): raise ValueError('unknown mixture type %s'%composition_type) # If it is a python model, return it immediately - if callable(model_info.Iq): + if not model_info.compiled: from . import kernelpy return kernelpy.PyModel(model_info) @@ -364,7 +364,7 @@ def precompile_dlls(path, dtype="double"): compiled_dlls = [] for model_name in list_models(): model_info = load_model_info(model_name) - if not callable(model_info.Iq): + if model_info.compiled: source = generate.make_source(model_info)['dll'] old_path = kerneldll.SAS_DLL_PATH try: diff --git a/sasmodels/generate.py b/sasmodels/generate.py index 13a5ac188..a2454a7aa 100644 --- a/sasmodels/generate.py +++ b/sasmodels/generate.py @@ -935,9 +935,8 @@ def make_source(model_info): Uses source files found in the given search path. Returns None if this is a pure python model, with no C source components. """ - if callable(model_info.Iq): + if not model_info.compiled: raise ValueError("can't compile python model") - #return None # TODO: need something other than volume to indicate dispersion parameters # No volume normalization despite having a volume parameter. diff --git a/sasmodels/model_test.py b/sasmodels/model_test.py index 49615ec31..9c1a68b70 100755 --- a/sasmodels/model_test.py +++ b/sasmodels/model_test.py @@ -111,10 +111,10 @@ def _add_model_to_suite(loaders, suite, model_info): #print('found tests in', model_name) #print('------') - # if ispy then use the dll loader to call pykernel + # if is python then use the dll loader to call pykernel # don't try to call cl kernel since it will not be # available in some environmentes. - is_py = callable(model_info.Iq) + is_py = not model_info.compiled # Some OpenCL drivers seem to be flaky, and are not producing the # expected result. Since we don't have known test values yet for diff --git a/sasmodels/modelinfo.py b/sasmodels/modelinfo.py index f0032a625..495c74af5 100644 --- a/sasmodels/modelinfo.py +++ b/sasmodels/modelinfo.py @@ -862,6 +862,7 @@ def isstr(x): return isinstance(x, str) +# Note: adding 'Fq' even though we can't yet use it to define C code in python. #: Set of variables defined in the model that might contain C code C_SYMBOLS = ['Imagnetic', 'Iq', 'Iqxy', 'Iqac', 'Iqabc', 'form_volume', 'shell_volume', 'c_code', 'valid'] @@ -882,8 +883,7 @@ def _find_source_lines(model_info, kernel_module): """ # Only need line numbers if we are creating a C module and the C symbols # are defined. - if (callable(model_info.Iq) - or not any(hasattr(model_info, s) for s in C_SYMBOLS)): + if not model_info.compiled: return # load the module source if we can @@ -966,8 +966,9 @@ def make_model_info(kernel_module): info.profile = getattr(kernel_module, 'profile', None) # type: ignore info.sesans = getattr(kernel_module, 'sesans', None) # type: ignore # Default single and opencl to True for C models. Python models have callable Iq. - info.opencl = getattr(kernel_module, 'opencl', not callable(info.Iq)) - info.single = getattr(kernel_module, 'single', not callable(info.Iq)) + info.compiled = not callable(info.Iq) and not callable(info.Fq) + info.opencl = getattr(kernel_module, 'opencl', info.compiled) + info.single = getattr(kernel_module, 'single', info.compiled) info.random = getattr(kernel_module, 'random', None) info.hidden = getattr(kernel_module, 'hidden', None) # type: ignore @@ -976,7 +977,7 @@ def make_model_info(kernel_module): if control is not None: parameters[control].is_control = True - if callable(info.Iq) and parameters.has_2d: + if not info.compiled and parameters.has_2d: raise ValueError("oriented python models not supported") # CRUFT: support old-style ER() for effective radius @@ -990,7 +991,7 @@ def make_model_info(kernel_module): # so just issue a warning if we see ER in a C model. ER = getattr(kernel_module, 'ER', None) if ER is not None: - if callable(info.Iq) and info.radius_effective is None: + if not info.compiled and info.radius_effective is None: info.radius_effective_modes = ['ER'] info.radius_effective = lambda mode, *args: ER(*args) # TODO: uncomment the following for the sasview 4.3 release @@ -1093,6 +1094,8 @@ class ModelInfo(object): #: the model cannot be run in opencl (e.g., because the model passes #: functions by reference), then set this to false. opencl = None # type: bool + #: True if the model is compiled with C or OpenCL + compiled = None # type: bool #: True if the model is a structure factor used to model the interaction #: between form factor models. This will default to False if it is not #: provided in the file.