Skip to content

Commit a1ac275

Browse files
authored
Merge pull request #321 from effigies/enh/mem_estimate
ENH: Dynamically choose number of resampling threads to adapt to memory constraints
2 parents f7d408a + 02d618e commit a1ac275

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

sdcflows/workflows/apply/correction.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
2727

2828

29-
def init_unwarp_wf(omp_nthreads=1, debug=False, name="unwarp_wf"):
29+
def init_unwarp_wf(*, free_mem=None, omp_nthreads=1, debug=False, name="unwarp_wf"):
3030
r"""
3131
Set up a workflow that unwarps the input :abbr:`EPI (echo-planar imaging)` dataset.
3232
@@ -103,9 +103,24 @@ def init_unwarp_wf(omp_nthreads=1, debug=False, name="unwarp_wf"):
103103

104104
rotime = pe.Node(GetReadoutTime(), name="rotime")
105105
rotime.interface._always_run = debug
106-
resample = pe.Node(ApplyCoeffsField(
107-
num_threads=omp_nthreads if not debug else 1
108-
), name="resample")
106+
107+
# resample is memory-hungry; choose a smaller number of threads
108+
# if we know how much memory we have to work with
109+
mem_per_thread = 5 # True for a 128x128x84 image; should generalize
110+
if debug:
111+
num_threads = 1
112+
elif free_mem is not None:
113+
mem_gb = min(0.9 * free_mem, mem_per_thread * omp_nthreads)
114+
num_threads = max(int(mem_gb // mem_per_thread), 1)
115+
else:
116+
num_threads = omp_nthreads
117+
118+
resample = pe.Node(
119+
ApplyCoeffsField(num_threads=num_threads),
120+
mem_gb=mem_per_thread * num_threads,
121+
name="resample",
122+
)
123+
109124
merge = pe.Node(MergeSeries(), name="merge")
110125
average = pe.Node(RobustAverage(mc_method=None), name="average")
111126

0 commit comments

Comments
 (0)