Changeset 56b2687 in sasmodels for sasmodels/kerneldll.py
- Timestamp:
- Jul 14, 2016 3:35:58 PM (8 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/kerneldll.py
rda63656 r56b2687 49 49 import os 50 50 from os.path import join as joinpath, split as splitpath, splitext 51 import subprocess 51 52 import tempfile 52 53 import ctypes as ct # type: ignore 53 54 from ctypes import c_void_p, c_int32, c_longdouble, c_double, c_float # type: ignore 55 import logging 54 56 55 57 import numpy as np # type: ignore … … 68 70 pass 69 71 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': 72 if 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 75 79 # call vcvarsall.bat before compiling to set path, headers, libs, etc. 76 80 if "VCINSTALLDIR" in os.environ: … … 81 85 # TODO: remove intermediate OBJ file created in the directory 82 86 # 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() 85 89 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() 93 102 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"] 99 106 else: 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"] 101 117 102 118 # Windows-specific solution … … 112 128 ALLOW_SINGLE_PRECISION_DLLS = True 113 129 130 def 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) 114 142 115 143 def dll_name(model_info, dtype): … … 120 148 """ 121 149 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) 123 159 124 160 … … 161 197 if not os.path.exists(dll): 162 198 need_recompile = True 163 elif getattr(sys, 'frozen', False):199 elif getattr(sys, 'frozen', None) is not None: 164 200 # TODO: don't suppress time stamp 165 201 # Currently suppressing recompile when running in a frozen environment … … 173 209 fid, filename = tempfile.mkstemp(suffix=".c", prefix=basename) 174 210 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 file182 183 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) 184 220 return dll 185 221
Note: See TracChangeset
for help on using the changeset viewer.