【发布时间】:2021-01-13 14:08:59
【问题描述】:
我想做什么:
我有一个脚本,用于在给定范围内分解素数:
# Python program to display all the prime numbers within an interval
lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
我想使用 GPU 而不是 CPU 来运行这样的脚本,这样会更快
问题:
我的Intel NUC NUC8i7HVK 上没有 NVIDIA GPU,但"Discrete GPU" 上没有 NVIDIA GPU
如果我运行此代码来检查我的 GPU 是什么:
import pyopencl as cl
import numpy as np
a = np.arange(32).astype(np.float32)
res = np.empty_like(a)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a)
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, res.nbytes)
prg = cl.Program(ctx, """
__kernel void sq(__global const float *a,
__global float *c)
{
int gid = get_global_id(0);
c[gid] = a[gid] * a[gid];
}
""").build()
prg.sq(queue, a.shape, None, a_buf, dest_buf)
cl.enqueue_copy(queue, res, dest_buf)
print (a, res)
我收到:
[0] <pyopencl.Platform 'AMD Accelerated Parallel Processing' at 0x7ffb3d492fd0>[1] <pyopencl.Platform 'Intel(R) OpenCL HD Graphics' at 0x187b648ed80>
解决问题的可能方法:
我找到了一个guide,它可以帮助您逐步解释如何在您的 GPU 上运行它。但是所有通过 GPU 传输 Python 的 Pyhton 库,例如 PyOpenGL、PyOpenCL、Tensorflow (Force python script on GPU)、PyTorch 等......都是为 NVIDIA 量身定制的。
如果您有 AMD,所有库都要求ROCm,但据我所知,此类软件仍然不支持集成 GPU 或离散 GPU(请参阅下面我自己的回复)。
我只找到了一个guide 谈论这种方法,但我无法让它发挥作用。
是有希望还是我只是想做一些不可能的事情?
编辑:回复@chapelo
如果我选择0,回复是:
Set the environment variable PYOPENCL_CTX='0' to avoid being asked again.
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.
18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31.] [ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81. 100. 121. 144. 169.
196. 225. 256. 289. 324. 361. 400. 441. 484. 529. 576. 625. 676. 729.
784. 841. 900. 961.]
如果我选择1,回复是:
Set the environment variable PYOPENCL_CTX='1' to avoid being asked again.
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.
18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31.] [ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81. 100. 121. 144. 169.
196. 225. 256. 289. 324. 361. 400. 441. 484. 529. 576. 625. 676. 729.
784. 841. 900. 961.]
【问题讨论】:
-
@Lescurel 谢谢,这看起来是一个很好的起点。显然只适用于Linux,但我可以解决它。我去看看。
-
@Lescurel,我现在正在关注本指南shawonashraf.github.io/rocm-tf-ubuntu。顺便说一句,我在英特尔 NUC8i7HVK ark.intel.com/content/www/us/en/ark/products/126143/… 上做这一切,它实际上没有 GPU,而是有两个“集成”GPU。我是在浪费时间,还是 Tensorflow、PyOpenGL 等...也可以与集成 GPU 一起使用?
-
我不知道,我从未尝试过。祝你好运!
-
我有同样的问题,基本上我不得不去Nvidia。
标签: python tensorflow pytorch pyopengl pyopencl