this example demonstrates how to use Boost.Compute's STL implementation to add two vectors on the GPU
| 20 | // this example demonstrates how to use Boost.Compute's STL |
| 21 | // implementation to add two vectors on the GPU |
| 22 | int main() |
| 23 | { |
| 24 | // setup input arrays |
| 25 | float a[] = { 1, 2, 3, 4 }; |
| 26 | float b[] = { 5, 6, 7, 8 }; |
| 27 | |
| 28 | // make space for the output |
| 29 | float c[] = { 0, 0, 0, 0 }; |
| 30 | |
| 31 | // create vectors and transfer data for the input arrays 'a' and 'b' |
| 32 | compute::vector<float> vector_a(a, a + 4); |
| 33 | compute::vector<float> vector_b(b, b + 4); |
| 34 | |
| 35 | // create vector for the output array |
| 36 | compute::vector<float> vector_c(4); |
| 37 | |
| 38 | // add the vectors together |
| 39 | compute::transform( |
| 40 | vector_a.begin(), |
| 41 | vector_a.end(), |
| 42 | vector_b.begin(), |
| 43 | vector_c.begin(), |
| 44 | compute::plus<float>() |
| 45 | ); |
| 46 | |
| 47 | // transfer results back to the host array 'c' |
| 48 | compute::copy(vector_c.begin(), vector_c.end(), c); |
| 49 | |
| 50 | // print out results in 'c' |
| 51 | std::cout << "c: [" << c[0] << ", " |
| 52 | << c[1] << ", " |
| 53 | << c[2] << ", " |
| 54 | << c[3] << "]" << std::endl; |
| 55 | |
| 56 | return 0; |
| 57 | } |