Created
August 21, 2016 22:11
-
-
Save hughperkins/a4d92ed96fae556a678923594b069159 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <iostream> | |
| #include <sstream> | |
| #include <stdexcept> | |
| using namespace std; | |
| #include "CL/cl.h" | |
| #include <boost/compute/core.hpp> | |
| template<typename T> | |
| std::string toString(T val ) { // not terribly efficient, but works... | |
| std::ostringstream myostringstream; | |
| myostringstream << val; | |
| return myostringstream.str(); | |
| } | |
| void checkError( cl_int err ) { | |
| if (err != CL_SUCCESS) { | |
| throw std::runtime_error( "Error: " + toString(err) ); | |
| } | |
| } | |
| int main( int argc, char *argv[] ) { | |
| cl_int err; | |
| cl_device_id *device_ids; | |
| cl_uint num_platforms; | |
| cl_uint num_devices; | |
| cl_platform_id platform_id; | |
| cl_device_id device; | |
| cl_context ctx; | |
| cl_command_queue queue; | |
| cl_program program; | |
| checkError( clGetPlatformIDs(1, &platform_id, &num_platforms) ); | |
| checkError( clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device, &num_devices) ); | |
| device_ids = new cl_device_id[num_devices]; | |
| checkError( clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, num_devices, device_ids, &num_devices) ); | |
| device = device_ids[0]; | |
| ctx = clCreateContext(0, 1, &device, NULL, NULL, &err); | |
| checkError(err); | |
| queue = clCreateCommandQueue(ctx, device, 0, &err); | |
| checkError(err); | |
| const int N = 5; | |
| float mask[N] = {1,1,0,0,1}; | |
| float data[N] = {3,4,5,1,2}; | |
| cl_mem bufMask = clCreateBuffer(ctx, CL_MEM_READ_ONLY, N * sizeof(*mask), | |
| NULL, &err); | |
| cl_mem bufData = clCreateBuffer(ctx, CL_MEM_READ_ONLY, N * sizeof(*data), | |
| NULL, &err); | |
| err = clEnqueueWriteBuffer(queue, bufMask, CL_TRUE, 0, | |
| N * sizeof(*mask), mask, 0, NULL, NULL); | |
| err = clEnqueueWriteBuffer(queue, bufData, CL_TRUE, 0, | |
| N * sizeof(*data), data, 0, NULL, NULL); | |
| boost::compute::context boost_context(ctx); | |
| boost::compute::command_queue boost_queue(queue); | |
| // trying to fill in this bit... | |
| checkError( clFinish( queue ) ); | |
| checkError( clEnqueueReadBuffer( queue, bufData, CL_TRUE, 0, sizeof(float) * N, data, 0, NULL, NULL) ); | |
| checkError( clFinish( queue ) ); | |
| for( int i = 0; i < N; i++ ) { | |
| cout << data[i] << " "; | |
| } | |
| cout << endl; | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment