""" C types wrapper for sasview models. """ import sys import os import ctypes as ct from ctypes import c_void_p, c_int, c_double import numpy as np from . import gen from .gen import F32, F64 # Compiler platform details if sys.platform == 'darwin': #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" COMPILE = "gcc -shared -fPIC -std=c99 -O2 -Wall %s -o %s -lm" elif os.name == 'nt': COMPILE = "gcc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" else: COMPILE = "cc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" DLL_PATH = "/tmp" def dll_path(info): """ Path to the compiled model defined by *info*. """ from os.path import join as joinpath, split as splitpath, splitext basename = splitext(splitpath(info['filename'])[1])[0] return joinpath(DLL_PATH, basename+'.so') def load_model(kernel_module, dtype=None): """ Load the compiled model defined by *kernel_module*. Recompile if any files are newer than the model file. *dtype* is ignored. Compiled files are always double. The DLL is not loaded until the kernel is called so models an be defined without using too many resources. """ import tempfile source, info = gen.make(kernel_module) source_files = gen.sources(info) + [info['filename']] newest = max(os.path.getmtime(f) for f in source_files) dllpath = dll_path(info) if not os.path.exists(dllpath) or os.path.getmtime(dllpath)