Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. $ cat testcuda.cu
  2. #include <thrust/version.h>
  3. #include <thrust/host_vector.h>
  4. #include <thrust/device_vector.h>
  5. #include <thrust/generate.h>
  6. #include <thrust/sort.h>
  7. #include <thrust/copy.h>
  8. #include <algorithm>
  9. #include <cstdlib>
  10. #include <time.h>
  11.  
  12. using namespace std;
  13.  
  14. #define USE_THRUST
  15.  
  16. int main(void)
  17. {
  18. try{
  19. int major = THRUST_MAJOR_VERSION;
  20. int minor = THRUST_MINOR_VERSION;
  21.  
  22. std::cout << "Thrust v" << major << "." << minor << std::endl;
  23.  
  24. // generate 32M random numbers serially
  25. #ifdef USE_THRUST
  26. thrust::host_vector<int> h_vec(32 << 24);
  27. #else
  28. vector<int> h_vec(32 << 23);
  29. #endif
  30.  
  31. std::generate(h_vec.begin(), h_vec.end(), rand);
  32. std::cout << "1." << time(NULL) << endl;
  33.  
  34. #ifdef USE_THRUST
  35. // transfer data to the device
  36. thrust::device_vector<int> d_vec = h_vec;
  37. cout << "2." << time(NULL) << endl;
  38.  
  39. // sort data on the device (846M keys per second on GeForce GTX 480)
  40. thrust::sort(d_vec.begin(), d_vec.end());
  41. //cout << "3." << time(NULL) << endl;
  42.  
  43. // transfer data back to host
  44. thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
  45. #else
  46. std::sort(h_vec.begin(), h_vec.end());
  47. #endif
  48.  
  49. std::cout << "4." << time(NULL) << endl;
  50. }catch (std::bad_alloc &e){
  51. std::cerr << "Thrust allocation error: " << e.what() << std::endl;
  52. exit(-1);
  53. }
  54. return 0;
  55. }
  56. $ nvcc -arch=sm_20 -o testcuda testcuda.cu
  57. $ ./testcuda
  58. Thrust v1.7
  59. 1.1408740810
  60. 2.1408740816
  61. Thrust allocation error: std::bad_alloc: out of memory
  62. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement