Advertisement
Guest User

Untitled

a guest
Jul 9th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include "thrust/device_ptr.h"
  2. #include "thrust/sort.h"
  3.  
  4. __global__ void calculate_hash(uint *hash_values, uint *particle_ids, int length)
  5. {
  6. int i = blockIdx.x*blockDim.x + threadIdx.x;
  7.  
  8. if(i >= length)
  9. return;
  10.  
  11. hash_values[i] = 1;
  12. particle_ids[i] = i;
  13. }
  14.  
  15. void hash_particles_gpu(uint *d_hash_values, uint *d_particle_ids, int length)
  16. {
  17. int block_size = 256;
  18. int num_blocks = ceil(length/(float)block_size);
  19.  
  20. calculate_hash<<<num_blocks, block_size>>>(d_hash_values, d_particle_ids, length);
  21.  
  22. cudaDeviceSynchronize();
  23.  
  24. thrust::device_ptr<uint> keys(d_hash_values);
  25. thrust::device_ptr<uint> values(d_particle_ids);
  26. thrust::sort_by_key(keys, keys+length, values);
  27. }
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31. int length = 15;
  32. int bytes;
  33.  
  34. #ifdef BROKE
  35. int *m_int;
  36. cudaMallocManaged((void**)&m_int, sizeof(int));
  37. #endif
  38.  
  39. // Allocate uint hash value array
  40. bytes = length*sizeof(unsigned int);
  41. unsigned int * hash_values;
  42. cudaMalloc((void**)&hash_values, bytes);
  43.  
  44. // Allocate uint particle ID array
  45. bytes = length*sizeof(unsigned int);
  46. unsigned int *particle_ids;
  47. cudaMalloc((void**)&particle_ids, bytes);
  48.  
  49. hash_particles_gpu(hash_values, particle_ids, length);
  50. }
  51.  
  52. $ nvcc -DBROKE -DTHRUST_DEBUG example.cu -o broke.exe
  53. $ nvcc -DTHRUST_DEBUG example.cu -o fixed.exe
  54. $ ./fixed.exe
  55. $ ./broke.exe
  56. terminate called after throwing an instance of 'thrust::system::system_error'
  57. what(): synchronize: RakingReduction: unknown error
  58. Abort
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement