You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sort_pair_algorithm.maca 10 kB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "test_utils.h"
  2. #include "performance_utils.h"
  3. #include "yaml_reporter.h"
  4. #include <iostream>
  5. #include <vector>
  6. #include <iomanip>
  7. // ============================================================================
  8. // 实现标记宏 - 参赛者修改实现时请将此宏设为0
  9. // ============================================================================
  10. #ifndef USE_DEFAULT_REF_IMPL
  11. #define USE_DEFAULT_REF_IMPL 1 // 1=默认实现, 0=参赛者自定义实现
  12. #endif
  13. #if USE_DEFAULT_REF_IMPL
  14. #include <thrust/sort.h>
  15. #include <thrust/device_vector.h>
  16. #include <thrust/execution_policy.h>
  17. #include <thrust/iterator/zip_iterator.h>
  18. #include <thrust/tuple.h>
  19. #endif
  20. // ============================================================================
  21. // SortPair算法实现接口
  22. // 参赛者需要替换Thrust实现为自己的高性能kernel
  23. // ============================================================================
  24. template <typename KeyType, typename ValueType>
  25. class SortPairAlgorithm {
  26. public:
  27. // 主要接口函数 - 参赛者需要实现这个函数
  28. void sort(const KeyType* d_keys_in, KeyType* d_keys_out,
  29. const ValueType* d_values_in, ValueType* d_values_out,
  30. int num_items, bool descending) {
  31. #if !USE_DEFAULT_REF_IMPL
  32. // ========================================
  33. // 参赛者自定义实现区域
  34. // ========================================
  35. // TODO: 参赛者在此实现自己的高性能排序算法
  36. // 示例:参赛者可以调用1个或多个自定义kernel
  37. // preprocessKernel<<<grid, block>>>(d_keys_in, d_values_in, num_items);
  38. // mainSortKernel<<<grid, block>>>(d_keys_out, d_values_out, num_items, descending);
  39. // postprocessKernel<<<grid, block>>>(d_keys_out, d_values_out, num_items);
  40. #else
  41. // ========================================
  42. // 默认基准实现
  43. // ========================================
  44. MACA_CHECK(mcMemcpy(d_keys_out, d_keys_in, num_items * sizeof(KeyType), mcMemcpyDeviceToDevice));
  45. MACA_CHECK(mcMemcpy(d_values_out, d_values_in, num_items * sizeof(ValueType), mcMemcpyDeviceToDevice));
  46. auto key_ptr = thrust::device_pointer_cast(d_keys_out);
  47. auto value_ptr = thrust::device_pointer_cast(d_values_out);
  48. if (descending) {
  49. thrust::stable_sort_by_key(thrust::device, key_ptr, key_ptr + num_items, value_ptr, thrust::greater<KeyType>());
  50. } else {
  51. thrust::stable_sort_by_key(thrust::device, key_ptr, key_ptr + num_items, value_ptr, thrust::less<KeyType>());
  52. }
  53. #endif
  54. }
  55. // 获取当前实现状态
  56. static const char* getImplementationStatus() {
  57. #if USE_DEFAULT_REF_IMPL
  58. return "DEFAULT_REF_IMPL";
  59. #else
  60. return "CUSTOM_IMPL";
  61. #endif
  62. }
  63. private:
  64. // 参赛者可以在这里添加辅助函数和成员变量
  65. // 例如:临时缓冲区、多个kernel函数、流等
  66. };
  67. // ============================================================================
  68. // 测试和性能评估
  69. // ============================================================================
  70. bool testCorrectness() {
  71. std::cout << "SortPair 正确性测试..." << std::endl;
  72. TestDataGenerator generator;
  73. SortPairAlgorithm<float, uint32_t> algorithm;
  74. // 测试小规模数据
  75. int size = 10000;
  76. auto keys = generator.generateRandomFloats(size);
  77. auto values = generator.generateRandomUint32(size);
  78. // 分配GPU内存
  79. float *d_keys_in, *d_keys_out;
  80. uint32_t *d_values_in, *d_values_out;
  81. MACA_CHECK(mcMalloc(&d_keys_in, size * sizeof(float)));
  82. MACA_CHECK(mcMalloc(&d_keys_out, size * sizeof(float)));
  83. MACA_CHECK(mcMalloc(&d_values_in, size * sizeof(uint32_t)));
  84. MACA_CHECK(mcMalloc(&d_values_out, size * sizeof(uint32_t)));
  85. MACA_CHECK(mcMemcpy(d_keys_in, keys.data(), size * sizeof(float), mcMemcpyHostToDevice));
  86. MACA_CHECK(mcMemcpy(d_values_in, values.data(), size * sizeof(uint32_t), mcMemcpyHostToDevice));
  87. // 测试升序和降序
  88. bool allPassed = true;
  89. for (bool descending : {false, true}) {
  90. std::cout << " " << (descending ? "降序" : "升序") << " 测试..." << std::endl;
  91. // CPU参考结果
  92. auto cpu_keys = keys;
  93. auto cpu_values = values;
  94. cpuSortPair(cpu_keys, cpu_values, descending);
  95. // GPU算法结果
  96. algorithm.sort(d_keys_in, d_keys_out, d_values_in, d_values_out, size, descending);
  97. // 获取结果
  98. std::vector<float> gpu_keys(size);
  99. std::vector<uint32_t> gpu_values(size);
  100. MACA_CHECK(mcMemcpy(gpu_keys.data(), d_keys_out, size * sizeof(float), mcMemcpyDeviceToHost));
  101. MACA_CHECK(mcMemcpy(gpu_values.data(), d_values_out, size * sizeof(uint32_t), mcMemcpyDeviceToHost));
  102. // 验证结果
  103. bool keysMatch = compareArrays(cpu_keys, gpu_keys, 1e-5);
  104. bool valuesMatch = compareArrays(cpu_values, gpu_values);
  105. if (!keysMatch || !valuesMatch) {
  106. std::cout << " 失败: 结果不匹配" << std::endl;
  107. allPassed = false;
  108. } else {
  109. std::cout << " 通过" << std::endl;
  110. }
  111. }
  112. // 清理内存
  113. mcFree(d_keys_in);
  114. mcFree(d_keys_out);
  115. mcFree(d_values_in);
  116. mcFree(d_values_out);
  117. return allPassed;
  118. }
  119. void benchmarkPerformance() {
  120. PerformanceDisplay::printSortPairHeader();
  121. TestDataGenerator generator;
  122. PerformanceMeter meter;
  123. SortPairAlgorithm<float, uint32_t> algorithm;
  124. const int WARMUP_ITERATIONS = 5;
  125. const int BENCHMARK_ITERATIONS = 10;
  126. // 用于YAML报告的数据收集
  127. std::vector<std::map<std::string, std::string>> perf_data;
  128. for (int i = 0; i < NUM_TEST_SIZES; i++) {
  129. int size = TEST_SIZES[i];
  130. // 生成测试数据
  131. auto keys = generator.generateRandomFloats(size);
  132. auto values = generator.generateRandomUint32(size);
  133. // 分配GPU内存
  134. float *d_keys_in, *d_keys_out;
  135. uint32_t *d_values_in, *d_values_out;
  136. MACA_CHECK(mcMalloc(&d_keys_in, size * sizeof(float)));
  137. MACA_CHECK(mcMalloc(&d_keys_out, size * sizeof(float)));
  138. MACA_CHECK(mcMalloc(&d_values_in, size * sizeof(uint32_t)));
  139. MACA_CHECK(mcMalloc(&d_values_out, size * sizeof(uint32_t)));
  140. MACA_CHECK(mcMemcpy(d_keys_in, keys.data(), size * sizeof(float), mcMemcpyHostToDevice));
  141. MACA_CHECK(mcMemcpy(d_values_in, values.data(), size * sizeof(uint32_t), mcMemcpyHostToDevice));
  142. float asc_time = 0, desc_time = 0;
  143. // 测试升序和降序
  144. for (bool descending : {false, true}) {
  145. // Warmup阶段
  146. for (int iter = 0; iter < WARMUP_ITERATIONS; iter++) {
  147. algorithm.sort(d_keys_in, d_keys_out, d_values_in, d_values_out, size, descending);
  148. }
  149. // 正式测试阶段
  150. float total_time = 0;
  151. for (int iter = 0; iter < BENCHMARK_ITERATIONS; iter++) {
  152. meter.startTiming();
  153. algorithm.sort(d_keys_in, d_keys_out, d_values_in, d_values_out, size, descending);
  154. total_time += meter.stopTiming();
  155. }
  156. float avg_time = total_time / BENCHMARK_ITERATIONS;
  157. if (descending) {
  158. desc_time = avg_time;
  159. } else {
  160. asc_time = avg_time;
  161. }
  162. }
  163. // 计算性能指标
  164. auto asc_metrics = PerformanceCalculator::calculateSortPair(size, asc_time);
  165. auto desc_metrics = PerformanceCalculator::calculateSortPair(size, desc_time);
  166. // 显示性能数据
  167. PerformanceDisplay::printSortPairData(size, asc_time, desc_time, asc_metrics, desc_metrics);
  168. // 收集YAML报告数据
  169. auto entry = YAMLPerformanceReporter::createEntry();
  170. entry["data_size"] = std::to_string(size);
  171. entry["asc_time_ms"] = std::to_string(asc_time);
  172. entry["desc_time_ms"] = std::to_string(desc_time);
  173. entry["asc_throughput_gps"] = std::to_string(asc_metrics.throughput_gps);
  174. entry["desc_throughput_gps"] = std::to_string(desc_metrics.throughput_gps);
  175. entry["key_type"] = "float";
  176. entry["value_type"] = "uint32_t";
  177. perf_data.push_back(entry);
  178. // 清理内存
  179. mcFree(d_keys_in);
  180. mcFree(d_keys_out);
  181. mcFree(d_values_in);
  182. mcFree(d_values_out);
  183. }
  184. // 生成YAML性能报告
  185. YAMLPerformanceReporter::generateSortPairYAML(perf_data, "sort_pair_performance.yaml");
  186. PerformanceDisplay::printSavedMessage("sort_pair_performance.yaml");
  187. }
  188. // ============================================================================
  189. // 主函数
  190. // ============================================================================
  191. int main(int argc, char* argv[]) {
  192. std::cout << "=== SortPair 算法测试 ===" << std::endl;
  193. // 检查参数
  194. std::string mode = "all";
  195. if (argc > 1) {
  196. mode = argv[1];
  197. }
  198. bool correctness_passed = true;
  199. bool performance_completed = true;
  200. try {
  201. if (mode == "correctness" || mode == "all") {
  202. correctness_passed = testCorrectness();
  203. }
  204. if (mode == "performance" || mode == "all") {
  205. if (correctness_passed || mode == "performance") {
  206. benchmarkPerformance();
  207. } else {
  208. std::cout << "跳过性能测试,因为正确性测试未通过" << std::endl;
  209. performance_completed = false;
  210. }
  211. }
  212. std::cout << "\n=== 测试完成 ===" << std::endl;
  213. std::cout << "实现状态: " << SortPairAlgorithm<float, uint32_t>::getImplementationStatus() << std::endl;
  214. if (mode == "all") {
  215. std::cout << "正确性: " << (correctness_passed ? "通过" : "失败") << std::endl;
  216. std::cout << "性能测试: " << (performance_completed ? "完成" : "跳过") << std::endl;
  217. }
  218. return correctness_passed ? 0 : 1;
  219. } catch (const std::exception& e) {
  220. std::cerr << "测试出错: " << e.what() << std::endl;
  221. return 1;
  222. }
  223. }