-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsetup.py
227 lines (183 loc) · 7.01 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# setup.py
import os
import re
import setuptools
from warnings import warn
import numpy
from Cython.Build import cythonize
def find_sundials():
search_paths = []
SUNDIALS_PREFIX = os.environ.get('SUNDIALS_PREFIX')
if SUNDIALS_PREFIX:
search_paths.append(SUNDIALS_PREFIX)
CONDA_PREFIX = os.environ.get('CONDA_PREFIX')
if CONDA_PREFIX:
search_paths.extend([
CONDA_PREFIX,
os.path.join(CONDA_PREFIX, 'Library'),
])
search_paths.extend([
'/usr',
'/usr/local',
'C:/SUNDIALS',
'C:/Program Files/SUNDIALS',
])
for BASE in search_paths:
include_dir = os.path.join(BASE, 'include')
CONFIG_H = os.path.join(include_dir, 'sundials', 'sundials_config.h')
if os.path.exists(CONFIG_H):
return BASE, CONFIG_H
raise FileNotFoundError("Can't find SUNDIALS installation in any of the"
f" {search_paths=}. Set the environment variable"
" SUNDIALS_PREFIX to the parent directory of the"
" 'include' and 'lib' directories and retry the"
" installation.")
def parse_config_h(file):
config = {}
define_rx = re.compile(r"#define\s+(\w+)(?:\s+(.*))?")
for line in file:
line = line.strip()
if define_rx.match(line):
name, value = define_rx.match(line).group(1, 2)
if isinstance(value, str):
value = value.strip('"')
try:
config[name] = int(value)
except (ValueError, TypeError): # Catch non int and NoneType
config[name] = value
return {k: config[k] for k in sorted(config)}
def get_extensions():
# Parse sundials_config.h
BASE, CONFIG_H = find_sundials()
with open(CONFIG_H, 'r') as f:
config = parse_config_h(f)
print("\n\n")
for k, v in config.items():
print(k, v)
print("\n\n")
# Write pxi files for C and Python to match types to sundials_config.h
SUNDIALS_VERSION = config.get('SUNDIALS_VERSION')
MAJOR_VERSION = SUNDIALS_VERSION.split('.')[0]
if int(MAJOR_VERSION) < 7:
raise RuntimeError(f"sksundae - incompatible {SUNDIALS_VERSION=}."
" MAJOR_VERSION must be at least 7.")
if config.get('SUNDIALS_SINGLE_PRECISION'):
precision = 'float'
np_precision = 'np.float32_t'
elif config.get('SUNDIALS_DOUBLE_PRECISION'):
precision = 'double'
np_precision = 'np.float64_t'
elif config.get('SUNDIALS_EXTENDED_PRECISION'):
precision = 'long double'
np_precision = 'np.float128_t'
else:
warn("Couldn't find SUNDIALS_PRECISION. Defaulting to double.")
precision = 'double'
np_precision = 'np.float64_t'
if config.get('SUNDIALS_INT32_T'):
indexsize = 'int'
np_indexsize = 'np.int32_t'
elif config.get('SUNDIALS_INT64_T'):
indexsize = 'long int'
np_indexsize = 'np.int64_t'
else:
warn("Couldn't find SUNDIALS_INDEX_SIZE. Defaulting to int.")
indexsize = 'int'
np_indexsize = 'np.int32_t'
if 'SUNDIALS_SUPERLUMT_ENABLED' in config:
has_superlu = True
superlu_threads = config['SUNDIALS_SUPERLUMT_THREAD_TYPE']
else:
has_superlu = False
superlu_threads = None
superlu_dir = os.path.join(BASE, 'include', 'superlu_mt')
if has_superlu and not os.path.exists(superlu_dir):
warn("SuperLU_MT was enabled, but is not on the expected path. It is"
" expected that the header and library files for SuperLU_MT are"
" in the same include and lib dirs as SUNDIALS. scikit-SUNDAE"
" will build without SuperLU_MT. If you need these capabilities"
" please move your installation and try again.")
has_superlu = False
superlu_threads = None
with open('src/sksundae/py_config.pxi', 'w') as f: # Python config
f.write(f"SUNDIALS_VERSION = \"{SUNDIALS_VERSION}\"\n")
f.write(f"SUNDIALS_FLOAT_TYPE = \"{precision}\"\n")
f.write(f"SUNDIALS_INT_TYPE = \"{indexsize}\"\n")
f.write(f"SUNDIALS_SUPERLUMT_ENABLED = {has_superlu}\n")
f.write(f"SUNDIALS_SUPERLUMT_THREAD_TYPE = \"{superlu_threads}\"\n")
with open('src/sksundae/c_config.pxi', 'w') as f: # C config
f.write("cimport numpy as np\n\n")
f.write(f"ctypedef {precision} sunrealtype\n")
f.write(f"ctypedef {np_precision} DTYPE_t\n\n")
f.write(f"ctypedef {indexsize} sunindextype\n")
f.write(f"ctypedef {np_indexsize} INT_TYPE_t\n\n")
# Specify include_dirs, library_dirs, and libraries for each extension
SUNDIALS_INCLUDE_DIRS = [numpy.get_include(), os.path.join(BASE, 'include')]
SUNDIALS_LIBRARY_DIRS = [os.path.join(BASE, 'lib')]
LIBRARIES = [
'sundials_core',
'sundials_nvecserial',
'sundials_sunlinsoldense',
'sundials_sunlinsolband',
'sundials_sunmatrixdense',
'sundials_sunmatrixband',
'sundials_sunmatrixsparse',
'sundials_sunlinsolspgmr',
'sundials_sunlinsolspbcgs',
'sundials_sunlinsolsptfqmr',
]
MACROS = [('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')]
# Optional solvers - SuperLU_MT
if has_superlu:
SUNDIALS_INCLUDE_DIRS.append(
os.path.join(BASE, 'include', 'superlu_mt'),
)
LIBRARIES.extend([
'sundials_sunlinsolsuperlumt',
'superlu_mt_' + superlu_threads,
])
MACROS.append(('__' + superlu_threads, None))
MACROS.append(('SUNDIALS_HAS_SUPERLUMT', None))
# Define the extension modules
extensions = [
setuptools.Extension(
name='sksundae._cy_common',
sources=['src/sksundae/_cy_common.pyx'],
include_dirs=SUNDIALS_INCLUDE_DIRS,
library_dirs=SUNDIALS_LIBRARY_DIRS,
libraries=LIBRARIES,
define_macros=MACROS,
),
setuptools.Extension(
name='sksundae._cy_cvode',
sources=['src/sksundae/_cy_cvode.pyx'],
include_dirs=SUNDIALS_INCLUDE_DIRS,
library_dirs=SUNDIALS_LIBRARY_DIRS,
libraries=LIBRARIES + ['sundials_cvode'],
define_macros=MACROS,
),
setuptools.Extension(
name='sksundae._cy_ida',
sources=['src/sksundae/_cy_ida.pyx'],
include_dirs=SUNDIALS_INCLUDE_DIRS,
library_dirs=SUNDIALS_LIBRARY_DIRS,
libraries=LIBRARIES + ['sundials_ida'],
define_macros=MACROS,
),
]
ext_modules = cythonize(
extensions,
compiler_directives={'language_level': 3},
)
return ext_modules
# Run the setup
BUILD_SDIST = os.environ.get('BUILD_SDIST', 0)
if int(BUILD_SDIST): # Don't compile extensions if just building sdist
setuptools.setup(
include_package_data=True,
)
else:
setuptools.setup(
include_package_data=True,
ext_modules=get_extensions(),
)