Skip to content

Instantly share code, notes, and snippets.

@pklaus
Created October 9, 2012 11:11
Show Gist options
  • Save pklaus/3858016 to your computer and use it in GitHub Desktop.
Save pklaus/3858016 to your computer and use it in GitHub Desktop.
Using OpenCL on Mac OS X - Test for OpenCL Devices
// http://stackoverflow.com/a/7898347
#include <stdio.h>
#include <stdlib.h>
#include <OpenCL/opencl.h>
int main(int argc, char* const argv[]) {
cl_uint num_devices, i;
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
cl_device_id* devices = calloc(sizeof(cl_device_id), num_devices);
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
char buf[128];
for (i = 0; i < num_devices; i++) {
clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL);
fprintf(stdout, "Device %s supports ", buf);
clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, 128, buf, NULL);
fprintf(stdout, "%s\n", buf);
}
free(devices);
}
dumpcl:
clang -framework OpenCL dumpcl.c -o dumpcl && ./dumpcl
#!/usr/bin/env python
# http://documen.tician.de/pyopencl/
import pyopencl as cl
import numpy
import numpy.linalg as la
N = 1E8
a = numpy.random.rand(N).astype(numpy.float32)
b = numpy.random.rand(N).astype(numpy.float32)
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)
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes)
prg = cl.Program(ctx, """
__kernel void sum(__global const float *a,
__global const float *b, __global float *c)
{
int gid = get_global_id(0);
c[gid] = a[gid] + b[gid];
}
""").build()
prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf)
a_plus_b = numpy.empty_like(a)
cl.enqueue_copy(queue, a_plus_b, dest_buf)
print la.norm(a_plus_b - (a+b))
@jvmvik
Copy link

jvmvik commented Jun 3, 2018

The code above is no longer meaningfull in 2018.

  • calloc return void
  • buf is never declared.

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