---
title: 细粒度调优
description: "此处我们以 GCC 内循环展开优化 pass 的细粒度调优为例，展开调优工具的使用流程。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevkithistory/hgcc/hist-hgcc/kunpenghgcc_06_0072.html
sourcePath: /source/zh/kunpengdevkithistory/hgcc/hist-hgcc/kunpenghgcc_06_0072.html
indexId: 6dd59adf3b62ee1132098375fb67bce60952418dab09a777f7f5aaa40ed74e2271
---
# 细粒度调优

此处我们以 GCC 内循环展开优化 pass 的细粒度调优为例，展开调优工具的使用流程。

当前的细粒度调优模块，由两部分输入组成：

- 应用的调优配置文件（.ini）：处理应用的编译流程、执行流程。
- 搜索参数空间配置文件（YAML）：Autotuner 阶段配置的选项调优参数搜索空间，可替换默认参数搜索空间。


当前细粒度调优基于Autotuner(https://gitee.com/openeuler/BiSheng-Autotuner)实现：

1. 在编译器的generate阶段，生成一组可调优的编译数据结构与可调优系数集合，保存在opp/*.yaml内。
2. 根据额外提供的编译参数搜索空间（search_space.yaml）与可调优数据结构，Autotuner 通过调优算法针对每个可调优数据结构生成下一组调优系数，保存在input.yaml中。
3. 在编译器的autotune阶段，根据input.yaml内数据结构的 hash 值，将调优系数标注到对应的数据结构里，完成调优。


下列测试例中，我们将对CoreMark(https://gitee.com/link?target=https://github.com/eembc/coremark)的循环展开参数进行调优。首先，需准备CoreMark的调优配置文件coremark_sample.ini。用户需提供以下内容：

- 应用路径、编译命令及运行命令。
- 在基础编译命令中加入细粒度调优的动态库-fplugin=%(PluginPath)s/rtl_unroll_autotune_plugin_gcc12.so。

  - 在generate和autotune阶段，分别加入-fplugin-arg-rtl_unroll_autotune_plugin_gcc12-<stage>的相应输入文件。
- 可自定义可调优结构配置文件的路径（./opp/*.yaml）、Autotuner 生成的编译器输入文件路径（input.yaml）等。

```
[DEFAULT] # optional
# PluginPath = /path/to/gcc-plugins

[Environment Setting]  # optional
# prepend a list of paths into the PATH in order.
# PATH = /path/to/bin# you can also set other environment variables here too [Compiling Setting] # required
# NOTE: ConfigFilePath is set to the path to the current config file automatically by default.
CompileDir = /path/to/coremark
LLVMInputFile = %(CompileDir)s/input.yaml

# OppDir and OppCompileCommand are optional,
# do not have to specify this if not using auto_run sub-command
OppDir = autotune_datadir/opp

CompilerCXX = /path/to/bin/gcc
BaseCommand = %(CompilerCXX)s -I. -I./posix -DFLAGS_STR=\""  -lrt"\" \
                                               -DPERFORMANCE_RUN=1 -DITERATIONS=10000 -g            \
                                               core_list_join.c  core_main.c core_matrix.c          \
                                               core_state.c core_util.c posix/core_portme.c         \
                                               -funroll-loops -O2 -o coremark                       \
                                               -fplugin=%(PluginPath)s/rtl_unroll_autotune_plugin_gcc12.so

# auto-tuning
CompileCommand = %(BaseCommand)s \
    -fplugin-arg-rtl_unroll_autotune_plugin_gcc12-autotune=%(LLVMInputFile)s
RunDir = %(CompileDir)s
RunCommand = ./coremark 0x0 0x0 0x66 100000 # run 300000 iterations for coremark

# generate
OppCompileCommand = %(BaseCommand)s \
     -fplugin-arg-rtl_unroll_autotune_plugin_gcc12-generate=%(OppDir)s
```

其次，我们可以准备一份额外的参数搜索空间文件search_space.yaml，自定义缩小参数空间。例如，动态库默认选择循环展开系数空间为

```
CodeRegion:
    CodeRegionType: loop
    Pass: loop2_unroll
    Args:
      UnrollCount:
        Value: [0, 1, 2, 4, 8, 16, 32]
        Type: enum
```

最终我们将 coremark，coremark_sample.ini，和search_space.yaml 放在同一个文件夹下，并运行以下脚本：

```
ai4c-autotune autorun coremark_sample.ini \
   -scf search_space.yaml --stage-order loop \
   --time-after-convergence=100
```

其中，参数time-after-convergence代表历史最佳值后多少秒未发现新的最优配置时，即提早结束调优。

调优完成后，最佳调优配置将保存在loop.yaml内，并可通过重新调用autotune阶段编译命令，同时修改autotune选项的输入文件（i.e., -fplugin-arg-rtl_unroll_autotune_plugin_gcc12-autotune=loop.yaml），复现该调优组合的性能值。

用户可以通过以下方式调取历史调优配置文件（autotune_config.csv）与性能数据文件（autotune_data.csv）：

```
ai4c-autotune dump -c coremark/input.yaml \     --database=opentuner.db/localhost.localdomain.db -o autotune
```

- 当前默认支持程序运行时间作为性能值。
