this example demonstrates how to use the Boost.Compute classes to setup and run a simple vector addition kernel on the GPU
| 17 | // this example demonstrates how to use the Boost.Compute classes to |
| 18 | // setup and run a simple vector addition kernel on the GPU |
| 19 | int main() |
| 20 | { |
| 21 | // get the default device |
| 22 | compute::device device = compute::system::default_device(); |
| 23 | |
| 24 | // create a context for the device |
| 25 | compute::context context(device); |
| 26 | |
| 27 | // setup input arrays |
| 28 | float a[] = { 1, 2, 3, 4 }; |
| 29 | float b[] = { 5, 6, 7, 8 }; |
| 30 | |
| 31 | // make space for the output |
| 32 | float c[] = { 0, 0, 0, 0 }; |
| 33 | |
| 34 | // create memory buffers for the input and output |
| 35 | compute::buffer buffer_a(context, 4 * sizeof(float)); |
| 36 | compute::buffer buffer_b(context, 4 * sizeof(float)); |
| 37 | compute::buffer buffer_c(context, 4 * sizeof(float)); |
| 38 | |
| 39 | // source code for the add kernel |
| 40 | const char source[] = |
| 41 | "__kernel void add(__global const float *a," |
| 42 | " __global const float *b," |
| 43 | " __global float *c)" |
| 44 | "{" |
| 45 | " const uint i = get_global_id(0);" |
| 46 | " c[i] = a[i] + b[i];" |
| 47 | "}"; |
| 48 | |
| 49 | // create the program with the source |
| 50 | compute::program program = |
| 51 | compute::program::create_with_source(source, context); |
| 52 | |
| 53 | // compile the program |
| 54 | program.build(); |
| 55 | |
| 56 | // create the kernel |
| 57 | compute::kernel kernel(program, "add"); |
| 58 | |
| 59 | // set the kernel arguments |
| 60 | kernel.set_arg(0, buffer_a); |
| 61 | kernel.set_arg(1, buffer_b); |
| 62 | kernel.set_arg(2, buffer_c); |
| 63 | |
| 64 | // create a command queue |
| 65 | compute::command_queue queue(context, device); |
| 66 | |
| 67 | // write the data from 'a' and 'b' to the device |
| 68 | queue.enqueue_write_buffer(buffer_a, 0, 4 * sizeof(float), a); |
| 69 | queue.enqueue_write_buffer(buffer_b, 0, 4 * sizeof(float), b); |
| 70 | |
| 71 | // run the add kernel |
| 72 | queue.enqueue_1d_range_kernel(kernel, 0, 4, 0); |
| 73 | |
| 74 | // transfer results back to the host array 'c' |
| 75 | queue.enqueue_read_buffer(buffer_c, 0, 4 * sizeof(float), c); |
| 76 |
nothing calls this directly
no test coverage detected