Changeset f734e7d in sasmodels for sasmodels/kerneldll.py


Ignore:
Timestamp:
Feb 22, 2015 1:44:54 AM (9 years ago)
Author:
pkienzle
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
6137124
Parents:
711d8e2
Message:

restructure c code generation for maintainability; extend test harness to allow opencl and ctypes tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/kerneldll.py

    r68d3c1b rf734e7d  
    1111 
    1212from . import generate 
    13 from .kernelpy import PyInput, PyKernel 
     13from .kernelpy import PyInput, PyModel 
    1414 
    1515from .generate import F32, F64 
     
    2222    if "VCINSTALLDIR" in os.environ: 
    2323        # MSVC compiler is available, so use it. 
     24        # TODO: remove intermediate OBJ file created in the directory 
     25        # TODO: maybe don't use randomized name for the c file 
    2426        COMPILE = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG /Tp%(source)s /openmp /link /DLL /INCREMENTAL:NO /MANIFEST /OUT:%(output)s" 
    2527        # Can't find VCOMP90.DLL (don't know why), so remove openmp support from windows compiler build 
     
    5456    be defined without using too many resources. 
    5557    """ 
    56     import tempfile 
    57  
    5858    source, info = generate.make(kernel_module) 
     59    if callable(info.get('Iq',None)): 
     60        return PyModel(info) 
    5961    source_files = generate.sources(info) + [info['filename']] 
    6062    newest = max(os.path.getmtime(f) for f in source_files) 
     
    6870        status = os.system(command) 
    6971        if status != 0: 
    70             print "compile failed.  File is in %r"%filename 
     72            raise RuntimeError("compile failed.  File is in %r"%filename) 
    7173        else: 
    7274            ## uncomment the following to keep the generated c file 
     
    7678 
    7779 
    78 IQ_ARGS = [c_void_p, c_void_p, c_int, c_void_p, c_double] 
    79 IQXY_ARGS = [c_void_p, c_void_p, c_void_p, c_int, c_void_p, c_double] 
     80IQ_ARGS = [c_void_p, c_void_p, c_int] 
     81IQXY_ARGS = [c_void_p, c_void_p, c_void_p, c_int] 
    8082 
    8183class DllModel(object): 
     
    107109        self.dll = ct.CDLL(self.dllpath) 
    108110 
     111        pd_args_1d = [c_void_p, c_double] + [c_int]*Npd1d if Npd1d else [] 
     112        pd_args_2d= [c_void_p, c_double] + [c_int]*Npd2d if Npd2d else [] 
    109113        self.Iq = self.dll[generate.kernel_name(self.info, False)] 
    110         self.Iq.argtypes = IQ_ARGS + [c_double]*Nfixed1d + [c_int]*Npd1d 
     114        self.Iq.argtypes = IQ_ARGS + pd_args_1d + [c_double]*Nfixed1d 
    111115 
    112116        self.Iqxy = self.dll[generate.kernel_name(self.info, True)] 
    113         self.Iqxy.argtypes = IQXY_ARGS + [c_double]*Nfixed2d + [c_int]*Npd2d 
     117        self.Iqxy.argtypes = IQXY_ARGS + pd_args_2d + [c_double]*Nfixed2d 
    114118 
    115119    def __getstate__(self): 
     
    120124 
    121125    def __call__(self, input): 
    122         # Support pure python kernel call 
    123         if input.is_2D and callable(self.info['Iqxy']): 
    124             return PyKernel(self.info['Iqxy'], self.info, input) 
    125         elif not input.is_2D and callable(self.info['Iq']): 
    126             return PyKernel(self.info['Iq'], self.info, input) 
    127  
    128126        if self.dll is None: self._load_dll() 
    129127        kernel = self.Iqxy if input.is_2D else self.Iq 
     
    175173        self.p_res = self.res.ctypes.data 
    176174 
    177     def __call__(self, pars, pd_pars, cutoff): 
     175    def __call__(self, fixed_pars, pd_pars, cutoff): 
    178176        real = np.float32 if self.input.dtype == F32 else np.float64 
    179         fixed = [real(p) for p in pars] 
    180         cutoff = real(cutoff) 
    181         loops = np.hstack(pd_pars) 
    182         loops = np.ascontiguousarray(loops.T, self.input.dtype).flatten() 
    183         loops_N = [np.uint32(len(p[0])) for p in pd_pars] 
    184177 
    185178        nq = c_int(self.input.nq) 
    186         p_loops = loops.ctypes.data 
    187         args = self.input.q_pointers + [self.p_res, nq, p_loops, cutoff] + fixed + loops_N 
     179        if pd_pars: 
     180            cutoff = real(cutoff) 
     181            loops_N = [np.uint32(len(p[0])) for p in pd_pars] 
     182            loops = np.hstack(pd_pars) 
     183            loops = np.ascontiguousarray(loops.T, self.input.dtype).flatten() 
     184            p_loops = loops.ctypes.data 
     185            dispersed = [p_loops, cutoff] + loops_N 
     186        else: 
     187            dispersed = [] 
     188        fixed = [real(p) for p in fixed_pars] 
     189        args = self.input.q_pointers + [self.p_res, nq] + dispersed + fixed 
    188190        #print pars 
    189191        self.kernel(*args) 
Note: See TracChangeset for help on using the changeset viewer.