| 20 | namespace compute = boost::compute; |
| 21 | |
| 22 | int main() |
| 23 | { |
| 24 | // get default device and setup context |
| 25 | compute::device device = compute::system::default_device(); |
| 26 | compute::context context(device); |
| 27 | compute::command_queue queue(context, device); |
| 28 | |
| 29 | // generate random data on the host |
| 30 | std::vector<float> host_vector(10000); |
| 31 | std::generate(host_vector.begin(), host_vector.end(), rand); |
| 32 | |
| 33 | // create a vector on the device |
| 34 | compute::vector<float> device_vector(host_vector.size(), context); |
| 35 | |
| 36 | // transfer data from the host to the device |
| 37 | compute::copy( |
| 38 | host_vector.begin(), host_vector.end(), device_vector.begin(), queue |
| 39 | ); |
| 40 | |
| 41 | // calculate the square-root of each element in-place |
| 42 | compute::transform( |
| 43 | device_vector.begin(), |
| 44 | device_vector.end(), |
| 45 | device_vector.begin(), |
| 46 | compute::sqrt<float>(), |
| 47 | queue |
| 48 | ); |
| 49 | |
| 50 | // copy values back to the host |
| 51 | compute::copy( |
| 52 | device_vector.begin(), device_vector.end(), host_vector.begin(), queue |
| 53 | ); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | //] |