simple_kernel.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #include <iostream>
  11. #include <boost/compute/core.hpp>
  12. namespace compute = boost::compute;
  13. // this example demonstrates how to use the Boost.Compute classes to
  14. // setup and run a simple vector addition kernel on the GPU
  15. int main()
  16. {
  17. // get the default device
  18. compute::device device = compute::system::default_device();
  19. // create a context for the device
  20. compute::context context(device);
  21. // setup input arrays
  22. float a[] = { 1, 2, 3, 4 };
  23. float b[] = { 5, 6, 7, 8 };
  24. // make space for the output
  25. float c[] = { 0, 0, 0, 0 };
  26. // create memory buffers for the input and output
  27. compute::buffer buffer_a(context, 4 * sizeof(float));
  28. compute::buffer buffer_b(context, 4 * sizeof(float));
  29. compute::buffer buffer_c(context, 4 * sizeof(float));
  30. // source code for the add kernel
  31. const char source[] =
  32. "__kernel void add(__global const float *a,"
  33. " __global const float *b,"
  34. " __global float *c)"
  35. "{"
  36. " const uint i = get_global_id(0);"
  37. " c[i] = a[i] + b[i];"
  38. "}";
  39. // create the program with the source
  40. compute::program program =
  41. compute::program::create_with_source(source, context);
  42. // compile the program
  43. program.build();
  44. // create the kernel
  45. compute::kernel kernel(program, "add");
  46. // set the kernel arguments
  47. kernel.set_arg(0, buffer_a);
  48. kernel.set_arg(1, buffer_b);
  49. kernel.set_arg(2, buffer_c);
  50. // create a command queue
  51. compute::command_queue queue(context, device);
  52. // write the data from 'a' and 'b' to the device
  53. queue.enqueue_write_buffer(buffer_a, 0, 4 * sizeof(float), a);
  54. queue.enqueue_write_buffer(buffer_b, 0, 4 * sizeof(float), b);
  55. // run the add kernel
  56. queue.enqueue_1d_range_kernel(kernel, 0, 4, 0);
  57. // transfer results back to the host array 'c'
  58. queue.enqueue_read_buffer(buffer_c, 0, 4 * sizeof(float), c);
  59. // print out results in 'c'
  60. std::cout << "c: [" << c[0] << ", "
  61. << c[1] << ", "
  62. << c[2] << ", "
  63. << c[3] << "]" << std::endl;
  64. return 0;
  65. }