Advertisement
Guest User

Untitled

a guest
May 30th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include <thrust/device_vector.h>
  2. //#include <thrust/reduce.h>
  3. #include <thrust/extrema.h>
  4. #include <thrust/iterator/zip_iterator.h>
  5. #include <thrust/functional.h>
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <cstdio>
  9.  
  10.  
  11. typedef thrust::device_ptr<bool> BoolIterator;
  12. typedef thrust::device_ptr<float> ValueIterator;
  13. typedef thrust::tuple<BoolIterator, ValueIterator> IteratorTuple;
  14. typedef thrust::tuple<bool, float> DereferencedIteratorTuple;
  15. typedef thrust::zip_iterator<IteratorTuple> NodePropIterator;
  16.  
  17.  
  18. struct nodeProp_comp : public thrust::binary_function<DereferencedIteratorTuple, DereferencedIteratorTuple, bool>
  19. {
  20. __host__ __device__
  21. bool operator()( const DereferencedIteratorTuple &lhs, const DereferencedIteratorTuple &rhs ) const
  22. {
  23. if( !( thrust::get<0>( lhs ) ) && !( thrust::get<0>( rhs ) ) )
  24. {
  25. return ( thrust::get<1>( lhs ) < thrust::get<1>( rhs ) );
  26. }
  27. else
  28. {
  29. return !( thrust::get<0>( lhs ) );
  30. }
  31. }
  32. };
  33.  
  34.  
  35. void checkCUDAError(const char *msg)
  36. {
  37. cudaError_t err = cudaGetLastError();
  38. if( cudaSuccess != err)
  39. {
  40. fprintf(stderr, "Cuda error: %s: %s.\n", msg,
  41. cudaGetErrorString( err) );
  42. exit(EXIT_FAILURE);
  43. }
  44. }
  45.  
  46.  
  47. using namespace std;
  48. int main(void)
  49. {
  50. float *cost=NULL;
  51. cudaMalloc( (void **) &cost, 5 * sizeof(float) );
  52. checkCUDAError("## cuda malloc cost ##");
  53.  
  54. bool *visited=NULL;
  55. cudaMalloc( (void **) &visited, 5 * sizeof(bool) );
  56. checkCUDAError("## cuda malloc visited ##");
  57.  
  58.  
  59. /////////////////////////////////////////////////////////////////////////////////////////
  60.  
  61. thrust::device_ptr< float > dp_cost( cost );
  62. thrust::device_ptr< bool > dp_visited( visited );
  63.  
  64.  
  65. for (int i = 0; i < 5; ++i)
  66. {
  67. float c;
  68. bool vs;
  69.  
  70. cin>> c >> vs;
  71.  
  72. dp_cost[i] = c;
  73. dp_visited[i] = vs;
  74.  
  75. }
  76.  
  77.  
  78. for (int i = 0; i < 5; ++i)
  79. {
  80. cout<< dp_cost[i] << " , " << dp_visited[i] <<endl;
  81. }
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////////////////////
  85.  
  86.  
  87. BoolIterator bools_begin = dp_visited, bools_end = dp_visited +5;
  88. ValueIterator values_begin = dp_cost, values_end = dp_cost +5;
  89.  
  90.  
  91.  
  92.  
  93. NodePropIterator iter_begin (thrust::make_tuple(bools_begin, values_begin));
  94. NodePropIterator iter_end (thrust::make_tuple(bools_end, values_end));
  95.  
  96. /*NodePropIterator iter_begin (thrust::make_tuple(dp_visited, dp_cost));
  97. NodePropIterator iter_end (thrust::make_tuple(dp_visited +5, dp_cost +5));*/
  98.  
  99. NodePropIterator min_el_pos = thrust::min_element( iter_begin, iter_end, nodeProp_comp() );
  100.  
  101. /*NodePropIterator min_el_pos = thrust::min_element(
  102. thrust::make_zip_iterator( thrust::make_tuple(dp_visited, dp_cost) ),
  103. thrust::make_zip_iterator( thrust::make_tuple(dp_visited +5, dp_cost +5) ),
  104. nodeProp_comp());*/
  105.  
  106.  
  107.  
  108. cudaThreadSynchronize();
  109. checkCUDAError("## thrust min el ##");
  110.  
  111. /////////////////////////////////////////////////////////////////////////////////////////
  112.  
  113. DereferencedIteratorTuple tmp = *min_el_pos;
  114.  
  115. cout<< " \n#### "<< thrust::get<0>( tmp )
  116. << " , " << thrust::get<1>( tmp ) <<endl;
  117.  
  118. cout<< "Pos : " << min_el_pos - iter_begin <<endl;
  119.  
  120. cudaFree(cost);
  121. cudaFree(visited);
  122.  
  123.  
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement