""" C types wrapper for sasview models. """ import sys import os import tempfile 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 %(source)s -o %(output)s -lm" elif os.name == 'nt': # make sure vcvarsall.bat is called first in order to set compiler, headers, lib paths, etc. ##COMPILER = r'"C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe"' # Can't find VCOMP90.DLL (don't know why), so remove openmp support from windows compiler build #COMPILE = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG /Tp%(source)s /link /DLL /INCREMENTAL:NO /MANIFEST /OUT:%(output)s" COMPILE = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG /Tp%(source)s /openmp /link /DLL /INCREMENTAL:NO /MANIFEST /OUT:%(output)s" #/MANIFESTFILE:build\temp.win32-2.7\Release\src\sans\models\c_extension\libigor\c_models.pyd.manifest #COMPILE = "gcc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %(source)s -o %(output)s -lm" else: COMPILE = "cc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %(source)s -o %(output)s -lm" DLL_PATH = tempfile.gettempdir() 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)