Guest User

Untitled

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