]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/example/simple_kernel.cpp
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
13 #include <boost/compute/core.hpp>
15 namespace compute
= boost::compute
;
17 // this example demonstrates how to use the Boost.Compute classes to
18 // setup and run a simple vector addition kernel on the GPU
21 // get the default device
22 compute::device device
= compute::system::default_device();
24 // create a context for the device
25 compute::context
context(device
);
28 float a
[] = { 1, 2, 3, 4 };
29 float b
[] = { 5, 6, 7, 8 };
31 // make space for the output
32 float c
[] = { 0, 0, 0, 0 };
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));
39 // source code for the add kernel
41 "__kernel void add(__global const float *a,"
42 " __global const float *b,"
45 " const uint i = get_global_id(0);"
46 " c[i] = a[i] + b[i];"
49 // create the program with the source
50 compute::program program
=
51 compute::program::create_with_source(source
, context
);
53 // compile the program
57 compute::kernel
kernel(program
, "add");
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
);
64 // create a command queue
65 compute::command_queue
queue(context
, device
);
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
);
72 queue
.enqueue_1d_range_kernel(kernel
, 0, 4, 0);
74 // transfer results back to the host array 'c'
75 queue
.enqueue_read_buffer(buffer_c
, 0, 4 * sizeof(float), c
);
77 // print out results in 'c'
78 std::cout
<< "c: [" << c
[0] << ", "
81 << c
[3] << "]" << std::endl
;