MCPcopy Create free account
hub / github.com/antmachineintelligence/mtgbmcode / main

Function main

compute/example/sort_vector.cpp:28–68  ·  view source on GitHub ↗

this example demonstrates how to sort a vector of ints on the GPU

Source from the content-addressed store, hash-verified

26
27// this example demonstrates how to sort a vector of ints on the GPU
28int 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}

Callers

nothing calls this directly

Calls 6

sortFunction · 0.85
copyFunction · 0.85
generateFunction · 0.50
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected