Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. #include <thrust/device_vector.h>
  6. #include <thrust/sort.h>
  7. #include <thrust/gather.h>
  8.  
  9. #include "gtest/gtest.h"
  10. #include "gmock/gmock.h"
  11. #include <gdf/gdf.h>
  12. #include <gdf/cffi/functions.h>
  13.  
  14. using namespace testing;
  15.  
  16. gdf_column
  17. create_gdf_column(thrust::device_vector<int> &d) {
  18. gdf_column c = {thrust::raw_pointer_cast(d.data()), nullptr, d.size(), GDF_INT32, TIME_UNIT_NONE};
  19. return c;
  20. }
  21.  
  22. std::vector<int> host_vec(thrust::device_vector<int> &dev_vec) {
  23. std::vector<int> data(dev_vec.size());
  24. thrust::copy(dev_vec.begin(), dev_vec.end(), data.begin());
  25. return data;
  26. }
  27.  
  28. gdf_error
  29. call_gdf_test(
  30. std::array<thrust::device_vector<int>, 3> &l,
  31. std::array<thrust::device_vector<int>, 3> &r,
  32. thrust::device_vector<int> &out_left_pos,
  33. thrust::device_vector<int> &out_right_pos,
  34. const int index) {
  35. std::vector<int> l0{0, 0, 4, 5, 5};
  36. std::vector<int> l1{1, 2, 2, 3, 4};
  37. std::vector<int> l2{1, 1, 3, 1, 2};
  38. std::vector<int> r0{0, 0, 2, 3, 5};
  39. std::vector<int> r1{1, 2, 3, 3, 4};
  40. std::vector<int> r2{3, 3, 2, 1, 1};
  41.  
  42. thrust::device_vector<int> dl0 = l0; thrust::swap(dl0, l[0]);
  43. thrust::device_vector<int> dl1 = l1; thrust::swap(dl1, l[1]);
  44. thrust::device_vector<int> dl2 = l2; thrust::swap(dl2, l[2]);
  45. thrust::device_vector<int> dr0 = r0; thrust::swap(dr0, r[0]);
  46. thrust::device_vector<int> dr1 = r1; thrust::swap(dr1, r[1]);
  47. thrust::device_vector<int> dr2 = r2; thrust::swap(dr2, r[2]);
  48.  
  49. gdf_column gdl0 = create_gdf_column(l[0]);
  50. gdf_column gdl1 = create_gdf_column(l[1]);
  51. gdf_column gdl2 = create_gdf_column(l[2]);
  52.  
  53. gdf_column gdr0 = create_gdf_column(r[0]);
  54. gdf_column gdr1 = create_gdf_column(r[1]);
  55. gdf_column gdr2 = create_gdf_column(r[2]);
  56.  
  57. gdf_column* gl[3] = {&gdl0, &gdl1, &gdl2};
  58. gdf_column* gr[3] = {&gdr0, &gdr1, &gdr2};
  59. gdf_join_result_type *out;
  60. gdf_error err = gdf_multi_left_join_generic(index, gl, gr, &out);
  61.  
  62. size_t len = gdf_join_result_size(out);
  63. size_t hlen = len/2;
  64. int* out_ptr = reinterpret_cast<int*>(gdf_join_result_data(out));
  65. thrust::device_vector<int> out_data(out_ptr, out_ptr + len);
  66.  
  67. thrust::sort_by_key(out_data.begin() + hlen, out_data.end(), out_data.begin());
  68. thrust::sort_by_key(out_data.begin(), out_data.begin() + hlen, out_data.begin() + hlen);
  69. out_left_pos.resize(hlen);
  70. out_right_pos.resize(hlen);
  71.  
  72. thrust::copy(out_data.begin(), out_data.begin() + out_left_pos.size(), out_left_pos.begin());
  73. thrust::copy(out_data.begin() + out_right_pos.size(), out_data.end(), out_right_pos.begin());
  74. return err;
  75. }
  76.  
  77. TEST(gdf_pr58_TEST, case1) {
  78. std::array<thrust::device_vector<int>, 3> l;
  79. std::array<thrust::device_vector<int>, 3> r;
  80. thrust::device_vector<int> l_pos;
  81. thrust::device_vector<int> r_pos;
  82. auto err = call_gdf_test(l, r, l_pos, r_pos, 1);
  83. thrust::device_vector<int> map_out(l_pos.size());
  84.  
  85. EXPECT_THAT(host_vec(l_pos), ElementsAre(0, 0, 1, 1, 2, 3, 4));
  86. EXPECT_THAT(host_vec(r_pos), ElementsAre(0, 1, 0, 1, -1, 4, 4));
  87.  
  88. thrust::gather(l_pos.begin(), l_pos.end(), l[0].begin(), map_out.begin());
  89. EXPECT_THAT(host_vec(map_out), ElementsAre(0, 0, 0, 0, 4, 5, 5));
  90.  
  91. ASSERT_EQ(err, GDF_SUCCESS);
  92. }
  93.  
  94. TEST(gdf_pr58_TEST, case2) {
  95. std::array<thrust::device_vector<int>, 3> l;
  96. std::array<thrust::device_vector<int>, 3> r;
  97. thrust::device_vector<int> l_pos;
  98. thrust::device_vector<int> r_pos;
  99. auto err = call_gdf_test(l, r, l_pos, r_pos, 2);
  100. thrust::device_vector<int> map_out(l_pos.size());
  101.  
  102. EXPECT_THAT(host_vec(l_pos), ElementsAre(0, 1, 2, 3, 4));
  103.  
  104. {
  105. thrust::gather(l_pos.begin(), l_pos.end(), l[0].begin(), map_out.begin());
  106. EXPECT_THAT(host_vec(map_out), ElementsAre(0, 0, 4, 5, 5));
  107. }
  108.  
  109. {
  110. thrust::gather(l_pos.begin(), l_pos.end(), l[1].begin(), map_out.begin());
  111. EXPECT_THAT(host_vec(map_out), ElementsAre(1, 2, 2, 3, 4));
  112. }
  113.  
  114. ASSERT_EQ(err, GDF_SUCCESS);
  115. }
  116.  
  117. TEST(gdf_pr58_TEST, case3) {
  118. std::array<thrust::device_vector<int>, 3> l;
  119. std::array<thrust::device_vector<int>, 3> r;
  120. thrust::device_vector<int> l_pos;
  121. thrust::device_vector<int> r_pos;
  122. auto err = call_gdf_test(l, r, l_pos, r_pos, 2);
  123. thrust::device_vector<int> map_out(l_pos.size());
  124.  
  125. EXPECT_THAT(host_vec(l_pos), ElementsAre(0, 1, 2, 3, 4));
  126.  
  127. {
  128. thrust::gather(l_pos.begin(), l_pos.end(), l[0].begin(), map_out.begin());
  129. EXPECT_THAT(host_vec(map_out), ElementsAre(0, 0, 4, 5, 5));
  130. }
  131.  
  132. {
  133. thrust::gather(l_pos.begin(), l_pos.end(), l[1].begin(), map_out.begin());
  134. EXPECT_THAT(host_vec(map_out), ElementsAre(1, 2, 2, 3, 4));
  135. }
  136.  
  137. {
  138. thrust::gather(l_pos.begin(), l_pos.end(), l[2].begin(), map_out.begin());
  139. EXPECT_THAT(host_vec(map_out), ElementsAre(1, 1, 3, 1, 2));
  140. }
  141.  
  142. ASSERT_EQ(err, GDF_SUCCESS);
  143. }
Add Comment
Please, Sign In to add comment