Skip to content

Commit 70ca457

Browse files
committed
Started adding in logic for num_threads and changed names of real memory stats keys
1 parent b5a6024 commit 70ca457

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

nipype/interfaces/base.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,8 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
12481248
outfile = os.path.join(runtime.cwd, 'stdout.nipype')
12491249

12501250
# Init variables for memory profiling
1251-
ret = -1
1251+
mem_mb = -1
1252+
num_threads = -1
12521253
interval = 0.1
12531254

12541255
if output == 'stream':
@@ -1268,7 +1269,7 @@ def _process(drain=0):
12681269
stream.read(drain)
12691270
while proc.returncode is None:
12701271
if mem_prof:
1271-
ret = max([ret, _get_memory(proc.pid, include_children=True)])
1272+
mem_mb = max([mem_mb, _get_memory(proc.pid, include_children=True)])
12721273
time.sleep(interval)
12731274
proc.poll()
12741275
_process()
@@ -1287,7 +1288,8 @@ def _process(drain=0):
12871288
if output == 'allatonce':
12881289
if mem_prof:
12891290
while proc.returncode is None:
1290-
ret = max([ret, _get_memory(proc.pid, include_children=True)])
1291+
mem_mb = max([mem_mb, _get_memory(proc.pid, include_children=True)])
1292+
num_threads = max([num_threads, psutil.Proc(proc.pid).num_threads()])
12911293
time.sleep(interval)
12921294
proc.poll()
12931295
stdout, stderr = proc.communicate()
@@ -1297,7 +1299,8 @@ def _process(drain=0):
12971299
if output == 'file':
12981300
if mem_prof:
12991301
while proc.returncode is None:
1300-
ret = max([ret, _get_memory(proc.pid, include_children=True)])
1302+
mem_mb = max([mem_mb, _get_memory(proc.pid, include_children=True)])
1303+
num_threads = max([num_threads, psutil.Proc(proc.pid).num_threads()])
13011304
time.sleep(interval)
13021305
proc.poll()
13031306
ret_code = proc.wait()
@@ -1309,15 +1312,17 @@ def _process(drain=0):
13091312
if output == 'none':
13101313
if mem_prof:
13111314
while proc.returncode is None:
1312-
ret = max([ret, _get_memory(proc.pid, include_children=True)])
1315+
mem_mb = max([mem_mb, _get_memory(proc.pid, include_children=True)])
1316+
num_threads = max([num_threads, psutil.Proc(proc.pid).num_threads()])
13131317
time.sleep(interval)
13141318
proc.poll()
13151319
proc.communicate()
13161320
result['stdout'] = []
13171321
result['stderr'] = []
13181322
result['merged'] = ''
13191323

1320-
setattr(runtime, 'real_memory2', ret/1024.0)
1324+
setattr(runtime, 'cmd_memory', mem_mb/1024.0)
1325+
setattr(runtime, 'num_threads', num_threads)
13211326
runtime.stderr = '\n'.join(result['stderr'])
13221327
runtime.stdout = '\n'.join(result['stdout'])
13231328
runtime.merged = result['merged']

nipype/interfaces/utility.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,15 @@ def _run_interface(self, runtime):
442442
if isdefined(value):
443443
args[name] = value
444444

445-
# mem stuff
446-
import memory_profiler
447-
proc = (function_handle, (), args)
448-
mem_mb, out = memory_profiler.memory_usage(proc=proc, retval=True, include_children=True, max_usage=True)
449-
setattr(runtime, 'real_memory2', mem_mb[0]/1024.0)
450-
#out = function_handle(**args)
445+
# Record memory of function_handle
446+
try:
447+
import memory_profiler
448+
proc = (function_handle, (), args)
449+
mem_mb, out = memory_profiler.memory_usage(proc=proc, retval=True, include_children=True, max_usage=True)
450+
setattr(runtime, 'cmd_memory', mem_mb[0]/1024.0)
451+
# If no memory_profiler package, run without recording memory
452+
except ImportError:
453+
out = function_handle(**args)
451454

452455
if len(self._output_names) == 1:
453456
self._out[self._output_names[0]] = out

nipype/pipeline/plugins/multiproc.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ def run_node(node, updatehash, plugin_args=None):
1717
result = dict(result=None, traceback=None)
1818
try:
1919
run_memory = plugin_args['memory_profile']
20-
except Exception:
20+
import memory_profiler
21+
except KeyError:
22+
run_memory = False
23+
except ImportError:
2124
run_memory = False
2225
if run_memory:
23-
import memory_profiler
2426
import datetime
2527
proc = (node.run, (), {'updatehash' : updatehash})
2628
start = datetime.datetime.now()
2729
mem_mb, retval = memory_profiler.memory_usage(proc=proc, retval=True, include_children=True, max_usage=True)
2830
runtime = (datetime.datetime.now() - start).total_seconds()
2931
result['result'] = retval
30-
result['real_memory'] = mem_mb[0]/1024.0
31-
result['real_memory2'] = retval.runtime.get('real_memory2')
32+
result['node_memory'] = mem_mb[0]/1024.0
33+
result['cmd_memory'] = retval.runtime.get('cmd_memory')
3234
result['run_seconds'] = runtime
3335
else:
3436
try:

0 commit comments

Comments
 (0)