Guest User

Untitled

a guest
Jul 5th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <boost/compute/core.hpp>
  4.  
  5. namespace compute = boost::compute;
  6.  
  7. // this example demonstrates how to use the Boost.Compute classes to
  8. // setup and run a simple vector addition kernel on the GPU
  9. int main()
  10. {
  11.     // get the default device
  12.     compute::device device = compute::system::default_device();
  13.  
  14.     // create a context for the device
  15.     compute::context context(device);
  16.  
  17.     // setup input arrays
  18.     std::vector<float> a = { 1, 2, 3, 4 };
  19.     std::vector<float> b = { 5, 6, 7, 8 };
  20.  
  21.     // make space for the output
  22.     std::vector<float> c = { 5, 6, 7, 8 };
  23.  
  24.     // create memory buffers for the input and output
  25.     compute::buffer buffer_a(context, 4 * sizeof(float), compute::buffer::read_only);
  26.     compute::buffer buffer_b(context, 4 * sizeof(float), compute::buffer::read_only);
  27.     compute::buffer buffer_c(context, 4 * sizeof(float));
  28.  
  29.     // source code for the add kernel
  30.     const char source[] =
  31.         "__kernel void add(__global __read_only const float *a,"
  32.         "                  __global __read_only  const float *b,"
  33.         "                  __global float *c)"
  34.         "{"
  35.         "    const uint i = get_global_id(0);"
  36.         "    c[i] = a[i] + b[i];"
  37.         "}";
  38.  
  39.     // create the program with the source
  40.     compute::program program =
  41.         compute::program::create_with_source(source, context);
  42.  
  43.     // compile the program
  44.     program.build();
  45.  
  46.     // create the kernel
  47.     compute::kernel kernel(program, "add");
  48.  
  49.     // set the kernel arguments
  50.     kernel.set_arg(0, buffer_a);
  51.     kernel.set_arg(1, buffer_b);
  52.     kernel.set_arg(2, buffer_c);
  53.  
  54.     // create a command queue
  55.     compute::command_queue queue(context, device);
  56.  
  57.     // write the data from 'a' and 'b' to the device
  58.     queue.enqueue_write_buffer(buffer_a, 0, 4 * sizeof(float), a.data());
  59.     queue.enqueue_write_buffer(buffer_b, 0, 4 * sizeof(float), b.data());
  60.  
  61.     // run the add kernel
  62.     queue.enqueue_1d_range_kernel(kernel, 0, 4, 0);
  63.     // transfer results back to the host array 'c'
  64.     queue.enqueue_read_buffer(buffer_c, 0, 4 * sizeof(float), c.data());
  65.  
  66.     // print out results in 'c'
  67.     std::cout << "c: [" << c[0] << ", "
  68.                         << c[1] << ", "
  69.                         << c[2] << ", "
  70.                         << c[3] << "]" << std::endl;
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment