Index: sasmodels/kernel.py
===================================================================
--- sasmodels/kernel.py	(revision cd289474f05b7a39cccd795f21f84befdf2a8788)
+++ sasmodels/kernel.py	(revision 3199b177cb91c6bd74017b7f942ddbbbc9bb46c0)
@@ -23,4 +23,5 @@
 # pylint: enable=unused-import
 
+
 class KernelModel(object):
     info = None  # type: ModelInfo
@@ -33,4 +34,5 @@
         # type: () -> None
         pass
+
 
 class Kernel(object):
Index: sasmodels/kernelcl.py
===================================================================
--- sasmodels/kernelcl.py	(revision f872fd13522af1df2bf929529d37c828956eb8ef)
+++ sasmodels/kernelcl.py	(revision 3199b177cb91c6bd74017b7f942ddbbbc9bb46c0)
@@ -69,5 +69,5 @@
 import numpy as np  # type: ignore
 
-# Attempt to setup opencl. This may fail if the pyopencl package is not
+# Attempt to setup OpenCL. This may fail if the pyopencl package is not
 # installed or if it is installed but there are no devices available.
 try:
@@ -75,5 +75,5 @@
     from pyopencl import mem_flags as mf
     from pyopencl.characterize import get_fast_inaccurate_build_options
-    # Ask OpenCL for the default context so that we know that one exists
+    # Ask OpenCL for the default context so that we know that one exists.
     cl.create_some_context(interactive=False)
     HAVE_OPENCL = True
@@ -96,5 +96,6 @@
 # pylint: enable=unused-import
 
-# CRUFT: pyopencl < 2017.1  (as of June 2016 needs quotes around include path)
+
+# CRUFT: pyopencl < 2017.1 (as of June 2016 needs quotes around include path).
 def quote_path(v):
     """
@@ -107,4 +108,5 @@
     return '"'+v+'"' if v and ' ' in v and not v[0] in "\"'-" else v
 
+
 def fix_pyopencl_include():
     """
@@ -113,5 +115,8 @@
     import pyopencl as cl
     if hasattr(cl, '_DEFAULT_INCLUDE_OPTIONS'):
-        cl._DEFAULT_INCLUDE_OPTIONS = [quote_path(v) for v in cl._DEFAULT_INCLUDE_OPTIONS]
+        cl._DEFAULT_INCLUDE_OPTIONS = [
+            quote_path(v) for v in cl._DEFAULT_INCLUDE_OPTIONS
+            ]
+
 
 if HAVE_OPENCL:
@@ -126,5 +131,4 @@
 MAX_LOOPS = 2048
 
-
 # Pragmas for enable OpenCL features.  Be sure to protect them so that they
 # still compile even if OpenCL is not present.
@@ -141,8 +145,10 @@
 """
 
+
 def use_opencl():
     sas_opencl = os.environ.get("SAS_OPENCL", "OpenCL").lower()
     return HAVE_OPENCL and sas_opencl != "none" and not sas_opencl.startswith("cuda")
 
+
 ENV = None
 def reset_environment():
@@ -152,4 +158,5 @@
     global ENV
     ENV = GpuEnvironment() if use_opencl() else None
+
 
 def environment():
@@ -169,4 +176,5 @@
     return ENV
 
+
 def has_type(device, dtype):
     # type: (cl.Device, np.dtype) -> bool
@@ -179,6 +187,7 @@
         return "cl_khr_fp64" in device.extensions
     else:
-        # Not supporting F16 type since it isn't accurate enough
+        # Not supporting F16 type since it isn't accurate enough.
         return False
+
 
 def get_warp(kernel, queue):
@@ -190,4 +199,5 @@
         cl.kernel_work_group_info.PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
         queue.device)
+
 
 def compile_model(context, source, dtype, fast=False):
@@ -211,5 +221,5 @@
         source_list.insert(0, _F64_PRAGMA)
 
-    # Note: USE_SINCOS makes the intel cpu slower under opencl
+    # Note: USE_SINCOS makes the Intel CPU slower under OpenCL.
     if context.devices[0].type == cl.device_type.GPU:
         source_list.insert(0, "#define USE_SINCOS\n")
@@ -218,30 +228,18 @@
     source = "\n".join(source_list)
     program = cl.Program(context, source).build(options=options)
+
     #print("done with "+program)
     return program
 
 
-# for now, this returns one device in the context
-# TODO: create a context that contains all devices on all platforms
+# For now, this returns one device in the context.
+# TODO: Create a context that contains all devices on all platforms.
 class GpuEnvironment(object):
     """
-    GPU context, with possibly many devices, and one queue per device.
-
-    Because the environment can be reset during a live program (e.g., if the
-    user changes the active GPU device in the GUI), everything associated
-    with the device context must be cached in the environment and recreated
-    if the environment changes.  The *cache* attribute is a simple dictionary
-    which holds keys and references to objects, such as compiled kernels and
-    allocated buffers.  The running program should check in the cache for
-    long lived objects and create them if they are not there.  The program
-    should not hold onto cached objects, but instead only keep them active
-    for the duration of a function call.  When the environment is destroyed
-    then the *release* method for each active cache item is called before
-    the environment is freed.  This means that each cl buffer should be
-    in its own cache entry.
+    GPU context for OpenCL, with possibly many devices and one queue per device.
     """
     def __init__(self):
         # type: () -> None
-        # find gpu context
+        # Find gpu context.
         context_list = _create_some_context()
 
@@ -257,5 +255,5 @@
                 self.context[dtype] = None
 
-        # Build a queue for each context
+        # Build a queue for each context.
         self.queue = {}
         context = self.context[F32]
@@ -267,11 +265,10 @@
             self.queue[F64] = cl.CommandQueue(context, context.devices[0])
 
-        # Byte boundary for data alignment
+        ## Byte boundary for data alignment.
         #self.data_boundary = max(context.devices[0].min_data_type_align_size
         #                         for context in self.context.values())
 
-        # Cache for compiled programs, and for items in context
+        # Cache for compiled programs, and for items in context.
         self.compiled = {}
-        self.cache = {}
 
     def has_type(self, dtype):
@@ -288,9 +285,8 @@
         """
         # Note: PyOpenCL caches based on md5 hash of source, options and device
-        # so we don't really need to cache things for ourselves.  I'll do so
-        # anyway just to save some data munging time.
+        # but I'll do so as well just to save some data munging time.
         tag = generate.tag_source(source)
         key = "%s-%s-%s%s"%(name, dtype, tag, ("-fast" if fast else ""))
-        # Check timestamp on program
+        # Check timestamp on program.
         program, program_timestamp = self.compiled.get(key, (None, np.inf))
         if program_timestamp < timestamp:
@@ -305,20 +301,4 @@
         return program
 
-    def free_buffer(self, key):
-        if key in self.cache:
-            self.cache[key].release()
-            del self.cache[key]
-
-    def __del__(self):
-        for v in self.cache.values():
-            release = getattr(v, 'release', lambda: None)
-            release()
-        self.cache = {}
-
-_CURRENT_ID = 0
-def unique_id():
-    global _CURRENT_ID
-    _CURRENT_ID += 1
-    return _CURRENT_ID
 
 def _create_some_context():
@@ -333,8 +313,8 @@
     which one (and not a CUDA device, or no GPU).
     """
-    # Assume we do not get here if SAS_OPENCL is None or CUDA
+    # Assume we do not get here if SAS_OPENCL is None or CUDA.
     sas_opencl = os.environ.get('SAS_OPENCL', 'opencl')
     if sas_opencl.lower() != 'opencl':
-        # Setting PYOPENCL_CTX as a SAS_OPENCL to create cl context
+        # Setting PYOPENCL_CTX as a SAS_OPENCL to create cl context.
         os.environ["PYOPENCL_CTX"] = sas_opencl
 
@@ -344,8 +324,10 @@
         except Exception as exc:
             warnings.warn(str(exc))
-            warnings.warn("pyopencl.create_some_context() failed")
-            warnings.warn("the environment variable 'SAS_OPENCL' or 'PYOPENCL_CTX' might not be set correctly")
+            warnings.warn("pyopencl.create_some_context() failed.  The "
+                "environment variable 'SAS_OPENCL' or 'PYOPENCL_CTX' might "
+                "not be set correctly")
 
     return _get_default_context()
+
 
 def _get_default_context():
@@ -360,9 +342,9 @@
     # is running may increase throughput.
     #
-    # Macbook pro, base install:
+    # MacBook Pro, base install:
     #     {'Apple': [Intel CPU, NVIDIA GPU]}
-    # Macbook pro, base install:
+    # MacBook Pro, base install:
     #     {'Apple': [Intel CPU, Intel GPU]}
-    # 2 x nvidia 295 with Intel and NVIDIA opencl drivers installed
+    # 2 x NVIDIA 295 with Intel and NVIDIA opencl drivers install:
     #     {'Intel': [CPU], 'NVIDIA': [GPU, GPU, GPU, GPU]}
     gpu, cpu = None, None
@@ -387,5 +369,5 @@
             else:
                 # System has cl.device_type.ACCELERATOR or cl.device_type.CUSTOM
-                # Intel Phi for example registers as an accelerator
+                # Intel Phi for example registers as an accelerator.
                 # Since the user installed a custom device on their system
                 # and went through the pain of sorting out OpenCL drivers for
@@ -394,5 +376,5 @@
                 gpu = device
 
-    # order the devices by gpu then by cpu; when searching for an available
+    # Order the devices by gpu then by cpu; when searching for an available
     # device by data type they will be checked in this order, which means
     # that if the gpu supports double then the cpu will never be used (though
@@ -421,4 +403,11 @@
     that the compiler is allowed to take shortcuts.
     """
+    info = None  # type: ModelInfo
+    source = ""  # type: str
+    dtype = None  # type: np.dtype
+    fast = False  # type: bool
+    _program = None  # type: cl.Program
+    _kernels = None  # type: Dict[str, cl.Kernel]
+
     def __init__(self, source, model_info, dtype=generate.F32, fast=False):
         # type: (Dict[str,str], ModelInfo, np.dtype, bool) -> None
@@ -427,6 +416,4 @@
         self.dtype = dtype
         self.fast = fast
-        self.timestamp = generate.ocl_timestamp(self.info)
-        self._cache_key = unique_id()
 
     def __getstate__(self):
@@ -437,4 +424,5 @@
         # type: (Tuple[ModelInfo, str, np.dtype, bool]) -> None
         self.info, self.source, self.dtype, self.fast = state
+        self._program = self._kernels = None
 
     def make_kernel(self, q_vectors):
@@ -442,9 +430,5 @@
         return GpuKernel(self, q_vectors)
 
-    @property
-    def Iq(self):
-        return self._fetch_kernel('Iq')
-
-    def fetch_kernel(self, name):
+    def get_function(self, name):
         # type: (str) -> cl.Kernel
         """
@@ -452,25 +436,27 @@
         does not already exist.
         """
-        gpu = environment()
-        key = self._cache_key
-        if key not in gpu.cache:
-            program = gpu.compile_program(
-                self.info.name,
-                self.source['opencl'],
-                self.dtype,
-                self.fast,
-                self.timestamp)
-            variants = ['Iq', 'Iqxy', 'Imagnetic']
-            names = [generate.kernel_name(self.info, k) for k in variants]
-            kernels = [getattr(program, k) for k in names]
-            data = dict((k, v) for k, v in zip(variants, kernels))
-            # keep a handle to program so GC doesn't collect
-            data['program'] = program
-            gpu.cache[key] = data
-        else:
-            data = gpu.cache[key]
-        return data[name]
-
-# TODO: check that we don't need a destructor for buffers which go out of scope
+        if self._program is None:
+            self._prepare_program()
+        return self._kernels[name]
+
+    def _prepare_program(self):
+        # type: (str) -> None
+        env = environment()
+        timestamp = generate.ocl_timestamp(self.info)
+        program = env.compile_program(
+            self.info.name,
+            self.source['opencl'],
+            self.dtype,
+            self.fast,
+            timestamp)
+        variants = ['Iq', 'Iqxy', 'Imagnetic']
+        names = [generate.kernel_name(self.info, k) for k in variants]
+        functions = [getattr(program, k) for k in names]
+        self._kernels = {k: v for k, v in zip(variants, functions)}
+        # Keep a handle to program so GC doesn't collect.
+        self._program = program
+
+
+# TODO: Check that we don't need a destructor for buffers which go out of scope.
 class GpuInput(object):
     """
@@ -494,10 +480,10 @@
     def __init__(self, q_vectors, dtype=generate.F32):
         # type: (List[np.ndarray], np.dtype) -> None
-        # TODO: do we ever need double precision q?
+        # TODO: Do we ever need double precision q?
         self.nq = q_vectors[0].size
         self.dtype = np.dtype(dtype)
         self.is_2d = (len(q_vectors) == 2)
-        # TODO: stretch input based on get_warp()
-        # not doing it now since warp depends on kernel, which is not known
+        # TODO: Stretch input based on get_warp().
+        # Not doing it now since warp depends on kernel, which is not known
         # at this point, so instead using 32, which is good on the set of
         # architectures tested so far.
@@ -512,25 +498,20 @@
             self.q[:self.nq] = q_vectors[0]
         self.global_size = [self.q.shape[0]]
-        self._cache_key = unique_id()
-
-    @property
-    def q_b(self):
-        """Lazy creation of q buffer so it can survive context reset"""
+        #print("creating inputs of size", self.global_size)
+
+        # Transfer input value to GPU.
         env = environment()
-        key = self._cache_key
-        if key not in env.cache:
-            context = env.context[self.dtype]
-            #print("creating inputs of size", self.global_size)
-            buffer = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR,
-                               hostbuf=self.q)
-            env.cache[key] = buffer
-        return env.cache[key]
+        context = env.context[self.dtype]
+        self.q_b = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR,
+                             hostbuf=self.q)
 
     def release(self):
         # type: () -> None
         """
-        Free the buffer associated with the q value
-        """
-        environment().free_buffer(id(self))
+        Free the buffer associated with the q value.
+        """
+        if self.q_b is not None:
+            self.q_b.release()
+            self.q_b = None
 
     def __del__(self):
@@ -538,4 +519,5 @@
         self.release()
 
+
 class GpuKernel(Kernel):
     """
@@ -544,62 +526,54 @@
     *model* is the GpuModel object to call
 
-    The following attributes are defined:
-
-    *info* is the module information
-
-    *dtype* is the kernel precision
-
-    *dim* is '1d' or '2d'
-
-    *result* is a vector to contain the results of the call
-
-    The resulting call method takes the *pars*, a list of values for
-    the fixed parameters to the kernel, and *pd_pars*, a list of (value,weight)
-    vectors for the polydisperse parameters.  *cutoff* determines the
-    integration limits: any points with combined weight less than *cutoff*
-    will not be calculated.
+    The kernel is derived from :class:`Kernel`, providing the
+    :meth:`call_kernel` method to evaluate the kernel for a given set of
+    parameters.  Because of the need to move the q values to the GPU before
+    evaluation, the kernel is instantiated for a particular set of q vectors,
+    and can be called many times without transfering q each time.
 
     Call :meth:`release` when done with the kernel instance.
     """
+    #: SAS model information structure.
+    info = None  # type: ModelInfo
+    #: Kernel precision.
+    dtype = None  # type: np.dtype
+    #: Kernel dimensions (1d or 2d).
+    dim = ""  # type: str
+    #: Calculation results, updated after each call to :meth:`_call_kernel`.
+    result = None  # type: np.ndarray
+
     def __init__(self, model, q_vectors):
-        # type: (cl.Kernel, np.dtype, ModelInfo, List[np.ndarray]) -> None
+        # type: (GpuModel, List[np.ndarray]) -> None
         dtype = model.dtype
         self.q_input = GpuInput(q_vectors, dtype)
         self._model = model
-        # F16 isn't sufficient, so don't support it
-        self._as_dtype = np.float64 if dtype == generate.F64 else np.float32
-        self._cache_key = unique_id()
-
-        # attributes accessed from the outside
+
+        # Attributes accessed from the outside.
         self.dim = '2d' if self.q_input.is_2d else '1d'
         self.info = model.info
-        self.dtype = model.dtype
-
-        # holding place for the returned value
+        self.dtype = dtype
+
+        # Converter to translate input to target type.
+        self._as_dtype = np.float64 if dtype == generate.F64 else np.float32
+
+        # Holding place for the returned value.
         nout = 2 if self.info.have_Fq and self.dim == '1d' else 1
-        extra_q = 4  # total weight, form volume, shell volume and R_eff
-        self.result = np.empty(self.q_input.nq*nout+extra_q, dtype)
-
-    @property
-    def _result_b(self):
-        """Lazy creation of result buffer so it can survive context reset"""
+        extra_q = 4  # Total weight, form volume, shell volume and R_eff.
+        self.result = np.empty(self.q_input.nq*nout + extra_q, dtype)
+
+        # Allocate result value on GPU.
         env = environment()
-        key = self._cache_key
-        if key not in env.cache:
-            context = env.context[self.dtype]
-            width = ((self.result.size+31)//32)*32 * self.dtype.itemsize
-            buffer = cl.Buffer(context, mf.READ_WRITE, width)
-            env.cache[key] = buffer
-        return env.cache[key]
-
-    def _call_kernel(self, call_details, values, cutoff, magnetic, effective_radius_type):
-        # type: (CallDetails, np.ndarray, np.ndarray, float, bool) -> np.ndarray
+        context = env.context[self.dtype]
+        width = ((self.result.size+31)//32)*32 * self.dtype.itemsize
+        self._result_b = cl.Buffer(context, mf.READ_WRITE, width)
+
+    def _call_kernel(self, call_details, values, cutoff, magnetic,
+                     effective_radius_type):
+        # type: (CallDetails, np.ndarray, float, bool, int) -> np.ndarray
         env = environment()
         queue = env.queue[self._model.dtype]
         context = queue.context
 
-        # Arrange data transfer to/from card
-        q_b = self.q_input.q_b
-        result_b = self._result_b
+        # Arrange data transfer to card.
         details_b = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR,
                               hostbuf=call_details.buffer)
@@ -607,15 +581,22 @@
                              hostbuf=values)
 
+        # Setup kernel function and arguments.
         name = 'Iq' if self.dim == '1d' else 'Imagnetic' if magnetic else 'Iqxy'
-        kernel = self._model.fetch_kernel(name)
+        kernel = self._model.get_function(name)
         kernel_args = [
-            np.uint32(self.q_input.nq), None, None,
-            details_b, values_b, q_b, result_b,
-            self._as_dtype(cutoff),
-            np.uint32(effective_radius_type),
+            np.uint32(self.q_input.nq),  # Number of inputs.
+            None,  # Placeholder for pd_start.
+            None,  # Placeholder for pd_stop.
+            details_b,  # Problem definition.
+            values_b,  # Parameter values.
+            self.q_input.q_b,  # Q values.
+            self._result_b,   # Result storage.
+            self._as_dtype(cutoff),  # Probability cutoff.
+            np.uint32(effective_radius_type),  # R_eff mode.
         ]
+
+        # Call kernel and retrieve results.
         #print("Calling OpenCL")
         #call_details.show(values)
-        #Call kernel and retrieve results
         wait_for = None
         last_nap = clock()
@@ -628,5 +609,5 @@
                                *kernel_args, wait_for=wait_for)]
             if stop < call_details.num_eval:
-                # Allow other processes to run
+                # Allow other processes to run.
                 wait_for[0].wait()
                 current_time = clock()
@@ -634,11 +615,10 @@
                     time.sleep(0.001)
                     last_nap = current_time
-        cl.enqueue_copy(queue, self.result, result_b, wait_for=wait_for)
+        cl.enqueue_copy(queue, self.result, self._result_b, wait_for=wait_for)
         #print("result", self.result)
 
-        # Free buffers
-        for v in (details_b, values_b):
-            if v is not None:
-                v.release()
+        # Free buffers.
+        details_b.release()
+        values_b.release()
 
     def release(self):
@@ -647,6 +627,8 @@
         Release resources associated with the kernel.
         """
-        environment().free_buffer(id(self))
         self.q_input.release()
+        if self._result_b is not None:
+            self._result_b.release()
+            self._result_b = None
 
     def __del__(self):
Index: sasmodels/kernelcuda.py
===================================================================
--- sasmodels/kernelcuda.py	(revision f872fd13522af1df2bf929529d37c828956eb8ef)
+++ sasmodels/kernelcuda.py	(revision fa26e7834aa4332ef6ffc8eaf2a64c9ea1b51a64)
@@ -63,9 +63,10 @@
 import time
 import re
+import atexit
 
 import numpy as np  # type: ignore
 
 
-# Attempt to setup cuda. This may fail if the pycuda package is not
+# Attempt to setup CUDA. This may fail if the pycuda package is not
 # installed or if it is installed but there are no devices available.
 try:
@@ -107,7 +108,9 @@
 MAX_LOOPS = 2048
 
+
 def use_cuda():
-    env = os.environ.get("SAS_OPENCL", "").lower()
-    return HAVE_CUDA and (env == "" or env.startswith("cuda"))
+    sas_opencl = os.environ.get("SAS_OPENCL", "CUDA").lower()
+    return HAVE_CUDA and sas_opencl.startswith("cuda")
+
 
 ENV = None
@@ -121,4 +124,5 @@
         ENV.release()
     ENV = GpuEnvironment() if use_cuda() else None
+
 
 def environment():
@@ -138,4 +142,9 @@
     return ENV
 
+
+# PyTest is not freeing ENV, so make sure it gets freed.
+atexit.register(lambda: ENV.release() if ENV is not None else None)
+
+
 def has_type(dtype):
     # type: (np.dtype) -> bool
@@ -143,13 +152,13 @@
     Return true if device supports the requested precision.
     """
-    # Assume the nvidia card supports 32-bit and 64-bit floats.
-    # TODO: check if pycuda support F16
+    # Assume the NVIDIA card supports 32-bit and 64-bit floats.
+    # TODO: Check if pycuda support F16.
     return dtype in (generate.F32, generate.F64)
 
 
 FUNCTION_PATTERN = re.compile(r"""^
-  (?P<space>\s*)                   # initial space
-  (?P<qualifiers>^(?:\s*\b\w+\b\s*)+) # one or more qualifiers before function
-  (?P<function>\s*\b\w+\b\s*[(])      # function name plus open parens
+  (?P<space>\s*)                       # Initial space.
+  (?P<qualifiers>^(?:\s*\b\w+\b\s*)+)  # One or more qualifiers before function.
+  (?P<function>\s*\b\w+\b\s*[(])       # Function name plus open parens.
   """, re.VERBOSE|re.MULTILINE)
 
@@ -158,7 +167,8 @@
   """, re.VERBOSE|re.MULTILINE)
 
+
 def _add_device_tag(match):
     # type: (None) -> str
-    # Note: should be re.Match, but that isn't a simple type
+    # Note: Should be re.Match, but that isn't a simple type.
     """
     replace qualifiers with __device__ qualifiers if needed
@@ -173,4 +183,5 @@
         return "".join((space, "__device__ ", qualifiers, function))
 
+
 def mark_device_functions(source):
     # type: (str) -> str
@@ -179,4 +190,5 @@
     """
     return FUNCTION_PATTERN.sub(_add_device_tag, source)
+
 
 def show_device_functions(source):
@@ -188,4 +200,5 @@
         print(match.group('qualifiers').replace('\n',r'\n'), match.group('function'), '(')
     return source
+
 
 def compile_model(source, dtype, fast=False):
@@ -212,5 +225,5 @@
     #options = ['--verbose', '-E']
     options = ['--use_fast_math'] if fast else None
-    program = SourceModule(source, no_extern_c=True, options=options) # include_dirs=[...]
+    program = SourceModule(source, no_extern_c=True, options=options) #, include_dirs=[...])
 
     #print("done with "+program)
@@ -218,20 +231,17 @@
 
 
-# for now, this returns one device in the context
-# TODO: create a context that contains all devices on all platforms
+# For now, this returns one device in the context.
+# TODO: Create a context that contains all devices on all platforms.
 class GpuEnvironment(object):
     """
-    GPU context, with possibly many devices, and one queue per device.
+    GPU context for CUDA.
     """
     context = None # type: cuda.Context
     def __init__(self, devnum=None):
         # type: (int) -> None
-        # Byte boundary for data alignment
-        #self.data_boundary = max(d.min_data_type_align_size
-        #                         for d in self.context.devices)
-        self.compiled = {}
         env = os.environ.get("SAS_OPENCL", "").lower()
         if devnum is None and env.startswith("cuda:"):
             devnum = int(env[5:])
+
         # Set the global context to the particular device number if one is
         # given, otherwise use the default context.  Perhaps this will be set
@@ -242,4 +252,11 @@
             self.context = make_default_context()
 
+        ## Byte boundary for data alignment.
+        #self.data_boundary = max(d.min_data_type_align_size
+        #                         for d in self.context.devices)
+
+        # Cache for compiled programs, and for items in context.
+        self.compiled = {}
+
     def release(self):
         if self.context is not None:
@@ -262,10 +279,9 @@
         Compile the program for the device in the given context.
         """
-        # Note: PyOpenCL caches based on md5 hash of source, options and device
-        # so we don't really need to cache things for ourselves.  I'll do so
-        # anyway just to save some data munging time.
+        # Note: PyCuda (probably) caches but I'll do so as well just to
+        # save some data munging time.
         tag = generate.tag_source(source)
         key = "%s-%s-%s%s"%(name, dtype, tag, ("-fast" if fast else ""))
-        # Check timestamp on program
+        # Check timestamp on program.
         program, program_timestamp = self.compiled.get(key, (None, np.inf))
         if program_timestamp < timestamp:
@@ -277,4 +293,5 @@
         return program
 
+
 class GpuModel(KernelModel):
     """
@@ -292,10 +309,10 @@
     that the compiler is allowed to take shortcuts.
     """
-    info = None # type: ModelInfo
-    source = "" # type: str
-    dtype = None # type: np.dtype
-    fast = False # type: bool
-    program = None # type: SourceModule
-    _kernels = None # type: List[cuda.Function]
+    info = None  # type: ModelInfo
+    source = ""  # type: str
+    dtype = None  # type: np.dtype
+    fast = False  # type: bool
+    _program = None  # type: SourceModule
+    _kernels = None  # type: Dict[str, cuda.Function]
 
     def __init__(self, source, model_info, dtype=generate.F32, fast=False):
@@ -305,6 +322,4 @@
         self.dtype = dtype
         self.fast = fast
-        self.program = None # delay program creation
-        self._kernels = None
 
     def __getstate__(self):
@@ -315,41 +330,39 @@
         # type: (Tuple[ModelInfo, str, np.dtype, bool]) -> None
         self.info, self.source, self.dtype, self.fast = state
-        self.program = None
+        self._program = self._kernels = None
 
     def make_kernel(self, q_vectors):
         # type: (List[np.ndarray]) -> "GpuKernel"
-        if self.program is None:
-            compile_program = environment().compile_program
-            timestamp = generate.ocl_timestamp(self.info)
-            self.program = compile_program(
-                self.info.name,
-                self.source['opencl'],
-                self.dtype,
-                self.fast,
-                timestamp)
-            variants = ['Iq', 'Iqxy', 'Imagnetic']
-            names = [generate.kernel_name(self.info, k) for k in variants]
-            kernels = [self.program.get_function(k) for k in names]
-            self._kernels = dict((k, v) for k, v in zip(variants, kernels))
-        is_2d = len(q_vectors) == 2
-        if is_2d:
-            kernel = [self._kernels['Iqxy'], self._kernels['Imagnetic']]
-        else:
-            kernel = [self._kernels['Iq']]*2
-        return GpuKernel(kernel, self.dtype, self.info, q_vectors)
-
-    def release(self):
-        # type: () -> None
-        """
-        Free the resources associated with the model.
-        """
-        if self.program is not None:
-            self.program = None
-
-    def __del__(self):
-        # type: () -> None
-        self.release()
-
-# TODO: check that we don't need a destructor for buffers which go out of scope
+        return GpuKernel(self, q_vectors)
+
+    def get_function(self, name):
+        # type: (str) -> cuda.Function
+        """
+        Fetch the kernel from the environment by name, compiling it if it
+        does not already exist.
+        """
+        if self._program is None:
+            self._prepare_program()
+        return self._kernels[name]
+
+    def _prepare_program(self):
+        # type: (str) -> None
+        env = environment()
+        timestamp = generate.ocl_timestamp(self.info)
+        program = env.compile_program(
+            self.info.name,
+            self.source['opencl'],
+            self.dtype,
+            self.fast,
+            timestamp)
+        variants = ['Iq', 'Iqxy', 'Imagnetic']
+        names = [generate.kernel_name(self.info, k) for k in variants]
+        functions = [program.get_function(k) for k in names]
+        self._kernels = {k: v for k, v in zip(variants, functions)}
+        # Keep a handle to program so GC doesn't collect.
+        self._program = program
+
+
+# TODO: Check that we don't need a destructor for buffers which go out of scope.
 class GpuInput(object):
     """
@@ -373,25 +386,25 @@
     def __init__(self, q_vectors, dtype=generate.F32):
         # type: (List[np.ndarray], np.dtype) -> None
-        # TODO: do we ever need double precision q?
+        # TODO: Do we ever need double precision q?
         self.nq = q_vectors[0].size
         self.dtype = np.dtype(dtype)
         self.is_2d = (len(q_vectors) == 2)
-        # TODO: stretch input based on get_warp()
-        # not doing it now since warp depends on kernel, which is not known
+        # TODO: Stretch input based on get_warp().
+        # Not doing it now since warp depends on kernel, which is not known
         # at this point, so instead using 32, which is good on the set of
         # architectures tested so far.
         if self.is_2d:
-            # Note: 16 rather than 15 because result is 1 longer than input.
-            width = ((self.nq+16)//16)*16
+            width = ((self.nq+15)//16)*16
             self.q = np.empty((width, 2), dtype=dtype)
             self.q[:self.nq, 0] = q_vectors[0]
             self.q[:self.nq, 1] = q_vectors[1]
         else:
-            # Note: 32 rather than 31 because result is 1 longer than input.
-            width = ((self.nq+32)//32)*32
+            width = ((self.nq+31)//32)*32
             self.q = np.empty(width, dtype=dtype)
             self.q[:self.nq] = q_vectors[0]
         self.global_size = [self.q.shape[0]]
         #print("creating inputs of size", self.global_size)
+
+        # Transfer input value to GPU.
         self.q_b = cuda.to_device(self.q)
 
@@ -399,5 +412,5 @@
         # type: () -> None
         """
-        Free the memory.
+        Free the buffer associated with the q value.
         """
         if self.q_b is not None:
@@ -409,64 +422,78 @@
         self.release()
 
+
 class GpuKernel(Kernel):
     """
     Callable SAS kernel.
 
-    *kernel* is the GpuKernel object to call
-
-    *model_info* is the module information
-
-    *q_vectors* is the q vectors at which the kernel should be evaluated
-
-    *dtype* is the kernel precision
-
-    The resulting call method takes the *pars*, a list of values for
-    the fixed parameters to the kernel, and *pd_pars*, a list of (value,weight)
-    vectors for the polydisperse parameters.  *cutoff* determines the
-    integration limits: any points with combined weight less than *cutoff*
-    will not be calculated.
+    *model* is the GpuModel object to call
+
+    The kernel is derived from :class:`Kernel`, providing the
+    :meth:`call_kernel` method to evaluate the kernel for a given set of
+    parameters.  Because of the need to move the q values to the GPU before
+    evaluation, the kernel is instantiated for a particular set of q vectors,
+    and can be called many times without transfering q each time.
 
     Call :meth:`release` when done with the kernel instance.
     """
-    def __init__(self, kernel, dtype, model_info, q_vectors):
-        # type: (cl.Kernel, np.dtype, ModelInfo, List[np.ndarray]) -> None
+    #: SAS model information structure.
+    info = None  # type: ModelInfo
+    #: Kernel precision.
+    dtype = None  # type: np.dtype
+    #: Kernel dimensions (1d or 2d).
+    dim = ""  # type: str
+    #: Calculation results, updated after each call to :meth:`_call_kernel`.
+    result = None  # type: np.ndarray
+
+    def __init__(self, model, q_vectors):
+        # type: (GpuModel, List[np.ndarray]) -> None
+        dtype = model.dtype
         self.q_input = GpuInput(q_vectors, dtype)
-        self.kernel = kernel
-        # F16 isn't sufficient, so don't support it
+        self._model = model
+
+        # Attributes accessed from the outside.
+        self.dim = '2d' if self.q_input.is_2d else '1d'
+        self.info = model.info
+        self.dtype = dtype
+
+        # Converter to translate input to target type.
         self._as_dtype = np.float64 if dtype == generate.F64 else np.float32
 
-        # attributes accessed from the outside
-        self.dim = '2d' if self.q_input.is_2d else '1d'
-        self.info = model_info
-        self.dtype = dtype
-
-        # holding place for the returned value
+        # Holding place for the returned value.
         nout = 2 if self.info.have_Fq and self.dim == '1d' else 1
-        extra_q = 4  # total weight, form volume, shell volume and R_eff
-        self.result = np.empty(self.q_input.nq*nout+extra_q, dtype)
-
-        # Inputs and outputs for each kernel call
-        # Note: res may be shorter than res_b if global_size != nq
+        extra_q = 4  # Total weight, form volume, shell volume and R_eff.
+        self.result = np.empty(self.q_input.nq*nout + extra_q, dtype)
+
+        # Allocate result value on GPU.
         width = ((self.result.size+31)//32)*32 * self.dtype.itemsize
-        self.result_b = cuda.mem_alloc(width)
-        self._need_release = [self.result_b]
-
-    def _call_kernel(self, call_details, values, cutoff, magnetic, effective_radius_type):
-        # type: (CallDetails, np.ndarray, np.ndarray, float, bool) -> np.ndarray
-        # Arrange data transfer to card
+        self._result_b = cuda.mem_alloc(width)
+
+    def _call_kernel(self, call_details, values, cutoff, magnetic,
+                     effective_radius_type):
+        # type: (CallDetails, np.ndarray, float, bool, int) -> np.ndarray
+
+        # Arrange data transfer to card.
         details_b = cuda.to_device(call_details.buffer)
         values_b = cuda.to_device(values)
 
-        kernel = self.kernel[1 if magnetic else 0]
-        args = [
-            np.uint32(self.q_input.nq), None, None,
-            details_b, values_b, self.q_input.q_b, self.result_b,
-            self._as_dtype(cutoff),
-            np.uint32(effective_radius_type),
+        # Setup kernel function and arguments.
+        name = 'Iq' if self.dim == '1d' else 'Imagnetic' if magnetic else 'Iqxy'
+        kernel = self._model.get_function(name)
+        kernel_args = [
+            np.uint32(self.q_input.nq),  # Number of inputs.
+            None,  # Placeholder for pd_start.
+            None,  # Placeholder for pd_stop.
+            details_b,  # Problem definition.
+            values_b,  # Parameter values.
+            self.q_input.q_b,  # Q values.
+            self._result_b,   # Result storage.
+            self._as_dtype(cutoff),  # Probability cutoff.
+            np.uint32(effective_radius_type),  # R_eff mode.
         ]
         grid = partition(self.q_input.nq)
-        #print("Calling OpenCL")
+
+        # Call kernel and retrieve results.
+        #print("Calling CUDA")
         #call_details.show(values)
-        # Call kernel and retrieve results
         last_nap = time.clock()
         step = 100000000//self.q_input.nq + 1
@@ -475,9 +502,9 @@
             stop = min(start + step, call_details.num_eval)
             #print("queuing",start,stop)
-            args[1:3] = [np.int32(start), np.int32(stop)]
-            kernel(*args, **grid)
+            kernel_args[1:3] = [np.int32(start), np.int32(stop)]
+            kernel(*kernel_args, **grid)
             if stop < call_details.num_eval:
                 sync()
-                # Allow other processes to run
+                # Allow other processes to run.
                 current_time = time.clock()
                 if current_time - last_nap > 0.5:
@@ -485,5 +512,5 @@
                     last_nap = current_time
         sync()
-        cuda.memcpy_dtoh(self.result, self.result_b)
+        cuda.memcpy_dtoh(self.result, self._result_b)
         #print("result", self.result)
 
@@ -496,7 +523,8 @@
         Release resources associated with the kernel.
         """
-        for p in self._need_release:
-            p.free()
-        self._need_release = []
+        self.q_input.release()
+        if self._result_b is not None:
+            self._result_b.free()
+            self._result_b = None
 
     def __del__(self):
@@ -512,6 +540,5 @@
     Note: Maybe context.synchronize() is sufficient.
     """
-    #return # The following works in C++; don't know what pycuda is doing
-    # Create an event with which to synchronize
+    # Create an event with which to synchronize.
     done = cuda.Event()
 
@@ -519,5 +546,5 @@
     done.record()
 
-    #line added to not hog resources
+    # Make sure we don't hog resource while waiting to sync.
     while not done.query():
         time.sleep(0.01)
@@ -525,4 +552,5 @@
     # Block until the GPU executes the kernel.
     done.synchronize()
+
     # Clean up the event; I don't think they can be reused.
     del done
Index: sasmodels/kerneldll.py
===================================================================
--- sasmodels/kerneldll.py	(revision e44432d7890080c5b9a440696903718a52c60eba)
+++ sasmodels/kerneldll.py	(revision 3199b177cb91c6bd74017b7f942ddbbbc9bb46c0)
@@ -100,5 +100,5 @@
 # pylint: enable=unused-import
 
-# Compiler output is a byte stream that needs to be decode in python 3
+# Compiler output is a byte stream that needs to be decode in python 3.
 decode = (lambda s: s) if sys.version_info[0] < 3 else (lambda s: s.decode('utf8'))
 
@@ -115,7 +115,8 @@
         COMPILER = "tinycc"
     elif "VCINSTALLDIR" in os.environ:
-        # If vcvarsall.bat has been called, then VCINSTALLDIR is in the environment
-        # and we can use the MSVC compiler.  Otherwise, if tinycc is available
-        # the use it.  Otherwise, hope that mingw is available.
+        # If vcvarsall.bat has been called, then VCINSTALLDIR is in the
+        # environment and we can use the MSVC compiler.  Otherwise, if
+        # tinycc is available then use it.  Otherwise, hope that mingw
+        # is available.
         COMPILER = "msvc"
     else:
@@ -124,13 +125,13 @@
     COMPILER = "unix"
 
-ARCH = "" if ct.sizeof(ct.c_void_p) > 4 else "x86"  # 4 byte pointers on x86
+ARCH = "" if ct.sizeof(ct.c_void_p) > 4 else "x86"  # 4 byte pointers on x86.
 if COMPILER == "unix":
-    # Generic unix compile
-    # On mac users will need the X code command line tools installed
+    # Generic unix compile.
+    # On Mac users will need the X code command line tools installed.
     #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp"
     CC = "cc -shared -fPIC -std=c99 -O2 -Wall".split()
-    # add openmp support if not running on a mac
+    # Add OpenMP support if not running on a Mac.
     if sys.platform != "darwin":
-        # OpenMP seems to be broken on gcc 5.4.0 (ubuntu 16.04.9)
+        # OpenMP seems to be broken on gcc 5.4.0 (ubuntu 16.04.9).
         # Shut it off for all unix until we can investigate.
         #CC.append("-fopenmp")
@@ -144,8 +145,8 @@
     # vcomp90.dll on the path.  One may be found here:
     #       C:/Windows/winsxs/x86_microsoft.vc90.openmp*/vcomp90.dll
-    # Copy this to the python directory and uncomment the OpenMP COMPILE
-    # TODO: remove intermediate OBJ file created in the directory
-    # TODO: maybe don't use randomized name for the c file
-    # TODO: maybe ask distutils to find MSVC
+    # Copy this to the python directory and uncomment the OpenMP COMPILE.
+    # TODO: Remove intermediate OBJ file created in the directory.
+    # TODO: Maybe don't use randomized name for the c file.
+    # TODO: Maybe ask distutils to find MSVC.
     CC = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG".split()
     if "SAS_OPENMP" in os.environ:
@@ -172,4 +173,5 @@
 ALLOW_SINGLE_PRECISION_DLLS = True
 
+
 def compile(source, output):
     # type: (str, str) -> None
@@ -183,5 +185,5 @@
     logging.info(command_str)
     try:
-        # need shell=True on windows to keep console box from popping up
+        # Need shell=True on windows to keep console box from popping up.
         shell = (os.name == 'nt')
         subprocess.check_output(command, shell=shell, stderr=subprocess.STDOUT)
@@ -192,4 +194,5 @@
         raise RuntimeError("compile failed.  File is in %r"%source)
 
+
 def dll_name(model_info, dtype):
     # type: (ModelInfo, np.dtype) ->  str
@@ -202,5 +205,5 @@
     basename += ARCH + ".so"
 
-    # Hack to find precompiled dlls
+    # Hack to find precompiled dlls.
     path = joinpath(generate.DATA_PATH, '..', 'compiled_models', basename)
     if os.path.exists(path):
@@ -242,6 +245,6 @@
         raise ValueError("16 bit floats not supported")
     if dtype == F32 and not ALLOW_SINGLE_PRECISION_DLLS:
-        dtype = F64  # Force 64-bit dll
-    # Note: dtype may be F128 for long double precision
+        dtype = F64  # Force 64-bit dll.
+    # Note: dtype may be F128 for long double precision.
 
     dll = dll_path(model_info, dtype)
@@ -254,5 +257,5 @@
         need_recompile = dll_time < newest_source
     if need_recompile:
-        # Make sure the DLL path exists
+        # Make sure the DLL path exists.
         if not os.path.exists(SAS_DLL_PATH):
             os.makedirs(SAS_DLL_PATH)
@@ -263,6 +266,6 @@
             file_handle.write(source)
         compile(source=filename, output=dll)
-        # comment the following to keep the generated c file
-        # Note: if there is a syntax error then compile raises an error
+        # Comment the following to keep the generated C file.
+        # Note: If there is a syntax error then compile raises an error
         # and the source file will not be deleted.
         os.unlink(filename)
@@ -303,5 +306,5 @@
         self.dllpath = dllpath
         self._dll = None  # type: ct.CDLL
-        self._kernels = None # type: List[Callable, Callable]
+        self._kernels = None  # type: List[Callable, Callable]
         self.dtype = np.dtype(dtype)
 
@@ -338,5 +341,5 @@
         # type: (List[np.ndarray]) -> DllKernel
         q_input = PyInput(q_vectors, self.dtype)
-        # Note: pickle not supported for DllKernel
+        # Note: DLL is lazy loaded.
         if self._dll is None:
             self._load_dll()
@@ -358,4 +361,5 @@
         self._dll = None
 
+
 class DllKernel(Kernel):
     """
@@ -379,44 +383,62 @@
     def __init__(self, kernel, model_info, q_input):
         # type: (Callable[[], np.ndarray], ModelInfo, PyInput) -> None
-        #,model_info,q_input)
+        dtype = q_input.dtype
+        self.q_input = q_input
         self.kernel = kernel
+
+        # Attributes accessed from the outside.
+        self.dim = '2d' if q_input.is_2d else '1d'
         self.info = model_info
-        self.q_input = q_input
-        self.dtype = q_input.dtype
-        self.dim = '2d' if q_input.is_2d else '1d'
-        # leave room for f1/f2 results in case we need to compute beta for 1d models
+        self.dtype = dtype
+
+        # Converter to translate input to target type.
+        self._as_dtype = (np.float32 if dtype == generate.F32
+                          else np.float64 if dtype == generate.F64
+                          else np.float128)
+
+        # Holding place for the returned value.
         nout = 2 if self.info.have_Fq else 1
-        # +4 for total weight, shell volume, effective radius, form volume
-        self.result = np.empty(q_input.nq*nout + 4, self.dtype)
-        self.real = (np.float32 if self.q_input.dtype == generate.F32
-                     else np.float64 if self.q_input.dtype == generate.F64
-                     else np.float128)
-
-    def _call_kernel(self, call_details, values, cutoff, magnetic, effective_radius_type):
-        # type: (CallDetails, np.ndarray, np.ndarray, float, bool, int) -> np.ndarray
+        extra_q = 4  # Total weight, form volume, shell volume and R_eff.
+        self.result = np.empty(self.q_input.nq*nout + extra_q, dtype)
+
+    def _call_kernel(self, call_details, values, cutoff, magnetic,
+                     effective_radius_type):
+        # type: (CallDetails, np.ndarray, float, bool, int) -> np.ndarray
+
+        # Setup kernel function and arguments.
         kernel = self.kernel[1 if magnetic else 0]
-        args = [
-            self.q_input.nq, # nq
-            None, # pd_start
-            None, # pd_stop pd_stride[MAX_PD]
-            call_details.buffer.ctypes.data, # problem
-            values.ctypes.data,  # pars
-            self.q_input.q.ctypes.data, # q
-            self.result.ctypes.data,   # results
-            self.real(cutoff), # cutoff
-            effective_radius_type, # cutoff
+        kernel_args = [
+            self.q_input.nq,  # Number of inputs.
+            None,  # Placeholder for pd_start.
+            None,  # Placeholder for pd_stop.
+            call_details.buffer.ctypes.data,  # Problem definition.
+            values.ctypes.data,  # Parameter values.
+            self.q_input.q.ctypes.data,  # Q values.
+            self.result.ctypes.data,   # Result storage.
+            self._as_dtype(cutoff),  # Probability cutoff.
+            effective_radius_type,  # R_eff mode.
         ]
+
+        # Call kernel and retrieve results.
         #print("Calling DLL")
         #call_details.show(values)
         step = 100
+        # TODO: Do we need the explicit sleep like the OpenCL and CUDA loops?
         for start in range(0, call_details.num_eval, step):
             stop = min(start + step, call_details.num_eval)
-            args[1:3] = [start, stop]
-            kernel(*args) # type: ignore
+            kernel_args[1:3] = [start, stop]
+            kernel(*kernel_args) # type: ignore
 
     def release(self):
         # type: () -> None
         """
-        Release any resources associated with the kernel.
+        Release resources associated with the kernel.
         """
-        self.q_input.release()
+        # TODO: OpenCL/CUDA allocate q_input in __init__ and free it in release.
+        # Should we be doing the same for DLL?
+        #self.q_input.release()
+        pass
+
+    def __del__(self):
+        # type: () -> None
+        self.release()
Index: sasmodels/kernelpy.py
===================================================================
--- sasmodels/kernelpy.py	(revision e44432d7890080c5b9a440696903718a52c60eba)
+++ sasmodels/kernelpy.py	(revision 3199b177cb91c6bd74017b7f942ddbbbc9bb46c0)
@@ -33,4 +33,5 @@
 logger = logging.getLogger(__name__)
 
+
 class PyModel(KernelModel):
     """
@@ -38,5 +39,5 @@
     """
     def __init__(self, model_info):
-        # Make sure Iq is available and vectorized
+        # Make sure Iq is available and vectorized.
         _create_default_functions(model_info)
         self.info = model_info
@@ -53,4 +54,5 @@
         """
         pass
+
 
 class PyInput(object):
@@ -91,4 +93,5 @@
         self.q = None
 
+
 class PyKernel(Kernel):
     """
@@ -131,5 +134,5 @@
         parameter_vector = np.empty(len(partable.call_parameters)-2, 'd')
 
-        # Create views into the array to hold the arguments
+        # Create views into the array to hold the arguments.
         offset = 0
         kernel_args, volume_args = [], []
@@ -174,6 +177,4 @@
                         else (lambda mode: 1.0))
 
-
-
     def _call_kernel(self, call_details, values, cutoff, magnetic, effective_radius_type):
         # type: (CallDetails, np.ndarray, np.ndarray, float, bool) -> np.ndarray
@@ -195,4 +196,5 @@
         self.q_input.release()
         self.q_input = None
+
 
 def _loops(parameters,    # type: np.ndarray
@@ -254,5 +256,5 @@
         total = np.zeros(nq, 'd')
         for loop_index in range(call_details.num_eval):
-            # update polydispersity parameter values
+            # Update polydispersity parameter values.
             if p0_index == p0_length:
                 pd_index = (loop_index//pd_stride)%pd_length
@@ -265,5 +267,5 @@
             p0_index += 1
             if weight > cutoff:
-                # Call the scattering function
+                # Call the scattering function.
                 # Assume that NaNs are only generated if the parameters are bad;
                 # exclude all q for that NaN.  Even better would be to have an
@@ -273,5 +275,5 @@
                     continue
 
-                # update value and norm
+                # Update value and norm.
                 total += weight * Iq
                 weight_norm += weight
@@ -293,5 +295,5 @@
     any functions that are not already marked as vectorized.
     """
-    # Note: must call create_vector_Iq before create_vector_Iqxy
+    # Note: Must call create_vector_Iq before create_vector_Iqxy.
     _create_vector_Iq(model_info)
     _create_vector_Iqxy(model_info)
Index: sasmodels/model_test.py
===================================================================
--- sasmodels/model_test.py	(revision 5024a563075fb86e48bd868acf2f39ea4af2e90b)
+++ sasmodels/model_test.py	(revision 00afc158f4641f755fc8518e7528d23f8655d30b)
@@ -167,5 +167,5 @@
         # test using cuda if desired and available
         if 'cuda' in loaders and use_cuda():
-            test_name = "%s-cuda"%model_name
+            test_name = "%s-cuda" % model_info.id
             test_method_name = "test_%s_cuda" % model_info.id
             # Using dtype=None so that the models that are only
Index: sasmodels/models/rpa.c
===================================================================
--- sasmodels/models/rpa.c	(revision 71b751d6a3a4ef4164f3b322841ddf3875c025c5)
+++ sasmodels/models/rpa.c	(revision 19dc29e78af078747e07090e31da3c9902c10ba2)
@@ -25,9 +25,9 @@
   double S0ba,Pbb,S0bb,Pbc,S0bc,Pbd,S0bd;
   double S0ca,S0cb,Pcc,S0cc,Pcd,S0cd;
-  double S0da,S0db,S0dc;
+  //double S0da,S0db,S0dc;
   double Pdd,S0dd;
   double Kaa,Kbb,Kcc;
   double Kba,Kca,Kcb;
-  double Kda,Kdb,Kdc,Kdd;
+  //double Kda,Kdb,Kdc,Kdd;
   double Zaa,Zab,Zac,Zba,Zbb,Zbc,Zca,Zcb,Zcc;
   double DenT,T11,T12,T13,T21,T22,T23,T31,T32,T33;
@@ -36,6 +36,7 @@
   double N11,N12,N13,N21,N22,N23,N31,N32,N33;
   double M11,M12,M13,M21,M22,M23,M31,M32,M33;
-  double S11,S12,S13,S14,S21,S22,S23,S24;
-  double S31,S32,S33,S34,S41,S42,S43,S44;
+  double S11,S12,S22,S23,S13,S33;
+  //double S21,S31,S32,S44; 
+  //double S14,S24,S34,S41,S42,S43;
   double Lad,Lbd,Lcd,Nav,Intg;
 
@@ -115,7 +116,7 @@
   S0cd=(Phicd*vcd*Ncd)*Pcd;
 
-  S0da=S0ad;
-  S0db=S0bd;
-  S0dc=S0cd;
+  //S0da=S0ad;
+  //S0db=S0bd;
+  //S0dc=S0cd;
   Pdd=2.0*(exp(-Xd)-1.0+Xd)/(Xd*Xd); // free D chain
   S0dd=N[3]*Phi[3]*v[3]*Pdd;
@@ -198,7 +199,7 @@
   S0ca=S0ac;
   S0cb=S0bc;
-  S0da=S0ad;
-  S0db=S0bd;
-  S0dc=S0cd;
+  //S0da=S0ad;
+  //S0db=S0bd;
+  //S0dc=S0cd;
 
   // self chi parameter is 0 ... of course
@@ -206,12 +207,12 @@
   Kbb=0.0;
   Kcc=0.0;
-  Kdd=0.0;
+  //Kdd=0.0;
 
   Kba=Kab;
   Kca=Kac;
   Kcb=Kbc;
-  Kda=Kad;
-  Kdb=Kbd;
-  Kdc=Kcd;
+  //Kda=Kad;
+  //Kdb=Kbd;
+  //Kdc=Kcd;
 
   Zaa=Kaa-Kad-Kad;
@@ -294,17 +295,17 @@
   S12= Q12*S0aa + Q22*S0ab + Q32*S0ac;
   S13= Q13*S0aa + Q23*S0ab + Q33*S0ac;
-  S14=-S11-S12-S13;
-  S21= Q11*S0ba + Q21*S0bb + Q31*S0bc;
   S22= Q12*S0ba + Q22*S0bb + Q32*S0bc;
   S23= Q13*S0ba + Q23*S0bb + Q33*S0bc;
-  S24=-S21-S22-S23;
-  S31= Q11*S0ca + Q21*S0cb + Q31*S0cc;
-  S32= Q12*S0ca + Q22*S0cb + Q32*S0cc;
   S33= Q13*S0ca + Q23*S0cb + Q33*S0cc;
-  S34=-S31-S32-S33;
-  S41=S14;
-  S42=S24;
-  S43=S34;
-  S44=S11+S22+S33+2.0*S12+2.0*S13+2.0*S23;
+  //S21= Q11*S0ba + Q21*S0bb + Q31*S0bc;
+  //S31= Q11*S0ca + Q21*S0cb + Q31*S0cc;
+  //S32= Q12*S0ca + Q22*S0cb + Q32*S0cc;
+  //S44=S11+S22+S33+2.0*S12+2.0*S13+2.0*S23;
+  //S14=-S11-S12-S13;
+  //S24=-S21-S22-S23;
+  //S34=-S31-S32-S33;
+  //S41=S14;
+  //S42=S24;
+  //S43=S34;
 
   //calculate contrast where L[i] is the scattering length of i and D is the matrix
Index: sasmodels/resolution.py
===================================================================
--- sasmodels/resolution.py	(revision e2592f02ab8e424de77e5b72ae9adfc84b47a185)
+++ sasmodels/resolution.py	(revision da3638ff2a2c2b5545bc7ad33fcacddd3b2b65c0)
@@ -498,11 +498,11 @@
         if q_min < 0:
             q_min = q[0]*MINIMUM_ABSOLUTE_Q
-        n_low = log_delta_q * (log(q[0])-log(q_min))
-        q_low = np.logspace(log10(q_min), log10(q[0]), int(np.ceil(n_low))+1)[:-1]
+        n_low = int(np.ceil(log_delta_q * (log(q[0])-log(q_min))))
+        q_low = np.logspace(log10(q_min), log10(q[0]), n_low+1)[:-1]
     else:
         q_low = []
     if q_max > q[-1]:
-        n_high = log_delta_q * (log(q_max)-log(q[-1]))
-        q_high = np.logspace(log10(q[-1]), log10(q_max), int(np.ceil(n_high))+1)[1:]
+        n_high = int(np.ceil(log_delta_q * (log(q_max)-log(q[-1]))))
+        q_high = np.logspace(log10(q[-1]), log10(q_max), n_high+1)[1:]
     else:
         q_high = []
