Skip to content

Instantly share code, notes, and snippets.

@didyhu
Last active January 23, 2019 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didyhu/0b432f8e2c3388b38e38e940c47b46e0 to your computer and use it in GitHub Desktop.
Save didyhu/0b432f8e2c3388b38e38e940c47b46e0 to your computer and use it in GitHub Desktop.
Windows 环境下搭建 PyOpenCL
  1. 下载驱动 Nvidia
https://developer.nvidia.com/opencl
  1. 安装 pybind11
py -3 -m pip install pybind11
  1. 安装 pyopencl
$env:include="<path to opencl include dir, such as E:\dev\NVIDIA GPU Computing SDK\OpenCL\common\inc>"
$env:lib="<path to opencl lib dir, such as E:\dev\NVIDIA GPU Computing SDK\OpenCL\common\lib\x64>"
py -3 -m pip install pyopencl
  1. https://documen.tician.de/pyopencl/ 创建 demo 文件
#!/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)))
  1. 执行 demo
py -3 demo.py
@didyhu
Copy link
Author

didyhu commented Jan 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment