PyOpenCL库安装
windows+R 输入"dxdiag" 查看显卡信息
NVIDIA 用户
访问NVIDIA OpenCL这个站点,下载OpenCL Device Query:
下载之后解压缩,从中找到任意的一个名字叫做oclDeviceQuery.exe的可执行文件,双击运行,会在同一目录下生成一个名为oclDeviceQuery.txt的文件.用记事本等编辑器打开这个文件,从其中查找关键词CL_PLATFORM_VERSION
,后面就会有版本信息.
OpenCL SW Info: CL_PLATFORM_NAME: NVIDIA CUDA CL_PLATFORM_VERSION: OpenCL 1.2 CUDA 9.1.228 OpenCL SDK Revision: 7027912 OpenCL Device Info: 1 devices found supporting OpenCL: --------------------------------- Device GeForce GTX 1050 Ti --------------------------------- CL_DEVICE_NAME: GeForce GTX 1050 Ti CL_DEVICE_VENDOR: NVIDIA Corporation CL_DRIVER_VERSION: 389.22 CL_DEVICE_VERSION: OpenCL 1.2 CUDA CL_DEVICE_OPENCL_C_VERSION: OpenCL C 1.2 CL_DEVICE_TYPE: CL_DEVICE_TYPE_GPU CL_DEVICE_MAX_COMPUTE_UNITS: 6 CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS: 3 CL_DEVICE_MAX_WORK_ITEM_SIZES: 1024 / 1024 / 64 CL_DEVICE_MAX_WORK_GROUP_SIZE: 1024 CL_DEVICE_MAX_CLOCK_FREQUENCY: 1620 MHz CL_DEVICE_ADDRESS_BITS: 64 CL_DEVICE_MAX_MEM_ALLOC_SIZE: 1024 MByte CL_DEVICE_GLOBAL_MEM_SIZE: 4096 MByte
可以看见支持OpenCL1.2
下载编译好的二进制安装文件
在确定了所用显卡支持的OpenCL版本后,到https://www.lfd.uci.edu/~gohlke/pythonlibs下载和OpenCL版本以及Python版本/操作系统版本都对应的whl文件.(也可以下载其他新的包)
例如,如上文所述,我的1050显卡支持的OpenCL版本是1.2,我的Python版本是3.7,我的操作系统是64bit的Windows,那么就下载pyopencl-2018.2.2+cl12-cp37-cp37m-win_amd64.whl.
进入cmd输入命令: python --version
这里面的cl12的意思就是OpenCL1.2,cp37表示的应该是Python3.7,win_amd64表示是64位Windows.
一定要先确定你的OpenCL版本/Python版本/Windows版本,然后找好对应的whl文件下载.
下载好了之后,通过 powershell 或者 cmd 到达刚刚下载的 whl 文件所在目录,用 pip 来安装就可以了.
cd D:\GISSAWORK\OPENCL pip install pyopencl-2018.2.2+cl12-cp37-cp37m-win_amd64.whl
测试是否按照成功
在Python中运行下面的来自PyOpenCL官网的代码来测试是否能够在Python下调用CL:
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function 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) prg.sum(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(np.linalg.norm(res_np - (a_np + b_np))) # 代码来自 https://documen.tician.de/pyopencl/index.html
如果你的机器上有多个支持OpenCL的设备,比如同时有核显和独显或者多块独显,就会遇到一个提问界面,询问你用哪个设备执行上述OpenCL操作.比如我就遇到了两个选择:
没有报错 说明安装成功
一些教程参考
Tutorials
-
Gaston Hillar’s two-part article series in Dr. Dobb’s Journal provides a friendly introduction to PyOpenCL.
-
Simon McIntosh-Smith and Tom Deakin’s course Hands-on OpenCL contains both lecture slides and exercises (with solutions) (The course covers PyOpenCL as well as OpenCL’s C and C++ APIs.)
-
PyOpenCL course at DTU GPULab and Simula (2011): Lecture 1 Lecture 2 Problem set 1 Problem set 2
-
Ian Johnson’s PyOpenCL tutorial.
参考:
https://blog.cycleuser.org/pyopencl-installation-on-windows.html
https://documen.tician.de/pyopencl/ 一些教程