Created
January 17, 2018 22:11
-
-
Save kingjr/13aa3e00b3793c1117f5681e9945af58 to your computer and use it in GitHub Desktop.
embarassingly parallel loop across numpy arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loop(X, func, n_jobs=-1, *args, **kwargs): | |
""""will apply func(x, *args) for x in X in parallel""" | |
from joblib import Parallel, delayed, cpu_count | |
max_jobs = cpu_count() | |
n_jobs = max_jobs if n_jobs==-1 else n_jobs | |
n_jobs = max_jobs if n_jobs>max_jobs else n_jobs | |
n_jobs = min(n_jobs, len(X)) | |
parallel = Parallel(n_jobs=n_jobs) | |
p_func = delayed(_loop) | |
X_splits = np.array_split(X, n_jobs, axis=0) | |
out = parallel(p_func(func, x, *args, **kwargs) for x in X_splits) | |
out = np.array(out) | |
if out.ndim > 1: | |
out = np.concatenate(out, axis=0) | |
return out | |
def _loop(func, X, *args, **kwargs): | |
out = list() | |
for x in tqdm(X): | |
out.append(func(x, *args, **kwargs)) | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment