Skip to content

Instantly share code, notes, and snippets.

@ryanorendorff
Last active October 6, 2020 04:34
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 ryanorendorff/dfea0dc2051e70b282a23eadbb63d05d to your computer and use it in GitHub Desktop.
Save ryanorendorff/dfea0dc2051e70b282a23eadbb63d05d to your computer and use it in GitHub Desktop.
Installing PyOpenCL on Mac OS Catalina
let
pkgs = import (builtins.fetchTarball {
url =
"https://github.com/NixOS/nixpkgs-channels/archive/nixpkgs-20.03-darwin.tar.gz";
sha256 = "0wci9vylb31wvl3zslw38537dgdvd89w58fvj9acnbv9l378x3hh";
}) { };
packageOverrides = self: super: {
pyopencl = super.pyopencl.overridePythonAttrs (old: rec {
buildInputs = with pkgs; [ opencl-headers pybind11 libGL_driver.dev ];
});
};
python = pkgs.python3.override {
inherit packageOverrides;
};
# `setuptools` is required to solve the missing `pkg_resources` module not
# found error. See https://nixos.wiki/wiki/Packaging/Python
pythonEnv =
python.withPackages (ps: with ps; [ numpy ipython pyopencl setuptools ]);
in pythonEnv.env
#!/usr/bin/env python
# Copied from the pyopencl website with minor modifications
import numpy as np
import pyopencl as cl
a_np = np.random.rand(500).astype(np.float32) # Modified from original 50000 elements to 500
b_np = np.random.rand(500).astype(np.float32) # Modified from original 50000 elements to 500
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)))
assert np.allclose(res_np, a_np + b_np)
@ryanorendorff
Copy link
Author

Note that setuptools is needed when running in an environment in order to solve the error

ModuleNotFoundError: No module named 'pkg_resources'

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