-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetup.py
More file actions
240 lines (196 loc) · 7.18 KB
/
Copy pathsetup.py
File metadata and controls
240 lines (196 loc) · 7.18 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
from setuptools import setup, Extension
from setuptools.command.build import build as _build
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.build_py import build_py as _build_py
import numpy as np
import shutil
import os
import sys
import shlex
import platform
IS_WINDOWS = platform.system() == "Windows"
IS_DARWIN = platform.system() == "Darwin"
IS_LINUX = platform.system() == "Linux"
# When set, build a single-rank variant that requires neither a native MPI
# library nor mpi4py: bundled stub headers replace <mpi.h> and <mpi4py/mpi4py.h>,
# and the build records the choice in repast4py/_mpi_config.py so the Python side
# (repast4py._mpi) matches what the _space extension was compiled against.
SINGLE_RANK = os.environ.get("R4PY_NO_MPI", "") not in ("", "0")
SRC_DIR = os.path.join("src", "repast4py")
STUB_INCLUDE_DIR = os.path.join(SRC_DIR, "single_rank_include")
def is_64bit() -> bool:
"""Returns True if the python interpreter is 64-bit, independent of the OS arch."""
return sys.maxsize > 2**32
def run_command(exe, args):
cmd = shutil.which(exe)
if not cmd:
return []
if not isinstance(args, str):
args = " ".join(args)
try:
with os.popen(cmd + " " + args) as f:
return shlex.split(f.read())
except Exception:
return []
def get_mpi_include_dirs():
"""MPI/mpi4py include directories.
Single-rank builds use the bundled stub headers and do not depend on mpi4py.
"""
if SINGLE_RANK:
return [STUB_INCLUDE_DIR]
try:
import mpi4py
except ImportError:
# mpi4py is a *dynamic* build requirement (see build_support/r4py_backend.py).
# During the PEP 517 get_requires / egg_info phase it may not be installed
# yet; the include dir is only needed when the extensions are actually
# compiled, by which point pip has installed mpi4py.
return []
return [mpi4py.get_include()]
def get_linker_args():
linker_args = []
# Single-rank builds link no MPI library.
if SINGLE_RANK:
return linker_args
# NOTE Windows setuptools apparently does not use the CC env variable.
if IS_WINDOWS:
pass
else:
compiler = os.getenv("CC")
if compiler is None:
print(
'Error: MPI compiler is not specified. Please specify the MPI compiler using the "CC" environment variable'
)
# Nothing to query for link flags. Commands that do not compile
# (sdist, egg_info) still need setup.py to import.
return linker_args
args = run_command(compiler, "-show")
for arg in args:
if arg.startswith("-l") or arg.startswith("-L"):
linker_args.append(arg)
# linker_args_str = ' '.join(linker_args)
# return linker_args_str
return linker_args
def get_compiler_args():
if IS_WINDOWS:
compile_args = ["/std:c++latest"]
else:
compile_args = ["-std=c++11"]
return compile_args
def get_extra_includes():
# Single-rank builds need no MSMPI headers.
if IS_WINDOWS and not SINGLE_RANK:
return [os.environ["MSMPI_INC"]]
else:
return []
def get_lib_dirs():
if IS_WINDOWS and not SINGLE_RANK:
if is_64bit():
return [os.environ["MSMPI_LIB64"]]
else:
return [os.environ["MSMPI_LIB32"]]
else:
return []
def get_libs():
if IS_WINDOWS and not SINGLE_RANK:
return ["msmpi"]
else:
return []
def write_mpi_config(package_dir):
"""Record the build choice so repast4py._mpi imports the matching MPI.
Written into whichever copy of the package is about to be used: the build
output for a wheel, the source tree for an in-place or editable build.
"""
os.makedirs(package_dir, exist_ok=True)
path = os.path.join(package_dir, "_mpi_config.py")
with open(path, "w") as f:
f.write("# Generated by setup.py - do not edit.\n")
f.write("# Records whether repast4py was built single-rank (no native MPI / mpi4py).\n")
f.write(f"SINGLE_RANK = {SINGLE_RANK}\n")
class build(_build):
"""Give each build mode its own build directory.
The two modes compile the same sources against different headers, so they
must not share object files: nothing about the sources changes when the mode
does, and build_ext compares timestamps only. Sharing a directory lets a
build in one mode silently relink the other mode's objects.
"""
def finalize_options(self):
if SINGLE_RANK and self.build_base == "build":
self.build_base = os.path.join("build", "nompi")
super().finalize_options()
class build_py(_build_py):
"""Write the marker into the build output, so it is packaged into the wheel
without touching the source tree."""
def run(self):
super().run()
if not self.dry_run:
write_mpi_config(os.path.join(self.build_lib, "repast4py"))
class build_ext(_build_ext):
"""Write the marker into the source tree for in-place builds.
`build_ext --inplace` puts the extensions next to the sources and is what
developers run; editable installs reach here too, since setuptools turns
`editable_mode` into `inplace`. Either way the package is imported from
`src/`, so that is where the marker has to be.
"""
def run(self):
super().run()
if self.inplace and not self.dry_run:
write_mpi_config(SRC_DIR)
def get_install_requires():
"""Runtime dependencies. mpi4py is required only for the default (native MPI)
build; the single-rank build uses the bundled pure-Python substitute."""
requires = [
"numpy",
"numba",
"torch",
"networkx >=2.6.2",
"pyyaml",
"Cython",
'typing_extensions;python_version < "3.8.0"',
"dill",
]
if not SINGLE_RANK:
requires.append("mpi4py")
return requires
# Single-rank stub headers must be found before any system MPI headers.
mpi_include_dirs = get_mpi_include_dirs()
core_module = Extension(
"repast4py._core",
sources=["src/repast4py/coremodule.cpp"],
language="c++",
extra_compile_args=get_compiler_args(),
depends=["core.h"],
extra_link_args=get_linker_args(),
include_dirs=[np.get_include(), *mpi_include_dirs],
)
space_module = Extension(
"repast4py._space",
sources=[
"src/repast4py/spacemodule.cpp",
"src/repast4py/geometry.cpp",
"src/repast4py/space.cpp",
"src/repast4py/distributed_space.cpp",
"src/repast4py/SpatialTree.cpp",
],
language="c++",
extra_compile_args=get_compiler_args(),
depends=[
"space.h",
"grid.h",
"cspace.h",
"space_types.h",
"geometry.h",
"distributed_space.h",
"borders.h",
],
extra_link_args=get_linker_args(),
include_dirs=[np.get_include(), *mpi_include_dirs, *get_extra_includes()],
library_dirs=get_lib_dirs(),
libraries=get_libs(),
)
setup(
description="repast4py package",
ext_modules=[core_module, space_module],
install_requires=get_install_requires(),
cmdclass={"build": build, "build_py": build_py, "build_ext": build_ext},
)