fork download
  1. #include <thrust/host_vector.h>
  2. #include <thrust/device_vector.h>
  3. #include <thrust/generate.h>
  4. #include <thrust/reduce.h>
  5. #include <thrust/functional.h>
  6. #include <thrust/sequence.h>
  7. #include <thrust/sort.h>
  8. #include <cstdlib>
  9.  
  10. #include <iostream>
  11.  
  12. const unsigned int kStart = 100000000;
  13. const unsigned int kEnd= 999999999;
  14.  
  15. struct divisible_by_9{
  16. __host__ __device__ unsigned int operator()(unsigned int const& input)
  17. const {
  18. //unsigned int idx=blockIdx.x*blockDim.x + threadIdx.x;
  19. unsigned int current_value = input;
  20. unsigned int l_sum=0;
  21. for(unsigned int n=9; n>1; --n){
  22. l_sum = (current_value%n) + l_sum;
  23. current_value = current_value/10;
  24. }
  25. return l_sum;
  26. }
  27. };
  28.  
  29. int main(void){
  30. //have to block memory allocation...
  31. unsigned int set_space = kEnd - kStart;
  32. unsigned int nblocks = 6;
  33. thrust::host_vector<unsigned int> block_sizes;
  34. unsigned int block_size = set_space / nblocks;
  35.  
  36. if( set_space % nblocks == 0){
  37. for(unsigned int i=0;i<nblocks; ++i) block_sizes.push_back(block_size);
  38. }
  39. else {
  40. for(unsigned int i=0;i<nblocks-1; ++i) block_sizes.push_back( block_size);
  41. block_sizes.push_back( block_size + set_space % nblocks );
  42. }
  43.  
  44. // Allocate once (reallocate later)
  45. thrust::device_vector<unsigned int> trial_set;
  46. thrust::device_vector<unsigned int> solution_set;
  47.  
  48. for( unsigned int i=0; i < nblocks; ++i){
  49. // find start
  50. unsigned int local_start = i*block_size + kStart;
  51. std::cout << "Start - End:" << local_start << " - " << block_sizes[i]+local_start << std::endl;
  52. std::cout << "Block size:" << block_sizes[i] << std::endl;
  53. trial_set.resize(block_sizes[i],0);
  54. solution_set.resize(block_sizes[i],0);
  55. // Initialized all data
  56. thrust::sequence(trial_set.begin(), trial_set.end(), local_start);
  57. // divisible by 1-9?
  58. thrust::transform(trial_set.begin(), trial_set.end(),
  59. solution_set.begin(), divisible_by_9() );
  60. // Find solution ==0, i.e. sort by key-value pair
  61. thrust::sort_by_key( solution_set.begin(), solution_set.end(), trial_set.begin());
  62. // Get the solution and print it
  63. thrust::host_vector<unsigned int> host_keys( trial_set.begin(), trial_set.begin()+2);
  64. thrust::host_vector<unsigned int> host_values( solution_set.begin(), solution_set.begin()+2);
  65. // Print top two results solution ( should be 0 )
  66. for ( int i=0; i < host_keys.size(); ++i)
  67. std::cout << "Solution[" << i << "]" << "Key=" << host_keys[i] <<",Value=" << host_values[i] << std::endl;
  68. }
  69. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:32: fatal error: thrust/host_vector.h: No such file or directory
compilation terminated.
stdout

Standard output is empty