Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eed2372

Browse files
committed
FEAT: blas/lapack can be configured from command line.
1 parent 20825a9 commit eed2372

1 file changed

Lines changed: 98 additions & 50 deletions

File tree

bscript

Lines changed: 98 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,55 +35,75 @@ try:
3535
finally:
3636
sys.path.pop(0)
3737

38-
def check_blas_lapack(conf):
39-
conf.env.HAS_CBLAS = False
40-
if sys.platform == "win32":
41-
mkl_libs = "mkl_lapack95,mkl_blas95,mkl_intel_c,mkl_intel_thread,mkl_core,libiomp5md".split(",")
42-
mkl_base = r"C:\Program Files\Intel\Compiler\11.1\051"
43-
conf.env.INCLUDES.append("%s\mkl\include" % mkl_base)
44-
conf.env.LIBPATH.extend(["%s\mkl\ia32\lib" % mkl_base,
45-
"%s\lib\ia32" % mkl_base])
46-
47-
try:
48-
conf.check_cc(lib=mkl_libs, msg="Checking for MKL (CBLAS)",
49-
uselib_store="CBLAS")
50-
conf.env.HAS_CBLAS = True
51-
except waflib.Errors.ConfigurationError:
52-
conf.env.HAS_LAPACK = False
53-
54-
try:
55-
conf.check_cc(lib=mkl_libs, msg="Checking for MKL (LAPACK)",
56-
uselib_store="LAPACK")
57-
conf.env.HAS_LAPACK = True
58-
except waflib.Errors.ConfigurationError:
59-
conf.env.HAS_LAPACK = False
60-
61-
62-
elif sys.platform == "darwin":
63-
try:
64-
conf.check(framework="Accelerate", msg="Checking for framework Accelerate (CBLAS)", uselib_store="CBLAS")
65-
conf.env.HAS_CBLAS = True
66-
except waflib.Errors.ConfigurationError:
67-
conf.env.HAS_CBLAS = False
68-
69-
try:
70-
conf.check(framework="Accelerate", msg="Checking for framework Accelerate (LAPACK)", uselib_store="LAPACK")
71-
conf.env.HAS_LAPACK = True
72-
except waflib.Errors.ConfigurationError:
73-
conf.env.HAS_LAPACK = False
38+
import collections
39+
_PLATFORM_TO_DEFAULT = collections.defaultdict(lambda: "atlas")
40+
_PLATFORM_TO_DEFAULT.update({
41+
"win32": "mkl",
42+
"darwin": "accelerate",
43+
})
44+
45+
_OPTIMIZED_CBLAS_TO_KWARGS = {
46+
"mkl": {"lib": "mkl_intel_c,mkl_intel_thread,mkl_core,libiomp5md".split(",")},
47+
"atlas": {"lib": ["cblas", "atlas"]},
48+
"accelerate": {"framework": ["Accelerate"]},
49+
"openblas": {"lib": ["openblas"]},
50+
}
51+
52+
_OPTIMIZED_LAPACK_TO_KWARGS = {
53+
"mkl": {"lib": "mkl_lapack95,mkl_blas95,mkl_intel_c,mkl_intel_thread,mkl_core,libiomp5md".split(",")},
54+
"atlas": {"lib": ["lapack", "f77blas", "cblas", "atlas"]},
55+
"accelerate": {"framework": ["Accelerate"]},
56+
"openblas": {"lib": ["openblas"]},
57+
}
58+
59+
def get_optimized_name(context):
60+
o, a = context.options_context.parser.parse_args(context.command_argv)
61+
if o.blas_lapack_type == "default" or o.blas_lapack_type is None:
62+
optimized = _PLATFORM_TO_DEFAULT[sys.platform]
7463
else:
75-
try:
76-
conf.check_cc(lib=["cblas", "atlas"], uselib_store="CBLAS")
77-
conf.env.HAS_CBLAS = True
78-
except waflib.Errors.ConfigurationError:
79-
conf.env.HAS_CBLAS = False
80-
81-
try:
82-
conf.check_cc(lib=["lapack", "f77blas", "cblas", "atlas"],
83-
uselib_store="LAPACK")
84-
conf.env.HAS_LAPACK = True
85-
except waflib.Errors.ConfigurationError:
86-
conf.env.HAS_LAPACK = False
64+
optimized = o.blas_lapack_type
65+
66+
return optimized
67+
68+
def check_cblas(context, optimized):
69+
conf = context.waf_context
70+
71+
msg = "Checking for %s (CBLAS)" % optimized.upper()
72+
73+
kwargs = _OPTIMIZED_CBLAS_TO_KWARGS[optimized]
74+
kwargs.update({"msg": msg, "uselib_store": "CBLAS"})
75+
76+
try:
77+
conf.check_cc(**kwargs)
78+
conf.env.HAS_CBLAS = True
79+
except waflib.Errors.ConfigurationError:
80+
conf.env.HAS_CBLAS = False
81+
82+
def check_lapack(context, optimized):
83+
conf = context.waf_context
84+
85+
msg = "Checking for %s (LAPACK)" % optimized.upper()
86+
if optimized in ["openblas", "atlas"]:
87+
check_fortran(context)
88+
89+
kwargs = _OPTIMIZED_LAPACK_TO_KWARGS[optimized]
90+
kwargs.update({"msg": msg, "uselib_store": "LAPACK"})
91+
92+
try:
93+
conf.check_cc(**kwargs)
94+
conf.env.HAS_LAPACK = True
95+
except waflib.Errors.ConfigurationError:
96+
conf.env.HAS_LAPACK = False
97+
98+
def check_blas_lapack(context):
99+
optimized = get_optimized_name(context)
100+
101+
o, a = context.options_context.parser.parse_args(context.command_argv)
102+
if o.blas_lapack_libdir:
103+
context.waf_context.env.append_value("LIBPATH", o.blas_lapack_libdir)
104+
105+
check_cblas(context, optimized)
106+
check_lapack(context, optimized)
87107

88108
# You can manually set up blas/lapack as follows:
89109
#conf.env.HAS_CBLAS = True
@@ -111,6 +131,19 @@ def _register_metadata(context):
111131
context.register_metadata("is_released", _SETUP_PY.ISRELEASED)
112132
context.register_metadata("full_version", full_version)
113133

134+
def check_fortran(context):
135+
opts = context.waf_options_context
136+
conf = context.waf_context
137+
138+
opts.load("compiler_fc")
139+
Options.options.check_fc = "gfortran"
140+
141+
conf.load("compiler_fc")
142+
conf.load("ordered_c", tooldir=[WAF_TOOLDIR])
143+
144+
conf.check_fortran_verbose_flag()
145+
conf.check_fortran_clib()
146+
114147
@hooks.post_configure
115148
def post_configure(context):
116149
conf = context.waf_context
@@ -124,8 +157,7 @@ def post_configure(context):
124157
archs = [conf.env.DEFAULT_CC_ARCH]
125158
conf.env.ARCH = archs
126159

127-
conf.env.LIBPATH = ["/Users/cournape/src/numeric/OpenBLAS-git"]
128-
check_blas_lapack(conf)
160+
check_blas_lapack(context)
129161

130162
@hooks.pre_build
131163
def pre_build(context):
@@ -134,3 +166,19 @@ def pre_build(context):
134166
@hooks.pre_sdist
135167
def pre_sdist(context):
136168
_register_metadata(context)
169+
170+
@hooks.options
171+
def options(global_context):
172+
from bento.commands.options import Option
173+
174+
global_context.add_option_group("configure", "blas_lapack", "blas/lapack")
175+
176+
available_optimized = ",".join(_OPTIMIZED_LAPACK_TO_KWARGS.keys())
177+
global_context.add_option("configure",
178+
Option("--blas-lapack-type", help="Which blas lapack to use (%s)" % available_optimized),
179+
"blas_lapack")
180+
181+
global_context.add_option("configure",
182+
Option("--with-blas-lapack-libdir", dest="blas_lapack_libdir",
183+
help="Where to look for BLAS/LAPACK dir"),
184+
"blas_lapack")

0 commit comments

Comments
 (0)