You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import torch.multiprocessing as mp
  2. from queue import Queue
  3. import time
  4. import random
  5. import os
  6. def dummy_func(dev,cfg):
  7. time.sleep(random.random()*2)
  8. def dummy_config():
  9. return list(range(20))
  10. def mp_exec(resources,configs,func):
  11. '''
  12. @ resources : list of gpu devices
  13. @ configs : list of params
  14. @ func : f(dev,cfg)
  15. '''
  16. q=Queue()
  17. ret=Queue()
  18. for res in resources:
  19. q.put(res)
  20. pool=mp.Pool()
  21. def put_back_dev(dev,cfg):
  22. def callback(*args):
  23. print(f"Device {dev} Finish cfg {cfg} ")
  24. q.put(dev)
  25. ret.put([cfg,args])
  26. print(*args)
  27. return callback
  28. for idx,cfg in enumerate(configs):
  29. dev = q.get()
  30. print(f"Start config {cfg} on device {dev}")
  31. pool.apply_async(func,args=[dev,cfg],callback=put_back_dev(dev,cfg),error_callback=put_back_dev(dev,cfg))
  32. pool.close()
  33. pool.join()
  34. lret=[]
  35. while not ret.empty():
  36. lret.append(ret.get())
  37. return lret