8000 Merge branch 'feature/cyma57' into develop · PythonOptimizers/HSL.py@790488f · GitHub
[go: up one dir, main page]

Skip to content

Commit 790488f

Browse files
committed
Merge branch 'feature/cyma57' into develop
2 parents f6efb32 + 54ed2d0 commit 790488f

29 files changed

+1895
-8825
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
site.cfg
22
.DS_Store
33

4-
# Byte-compiled / optimized / DLL files
4+
# C extensions
5+
*.so
6+
7+
# Generated files
8+
*.pyx
9+
*.pxd
10+
*.c
511
__pycache__/
612
*.py[cod]
713
*$py.class

DESCRIPTION.rst

Whitespace-only changes.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ To run the tests:
5353

5454
python setup.py install.
5555

56+
## Compilation of cython files
57+
58+
cython -I ~/work/VirtualEnvs/nlpy_new/programs/cysparse/ _cyma57.pyx
59+
60+
## Note
61+
weird behaviour of Cython, cannot cythonize some .pyx files using setup.py but works when invoking cython from command line ...
62+
So I modified setup.py to create c file using command line "cython"
63+
5664
## TODO
5765

5866
- [ ] make it work with CySparse

config/setup.cpy

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
#!/usr/bin/env python
2+
3+
# The file setup.py is automatically generated
4+
# Generate it with
5+
# python generate_code
6+
7+
import pysparse
8+
from distutils.core import setup
9+
from setuptools import find_packages
10+
from distutils.extension import Extension
11+
from Cython.Distutils import build_ext
12+
from numpy.distutils.misc_util import Configuration
13+
from numpy.distutils.system_info import get_info
14+
from numpy.distutils.core import setup
15+
16+
from Cython.Build import cythonize
17+
18+
import numpy as np
19+
20+
import ConfigParser
21+
import os
22+
import copy
23+
24+
from codecs import open
25+
from os import path
26+
27+
28+
from subprocess import call
29+
30+
# HELPERS
31+
#--------
32+
def prepare_Cython_extensions_as_C_extensions(extensions):
33+
"""
34+
Modify the list of sources to transform `Cython` extensions into `C` extensions.
35+
Args:
36+
extensions: A list of (`Cython`) `distutils` extensions.
37+
Warning:
38+
The extensions are changed in place. This function is not compatible with `C++` code.
39+
Note:
40+
Only `Cython` source files are modified into their `C` equivalent source files. Other file types are unchanged.
41+
"""
42+
for extension in extensions:
43+
c_sources = list()
44+
for source_path in extension.sources:
45+
path, source = os.path.split(source_path)
46+
filename, ext = os.path.splitext(source)
47+
48+
if ext == '.pyx':
49+
c_sources.append(os.path.join(path, filename + '.c'))
50+
elif ext in ['.pxd', '.pxi']:
51+
pass
52+
else:
53+
# copy source as is
54+
c_sources.append(source_path)
55+
56+
# modify extension in place
57+
extension.sources = c_sources
58+
59+
def files_exist(file_list):
60+
for fname in file_list:
61+
if os.path.isfile(fname) == False:
62+
return False
63+
return True
64+
65+
f2py_options = []
66+
67+
hsl_config = ConfigParser.SafeConfigParser()
68+
hsl_config.read('site.cfg')
69+
70+
version = {}
71+
with open(os.path.join('hsl', 'version.py')) as fp:
72+
exec(fp.read(), version)
73+
# later on we use: version['version']
74+
75+
numpy_include = np.get_include()
76+
pysparse_inc = pysparse.get_include()
77+
78+
# Use Cython?
79+
use_cython = hsl_config.getboolean('CODE_GENERATION', 'use_cython')
80+
if use_cython:
81+
try:
82+
from Cython.Distutils import build_ext
83+
from Cython.Build import cythonize
84+
except ImportError:
85+
raise ImportError("Check '%s': Cython is not properly installed." % hsl_config_file)
86+
87+
# DEFAULT
88+
default_include_dir = hsl_config.get('DEFAULT', 'include_dirs').split(os.pathsep)
89+
default_library_dir = hsl_config.get('DEFAULT', 'library_dirs').split(os.pathsep)
90+
91+
# Debug mode
92+
use_debug_symbols = hsl_config.getboolean('CODE_GENERATION', 'use_debug_symbols')
93+
94+
# find user defined directories
95+
hsl_rootdir = hsl_config.get('HSL', 'hsl_rootdir')
96+
if hsl_rootdir == '':
97+
raise ValueError("Please provide HSL source directory!")
98+
99+
metis_dir = hsl_config.get('METIS', 'metis_dir')
100+
metis_lib = hsl_config.get('METIS', 'metis_lib')
101+
102+
# OPTIONAL
103+
build_cysparse_ext = False
104+
if hsl_config.has_section('CYSPARSE'):
105+
build_cysparse_ext = True
106+
cysparse_rootdir = hsl_config.get('CYSPARSE', 'cysparse_rootdir').split(os.pathsep)
107+
if cysparse_rootdir == '':
108+
raise ValueError("You must specify where CySparse source code is" +
109+
"located. Use `cysparse_rootdir` to specify its path.")
110+
111+
config = Configuration('hsl', '', '')
112+
config.set_options(ignore_setup_xxx_py=True,
113+
assume_default_configuration=True,
114+
delegate_options_to_subpackages=True,
115+
quiet=True)
116+
117+
# Set config.version
118+
config.get_version(os.path.join('hsl', 'version.py'))
119+
120+
121+
config.add_include_dirs([np.get_include(), pysparse.get_include()])
122+
config.add_include_dirs('include')
123+
124+
# Get info from site.cfg
125+
blas_info = get_info('blas_opt', 0)
126+
if not blas_info:
127+
print 'No blas info found'
128+
129+
lapack_info = get_info('lapack_opt', 0)
130+
if not lapack_info:
131+
print 'No lapack info found'
132+
133+
134+
########################################################################################################################
135+
# EXTENSIONS
136+
########################################################################################################################
137+
include_dirs = [numpy_include, 'include', '.']
138+
139+
## Extensions using Fortran codes
140+
141+
# Relevant files for building MA27 extension.
142+
ma27_src = ['fd05ad.f', 'ma27ad.f']
143+
libma27_src = ['ma27fact.f']
144+
pyma27_src = ['ma27_lib.c', 'hsl_alloc.c', '_pyma27.c']
145+
146+
# Build PyMA27
147+
ma27_sources = [os.path.join(hsl_rootdir, name) for name in ma27_src]
148+
ma27_sources += [os.path.join('hsl', 'solvers', 'src', name) for name in libma27_src]
149+
pyma27_sources = [os.path.join('hsl', 'solvers', 'src', name) for name in pyma27_src]
150+
151+
if files_exist(ma27_sources):
152+
153+
config.add_library(name='hsl_ma27',
154+
sources=ma27_sources,
155+
include_dirs=[hsl_rootdir, os.path.join('hsl', 'solvers', 'src')],)
156+
157+
158+
config.add_extension(name='solvers._pyma27',
159+
sources=pyma27_sources,
160+
depends=[],
161+
libraries=['hsl_ma27'],
162+
include_dirs=['include', os.path.join('hsl', 'solvers', 'src')],)
163+
164+
165+
# Build PyMA57
166+
ma57_src = ['ddeps.f', 'ma57d.f']
167+
ma57_sources = [os.path.join(hsl_rootdir, 'ma57d', name) for name in ma57_src]
168+
169+
if files_exist(ma57_sources):
170+
config.add_library(
171+
name='hsl_ma57',
172+
sources=ma57_sources,
173+
libraries=[metis_lib],
174+
library_dirs=[metis_dir],
175+
include_dirs=[hsl_rootdir, os.path.join('hsl', 'solvers', 'src')],)
176+
177+
mc29_sources = [os.path.join(hsl_rootdir, 'mc29d', 'mc29d.f'),
178+
os.path.join('hsl', 'scaling', 'src', 'mc29.pyf')]
179+
180+
config.add_extension(
181+
name='scaling._mc29',
182+
sources=mc29_sources,
183+
include_dirs=[os.path.join('hsl', 'scaling', 'src')],
184+
extra_link_args=[])
10000 185+
186+
187+
mc21_sources = [os.path.join(hsl_rootdir, 'mc21d', 'mc21d.f'),
188+
os.path.join('hsl', 'ordering', 'src', 'mc21.pyf')]
189+
190+
config.add_extension(
191+
name='ordering._mc21',
192+
sources=mc21_sources,
193+
include_dirs=[os.path.join('hsl', 'ordering', 'src')],
194+
extra_link_args=[])
195+
196+
197+
mc60_sources = [os.path.join(hsl_rootdir, 'mc60d', 'mc60d.f'),
198+
os.path.join('hsl', 'ordering', 'src', 'mc60.pyf')]
199+
200+
config.add_extension(
201+
name='ordering._mc60',
202+
sources=mc60_sources,
203+
include_dirs=[os.path.join('hsl', 'ordering', 'src')],
204+
extra_link_args=[])
205+
206+
## Extensions using Cython
207+
hsl_ext = []
208+
ext_params = {}
209+
ext_params['include_dirs'] = include_dirs
210+
if not use_debug_symbols:
211+
ext_params['extra_compile_args'] = ["-O2", '-std=c99', '-Wno-unused-function']
212+
ext_params['extra_link_args'] = []
213+
else:
214+
ext_params['extra_compile_args'] = ["-g", '-std=c99', '-Wno-unused-function']
215+
ext_params['extra_link_args'] = ["-g"]
216+
217+
context_ext_params = copy.deepcopy(ext_params)
218+
{% for index_type in index_list %}
219+
{% for element_type in type_list %}
220+
cyma57_src_@index_type@_@element_type@ = ['ma57_lib.c',
221+
'hsl_alloc.c',
222+
'_cyma57_base_@index_type@_@element_type@.c']
223+
cyma57_sources_@index_type@_@element_type@ = [os.path.join('hsl', 'solvers', 'src', name) for name in cyma57_src_@index_type@_@element_type@]
224+
225+
base_ext_params_@index_type@_@element_type@ = copy.deepcopy(ext_params)
226+
base_ext_params_@index_type@_@element_type@['library_dirs'] = [metis_dir]
227+
base_ext_params_@index_type@_@element_type@['libraries'] = D96B [metis_lib, 'hsl_ma57']
228+
retval = os.getcwd()
229+
os.chdir('hsl/solvers/src')
230+
call(['cython', '_cyma57_base_@index_type@_@element_type@.pyx'])
231+
os.chdir(retval)
232+
config.add_extension(
233+
name='solvers.src._cyma57_base_@index_type@_@element_type@',
234+
sources=cyma57_sources_@index_type@_@element_type@,
235+
**base_ext_params_@index_type@_@element_type@)
236+
237+
retval = os.getcwd()
238+
os.chdir('hsl/solvers/src')
239+
call(['cython', '_cyma57_numpy_@index_type@_@element_type@.pyx'])
240+
os.chdir(retval)
241+
numpy_ext_params_@index_type@_@element_type@ = copy.deepcopy(ext_params)
242+
config.add_extension(name="solvers.src._cyma57_numpy_@index_type@_@element_type@",
243+
sources=['hsl/solvers/src/_cyma57_numpy_@index_type@_@element_type@.c'],
244+
**numpy_ext_params_@index_type@_@element_type@)
245+
246+
{% endfor %}
247+
{% endfor %}
248+
249+
if build_cysparse_ext:
250+
{% for index_type in index_list %}
251+
{% for element_type in type_list %}
252+
retval = os.getcwd()
253+
os.chdir('hsl/solvers/src')
254+
call(['cython', '-I', cysparse_rootdir[0], '_cyma57_cysparse_@index_type@_@element_type@.pyx'])
255+
os.chdir(retval)
256+
cysparse_ext_params_@index_type@_@element_type@ = copy.deepcopy(ext_params)
257+
cysparse_ext_params_@index_type@_@element_type@['include_dirs'].extend(cysparse_rootdir)
258+
config.add_extension(name="solvers.src._cyma57_cysparse_@index_type@_@element_type@",
259+
sources=['hsl/solvers/src/_cyma57_cysparse_@index_type@_@element_type@.c'],
260+
**cysparse_ext_params_@index_type@_@element_type@)
261+
{% endfor %}
262+
{% endfor %}
263+
264+
265+
packages_list = ['hsl', 'hsl.ordering', 'hsl.scaling', 'hsl.solvers',
266+
'hsl.solvers.src']
267+
268+
269+
# PACKAGE PREPARATION FOR EXCLUSIVE C EXTENSIONS
270+
########################################################################################################################
271+
# We only use the C files **without** Cython. In fact, Cython doesn't need to be installed.
272+
cythonize(hsl_ext)
273+
prepare_Cython_extensions_as_C_extensions(hsl_ext)
274+
275+
for ext in hsl_ext:
276+
print ext.name
277+
print ext.sources
278+
print ext.libraries
279+
print ext.include_dirs
280+
config.add_extension(name=ext.name[4:],
281+
sources=ext.sources,
282+
depends=ext.depends,
283+
library_dirs=ext.library_dirs,
284+
libraries=ext.libraries,
285+
include_dirs=ext.include_dirs,)
286+
287+
288+
CLASSIFIERS = """\
289+
Development Status :: 4 - Beta
290+
Intended Audience :: Science/Research
291+
Intended Audience :: Developers
292+
License :: OSI Approved
293+
Programming Language :: Python
294+
Programming Language :: Cython
295+
Topic :: Software Development
296+
Topic :: Scientific/Engineering
297+
Operating System :: POSIX
298+
Operating System :: Unix
299+
Operating System :: MacOS :: MacOS X
300+
Natural Language :: English
301+
"""
302+
303+
here = path.abspath(path.dirname(__file__))
304+
# Get the long description from the relevant file
305+
with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f:
306+
long_description = f.read()
307+
308+
config.make_config_py()
309+
310+
setup(packages=packages_list, zip_safe=False, **config.todict())

0 commit comments

Comments
 (0)
0