update README and CP

This commit is contained in:
Kevin Zhang 2025-09-05 14:54:42 +08:00
parent 44cc431b1d
commit e0a344c881
7 changed files with 696 additions and 557 deletions

2
.gitignore vendored
View File

@ -2,6 +2,6 @@
*.bak
*.pyc
*.o
*/build/
*/**/build/*
cp_template/*.yaml
S1/**/*.yaml

View File

@ -43,10 +43,16 @@
### 📥 选手赛题准备
1. 点击[创建赛题](https://gitee.com/ccf-ai-infra/GPUKernelContest/issues/new?template=cp.yml)
2. 记录赛题的ID并创建以赛题ID为名称的目录。例如[ICTN0N](https://gitee.com/ccf-ai-infra/GPUKernelContest/issues/ICTN0N)
1. 点击[创建赛题](https://gitee.com/ccf-ai-infra/GPUKernelContest/issues/new?template=cp.yml)并记录赛题的ID
2. 算力平台启动一个MACA3.0.0.4+PyTorch2.4.0的容器算力,详细步骤参考:[算力平台使用说明](https://ai.gitee.com/docs/compute/container)
3. 用ssh或者vscode进入容器环境Clone自己Fork的仓库
```bash
# cd GPUKernelContest
git clone https://gitee.com/giteeuseid/GPUKernelContest.git
```
> 备注:`giteeuseid替换您的url`。
4. 进入参赛项目创建以赛题ID为名称的目录。例如[ICTN0N](https://gitee.com/ccf-ai-infra/GPUKernelContest/issues/ICTN0N)
```bash
# 进入Clone的仓库
cd GPUKernelContest
# 创建以赛题ID为名称的目录
mkdir -p S1/ICTN0N
@ -58,7 +64,7 @@
├── S1(说明:第一季比赛名)
│ ├── ICTN0N(说明以自己创建赛题ID命名目录存放自己需要提交的内容)
```
3. Fork仓库并初始化比赛环境(三个核心算法题优化赛题以外自定义的赛题需有入口run.sh脚本供CI自动测试验证)
4. Fork仓库并初始化比赛环境(三个核心算法题优化赛题以外自定义的赛题需有入口run.sh脚本供CI自动测试验证)
1. 拷贝赛题样例`cp_template`到赛题`ICTN0N`目录
```bash
# cp -r cp_template/* S1/ICTN0N
@ -68,10 +74,10 @@
├── S1(说明:第一季比赛名)
│ ├── ICTN0N(说明以自己创建赛题ID命名目录存放自己需要提交的内容)
| | ├── utils
│ | ├── reduce_sum_algorithm.maca
│ | ├── run.sh说明作为CI自动测试验证的入口
| | └── competition_parallel_algorithms.md
| | └── competition_parallel_algorithms.md
| | └── competition_parallel_algorithms.md
| | └── sort_pair_algorithm.maca
| | └── topk_pair_algorithm.maca
│ └── ……
```
@ -80,27 +86,35 @@
选手赛题目录内提供了编译、测试的脚本,供选手熟悉比赛环境,步骤如下:
```bash
# 注意参赛选手需要根据自己的赛题ID进入自己初始化的目录!!!!
cd GPUKernelContest/S1/ICTN0N
# 注意参赛选手需要根据自己的赛题ID进入自己完成题目的目录!!!!
cd S1/ICTN0N
```
#### 1. 编译和运行
#### 1. 全量编译和运行
编译并运行所有算法测试(默认行为),如下:
```bash
# 编译并运行所有算法测试(默认行为)
./run.sh
```
# 编译并运行单个算法测试
./run.sh --run_reduce # ReduceSum算法
./run.sh --run_sort # SortPair算法
./run.sh --run_topk # TopkPair算法
单个或几个赛题测试验证,修改`run.sh`脚本,详细如下:
```bash
#!/bin/bash
# 单个赛题测试验证(ReduceSum算法)
./build_and_run.sh --run_reduce
```
编译运行单个ReduceSum测试如下
```bash
./run.sh # ReduceSum算法
```
#### 2. 手动运行测试
```bash
# 仅编译所有算法,不运行测试
./run.sh --build-only
./build_and_run.sh --build-only
# 单个运行不同算法的测试
./build/test_reducesum [correctness|performance|all]
@ -132,7 +146,9 @@ cd GPUKernelContest/S1/ICTN0N
| Level 2 | 融合优化 2~9 个算子 | 10 分 |
| Level 3 | 含 MMA多维矩阵乘融合算子 | 50 分 |
| Level 4 | 用于大模型推理的复杂融合算子 | 50 分 |
| 合并至MACA开源项目仓库的每个PR<需要在赛题提供对应合并的记录并确保和参赛使用的邮箱一致的提交邮箱> | - | 50 分 |
| 合并至MACA开源项目仓库的每个PR | 参考:[mcTVM](https://github.com/metax-maca/mcTVM) | 50 分 |
> 注释事项非AI Infra组下的项目PR需在赛题Iusse中提供合并记录并确保和参赛时使用邮箱一致的提交邮箱方为有效。
### ✨ 加分项:
| 内容 | 分值 |
@ -162,8 +178,8 @@ cd GPUKernelContest/S1/ICTN0N
你可以参考以下项目仓库,了解算子开发与提交格式:
- [FlashMLA](https://github.com/MetaX-MACA/FlashMLA)
- [mcTVM](https://github.com/MetaX-MACA/mcTVM)
- [FlashMLA](https://github.com/MetaX-MACA/FlashMLA)
- [mcEigen](https://github.com/MetaX-MACA/mcEigen)
- [mcPytorch](https://github.com/MetaX-MACA/mcPytorch)

274
S1/ICTN0N/build_and_run.sh Executable file
View File

@ -0,0 +1,274 @@
#!/bin/bash
# GPU高性能并行计算算法优化竞赛 - 统一编译和运行脚本
# 整合了所有算法的编译、运行和公共配置
# ============================================================================
# 公共配置和工具函数
# ============================================================================
# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# 打印函数
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# 编译配置 - 可通过环境变量自定义
COMPILER=${COMPILER:-mxcc}
COMPILER_FLAGS=${COMPILER_FLAGS:-"-O3 -std=c++17 --extended-lambda -DRUN_FULL_TEST"}
# ***** 这里是关键修改点1头文件目录 *****
# 现在头文件在 utils/ 目录下
HEADER_DIR=${HEADER_DIR:-utils}
# ***** 这里是关键修改点2源文件目录 *****
# 现在源文件在 ./ 目录下
SOURCE_CODE_DIR=${SOURCE_CODE_DIR:-}
BUILD_DIR=${BUILD_DIR:-build}
# 编译单个算法的通用函数
# 参数: $1=算法名称, $2=源文件名(不含路径)
compile_algorithm() {
local algo_name="$1"
local source_file_name="$2" # 例如 "reduce_sum_algorithm.maca"
local target_file="$BUILD_DIR/test_${algo_name,,}" # 转换为小写
print_info "编译 $algo_name 算法..."
# 创建构建目录
mkdir -p "$BUILD_DIR"
# ***** 这里是关键修改点3编译命令 *****
# -I$HEADER_DIR 用于告诉编译器头文件在哪里
# $SOURCE_CODE_DIR/$source_file_name 用于指定要编译的源文件的完整路径
local compile_cmd="$COMPILER $COMPILER_FLAGS -I$HEADER_DIR $source_file_name -o $target_file"
print_info "执行: $compile_cmd"
if $compile_cmd; then
print_success "$algo_name 编译完成!"
echo ""
echo "运行测试:"
echo " ./$target_file [correctness|performance|all]"
return 0
else
print_error "$algo_name 编译失败!"
return 1
fi
}
# 显示编译配置信息
show_build_config() {
print_info "编译配置:"
echo " COMPILER: $COMPILER"
echo " COMPILER_FLAGS: $COMPILER_FLAGS"
echo " HEADER_DIR: $HEADER_DIR" # 显示头文件目录
echo " SOURCE_CODE_DIR: $SOURCE_CODE_DIR" # 显示源文件目录
echo " BUILD_DIR: $BUILD_DIR"
echo ""
}
# 运行单个测试
run_single_test() {
local algo_name="$1"
local test_mode="${2:-all}"
local test_file="$BUILD_DIR/test_${algo_name,,}"
if [ -f "$test_file" ]; then
print_info "运行 $algo_name 测试 (模式: $test_mode)..."
"./$test_file" "$test_mode"
return $?
else
print_error "$algo_name 测试程序不存在: $test_file"
return 1
fi
}
# ============================================================================
# 主脚本逻辑
# ============================================================================
# 显示帮助信息 (整合了所有选项)
show_help() {
echo "GPU算法竞赛统一编译和运行脚本"
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " --help 显示帮助信息"
echo " --build-only 仅编译所有算法,不运行测试"
echo " --run_reduce [MODE] 编译并运行ReduceSum算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_sort [MODE] 编译并运行SortPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_topk [MODE] 编译并运行TopkPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo ""
echo "示例:"
echo " $0 # 编译并运行所有测试(默认行为)"
echo " $0 --build-only # 仅编译所有算法"
echo " $0 --run_sort performance # 编译并运行SortPair性能测试"
echo ""
}
# 解析命令行参数
RUN_MODE="run_all" # 默认为编译并运行所有测试
ALGO_TO_RUN="" # 记录要运行的单个算法
SINGLE_ALGO_TEST_MODE="all" # 单个算法的测试模式
while [[ $# -gt 0 ]]; do
case $1 in
--help)
show_help
exit 0
;;
--build-only)
RUN_MODE="build_only"
shift
;;
--run_reduce)
RUN_MODE="run_single"
ALGO_TO_RUN="ReduceSum"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_sort)
RUN_MODE="run_single"
ALGO_TO_RUN="SortPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_topk)
RUN_MODE="run_single"
ALGO_TO_RUN="TopkPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
*)
print_error "未知选项: $1"
show_help
exit 1
;;
esac
done
if [ "$RUN_MODE" = "build_only" ]; then
print_info "开始编译所有算法..."
else
print_info "开始编译并运行所有算法..."
fi
print_info "工作目录: $(pwd)"
print_info "编译时间: $(date '+%Y-%m-%d %H:%M:%S')"
show_build_config
# 清理构建目录
if [ -d "$BUILD_DIR" ]; then
print_info "清理现有构建目录: $BUILD_DIR"
rm -rf "$BUILD_DIR"
fi
# 核心逻辑:根据 RUN_MODE 执行操作
case "$RUN_MODE" in
"build_only")
print_info "编译所有算法..."
# 直接调用 compile_algorithm 函数
print_info "[1/3] 编译ReduceSum..."
if ! compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
print_error "ReduceSum编译失败"
exit 1
fi
print_info "[2/3] 编译SortPair..."
if ! compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
print_error "SortPair编译失败"
exit 1
fi
print_info "[3/3] 编译TopkPair..."
if ! compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
print_error "TopkPair编译失败"
exit 1
fi
print_success "所有算法编译完成!"
echo ""
echo "可执行文件:"
echo " $BUILD_DIR/test_reducesum - ReduceSum算法测试"
echo " $BUILD_DIR/test_sortpair - SortPair算法测试"
echo " $BUILD_DIR/test_topkpair - TopkPair算法测试"
echo ""
echo "使用方法:"
echo " ./$BUILD_DIR/test_reducesum [correctness|performance|all]"
echo " ./$BUILD_DIR/test_sortpair [correctness|performance|all]"
echo " ./$BUILD_DIR/test_topkpair [correctness|performance|all]"
;;
"run_all")
print_info "编译并运行所有算法测试..."
# 直接调用 compile_algorithm 和 run_single_test 函数
print_info "[1/3] ReduceSum..."
if compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
run_single_test "ReduceSum" "all"
else
exit 1
fi
print_info "[2/3] SortPair..."
if compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
run_single_test "SortPair" "all"
else
exit 1
fi
print_info "[3/3] TopkPair..."
if compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
run_single_test "TopkPair" "all"
else
exit 1
fi
print_success "所有测试完成!"
;;
"run_single")
print_info "编译并运行 ${ALGO_TO_RUN} 测试 (模式: ${SINGLE_ALGO_TEST_MODE})..."
local source_file_name=""
case "$ALGO_TO_RUN" in
"ReduceSum") source_file_name="reduce_sum_algorithm.maca" ;;
"SortPair") source_file_name="sort_pair_algorithm.maca" ;;
"TopkPair") source_file_name="topk_pair_algorithm.maca" ;;
esac
if compile_algorithm "$ALGO_TO_RUN" "$source_file_name"; then
run_single_test "$ALGO_TO_RUN" "$SINGLE_ALGO_TEST_MODE"
else
exit 1
fi
;;
esac

View File

@ -0,0 +1,97 @@
# 样例赛题说明
## GPU高性能并行计算算法优化
要求参赛者通过一个或多个global kernel 函数(允许配套 device 辅助函数),实现高性能算法。
在正确性、稳定性前提下,比拼算法性能。
# 1. ReduceSum算法优化
```cpp
template <typename InputT = float, typename OutputT = float>
class ReduceSumAlgorithm {
public:
// 主要接口函数 - 参赛者需要实现这个函数
void reduce(const InputT* d_in, OutputT* d_out, int num_items, OutputT init_value) {
// TODO
}
};
```
其中
* 数据类型InputT: float, OutputT: float
* 系统将测试评估1M, 128M, 512M, 1G element number下的算法性能
* 假定输入d\_in数据量为num\_items
注意事项
* 累计误差不大于cpu double golden基准的0.5%
* 注意针对NAN和INF等异常值的处理
加分项
* 使用tensor core计算reduce
* 覆盖更全面的数据范围,提供良好稳定的性能表现
# 2. Sort Pair算法优化
```cpp
template <typename KeyType, typename ValueType>
class SortPairAlgorithm {
public:
// 主要接口函数 - 参赛者需要实现这个函数
void sort(const KeyType* d_keys_in, KeyType* d_keys_out,
const ValueType* d_values_in, ValueType* d_values_out,
int num_items, bool descending) {
// TODO
}
};
```
其中
* 数据类型key: float, value: int32\_t
* 系统将测试评估1M, 128M, 512M, 1G element number下的算法性能
* 假定输入、输出的key和value的数据量一致均为num\_items
注意事项
* 需要校验结果正确性
* 结果必须稳定排序
加分项
* 支持其他不同数据类型的排序如half、double、int32_t等
* 覆盖更全面的数据范围,提供良好稳定的性能表现
# 3. Topk Pair算法优化
```cpp
template <typename KeyType, typename ValueType>
class TopkPairAlgorithm {
public:
// 主要接口函数 - 参赛者需要实现这个函数
void topk(const KeyType* d_keys_in, KeyType* d_keys_out,
const ValueType* d_values_in, ValueType* d_values_out,
int num_items, int k, bool descending) {
// TODO
}
};
```
其中
* 数据类型key: float, value: int32\_t
* 系统将测试评估1M, 128M, 512M, 1G element number下的算法性能
* 假定输入的key和value的数据量一致为num\_items输出的key和value的数据量一致为k
* k的范围32501002561024。k不大于num\_items
注意事项
* 结果必须稳定排序
加分项
* 支持其他不同数据类型的键值对,实现类型通用算法
* 覆盖更全面的数据范围,提供良好稳定的性能表现

View File

@ -1,274 +1,13 @@
#!/bin/bash
# GPU高性能并行计算算法优化竞赛 - 统一编译和运行脚本
# 整合了所有算法的编译、运行和公共配置
# 单个赛题测试验证(ReduceSum算法)
#./build_and_run.sh --run_reduce
# ============================================================================
# 公共配置和工具函数
# ============================================================================
# 单个赛题测试验证(SortPair算法)
#./build_and_run.sh --run_reduce
# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# 单个赛题测试验证(TopkPair算法)
# ./build_and_run.sh --run_topk
# 打印函数
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# 编译配置 - 可通过环境变量自定义
COMPILER=${COMPILER:-mxcc}
COMPILER_FLAGS=${COMPILER_FLAGS:-"-O3 -std=c++17 --extended-lambda -DRUN_FULL_TEST"}
# ***** 这里是关键修改点1头文件目录 *****
# 现在头文件在 includes/ 目录下
HEADER_DIR=${HEADER_DIR:-utils}
# ***** 这里是关键修改点2源文件目录 *****
# 现在源文件在 algorithms/ 目录下
SOURCE_CODE_DIR=${SOURCE_CODE_DIR:-}
BUILD_DIR=${BUILD_DIR:-build}
# 编译单个算法的通用函数
# 参数: $1=算法名称, $2=源文件名(不含路径)
compile_algorithm() {
local algo_name="$1"
local source_file_name="$2" # 例如 "reduce_sum_algorithm.maca"
local target_file="$BUILD_DIR/test_${algo_name,,}" # 转换为小写
print_info "编译 $algo_name 算法..."
# 创建构建目录
mkdir -p "$BUILD_DIR"
# ***** 这里是关键修改点3编译命令 *****
# -I$HEADER_DIR 用于告诉编译器头文件在哪里
# $SOURCE_CODE_DIR/$source_file_name 用于指定要编译的源文件的完整路径
local compile_cmd="$COMPILER $COMPILER_FLAGS -I$HEADER_DIR $source_file_name -o $target_file"
print_info "执行: $compile_cmd"
if $compile_cmd; then
print_success "$algo_name 编译完成!"
echo ""
echo "运行测试:"
echo " ./$target_file [correctness|performance|all]"
return 0
else
print_error "$algo_name 编译失败!"
return 1
fi
}
# 显示编译配置信息
show_build_config() {
print_info "编译配置:"
echo " COMPILER: $COMPILER"
echo " COMPILER_FLAGS: $COMPILER_FLAGS"
echo " HEADER_DIR: $HEADER_DIR" # 显示头文件目录
echo " SOURCE_CODE_DIR: $SOURCE_CODE_DIR" # 显示源文件目录
echo " BUILD_DIR: $BUILD_DIR"
echo ""
}
# 运行单个测试
run_single_test() {
local algo_name="$1"
local test_mode="${2:-all}"
local test_file="$BUILD_DIR/test_${algo_name,,}"
if [ -f "$test_file" ]; then
print_info "运行 $algo_name 测试 (模式: $test_mode)..."
"./$test_file" "$test_mode"
return $?
else
print_error "$algo_name 测试程序不存在: $test_file"
return 1
fi
}
# ============================================================================
# 主脚本逻辑
# ============================================================================
# 显示帮助信息 (整合了所有选项)
show_help() {
echo "GPU算法竞赛统一编译和运行脚本"
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " --help 显示帮助信息"
echo " --build-only 仅编译所有算法,不运行测试"
echo " --run_reduce [MODE] 编译并运行ReduceSum算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_sort [MODE] 编译并运行SortPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_topk [MODE] 编译并运行TopkPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo ""
echo "示例:"
echo " $0 # 编译并运行所有测试(默认行为)"
echo " $0 --build-only # 仅编译所有算法"
echo " $0 --run_sort performance # 编译并运行SortPair性能测试"
echo ""
}
# 解析命令行参数
RUN_MODE="run_all" # 默认为编译并运行所有测试
ALGO_TO_RUN="" # 记录要运行的单个算法
SINGLE_ALGO_TEST_MODE="all" # 单个算法的测试模式
while [[ $# -gt 0 ]]; do
case $1 in
--help)
show_help
exit 0
;;
--build-only)
RUN_MODE="build_only"
shift
;;
--run_reduce)
RUN_MODE="run_single"
ALGO_TO_RUN="ReduceSum"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_sort)
RUN_MODE="run_single"
ALGO_TO_RUN="SortPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_topk)
RUN_MODE="run_single"
ALGO_TO_RUN="TopkPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
*)
print_error "未知选项: $1"
show_help
exit 1
;;
esac
done
if [ "$RUN_MODE" = "build_only" ]; then
print_info "开始编译所有算法..."
else
print_info "开始编译并运行所有算法..."
fi
print_info "工作目录: $(pwd)"
print_info "编译时间: $(date '+%Y-%m-%d %H:%M:%S')"
show_build_config
# 清理构建目录
if [ -d "$BUILD_DIR" ]; then
print_info "清理现有构建目录: $BUILD_DIR"
rm -rf "$BUILD_DIR"
fi
# 核心逻辑:根据 RUN_MODE 执行操作
case "$RUN_MODE" in
"build_only")
print_info "编译所有算法..."
# 直接调用 compile_algorithm 函数
print_info "[1/3] 编译ReduceSum..."
if ! compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
print_error "ReduceSum编译失败"
exit 1
fi
print_info "[2/3] 编译SortPair..."
if ! compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
print_error "SortPair编译失败"
exit 1
fi
print_info "[3/3] 编译TopkPair..."
if ! compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
print_error "TopkPair编译失败"
exit 1
fi
print_success "所有算法编译完成!"
echo ""
echo "可执行文件:"
echo " $BUILD_DIR/test_reducesum - ReduceSum算法测试"
echo " $BUILD_DIR/test_sortpair - SortPair算法测试"
echo " $BUILD_DIR/test_topkpair - TopkPair算法测试"
echo ""
echo "使用方法:"
echo " ./$BUILD_DIR/test_reducesum [correctness|performance|all]"
echo " ./$BUILD_DIR/test_sortpair [correctness|performance|all]"
echo " ./$BUILD_DIR/test_topkpair [correctness|performance|all]"
;;
"run_all")
print_info "编译并运行所有算法测试..."
# 直接调用 compile_algorithm 和 run_single_test 函数
print_info "[1/3] ReduceSum..."
if compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
run_single_test "ReduceSum" "all"
else
exit 1
fi
print_info "[2/3] SortPair..."
if compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
run_single_test "SortPair" "all"
else
exit 1
fi
print_info "[3/3] TopkPair..."
if compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
run_single_test "TopkPair" "all"
else
exit 1
fi
print_success "所有测试完成!"
;;
"run_single")
print_info "编译并运行 ${ALGO_TO_RUN} 测试 (模式: ${SINGLE_ALGO_TEST_MODE})..."
local source_file_name=""
case "$ALGO_TO_RUN" in
"ReduceSum") source_file_name="reduce_sum_algorithm.maca" ;;
"SortPair") source_file_name="sort_pair_algorithm.maca" ;;
"TopkPair") source_file_name="topk_pair_algorithm.maca" ;;
esac
if compile_algorithm "$ALGO_TO_RUN" "$source_file_name"; then
run_single_test "$ALGO_TO_RUN" "$SINGLE_ALGO_TEST_MODE"
else
exit 1
fi
;;
esac
# 默认全量赛题测试验证参赛选手单个优化参考单个脚本执行方式CI入口run.sh
./build_and_run.sh

274
cp_template/build_and_run.sh Executable file
View File

@ -0,0 +1,274 @@
#!/bin/bash
# GPU高性能并行计算算法优化竞赛 - 统一编译和运行脚本
# 整合了所有算法的编译、运行和公共配置
# ============================================================================
# 公共配置和工具函数
# ============================================================================
# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# 打印函数
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# 编译配置 - 可通过环境变量自定义
COMPILER=${COMPILER:-mxcc}
COMPILER_FLAGS=${COMPILER_FLAGS:-"-O3 -std=c++17 --extended-lambda -DRUN_FULL_TEST"}
# ***** 这里是关键修改点1头文件目录 *****
# 现在头文件在 utils/ 目录下
HEADER_DIR=${HEADER_DIR:-utils}
# ***** 这里是关键修改点2源文件目录 *****
# 现在源文件在 ./ 目录下
SOURCE_CODE_DIR=${SOURCE_CODE_DIR:-}
BUILD_DIR=${BUILD_DIR:-build}
# 编译单个算法的通用函数
# 参数: $1=算法名称, $2=源文件名(不含路径)
compile_algorithm() {
local algo_name="$1"
local source_file_name="$2" # 例如 "reduce_sum_algorithm.maca"
local target_file="$BUILD_DIR/test_${algo_name,,}" # 转换为小写
print_info "编译 $algo_name 算法..."
# 创建构建目录
mkdir -p "$BUILD_DIR"
# ***** 这里是关键修改点3编译命令 *****
# -I$HEADER_DIR 用于告诉编译器头文件在哪里
# $SOURCE_CODE_DIR/$source_file_name 用于指定要编译的源文件的完整路径
local compile_cmd="$COMPILER $COMPILER_FLAGS -I$HEADER_DIR $source_file_name -o $target_file"
print_info "执行: $compile_cmd"
if $compile_cmd; then
print_success "$algo_name 编译完成!"
echo ""
echo "运行测试:"
echo " ./$target_file [correctness|performance|all]"
return 0
else
print_error "$algo_name 编译失败!"
return 1
fi
}
# 显示编译配置信息
show_build_config() {
print_info "编译配置:"
echo " COMPILER: $COMPILER"
echo " COMPILER_FLAGS: $COMPILER_FLAGS"
echo " HEADER_DIR: $HEADER_DIR" # 显示头文件目录
echo " SOURCE_CODE_DIR: $SOURCE_CODE_DIR" # 显示源文件目录
echo " BUILD_DIR: $BUILD_DIR"
echo ""
}
# 运行单个测试
run_single_test() {
local algo_name="$1"
local test_mode="${2:-all}"
local test_file="$BUILD_DIR/test_${algo_name,,}"
if [ -f "$test_file" ]; then
print_info "运行 $algo_name 测试 (模式: $test_mode)..."
"./$test_file" "$test_mode"
return $?
else
print_error "$algo_name 测试程序不存在: $test_file"
return 1
fi
}
# ============================================================================
# 主脚本逻辑
# ============================================================================
# 显示帮助信息 (整合了所有选项)
show_help() {
echo "GPU算法竞赛统一编译和运行脚本"
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " --help 显示帮助信息"
echo " --build-only 仅编译所有算法,不运行测试"
echo " --run_reduce [MODE] 编译并运行ReduceSum算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_sort [MODE] 编译并运行SortPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_topk [MODE] 编译并运行TopkPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo ""
echo "示例:"
echo " $0 # 编译并运行所有测试(默认行为)"
echo " $0 --build-only # 仅编译所有算法"
echo " $0 --run_sort performance # 编译并运行SortPair性能测试"
echo ""
}
# 解析命令行参数
RUN_MODE="run_all" # 默认为编译并运行所有测试
ALGO_TO_RUN="" # 记录要运行的单个算法
SINGLE_ALGO_TEST_MODE="all" # 单个算法的测试模式
while [[ $# -gt 0 ]]; do
case $1 in
--help)
show_help
exit 0
;;
--build-only)
RUN_MODE="build_only"
shift
;;
--run_reduce)
RUN_MODE="run_single"
ALGO_TO_RUN="ReduceSum"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_sort)
RUN_MODE="run_single"
ALGO_TO_RUN="SortPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_topk)
RUN_MODE="run_single"
ALGO_TO_RUN="TopkPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
*)
print_error "未知选项: $1"
show_help
exit 1
;;
esac
done
if [ "$RUN_MODE" = "build_only" ]; then
print_info "开始编译所有算法..."
else
print_info "开始编译并运行所有算法..."
fi
print_info "工作目录: $(pwd)"
print_info "编译时间: $(date '+%Y-%m-%d %H:%M:%S')"
show_build_config
# 清理构建目录
if [ -d "$BUILD_DIR" ]; then
print_info "清理现有构建目录: $BUILD_DIR"
rm -rf "$BUILD_DIR"
fi
# 核心逻辑:根据 RUN_MODE 执行操作
case "$RUN_MODE" in
"build_only")
print_info "编译所有算法..."
# 直接调用 compile_algorithm 函数
print_info "[1/3] 编译ReduceSum..."
if ! compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
print_error "ReduceSum编译失败"
exit 1
fi
print_info "[2/3] 编译SortPair..."
if ! compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
print_error "SortPair编译失败"
exit 1
fi
print_info "[3/3] 编译TopkPair..."
if ! compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
print_error "TopkPair编译失败"
exit 1
fi
print_success "所有算法编译完成!"
echo ""
echo "可执行文件:"
echo " $BUILD_DIR/test_reducesum - ReduceSum算法测试"
echo " $BUILD_DIR/test_sortpair - SortPair算法测试"
echo " $BUILD_DIR/test_topkpair - TopkPair算法测试"
echo ""
echo "使用方法:"
echo " ./$BUILD_DIR/test_reducesum [correctness|performance|all]"
echo " ./$BUILD_DIR/test_sortpair [correctness|performance|all]"
echo " ./$BUILD_DIR/test_topkpair [correctness|performance|all]"
;;
"run_all")
print_info "编译并运行所有算法测试..."
# 直接调用 compile_algorithm 和 run_single_test 函数
print_info "[1/3] ReduceSum..."
if compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
run_single_test "ReduceSum" "all"
else
exit 1
fi
print_info "[2/3] SortPair..."
if compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
run_single_test "SortPair" "all"
else
exit 1
fi
print_info "[3/3] TopkPair..."
if compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
run_single_test "TopkPair" "all"
else
exit 1
fi
print_success "所有测试完成!"
;;
"run_single")
print_info "编译并运行 ${ALGO_TO_RUN} 测试 (模式: ${SINGLE_ALGO_TEST_MODE})..."
local source_file_name=""
case "$ALGO_TO_RUN" in
"ReduceSum") source_file_name="reduce_sum_algorithm.maca" ;;
"SortPair") source_file_name="sort_pair_algorithm.maca" ;;
"TopkPair") source_file_name="topk_pair_algorithm.maca" ;;
esac
if compile_algorithm "$ALGO_TO_RUN" "$source_file_name"; then
run_single_test "$ALGO_TO_RUN" "$SINGLE_ALGO_TEST_MODE"
else
exit 1
fi
;;
esac

View File

@ -1,274 +1,13 @@
#!/bin/bash
# GPU高性能并行计算算法优化竞赛 - 统一编译和运行脚本
# 整合了所有算法的编译、运行和公共配置
# 单个赛题测试验证(ReduceSum算法)
#./build_and_run.sh --run_reduce
# ============================================================================
# 公共配置和工具函数
# ============================================================================
# 单个赛题测试验证(SortPair算法)
#./build_and_run.sh --run_reduce
# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# 单个赛题测试验证(TopkPair算法)
# ./build_and_run.sh --run_topk
# 打印函数
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# 编译配置 - 可通过环境变量自定义
COMPILER=${COMPILER:-mxcc}
COMPILER_FLAGS=${COMPILER_FLAGS:-"-O3 -std=c++17 --extended-lambda -DRUN_FULL_TEST"}
# ***** 这里是关键修改点1头文件目录 *****
# 现在头文件在 utils/ 目录下
HEADER_DIR=${HEADER_DIR:-utils}
# ***** 这里是关键修改点2源文件目录 *****
# 现在源文件在 ./ 目录下
SOURCE_CODE_DIR=${SOURCE_CODE_DIR:-}
BUILD_DIR=${BUILD_DIR:-build}
# 编译单个算法的通用函数
# 参数: $1=算法名称, $2=源文件名(不含路径)
compile_algorithm() {
local algo_name="$1"
local source_file_name="$2" # 例如 "reduce_sum_algorithm.maca"
local target_file="$BUILD_DIR/test_${algo_name,,}" # 转换为小写
print_info "编译 $algo_name 算法..."
# 创建构建目录
mkdir -p "$BUILD_DIR"
# ***** 这里是关键修改点3编译命令 *****
# -I$HEADER_DIR 用于告诉编译器头文件在哪里
# $SOURCE_CODE_DIR/$source_file_name 用于指定要编译的源文件的完整路径
local compile_cmd="$COMPILER $COMPILER_FLAGS -I$HEADER_DIR $source_file_name -o $target_file"
print_info "执行: $compile_cmd"
if $compile_cmd; then
print_success "$algo_name 编译完成!"
echo ""
echo "运行测试:"
echo " ./$target_file [correctness|performance|all]"
return 0
else
print_error "$algo_name 编译失败!"
return 1
fi
}
# 显示编译配置信息
show_build_config() {
print_info "编译配置:"
echo " COMPILER: $COMPILER"
echo " COMPILER_FLAGS: $COMPILER_FLAGS"
echo " HEADER_DIR: $HEADER_DIR" # 显示头文件目录
echo " SOURCE_CODE_DIR: $SOURCE_CODE_DIR" # 显示源文件目录
echo " BUILD_DIR: $BUILD_DIR"
echo ""
}
# 运行单个测试
run_single_test() {
local algo_name="$1"
local test_mode="${2:-all}"
local test_file="$BUILD_DIR/test_${algo_name,,}"
if [ -f "$test_file" ]; then
print_info "运行 $algo_name 测试 (模式: $test_mode)..."
"./$test_file" "$test_mode"
return $?
else
print_error "$algo_name 测试程序不存在: $test_file"
return 1
fi
}
# ============================================================================
# 主脚本逻辑
# ============================================================================
# 显示帮助信息 (整合了所有选项)
show_help() {
echo "GPU算法竞赛统一编译和运行脚本"
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " --help 显示帮助信息"
echo " --build-only 仅编译所有算法,不运行测试"
echo " --run_reduce [MODE] 编译并运行ReduceSum算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_sort [MODE] 编译并运行SortPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo " --run_topk [MODE] 编译并运行TopkPair算法测试 (MODE: correctness|performance|all, 默认all)"
echo ""
echo "示例:"
echo " $0 # 编译并运行所有测试(默认行为)"
echo " $0 --build-only # 仅编译所有算法"
echo " $0 --run_sort performance # 编译并运行SortPair性能测试"
echo ""
}
# 解析命令行参数
RUN_MODE="run_all" # 默认为编译并运行所有测试
ALGO_TO_RUN="" # 记录要运行的单个算法
SINGLE_ALGO_TEST_MODE="all" # 单个算法的测试模式
while [[ $# -gt 0 ]]; do
case $1 in
--help)
show_help
exit 0
;;
--build-only)
RUN_MODE="build_only"
shift
;;
--run_reduce)
RUN_MODE="run_single"
ALGO_TO_RUN="ReduceSum"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_sort)
RUN_MODE="run_single"
ALGO_TO_RUN="SortPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
--run_topk)
RUN_MODE="run_single"
ALGO_TO_RUN="TopkPair"
if [[ -n "$2" && "$2" != --* ]]; then
SINGLE_ALGO_TEST_MODE="$2"
shift
fi
shift
;;
*)
print_error "未知选项: $1"
show_help
exit 1
;;
esac
done
if [ "$RUN_MODE" = "build_only" ]; then
print_info "开始编译所有算法..."
else
print_info "开始编译并运行所有算法..."
fi
print_info "工作目录: $(pwd)"
print_info "编译时间: $(date '+%Y-%m-%d %H:%M:%S')"
show_build_config
# 清理构建目录
if [ -d "$BUILD_DIR" ]; then
print_info "清理现有构建目录: $BUILD_DIR"
rm -rf "$BUILD_DIR"
fi
# 核心逻辑:根据 RUN_MODE 执行操作
case "$RUN_MODE" in
"build_only")
print_info "编译所有算法..."
# 直接调用 compile_algorithm 函数
print_info "[1/3] 编译ReduceSum..."
if ! compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
print_error "ReduceSum编译失败"
exit 1
fi
print_info "[2/3] 编译SortPair..."
if ! compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
print_error "SortPair编译失败"
exit 1
fi
print_info "[3/3] 编译TopkPair..."
if ! compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
print_error "TopkPair编译失败"
exit 1
fi
print_success "所有算法编译完成!"
echo ""
echo "可执行文件:"
echo " $BUILD_DIR/test_reducesum - ReduceSum算法测试"
echo " $BUILD_DIR/test_sortpair - SortPair算法测试"
echo " $BUILD_DIR/test_topkpair - TopkPair算法测试"
echo ""
echo "使用方法:"
echo " ./$BUILD_DIR/test_reducesum [correctness|performance|all]"
echo " ./$BUILD_DIR/test_sortpair [correctness|performance|all]"
echo " ./$BUILD_DIR/test_topkpair [correctness|performance|all]"
;;
"run_all")
print_info "编译并运行所有算法测试..."
# 直接调用 compile_algorithm 和 run_single_test 函数
print_info "[1/3] ReduceSum..."
if compile_algorithm "ReduceSum" "reduce_sum_algorithm.maca"; then
run_single_test "ReduceSum" "all"
else
exit 1
fi
print_info "[2/3] SortPair..."
if compile_algorithm "SortPair" "sort_pair_algorithm.maca"; then
run_single_test "SortPair" "all"
else
exit 1
fi
print_info "[3/3] TopkPair..."
if compile_algorithm "TopkPair" "topk_pair_algorithm.maca"; then
run_single_test "TopkPair" "all"
else
exit 1
fi
print_success "所有测试完成!"
;;
"run_single")
print_info "编译并运行 ${ALGO_TO_RUN} 测试 (模式: ${SINGLE_ALGO_TEST_MODE})..."
local source_file_name=""
case "$ALGO_TO_RUN" in
"ReduceSum") source_file_name="reduce_sum_algorithm.maca" ;;
"SortPair") source_file_name="sort_pair_algorithm.maca" ;;
"TopkPair") source_file_name="topk_pair_algorithm.maca" ;;
esac
if compile_algorithm "$ALGO_TO_RUN" "$source_file_name"; then
run_single_test "$ALGO_TO_RUN" "$SINGLE_ALGO_TEST_MODE"
else
exit 1
fi
;;
esac
# 默认全量赛题测试验证参赛选手单个优化参考单个脚本执行方式CI入口run.sh
./build_and_run.sh