this example demonstrates how to sort a vector of ints on the GPU
| 26 | |
| 27 | // this example demonstrates how to sort a vector of ints on the GPU |
| 28 | int main() |
| 29 | { |
| 30 | // create vector of random values on the host |
| 31 | std::vector<int> host_vector(10); |
| 32 | std::generate(host_vector.begin(), host_vector.end(), rand_int); |
| 33 | |
| 34 | // print out input vector |
| 35 | std::cout << "input: [ "; |
| 36 | for(size_t i = 0; i < host_vector.size(); i++){ |
| 37 | std::cout << host_vector[i]; |
| 38 | |
| 39 | if(i != host_vector.size() - 1){ |
| 40 | std::cout << ", "; |
| 41 | } |
| 42 | } |
| 43 | std::cout << " ]" << std::endl; |
| 44 | |
| 45 | // transfer the values to the device |
| 46 | compute::vector<int> device_vector = host_vector; |
| 47 | |
| 48 | // sort the values on the device |
| 49 | compute::sort(device_vector.begin(), device_vector.end()); |
| 50 | |
| 51 | // transfer the values back to the host |
| 52 | compute::copy(device_vector.begin(), |
| 53 | device_vector.end(), |
| 54 | host_vector.begin()); |
| 55 | |
| 56 | // print out the sorted vector |
| 57 | std::cout << "output: [ "; |
| 58 | for(size_t i = 0; i < host_vector.size(); i++){ |
| 59 | std::cout << host_vector[i]; |
| 60 | |
| 61 | if(i != host_vector.size() - 1){ |
| 62 | std::cout << ", "; |
| 63 | } |
| 64 | } |
| 65 | std::cout << " ]" << std::endl; |
| 66 | |
| 67 | return 0; |
| 68 | } |