Changeset 56b2687 in sasmodels for sasmodels/kerneldll.py


Ignore:
Timestamp:
Jul 14, 2016 3:35:58 PM (8 years ago)
Author:
Paul Kienzle <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:
98ba1fc
Parents:
61f8638 (diff), fa800e72 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into polydisp

Conflicts:

README.rst
sasmodels/core.py
sasmodels/data.py
sasmodels/generate.py
sasmodels/kernelcl.py
sasmodels/kerneldll.py
sasmodels/sasview_model.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/kerneldll.py

    rda63656 r56b2687  
    4949import os 
    5050from os.path import join as joinpath, split as splitpath, splitext 
     51import subprocess 
    5152import tempfile 
    5253import ctypes as ct  # type: ignore 
    5354from ctypes import c_void_p, c_int32, c_longdouble, c_double, c_float  # type: ignore 
     55import logging 
    5456 
    5557import numpy as np  # type: ignore 
     
    6870    pass 
    6971 
    70 # Compiler platform details 
    71 if sys.platform == 'darwin': 
    72     #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" 
    73     COMPILE = "gcc -shared -fPIC -std=c99 -O2 -Wall %(source)s -o %(output)s -lm" 
    74 elif os.name == 'nt': 
     72if os.name == 'nt': 
     73    ARCH = "" if sys.maxint > 2**32 else "x86"  # maxint=2**31-1 on 32 bit 
     74    # Windows compiler; check if TinyCC is available 
     75    try: 
     76        import tinycc 
     77    except ImportError: 
     78        tinycc = None 
    7579    # call vcvarsall.bat before compiling to set path, headers, libs, etc. 
    7680    if "VCINSTALLDIR" in os.environ: 
     
    8185        # TODO: remove intermediate OBJ file created in the directory 
    8286        # TODO: maybe don't use randomized name for the c file 
    83         CC = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG /Tp%(source)s " 
    84         LN = "/link /DLL /INCREMENTAL:NO /MANIFEST /OUT:%(output)s" 
     87        # TODO: maybe ask distutils to find MSVC 
     88        CC = "cl /nologo /Ox /MD /W3 /GS- /DNDEBUG".split() 
    8589        if "SAS_OPENMP" in os.environ: 
    86             COMPILE = " ".join((CC, "/openmp", LN)) 
    87         else: 
    88             COMPILE = " ".join((CC, LN)) 
    89     elif True: 
    90         # If MSVC compiler is not available, try using mingw 
    91         # fPIC is not needed on windows 
    92         COMPILE = "gcc -shared -std=c99 -O2 -Wall %(source)s -o %(output)s -lm" 
     90            CC.append("/openmp") 
     91        LN = "/link /DLL /INCREMENTAL:NO /MANIFEST".split() 
     92        def compile_command(source, output): 
     93            return CC + ["/Tp%s"%source] + LN + ["/OUT:%s"%output] 
     94    elif tinycc: 
     95        # TinyCC compiler. 
     96        CC = [tinycc.TCC] + "-shared -rdynamic -Wall".split() 
     97        def compile_command(source, output): 
     98            return CC + [source, "-o", output] 
     99    else: 
     100        # MinGW compiler. 
     101        CC = "gcc -shared -std=c99 -O2 -Wall".split() 
    93102        if "SAS_OPENMP" in os.environ: 
    94             COMPILE += " -fopenmp" 
    95     else: 
    96         # If MSVC compiler is not available, try using tinycc 
    97         from tinycc import TCC 
    98         COMPILE = TCC + " -shared -rdynamic -Wall %(source)s -o %(output)s" 
     103            CC.append("-fopenmp") 
     104        def compile_command(source, output): 
     105            return CC + [source, "-o", output, "-lm"] 
    99106else: 
    100     COMPILE = "cc -shared -fPIC -fopenmp -std=c99 -O2 -Wall %(source)s -o %(output)s -lm" 
     107    ARCH = "" 
     108    # Generic unix compile 
     109    # On mac users will need the X code command line tools installed 
     110    #COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" 
     111    CC = "cc -shared -fPIC -std=c99 -O2 -Wall".split() 
     112    # add openmp support if not running on a mac 
     113    if sys.platform != "darwin": 
     114        CC.append("-fopenmp") 
     115    def compile_command(source, output): 
     116        return CC + [source, "-o", output, "-lm"] 
    101117 
    102118# Windows-specific solution 
     
    112128ALLOW_SINGLE_PRECISION_DLLS = True 
    113129 
     130def compile(source, output): 
     131    command = compile_command(source=source, output=output) 
     132    command_str = " ".join('"%s"'%p if ' ' in p else p for p in command) 
     133    logging.info(command_str) 
     134    try: 
     135        # need shell=True on windows to keep console box from popping up 
     136        shell = (os.name == 'nt') 
     137        subprocess.check_output(command, shell=shell, stderr=subprocess.STDOUT) 
     138    except subprocess.CalledProcessError as exc: 
     139        raise RuntimeError("compile failed.\n%s\n%s"%(command_str, exc.output)) 
     140    if not os.path.exists(output): 
     141        raise RuntimeError("compile failed.  File is in %r"%source) 
    114142 
    115143def dll_name(model_info, dtype): 
     
    120148    """ 
    121149    bits = 8*dtype.itemsize 
    122     return "sas%d_%s"%(bits, model_info.id) 
     150    basename = "sas%d_%s"%(bits, model_info.id) 
     151    basename += ARCH + ".so" 
     152 
     153    # Hack to find precompiled dlls 
     154    path = joinpath(generate.DATA_PATH, '..', 'compiled_models', basename) 
     155    if os.path.exists(path): 
     156        return path 
     157 
     158    return joinpath(DLL_PATH, basename) 
    123159 
    124160 
     
    161197    if not os.path.exists(dll): 
    162198        need_recompile = True 
    163     elif getattr(sys, 'frozen', False): 
     199    elif getattr(sys, 'frozen', None) is not None: 
    164200        # TODO: don't suppress time stamp 
    165201        # Currently suppressing recompile when running in a frozen environment 
     
    173209        fid, filename = tempfile.mkstemp(suffix=".c", prefix=basename) 
    174210        source = generate.convert_type(source, dtype) 
    175         os.fdopen(fid, "w").write(source) 
    176         command = COMPILE%{"source":filename, "output":dll} 
    177         status = os.system(command) 
    178         if status != 0 or not os.path.exists(dll): 
    179             raise RuntimeError("compile failed.  File is in %r"%filename) 
    180         else: 
    181             ## comment the following to keep the generated c file 
    182             os.unlink(filename) 
    183             #print("saving compiled file in %r"%filename) 
     211        fd, filename = tempfile.mkstemp(suffix=".c", prefix=tempfile_prefix) 
     212        with os.fdopen(fd, "w") as file: 
     213            file.write(source) 
     214        compile(source=filename, output=dll) 
     215        # comment the following to keep the generated c file 
     216        # Note: if there is a syntax error then compile raises an error 
     217        # and the source file will not be deleted. 
     218        os.unlink(filename) 
     219        #print("saving compiled file in %r"%filename) 
    184220    return dll 
    185221 
Note: See TracChangeset for help on using the changeset viewer.