#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/reduce.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>
#include <cstdlib>
#include <iostream>
const unsigned int kStart = 100000000;
const unsigned int kEnd= 999999999;
struct divisible_by_9{
__host__ __device__ unsigned int operator()(unsigned int const& input)
const {
//unsigned int idx=blockIdx.x*blockDim.x + threadIdx.x;
unsigned int current_value = input;
unsigned int l_sum=0;
for(unsigned int n=9; n>1; --n){
l_sum = (current_value%n) + l_sum;
current_value = current_value/10;
}
return l_sum;
}
};
int main(void){
//have to block memory allocation...
unsigned int set_space = kEnd - kStart;
unsigned int nblocks = 6;
thrust::host_vector<unsigned int> block_sizes;
unsigned int block_size = set_space / nblocks;
if( set_space % nblocks == 0){
for(unsigned int i=0;i<nblocks; ++i) block_sizes.push_back(block_size);
}
else {
for(unsigned int i=0;i<nblocks-1; ++i) block_sizes.push_back( block_size);
block_sizes.push_back( block_size + set_space % nblocks );
}
// Allocate once (reallocate later)
thrust::device_vector<unsigned int> trial_set;
thrust::device_vector<unsigned int> solution_set;
for( unsigned int i=0; i < nblocks; ++i){
// find start
unsigned int local_start = i*block_size + kStart;
std::cout << "Start - End:" << local_start << " - " << block_sizes[i]+local_start << std::endl;
std::cout << "Block size:" << block_sizes[i] << std::endl;
trial_set.resize(block_sizes[i],0);
solution_set.resize(block_sizes[i],0);
// Initialized all data
thrust::sequence(trial_set.begin(), trial_set.end(), local_start);
// divisible by 1-9?
thrust::transform(trial_set.begin(), trial_set.end(),
solution_set.begin(), divisible_by_9() );
// Find solution ==0, i.e. sort by key-value pair
thrust::sort_by_key( solution_set.begin(), solution_set.end(), trial_set.begin());
// Get the solution and print it
thrust::host_vector<unsigned int> host_keys( trial_set.begin(), trial_set.begin()+2);
thrust::host_vector<unsigned int> host_values( solution_set.begin(), solution_set.begin()+2);
// Print top two results solution ( should be 0 )
for ( int i=0; i < host_keys.size(); ++i)
std::cout << "Solution[" << i << "]" << "Key=" << host_keys[i] <<",Value=" << host_values[i] << std::endl;
}
}