Advertisement
Guest User

Untitled

a guest
Dec 19th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. $ cat t1008.cu
  2. #include <thrust/device_vector.h>
  3. #include <thrust/transform.h>
  4. #include <thrust/merge.h>
  5. #include <thrust/iterator/zip_iterator.h>
  6.  
  7. struct multiply_transformation : public thrust::binary_function<thrust::tuple<int, int>, thrust::tuple<int, int>, thrust::tuple<int, int> >
  8. {
  9. __host__ __device__
  10. thrust::tuple<int, int> operator()(thrust::tuple<int, int> d_left, thrust::tuple<int, int> d_right)
  11. {
  12. if (thrust::get<0>(d_left) == thrust::get<1>(d_right)) {
  13. return thrust::make_tuple(thrust::get<0>(d_left), thrust::get<1>(d_left) * thrust::get<1>(d_right));
  14. }
  15. return thrust::make_tuple(-1, -1);
  16. }
  17. };
  18.  
  19. int main(){
  20. int h_leftCount = 1;
  21. int h_rightCount = 1;
  22. thrust::device_vector<int> d_leftCountKeys(h_leftCount);
  23. thrust::device_vector<int> d_rightCountKeys(h_rightCount);
  24. thrust::device_vector<int> d_leftCounts(h_leftCount);
  25. thrust::device_vector<int> d_rightCounts(h_rightCount);
  26.  
  27. thrust::device_vector<int> d_mergedKeys(h_leftCount + h_rightCount);
  28. thrust::device_vector<int> d_mergedValues(h_leftCount + h_rightCount);
  29. thrust::merge_by_key(d_leftCountKeys.begin(), d_leftCountKeys.begin() + h_leftCount,
  30. d_rightCountKeys.begin(), d_rightCountKeys.begin() + h_rightCount,
  31. d_leftCounts.begin(), d_rightCounts.begin(), d_mergedKeys.begin(), d_mergedValues.begin());
  32.  
  33. typedef thrust::tuple<thrust::device_vector<int>::iterator, thrust::device_vector<int>::iterator > ZipTuple;
  34.  
  35. thrust::zip_iterator<ZipTuple> d_zippedCounts(thrust::make_tuple(d_mergedKeys.begin(), d_mergedValues.begin()));
  36.  
  37. thrust::zip_iterator<ZipTuple> d_zippedCountsOffset(d_zippedCounts + 1);
  38.  
  39.  
  40. multiply_transformation transformOperator;
  41. typedef thrust::tuple<int, int> IntTuple;
  42. thrust::device_vector<IntTuple> d_transformedResult(h_leftCount + h_rightCount);
  43. thrust::transform(d_zippedCounts, d_zippedCounts + h_leftCount + h_rightCount - 1, d_zippedCountsOffset, d_transformedResult.begin(), transformOperator);
  44.  
  45. }
  46. $ nvcc -o t1008 t1008.cu
  47. $ ./t1008
  48. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement