-
Notifications
You must be signed in to change notification settings - Fork 197
/
mpi.py
302 lines (235 loc) · 11.5 KB
/
mpi.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# #
# Copyright 2012-2020 Ghent University
#
# This file is part of EasyBuild,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
# with support of Ghent University (http://ugent.be/hpc),
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
# Flemish Research Foundation (FWO) (http://www.fwo.be/en)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# https://github.com/easybuilders/easybuild
#
# EasyBuild is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# EasyBuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>.
# #
"""
Toolchain mpi module. Contains all MPI related classes
:author: Stijn De Weirdt (Ghent University)
:author: Kenneth Hoste (Ghent University)
"""
import copy
import os
import tempfile
from distutils.version import LooseVersion
from easybuild.base import fancylogger
import easybuild.tools.environment as env
import easybuild.tools.toolchain as toolchain
from easybuild.tools.build_log import EasyBuildError
from easybuild.tools.config import build_option
from easybuild.tools.filetools import write_file
from easybuild.tools.toolchain.constants import COMPILER_VARIABLES, MPI_COMPILER_TEMPLATE, SEQ_COMPILER_TEMPLATE
from easybuild.tools.toolchain.toolchain import Toolchain
_log = fancylogger.getLogger('tools.toolchain.mpi', fname=False)
def get_mpi_cmd_template(mpi_family, params, mpi_version=None):
"""
Return template for MPI command, for specified MPI family.
:param mpi_family: MPI family to use to determine MPI command template
"""
params = copy.deepcopy(params)
mpi_cmd_template = build_option('mpi_cmd_template')
if mpi_cmd_template:
_log.info("Using specified template for MPI commands: %s", mpi_cmd_template)
else:
# different known mpirun commands
mpirun_n_cmd = "mpirun -n %(nr_ranks)s %(cmd)s"
mpi_cmds = {
toolchain.OPENMPI: mpirun_n_cmd,
toolchain.QLOGICMPI: "mpirun -H localhost -np %(nr_ranks)s %(cmd)s",
toolchain.INTELMPI: mpirun_n_cmd,
toolchain.MVAPICH2: mpirun_n_cmd,
toolchain.MPICH: mpirun_n_cmd,
toolchain.MPICH2: mpirun_n_cmd,
}
# Intel MPI mpirun needs more work
if mpi_cmd_template is None:
if mpi_family == toolchain.INTELMPI:
if mpi_version is None:
raise EasyBuildError("Intel MPI version unknown, can't determine MPI command template!")
# for old versions of Intel MPI, we need to use MPD
if LooseVersion(mpi_version) <= LooseVersion('4.1'):
mpi_cmds[toolchain.INTELMPI] = "mpirun %(mpdbf)s %(nodesfile)s -np %(nr_ranks)s %(cmd)s"
# set temporary dir for MPD
# note: this needs to be kept *short*,
# to avoid mpirun failing with "socket.error: AF_UNIX path too long"
# exact limit is unknown, but ~20 characters seems to be OK
env.setvar('I_MPI_MPD_TMPDIR', tempfile.gettempdir())
mpd_tmpdir = os.environ['I_MPI_MPD_TMPDIR']
if len(mpd_tmpdir) > 20:
_log.warning("$I_MPI_MPD_TMPDIR should be (very) short to avoid problems: %s", mpd_tmpdir)
# temporary location for mpdboot and nodes files
tmpdir = tempfile.mkdtemp(prefix='mpi_cmd_for-')
# set PBS_ENVIRONMENT, so that --file option for mpdboot isn't stripped away
env.setvar('PBS_ENVIRONMENT', "PBS_BATCH_MPI")
# make sure we're always using mpd as process manager
# only required for/picked up by Intel MPI v4.1 or higher, no harm done for others
env.setvar('I_MPI_PROCESS_MANAGER', 'mpd')
# create mpdboot file
mpdboot = os.path.join(tmpdir, 'mpdboot')
write_file(mpdboot, "localhost ifhn=localhost")
params.update({'mpdbf': "--file=%s" % mpdboot})
# create nodes file
nodes = os.path.join(tmpdir, 'nodes')
write_file(nodes, "localhost\n" * int(params['nr_ranks']))
params.update({'nodesfile': "-machinefile %s" % nodes})
if mpi_family in mpi_cmds:
mpi_cmd_template = mpi_cmds[mpi_family]
_log.info("Using template MPI command '%s' for MPI family '%s'", mpi_cmd_template, mpi_family)
else:
raise EasyBuildError("Don't know which template MPI command to use for MPI family '%s'", mpi_family)
missing = []
for key in sorted(params.keys()):
tmpl = '%(' + key + ')s'
if tmpl not in mpi_cmd_template:
missing.append(tmpl)
if missing:
raise EasyBuildError("Missing templates in mpi-cmd-template value '%s': %s",
mpi_cmd_template, ', '.join(missing))
return mpi_cmd_template, params
class Mpi(Toolchain):
"""General MPI-like class
can't be used without creating new class M(Mpi)
"""
MPI_MODULE_NAME = None
MPI_FAMILY = None
MPI_TYPE = None
MPI_LIBRARY_NAME = None
MPI_UNIQUE_OPTS = None
MPI_SHARED_OPTS = {
'usempi': (False, "Use MPI compiler as default compiler"), # also FFTW
}
MPI_UNIQUE_OPTION_MAP = None
MPI_SHARED_OPTION_MAP = {
'_opt_MPICC': 'cc=%(CC_base)s',
'_opt_MPICXX':'cxx=%(CXX_base)s',
'_opt_MPIF77':'fc=%(F77_base)s',
'_opt_MPIF90':'f90=%(F90_base)s',
'_opt_MPIFC':'fc=%(FC_base)s',
}
MPI_COMPILER_MPICC = 'mpicc'
MPI_COMPILER_MPICXX = 'mpicxx'
MPI_COMPILER_MPIF77 = 'mpif77'
MPI_COMPILER_MPIF90 = 'mpif90'
MPI_COMPILER_MPIFC = 'mpifc'
MPI_LINK_INFO_OPTION = None
def __init__(self, *args, **kwargs):
Toolchain.base_init(self)
self._set_mpi_options()
super(Mpi, self).__init__(*args, **kwargs)
def _set_mpi_options(self):
self.options.add_options(self.MPI_SHARED_OPTS, self.MPI_SHARED_OPTION_MAP)
self.options.add_options(self.MPI_UNIQUE_OPTS, self.MPI_UNIQUE_OPTION_MAP)
self.log.devel('_set_mpi_options: all current options %s', self.options)
def set_variables(self):
"""Set the variables"""
self._set_mpi_compiler_variables()
self._set_mpi_variables()
self.log.devel('set_variables: compiler variables %s', self.variables)
super(Mpi, self).set_variables()
def _set_mpi_compiler_variables(self):
"""Set the MPI compiler variables"""
is32bit = self.options.get('32bit', None)
if is32bit:
self.log.debug("_set_mpi_compiler_variables: 32bit set: changing compiler definitions")
for var_tuple in COMPILER_VARIABLES:
c_var = var_tuple[0] # [1] is the description
var = MPI_COMPILER_TEMPLATE % {'c_var':c_var}
value = getattr(self, 'MPI_COMPILER_%s' % var.upper(), None)
if value is None:
raise EasyBuildError("_set_mpi_compiler_variables: mpi compiler variable %s undefined", var)
self.variables.nappend_el(var, value)
# complete compiler variable template to produce e.g. 'mpicc -cc=icc -X -Y' from 'mpicc -cc=%(CC_base)'
templatedict = {
c_var:str(self.variables[c_var]),
'%s_base' % c_var: str(self.variables[c_var].get_first()),
}
self.variables.nappend_el(var, self.options.option('_opt_%s' % var, templatedict=templatedict))
if is32bit:
self.variables.nappend_el(var, self.options.option('32bit'))
if self.options.get('usempi', None):
var_seq = SEQ_COMPILER_TEMPLATE % {'c_var': c_var}
self.log.debug("usempi set: defining %s as %s", var_seq, self.variables[c_var])
self.variables[var_seq] = self.variables[c_var]
self.log.debug("usempi set: switching %s value %s for %s value %s",
c_var, self.variables[c_var], var, self.variables[var])
self.variables[c_var] = self.variables[var]
if self.options.get('cciscxx', None):
self.log.debug("_set_mpi_compiler_variables: cciscxx set: switching MPICXX %s for MPICC value %s",
self.variables['MPICXX'], self.variables['MPICC'])
self.variables['MPICXX'] = self.variables['MPICC']
if self.options.get('usempi', None):
# possibly/likely changed
self.variables['CXX'] = self.variables['CC']
def _set_mpi_variables(self):
"""Set the other MPI variables"""
lib_dir = ['lib']
incl_dir = ['include']
suffix = None
if not self.options.get('32bit', None):
suffix = '64'
# take into account that MPI_MODULE_NAME could be None (see Cray toolchains)
for root in self.get_software_root(self.MPI_MODULE_NAME or []):
self.variables.append_exists('MPI_LIB_STATIC', root, lib_dir, filename="lib%s.a" % self.MPI_LIBRARY_NAME,
suffix=suffix)
self.variables.append_exists('MPI_LIB_SHARED', root, lib_dir, filename="lib%s.so" % self.MPI_LIBRARY_NAME,
suffix=suffix)
self.variables.append_exists('MPI_LIB_DIR', root, lib_dir, suffix=suffix)
self.variables.append_exists('MPI_INC_DIR', root, incl_dir, suffix=suffix)
def mpi_family(self):
""" Return type of MPI library used in this toolchain."""
if self.MPI_FAMILY:
return self.MPI_FAMILY
else:
raise EasyBuildError("mpi_family: MPI_FAMILY is undefined.")
def mpi_cmd_prefix(self, nr_ranks=1):
"""Construct an MPI command prefix to precede an executable"""
# Verify that the command appears at the end of mpi_cmd_for
test_cmd = 'xxx_command_xxx'
mpi_cmd = self.mpi_cmd_for(test_cmd, nr_ranks)
if mpi_cmd.rstrip().endswith(test_cmd):
result = mpi_cmd.replace(test_cmd, '').rstrip()
else:
warning_msg = "mpi_cmd_for cannot be used by mpi_cmd_prefix, "
warning_msg += "requires that %(cmd)s template appears at the end"
self.log.warning(warning_msg)
result = None
return result
def mpi_cmd_for(self, cmd, nr_ranks):
"""Construct an MPI command for the given command and number of ranks."""
# parameter values for mpirun command
params = {
'nr_ranks': nr_ranks,
'cmd': cmd,
}
mpi_family = self.mpi_family()
if mpi_family == toolchain.INTELMPI:
mpi_version = self.get_software_version(self.MPI_MODULE_NAME)[0]
else:
mpi_version = None
mpi_cmd_template, params = get_mpi_cmd_template(mpi_family, params, mpi_version=mpi_version)
self.log.info("Using MPI command template '%s' (params: %s)", mpi_cmd_template, params)
try:
res = mpi_cmd_template % params
except KeyError as err:
raise EasyBuildError("Failed to complete MPI cmd template '%s' with %s: KeyError %s",
mpi_cmd_template, params, err)
return res