Skip to content

Commit

Permalink
Add flag for whether model is compiled or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Kienzle committed Jun 23, 2023
1 parent 8822358 commit e8935f2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion sasmodels/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
6 changes: 3 additions & 3 deletions sasmodels/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions sasmodels/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions sasmodels/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions sasmodels/modelinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e8935f2

Please sign in to comment.