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.

2 months ago
2 months ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/bin/bash
  2. # GPU高性能并行计算算法优化竞赛 - 统一编译和运行脚本
  3. # 整合了所有算法的编译、运行和公共配置
  4. # ============================================================================
  5. # 公共配置和工具函数
  6. # ============================================================================
  7. # 设置颜色
  8. RED='\033[0;31m'
  9. GREEN='\033[0;32m'
  10. BLUE='\033[0;34m'
  11. YELLOW='\033[0;33m'
  12. NC='\033[0m' # No Color
  13. # 打印函数
  14. print_info() {
  15. echo -e "${BLUE}[INFO]${NC} $1"
  16. }
  17. print_success() {
  18. echo -e "${GREEN}[SUCCESS]${NC} $1"
  19. }
  20. print_error() {
  21. echo -e "${RED}[ERROR]${NC} $1"
  22. }
  23. print_warning() {
  24. echo -e "${YELLOW}[WARNING]${NC} $1"
  25. }
  26. # 编译配置 - 可通过环境变量自定义
  27. COMPILER=${COMPILER:-mxcc}
  28. COMPILER_FLAGS=${COMPILER_FLAGS:-"-O3 -std=c++17 --extended-lambda -DRUN_FULL_TEST"}
  29. # ***** 这里是关键修改点1:头文件目录 *****
  30. # 现在头文件在 utils/ 目录下
  31. HEADER_DIR=${HEADER_DIR:-utils}
  32. # ***** 这里是关键修改点2:源文件目录 *****
  33. # 现在源文件在 ./ 目录下
  34. SOURCE_CODE_DIR=${SOURCE_CODE_DIR:-}
  35. BUILD_DIR=${BUILD_DIR:-build}
  36. # 编译单个算法的通用函数
  37. # 参数: $1=算法名称, $2=源文件名(不含路径)
  38. compile_algorithm() {
  39. local algo_name="$1"
  40. local source_file_name="$2" # 例如 "reduce_sum_algorithm.maca"
  41. local target_file="$BUILD_DIR/test_${algo_name,,}" # 转换为小写
  42. print_info "编译 $algo_name 算法..."
  43. # 创建构建目录
  44. mkdir -p "$BUILD_DIR"
  45. # ***** 这里是关键修改点3:编译命令 *****
  46. # -I$HEADER_DIR 用于告诉编译器头文件在哪里
  47. # $SOURCE_CODE_DIR/$source_file_name 用于指定要编译的源文件的完整路径
  48. local compile_cmd="$COMPILER $COMPILER_FLAGS -I$HEADER_DIR $source_file_name -o $target_file"
  49. print_info "执行: $compile_cmd"
  50. if $compile_cmd; then
  51. print_success "$algo_name 编译完成!"
  52. echo ""
  53. echo "运行测试:"
  54. echo " ./$target_file [correctness|performance|all]"
  55. return 0
  56. else
  57. print_error "$algo_name 编译失败!"
  58. return 1
  59. fi
  60. }
  61. # 显示编译配置信息
  62. show_build_config() {
  63. print_info "编译配置:"
  64. echo " COMPILER: $COMPILER"
  65. echo " COMPILER_FLAGS: $COMPILER_FLAGS"
  66. echo " HEADER_DIR: $HEADER_DIR" # 显示头文件目录
  67. echo " SOURCE_CODE_DIR: $SOURCE_CODE_DIR" # 显示源文件目录
  68. echo " BUILD_DIR: $BUILD_DIR"
  69. echo ""
  70. }
  71. # 运行单个测试
  72. run_single_test() {
  73. local algo_name="$1"
  74. local test_mode="${2:-all}"
  75. local test_file="$BUILD_DIR/test_${algo_name,,}"
  76. if [ -f "$test_file" ]; then
  77. print_info "运行 $algo_name 测试 (模式: $test_mode)..."
  78. "./$test_file" "$test_mode"
  79. return $?
  80. else
  81. print_error "$algo_name 测试程序不存在: $test_file"
  82. return 1
  83. fi
  84. }
  85. # ============================================================================
  86. # 主脚本逻辑
  87. # ============================================================================
  88. # 显示帮助信息 (整合了所有选项)
  89. show_help() {
  90. echo "GPU算法竞赛统一编译和运行脚本"
  91. echo "用法: $0 [选项]"
  92. echo ""
  93. echo "选项:"
  94. echo " --help 显示帮助信息"
  95. echo " --build-only 仅编译所有算法,不运行测试"
  96. echo " --run_reduce [MODE] 编译并运行ReduceSum算法测试 (MODE: correctness|performance|all, 默认all)"
  97. echo " --run_sort [MODE] 编译并运行SortPair算法测试 (MODE: correctness|performance|all, 默认all)"
  98. echo " --run_topk [MODE] 编译并运行TopkPair算法测试 (MODE: correctness|performance|all, 默认all)"
  99. echo ""
  100. echo "示例:"
  101. echo " $0 # 编译并运行所有测试(默认行为)"
  102. echo " $0 --build-only # 仅编译所有算法"
  103. echo " $0 --run_sort performance # 编译并运行SortPair性能测试"
  104. echo ""
  105. }
  106. # 解析命令行参数
  107. RUN_MODE="run_all" # 默认为编译并运行所有测试
  108. ALGO_TO_RUN="" # 记录要运行的单个算法
  109. SINGLE_ALGO_TEST_MODE="all" # 单个算法的测试模式
  110. while [[ $# -gt 0 ]]; do
  111. case $1 in
  112. --help)
  113. show_help
  114. exit 0
  115. ;;
  116. --build-only)
  117. RUN_MODE="build_only"
  118. shift
  119. ;;
  120. --run_reduce)
  121. RUN_MODE="run_single"
  122. ALGO_TO_RUN="ReduceSum"
  123. if [[ -n "$2" && "$2" != --* ]]; then
  124. SINGLE_ALGO_TEST_MODE="$2"
  125. shift
  126. fi
  127. shift
  128. ;;
  129. --run_sort)
  130. RUN_MODE="run_single"
  131. ALGO_TO_RUN="SortPair"
  132. if [[ -n "$2" && "$2" != --* ]]; then
  133. SINGLE_ALGO_TEST_MODE="$2"
  134. shift
  135. fi
  136. shift
  137. ;;
  138. --run_topk)
  139. RUN_MODE="run_single"
  140. ALGO_TO_RUN="TopkPair"
  141. if [[ -n "$2" && "$2" != --* ]]; then
  142. SINGLE_ALGO_TEST_MODE="$2"
  143. shift
  144. fi
  145. shift
  146. ;;
  147. *)
  148. print_error "未知选项: $1"
  149. show_help
  150. exit 1
  151. ;;
  152. esac
  153. done
  154. if [ "$RUN_MODE" = "build_only" ]; then
  155. print_info "开始编译所有算法..."
  156. else
  157. print_info "开始编译并运行所有算法..."
  158. fi
  159. print_info "工作目录: $(pwd)"
  160. print_info "编译时间: $(date '+%Y-%m-%d %H:%M:%S')"
  161. show_build_config
  162. # 清理构建目录
  163. if [ -d "$BUILD_DIR" ]; then
  164. print_info "清理现有构建目录: $BUILD_DIR"
  165. rm -rf "$BUILD_DIR"
  166. fi
  167. # 核心逻辑:根据 RUN_MODE 执行操作
  168. case "$RUN_MODE" in
  169. "build_only")
  170. print_info "编译所有算法..."
  171. # 直接调用 compile_algorithm 函数
  172. print_info "[1/3] 编译ReduceSum..."
  173. if ! compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
  174. print_error "ReduceSum编译失败"
  175. exit 1
  176. fi
  177. print_info "[2/3] 编译SortPair..."
  178. if ! compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
  179. print_error "SortPair编译失败"
  180. exit 1
  181. fi
  182. print_info "[3/3] 编译TopkPair..."
  183. if ! compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
  184. print_error "TopkPair编译失败"
  185. exit 1
  186. fi
  187. print_success "所有算法编译完成!"
  188. echo ""
  189. echo "可执行文件:"
  190. echo " $BUILD_DIR/test_reducesum - ReduceSum算法测试"
  191. echo " $BUILD_DIR/test_sortpair - SortPair算法测试"
  192. echo " $BUILD_DIR/test_topkpair - TopkPair算法测试"
  193. echo ""
  194. echo "使用方法:"
  195. echo " ./$BUILD_DIR/test_reducesum [correctness|performance|all]"
  196. echo " ./$BUILD_DIR/test_sortpair [correctness|performance|all]"
  197. echo " ./$BUILD_DIR/test_topkpair [correctness|performance|all]"
  198. ;;
  199. "run_all")
  200. print_info "编译并运行所有算法测试..."
  201. # 直接调用 compile_algorithm 和 run_single_test 函数
  202. print_info "[1/3] ReduceSum..."
  203. if compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
  204. run_single_test "ReduceSum" "all"
  205. else
  206. exit 1
  207. fi
  208. print_info "[2/3] SortPair..."
  209. if compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
  210. run_single_test "SortPair" "all"
  211. else
  212. exit 1
  213. fi
  214. print_info "[3/3] TopkPair..."
  215. if compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
  216. run_single_test "TopkPair" "all"
  217. else
  218. exit 1
  219. fi
  220. print_success "所有测试完成!"
  221. ;;
  222. "run_single")
  223. print_info "编译并运行 ${ALGO_TO_RUN} 测试 (模式: ${SINGLE_ALGO_TEST_MODE})..."
  224. local source_file_name=""
  225. case "$ALGO_TO_RUN" in
  226. "ReduceSum") source_file_name="reduce_sum_algorithm.maca" ;;
  227. "SortPair") source_file_name="sort_pair_algorithm.maca" ;;
  228. "TopkPair") source_file_name="topk_pair_algorithm.maca" ;;
  229. esac
  230. if compile_algorithm "$ALGO_TO_RUN" "$source_file_name"; then
  231. run_single_test "$ALGO_TO_RUN" "$SINGLE_ALGO_TEST_MODE"
  232. else
  233. exit 1
  234. fi
  235. ;;
  236. esac