Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. 58458 -> 1000 occurences
  2. 15 -> 412 occurences
  3.  
  4. #include <time.h> // --- time
  5. #include <stdlib.h> // --- srand, rand
  6. #include <iostream>
  7.  
  8. #include <thrusthost_vector.h>
  9. #include <thrustdevice_vector.h>
  10. #include <thrustsort.h>
  11. #include <thrustiteratorzip_iterator.h>
  12. #include <thrustunique.h>
  13. #include <thrust/binary_search.h>
  14. #include <thrustadjacent_difference.h>
  15.  
  16. #include "Utilities.cuh"
  17. #include "TimingGPU.cuh"
  18.  
  19. //#define VERBOSE
  20. #define NO_HISTOGRAM
  21.  
  22. /********/
  23. /* MAIN */
  24. /********/
  25. int main() {
  26.  
  27. const int N = 1048576;
  28. //const int N = 20;
  29. //const int N = 128;
  30.  
  31. TimingGPU timerGPU;
  32.  
  33. // --- Initialize random seed
  34. srand(time(NULL));
  35.  
  36. thrust::host_vector<int> h_code(N);
  37.  
  38. for (int k = 0; k < N; k++) {
  39. // --- Generate random numbers between 0 and 9
  40. h_code[k] = (rand() % 10);
  41. }
  42.  
  43. thrust::device_vector<int> d_code(h_code);
  44. //thrust::device_vector<unsigned int> d_counting(N);
  45.  
  46. thrust::sort(d_code.begin(), d_code.end());
  47.  
  48. h_code = d_code;
  49.  
  50. timerGPU.StartCounter();
  51.  
  52. #ifdef NO_HISTOGRAM
  53. // --- The number of d_cumsum bins is equal to the maximum value plus one
  54. int num_bins = d_code.back() + 1;
  55.  
  56. thrust::device_vector<int> d_code_unique(num_bins);
  57. thrust::unique_copy(d_code.begin(), d_code.end(), d_code_unique.begin());
  58. thrust::device_vector<int> d_counting(num_bins);
  59. thrust::upper_bound(d_code.begin(), d_code.end(), d_code_unique.begin(), d_code_unique.end(), d_counting.begin());
  60. #else
  61. thrust::device_vector<int> d_cumsum;
  62.  
  63. // --- The number of d_cumsum bins is equal to the maximum value plus one
  64. int num_bins = d_code.back() + 1;
  65.  
  66. // --- Resize d_cumsum storage
  67. d_cumsum.resize(num_bins);
  68.  
  69. // --- Find the end of each bin of values - Cumulative d_cumsum
  70. thrust::counting_iterator<int> search_begin(0);
  71. thrust::upper_bound(d_code.begin(), d_code.end(), search_begin, search_begin + num_bins, d_cumsum.begin());
  72.  
  73. // --- Compute the histogram by taking differences of the cumulative d_cumsum
  74. //thrust::device_vector<int> d_counting(num_bins);
  75. //thrust::adjacent_difference(d_cumsum.begin(), d_cumsum.end(), d_counting.begin());
  76. #endif
  77.  
  78. printf("Timing GPU = %fn", timerGPU.GetCounter());
  79.  
  80. #ifdef VERBOSE
  81. thrust::host_vector<int> h_counting(d_counting);
  82. printf("Aftern");
  83. for (int k = 0; k < N; k++) printf("code = %in", h_code[k]);
  84. #ifndef NO_HISTOGRAM
  85. thrust::host_vector<int> h_cumsum(d_cumsum);
  86. printf("nCountingn");
  87. for (int k = 0; k < num_bins; k++) printf("element = %i; counting = %i; cumsum = %in", k, h_counting[k], h_cumsum[k]);
  88. #else
  89. thrust::host_vector<int> h_code_unique(d_code_unique);
  90.  
  91. printf("nCountingn");
  92. for (int k = 0; k < N; k++) printf("element = %i; counting = %in", h_code_unique[k], h_counting[k]);
  93. #endif
  94. #endif
  95. }
  96.  
  97. First approach: 2.35ms
  98. First approach without thrust::adjacent_difference: 1.52
  99. Second approach: 4.67ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement