Skip to content

Commit 5397fbb

Browse files
committed
add MKL thread number controling class
1 parent c7cc95c commit 5397fbb

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

nipype/interfaces/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"""
1111
from __future__ import print_function, division, unicode_literals, absolute_import
1212
from future import standard_library
13+
from py.path import ImportMismatchError
14+
1315
standard_library.install_aliases()
1416
from builtins import range, object, open, str, bytes
1517

@@ -1217,6 +1219,29 @@ def save_inputs_to_json(self, json_file):
12171219
json.dump(inputs, fhandle, indent=4, ensure_ascii=False)
12181220

12191221

1222+
class MKLInterfaceInputSpec(BaseInterfaceInputSpec):
1223+
mkl_num_threads = traits.Int(1, usedafult=True, nohash=True,
1224+
desc="Number of threads to use if mkl-service "
1225+
"package is installed.")
1226+
1227+
1228+
class MKLInterface(BaseInterface):
1229+
"""Interfaces that control number of threads available via MKL.
1230+
"""
1231+
1232+
def _run_interface(self, runtime):
1233+
try:
1234+
import mkl
1235+
except ImportError:
1236+
iflogger.warning("mkl-service package not found - cannot control "
1237+
"number of threads")
1238+
return runtime
1239+
else:
1240+
mkl.set_num_threads(self.inputs.mkl_num_threads)
1241+
self.num_threads = self.inputs.mkl_num_threads
1242+
return runtime
1243+
1244+
12201245
class Stream(object):
12211246
"""Function to capture stdout and stderr streams with timestamps
12221247

nipype/interfaces/nipy/preprocess.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
from ...utils.filemanip import split_filename, fname_presuffix
2222
from ..base import (TraitedSpec, BaseInterface, traits,
2323
BaseInterfaceInputSpec, isdefined, File,
24-
InputMultiPath, OutputMultiPath)
24+
InputMultiPath, OutputMultiPath, MKLInterface,
25+
MKLInterfaceInputSpec
26+
)
2527

2628

2729
have_nipy = True
@@ -207,7 +209,7 @@ def _list_outputs(self):
207209
return outputs
208210

209211

210-
class SpaceTimeRealignerInputSpec(BaseInterfaceInputSpec):
212+
class SpaceTimeRealignerInputSpec(MKLInterfaceInputSpec):
211213

212214
in_file = InputMultiPath(File(exists=True),
213215
mandatory=True, min_ver='0.4.0.dev',
@@ -246,7 +248,7 @@ class SpaceTimeRealignerOutputSpec(TraitedSpec):
246248
"euler angles"))
247249

248250

249-
class SpaceTimeRealigner(BaseInterface):
251+
class SpaceTimeRealigner(MKLInterface):
250252
"""Simultaneous motion and slice timing correction algorithm
251253
252254
If slice_times is not specified, this algorithm performs spatial motion
@@ -290,6 +292,7 @@ def version(self):
290292
return nipy_version
291293

292294
def _run_interface(self, runtime):
295+
runtime = super(SpaceTimeRealigner, self)._run_interface()
293296
all_ims = [load_image(fname) for fname in self.inputs.in_file]
294297

295298
if not isdefined(self.inputs.slice_times):

0 commit comments

Comments
 (0)