Python Multiprocessing with PyCUDA

举报
风吹稻花香 发表于 2021/06/05 01:41:11 2021/06/05
【摘要】 Python Multiprocessing with PyCUDA 参考:https://stackoverflow.com/questions/5904872/python-multiprocessing-with-pycuda You need to get all your bananas lined up on the CUDA side of things f...

Python Multiprocessing with PyCUDA

参考:https://stackoverflow.com/questions/5904872/python-multiprocessing-with-pycuda


You need to get all your bananas lined up on the CUDA side of things first, then think about the best way to get this done in Python [shameless rep whoring, I know].

The CUDA multi-GPU model is pretty straightforward pre 4.0 - each GPU has its own context, and each context must be established by a different host thread. So the idea in pseudocode is:

  1. Application starts, process uses the API to determine the number of usable GPUS (beware things like compute mode in Linux)
  2. Application launches a new host thread per GPU, passing a GPU id. Each thread implicitly/explicitly calls equivalent of cuCtxCreate() passing the GPU id it has been assigned
  3. Profit!

In Python, this might look something like this:


  
  1. import threading
  2. from pycuda import driver
  3. class gpuThread(threading.Thread):
  4. def __init__(self, gpuid):
  5. threading.Thread.__init__(self)
  6. self.ctx = driver.Device(gpuid).make_context()
  7. self.device = self.ctx.get_device()
  8. def run(self):
  9. print "%s has device %s, api version %s" \
  10. % (self.getName(), self.device.name(), self.ctx.get_api_version())
  11. # Profit!
  12. def join(self):
  13. self.ctx.detach()
  14. threading.Thread.join(self)
  15. driver.init()
  16. ngpus = driver.Device.count()
  17. for i in range(ngpus):
  18. t = gpuThread(i)
  19. t.start()
  20. t.join()

This assumes it is safe to just establish a context without any checking of the device beforehand. Ideally you would check the compute mode to make sure it is safe to try, then use an exception handler in case a device is busy. But hopefully this gives the basic idea.


文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/79705484

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。