Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
156 lines (133 loc) · 5.27 KB
/
Copy pathsetup.py
File metadata and controls
156 lines (133 loc) · 5.27 KB
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
import os
import sys
from pybind11.setup_helpers import Pybind11Extension, build_ext as pybind11_build_ext
from setuptools import setup
def find_mkl():
"""Find MKL include and library directories.
Search order:
1. MKLROOT environment variable
2. CONDA_PREFIX environment variable
3. Standard Intel oneAPI paths
4. System-installed MKL (apt libmkl-dev on Ubuntu/Debian)
"""
# Candidate base directories (MKLROOT-style layout: include/, lib/).
candidates = []
mklroot = os.environ.get("MKLROOT")
if mklroot:
candidates.append(mklroot)
conda = os.environ.get("CONDA_PREFIX")
if conda:
candidates.append(conda)
if sys.platform == "linux":
candidates.append("/opt/intel/oneapi/mkl/latest")
elif sys.platform == "win32":
for pf_env, default in (
("ProgramFiles", r"C:\Program Files"),
("ProgramFiles(x86)", r"C:\Program Files (x86)"),
):
pf = os.environ.get(pf_env, default)
candidates.append(os.path.join(pf, "Intel", "oneAPI", "mkl", "latest"))
# On Windows, conda-forge packages install under a Library/ subdirectory.
if sys.platform == "win32":
candidates += [os.path.join(c, "Library") for c in candidates]
for base in candidates:
inc = os.path.join(base, "include")
if sys.platform == "win32":
lib = os.path.join(base, "lib")
else:
lib = os.path.join(base, "lib", "intel64")
if not os.path.isdir(lib):
lib = os.path.join(base, "lib")
if os.path.isfile(os.path.join(inc, "mkl.h")):
return inc, lib
# Ubuntu/Debian: apt-get install libmkl-dev
if sys.platform == "linux":
for inc in ("/usr/include/mkl", "/usr/include"):
if os.path.isfile(os.path.join(inc, "mkl.h")):
return inc, "/usr/lib/x86_64-linux-gnu"
raise RuntimeError(
"Could not find MKL. Set the MKLROOT environment variable to your "
"MKL installation, or install via conda: conda install mkl-devel"
)
class build_ext(pybind11_build_ext):
"""Defer missing-MKL failures until extension build time."""
def build_extensions(self):
if mkl_error is not None:
raise RuntimeError(str(mkl_error))
super().build_extensions()
# MKL is required at compile time but not for sdist creation.
try:
mkl_include, mkl_libdir = find_mkl()
mkl_error = None
except RuntimeError as exc:
mkl_include = None
mkl_libdir = None
mkl_error = exc
# ILP64 interface (64-bit integers) -- matches MKL_INT64 / pardiso_64 in C++ source.
define_macros = [("MKL_ILP64", None)]
# Wheel CI prefers dynamic MKL plus wheel repair so the shared libraries are
# bundled into the built wheels. Static linking remains available as a local
# override if needed.
static = os.environ.get("PYMKLPARDISO_STATIC", "").lower() in ("1", "true", "yes")
def mkl_lib_paths(*filenames):
paths = [os.path.join(mkl_libdir, filename) for filename in filenames]
missing = [path for path in paths if not os.path.isfile(path)]
if missing:
raise RuntimeError(
"Could not find the expected MKL libraries:\n" + "\n".join(missing)
)
return paths
libraries = []
extra_link_args = []
if mkl_error is None:
try:
if sys.platform == "win32":
if static:
# Link the static oneMKL archives directly so Windows wheels do
# not depend on MKL DLLs at runtime.
extra_link_args = mkl_lib_paths(
"mkl_intel_ilp64.lib",
"mkl_sequential.lib",
"mkl_core.lib",
)
else:
# Dynamic Windows linkage uses the documented import libraries.
# Wheel CI then bundles the corresponding MKL DLLs via delvewheel.
libraries = ["mkl_intel_ilp64_dll", "mkl_sequential_dll", "mkl_core_dll"]
elif static:
# Static linking on Linux: pass full paths to .a archives inside
# --start-group / --end-group to resolve circular MKL dependencies.
_mkl_archives = mkl_lib_paths(
"libmkl_intel_ilp64.a",
"libmkl_sequential.a",
"libmkl_core.a",
)
extra_link_args = (
["-Wl,--start-group"] + _mkl_archives +
["-Wl,--end-group", "-lpthread", "-lm", "-ldl"]
)
else:
# Dynamic Linux linkage. Wheel CI bundles the needed MKL shared
# libraries into the repaired wheel via auditwheel.
libraries = ["mkl_intel_ilp64", "mkl_sequential", "mkl_core", "pthread", "m", "dl"]
extra_link_args = [f"-Wl,-rpath,{mkl_libdir}"]
except RuntimeError as exc:
mkl_error = exc
libraries = []
extra_link_args = []
ext_modules = [
Pybind11Extension(
"pymklpardiso._mkl_pardiso",
["py-mkl-pardiso.cpp"],
cxx_std=17,
include_dirs=[mkl_include] if mkl_include else [],
library_dirs=[mkl_libdir] if mkl_libdir and libraries else [],
libraries=libraries,
define_macros=define_macros,
extra_link_args=extra_link_args,
),
]
setup(
ext_modules=ext_modules,
cmdclass={"build_ext": build_ext},
)