Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: C++  |  size: 2.37 KB  |  views: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <thrust/device_vector.h>
  2. #include <thrust/sort.h>
  3. #include <thrust/extrema.h>
  4. #include <iostream>
  5.  
  6. extern "C" {
  7.  
  8. // ------ Sort algorithms ------ //
  9.  
  10. // Sort for integer arrays
  11. void sort_int_wrapper( int *data, int N)
  12. {
  13. // Wrap raw pointer with a device_ptr
  14. thrust::device_ptr <int> dev_ptr(data);
  15. // Use device_ptr in Thrust sort algorithm
  16. thrust::sort(dev_ptr, dev_ptr+N);
  17. }
  18.  
  19. // Sort for float arrays
  20. void sort_float_wrapper( float *data, int N)
  21. {
  22. thrust::device_ptr <float> dev_ptr(data);
  23. thrust::sort(dev_ptr, dev_ptr+N);
  24. }
  25.  
  26. // Sort for double arrays
  27. void sort_double_wrapper( double *data, int N)
  28. {
  29.  
  30. thrust::device_ptr <double> dev_ptr(data);
  31. thrust::sort(dev_ptr, dev_ptr+N);
  32. }
  33.  
  34. // ------ Min/Max algorithms ------ //
  35.  
  36. // Min for integer arrays
  37. int min_int_wrapper( int *data, int N)
  38. {
  39. thrust::device_ptr <int> dev_ptr(data);
  40. // Return index of minimum element:
  41. return thrust::min_element(dev_ptr, dev_ptr + N) - dev_ptr;
  42. }
  43.  
  44. int min_float_wrapper( float *data, int N)
  45. {
  46. thrust::device_ptr <float> dev_ptr(data);
  47. return thrust::min_element(dev_ptr, dev_ptr + N) - dev_ptr;
  48. }
  49.  
  50. int min_double_wrapper( double *data, int N)
  51. {
  52. thrust::device_ptr <double> dev_ptr(data);
  53. return thrust::min_element(dev_ptr, dev_ptr + N) - dev_ptr;
  54. }
  55.  
  56. // Max for integer arrays
  57. int max_int_wrapper( int *data, int N)
  58. {
  59. thrust::device_ptr <int> dev_ptr(data);
  60. // Return index of maximum element:
  61. return thrust::max_element(dev_ptr, dev_ptr + N) - dev_ptr;
  62. }
  63.  
  64. int max_float_wrapper( float *data, int N)
  65. {
  66. thrust::device_ptr <float> dev_ptr(data);
  67. return thrust::max_element(dev_ptr, dev_ptr + N) - dev_ptr;
  68. }
  69.  
  70. int max_double_wrapper( double *data, int N)
  71. {
  72. thrust::device_ptr <double> dev_ptr(data);
  73. return thrust::max_element(dev_ptr, dev_ptr + N) - dev_ptr;
  74. }
  75.  
  76. // ------ Reduction (sum) algorithms ------ //
  77.  
  78. // Sum for integer arrays
  79. int reduce_int_wrapper( int *data, int N)
  80. {
  81. // Wrap raw pointer with a device_ptr
  82. thrust::device_ptr <int> dev_ptr(data);
  83. // Use device_ptr in Thrust reduce algorithm
  84. return thrust::reduce(dev_ptr, dev_ptr+N);
  85. }
  86.  
  87. float reduce_float_wrapper( float *data, int N)
  88. {
  89. thrust::device_ptr <float> dev_ptr(data);
  90. return thrust::reduce(dev_ptr, dev_ptr+N);
  91. }
  92.  
  93. double reduce_double_wrapper( double *data, int N)
  94. {
  95.  
  96. thrust::device_ptr <double> dev_ptr(data);
  97. return thrust::reduce(dev_ptr, dev_ptr+N);
  98. }
  99.  
  100. }