this example demonstrates how to use the mapped_view class to map an array of numbers to device memory and use the reduce() algorithm to calculate the sum.
| 21 | // an array of numbers to device memory and use the reduce() algorithm |
| 22 | // to calculate the sum. |
| 23 | int main() |
| 24 | { |
| 25 | // get default device and setup context |
| 26 | compute::device gpu = compute::system::default_device(); |
| 27 | compute::context context(gpu); |
| 28 | compute::command_queue queue(context, gpu); |
| 29 | std::cout << "device: " << gpu.name() << std::endl; |
| 30 | |
| 31 | // create data on host |
| 32 | int data[] = { 4, 2, 3, 7, 8, 9, 1, 6 }; |
| 33 | |
| 34 | // create mapped view on device |
| 35 | compute::mapped_view<int> view(data, 8, context); |
| 36 | |
| 37 | // use reduce() to calculate sum on the device |
| 38 | int sum = 0; |
| 39 | compute::reduce(view.begin(), view.end(), &sum, queue); |
| 40 | |
| 41 | // print the sum on the host |
| 42 | std::cout << "sum: " << sum << std::endl; |
| 43 | |
| 44 | return 0; |
| 45 | } |