pyopencl 在 windows 上的安装及使用

发布时间:2024年01月16日

<2022-05-07 周六>

pyopenclwindows上的安装及使用

因为pyopenclwindows上安装和使用不是想象中的那么容易,所以还是需要记录一下。

我的环境Python 3.10.4,按照pip install pyopencl的提示,先后安装了numpywheelmako,总之,提示缺啥安装啥,然后就不管在cmdpowershell还是在Developer Command Prompt for VS 2022中都没有安装成功pyopencl,一直提示:

src\wrap_cl.hpp(70): fatal error C1083: Cannot open include file: 'CL/cl.h': No such file or directory

最后在官网上找到了方法,从“Christoph Gohlke distributes binary wheels for PyOpenCL on Windows.”下载pyopencl-2022.1.3-cp310-cp310-win_amd64.whl解决:

PS D:\dnld> pip install .\pyopencl-2022.1.3-cp310-cp310-win_amd64.whl

这样就安装成功了。使用官网的例子测试一下:

#!/usr/bin/env python

import numpy as np
import pyopencl as cl

a_np = np.random.rand(50000).astype(np.float32)
b_np = np.random.rand(50000).astype(np.float32)

ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)

mf = cl.mem_flags
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np)
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np)

prg = cl.Program(ctx, """
__kernel void sum(
    __global const float *a_g, __global const float *b_g, __global float *res_g)
{
  int gid = get_global_id(0);
  res_g[gid] = a_g[gid] + b_g[gid];
}
""").build()

res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes)
knl = prg.sum  # Use this Kernel object for repeated calls
knl(queue, a_np.shape, None, a_g, b_g, res_g)

res_np = np.empty_like(a_np)
cl.enqueue_copy(queue, res_np, res_g)

# Check on CPU with Numpy:
print(res_np - (a_np + b_np))
print("res_np       : ", res_np)
print("(a_np + b_np): ", (a_np + b_np))
print(np.linalg.norm(res_np - (a_np + b_np)))
assert np.allclose(res_np, a_np + b_np)

运行效果:

PS D:\demos\t_pyopencl\demo> python.exe .\demo.py
Choose platform:
[0] <pyopencl.Platform 'Intel(R) OpenCL' at 0x1c89caa1e40>
Choice [0]:
Choose device(s):
[0] <pyopencl.Device 'Intel(R) HD Graphics 530' on 'Intel(R) OpenCL' at 0x1c89cb07160>
[1] <pyopencl.Device 'Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz' on 'Intel(R) OpenCL' at 0x1c89c33df60>
Choice, comma-separated [0]:
Set the environment variable PYOPENCL_CTX=':' to avoid being asked again.
C:\Users\ysouyno\AppData\Local\Programs\Python\Python310\lib\site-packages\pyopencl\__init__.py:270: CompilerWarning: Non-empty compiler output encountered. Set the environment variable PYOPENCL_COMPILER_OUTPUT=1 to see more.
  warn("Non-empty compiler output encountered. Set the "
[0. 0. 0. ... 0. 0. 0.]
res_np       :  [1.3405421  0.803946   0.69187665 ... 0.9120757  0.5142325  0.5313911 ]
(a_np + b_np):  [1.3405421  0.803946   0.69187665 ... 0.9120757  0.5142325  0.5313911 ]
0.0
PS D:\demos\t_pyopencl\demo>

这里主要说明下PYOPENCL_CTX的环境变量设置,powershell的输出如下:

PS D:\demos\t_pyopencl\demo> $env:PYOPENCL_COMPILER_OUTPUT=1
PS D:\demos\t_pyopencl\demo> $env:PYOPENCL_CTX='0:0'
PS D:\demos\t_pyopencl\demo> python.exe .\demo.py
C:\Users\ysouyno\AppData\Local\Programs\Python\Python310\lib\site-packages\pyopencl\__init__.py:268: CompilerWarning: Built kernel retrieved from cache. Original from-source build had warnings:
Build on <pyopencl.Device 'Intel(R) HD Graphics 530' on 'Intel(R) OpenCL' at 0x1f43640f230> succeeded, but said:

fcl build 1 succeeded.
bcl build succeeded.

  warn(text, CompilerWarning)
[0. 0. 0. ... 0. 0. 0.]
res_np       :  [1.1701384  1.1061795  1.1798061  ... 0.99728197 0.5002522  1.80389   ]
(a_np + b_np):  [1.1701384  1.1061795  1.1798061  ... 0.99728197 0.5002522  1.80389   ]
0.0
PS D:\demos\t_pyopencl\demo> $env:PYOPENCL_CTX='0:1'
PS D:\demos\t_pyopencl\demo> python.exe .\demo.py
C:\Users\ysouyno\AppData\Local\Programs\Python\Python310\lib\site-packages\pyopencl\__init__.py:268: CompilerWarning: Built kernel retrieved from cache. Original from-source build had warnings:
Build on <pyopencl.Device 'Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz' on 'Intel(R) OpenCL' at 0x1a9cda3ade0> succeeded, but said:

Compilation started
Compilation done
Linking started
Linking done
Device build started
Device build done
Kernel <sum> was successfully vectorized (8)
Done.
  warn(text, CompilerWarning)
C:\Users\ysouyno\AppData\Local\Programs\Python\Python310\lib\site-packages\pyopencl\__init__.py:268: CompilerWarning: From-binary build succeeded, but resulted in non-empty logs:
Build on <pyopencl.Device 'Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz' on 'Intel(R) OpenCL' at 0x1a9cda3ade0> succeeded, but said:

Device build started
Device build done
Reload Program Binary Object.
  warn(text, CompilerWarning)
[0. 0. 0. ... 0. 0. 0.]
res_np       :  [0.3848073 1.6682227 1.1773763 ... 0.801935  1.1112832 1.5165817]
(a_np + b_np):  [0.3848073 1.6682227 1.1773763 ... 0.801935  1.1112832 1.5165817]
0.0
PS D:\demos\t_pyopencl\demo>

cmd的输出如下:

D:\demos\t_pyopencl\demo>set PYOPENCL_COMPILER_OUTPUT=1

D:\demos\t_pyopencl\demo>set PYOPENCL_CTX=0:0

D:\demos\t_pyopencl\demo>python demo.py
C:\Users\ysouyno\AppData\Local\Programs\Python\Python310\lib\site-packages\pyopencl\__init__.py:268: CompilerWarning: Built kernel retrieved from cache. Original from-source build had warnings:
Build on <pyopencl.Device 'Intel(R) HD Graphics 530' on 'Intel(R) OpenCL' at 0x1bc6e0b1250> succeeded, but said:

fcl build 1 succeeded.
bcl build succeeded.

  warn(text, CompilerWarning)
[0. 0. 0. ... 0. 0. 0.]
res_np       :  [0.6949563  1.1166335  0.57082427 ... 1.3923297  1.2337677  1.4651983 ]
(a_np + b_np):  [0.6949563  1.1166335  0.57082427 ... 1.3923297  1.2337677  1.4651983 ]
0.0

D:\demos\t_pyopencl\demo>set PYOPENCL_CTX=0:1

D:\demos\t_pyopencl\demo>python demo.py
C:\Users\ysouyno\AppData\Local\Programs\Python\Python310\lib\site-packages\pyopencl\__init__.py:268: CompilerWarning: Built kernel retrieved from cache. Original from-source build had warnings:
Build on <pyopencl.Device 'Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz' on 'Intel(R) OpenCL' at 0x22c8a4e1b60> succeeded, but said:

Compilation started
Compilation done
Linking started
Linking done
Device build started
Device build done
Kernel <sum> was successfully vectorized (8)
Done.
  warn(text, CompilerWarning)
C:\Users\ysouyno\AppData\Local\Programs\Python\Python310\lib\site-packages\pyopencl\__init__.py:268: CompilerWarning: From-binary build succeeded, but resulted in non-empty logs:
Build on <pyopencl.Device 'Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz' on 'Intel(R) OpenCL' at 0x22c8a4e1b60> succeeded, but said:

Device build started
Device build done
Reload Program Binary Object.
  warn(text, CompilerWarning)
[0. 0. 0. ... 0. 0. 0.]
res_np       :  [1.0808852  1.2096584  0.8938436  ... 0.35234842 0.3697133  0.88079625]
(a_np + b_np):  [1.0808852  1.2096584  0.8938436  ... 0.35234842 0.3697133  0.88079625]
0.0

D:\demos\t_pyopencl\demo>

注意powershellcmd的设置方法不一样:

SHELLENV
powershell$env:PYOPENCL_CTX=‘0:1’
cmdset PYOPENCL_CTX=0:1

:0表示platform:1表示device

为什么这个时候了还要去了解pyopencl的使用?因为python的代码更简洁,可以快速的测试kernel函数,减少开发周期,就像当年学习octave一样。

文章来源:https://blog.csdn.net/ftuc5dn/article/details/135588040
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。