Created
October 9, 2012 11:11
-
-
Save pklaus/3858016 to your computer and use it in GitHub Desktop.
Using OpenCL on Mac OS X - Test for OpenCL Devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dumpcl: | |
clang -framework OpenCL dumpcl.c -o dumpcl && ./dumpcl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code above is no longer meaningfull in 2018.