Skip to content

Instantly share code, notes, and snippets.

@DmitryUlyanov
Created January 31, 2017 19:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DmitryUlyanov/1a2e8882c664f235459b7aa25269ac86 to your computer and use it in GitHub Desktop.
Save DmitryUlyanov/1a2e8882c664f235459b7aa25269ac86 to your computer and use it in GitHub Desktop.
from joblib import Parallel, delayed
import Queue
import os
# Define number of GPUs available
N_GPU = 4
# Put indices in queue
q = Queue.Queue(maxsize=N_GPU)
for i in range(N_GPU):
q.put(i)
def runner(x):
gpu = q.get()
print (x, gpu)
# Put here your job cmd
cmd = "python main.py %s" % x
os.system("CUDA_VISIBLE_DEVICES=%d %s" % (gpu, cmd))
# return gpu id to queue
q.put(gpu)
# Change loop
Parallel(n_jobs=N_GPU, backend="threading")(
delayed(runner)(i) for i in range(100))
@cruyffturn
Copy link

cruyffturn commented Jan 29, 2021

Thank you. I was wondering whether it can be generalized to non-threading backends and for the "multiprocessing" backend, I have found the solution at which uses multiprocessing.Queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment