---
title: 函数级的粗粒度调优
description: "当前的函数级粗粒度调优模块，由三部分输入组成："
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevkithistory/hgcc/hist-hgcc/kunpenghgcc_06_0073.html
sourcePath: /source/zh/kunpengdevkithistory/hgcc/hist-hgcc/kunpenghgcc_06_0073.html
indexId: f8a18bdb9135856c959b4ad4bd48a6977f51b7bcc3f078cd1e0b0bbac924826b71
---
# 函数级的粗粒度调优

当前的函数级粗粒度调优模块，由三部分输入组成：

- 应用的调优配置文件（.ini）：处理应用的编译流程、执行流程。
- 参数搜索空间配置文件（YAML）：Autotuner 阶段配置的选项调优参数搜索空间，可替换默认参数搜索空间。
- 编译选项全集文件（YAML）：预先设置的编译选项搜索空间全集，默认文件位于path/to/your/python<version>/site-packages/ai4c/autotuner/yaml/coarse_options.yaml。


当前函数级粗粒度调优基于Autotuner(https://gitee.com/openeuler/BiSheng-Autotuner)实现，可为各函数应用不同的编译选项组合进行编译优化，其调优原理与细粒度调优一致。由于各函数可调优的编译选项众多，可预先对选项空间做裁剪。

粗粒度调优的使用流程基本与细粒度调优一致。下列测试例中，我们将调优test_coarse_tuning.cc中各函数的编译选项参数。首先，我们将准备test_coarse_tuning.cc的调优配置文件test_coarse_tuning.ini。用户需要

- 提供应用路径、应用的编译与运行命令。
- 在基础编译命令中加入粗粒度调优的动态库-fplugin=%(PluginPath)s/coarse_option_tuning_plugin_gcc12.so和编译选项全集文件-fplugin-arg-coarse_option_tuning_plugin_gcc12-yaml=<YAML_FILE>。

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

```
[DEFAULT] # optional 
# TuningYAMLFile = /path/to/coarse_option_tuning_yaml_config_file

[Environment Setting]  # optional

[Compiling Setting] #required
CompileDir = ./autotune_datadir
LLVMInputFile = %(CompileDir)s/input.yaml

OppDir = opp
Compiler = g++
BaseCommand = %(Compiler)s ../test_coarse_tuning.cc -O2 -o test_coarse_tuning \     
                                -fplugin=%(PluginPath)s/coarse_option_tuning_plugin_gcc12.so \
                                -fplugin-arg-coarse_option_tuning_plugin_gcc12-yaml=%(TuningYAMLFile)s  # auto-tuning CompileCommand = %(BaseCommand)s \     -fplugin-arg-coarse_option_tuning_plugin_gcc12-autotune=input.yaml  RunDir = %(CompileDir)s RunCommand = ./test_coarse_tuning 3  # generate OppCompileCommand = %(BaseCommand)s \     -fplugin-arg-coarse_option_tuning_plugin_gcc12-generate=%(OppDir)s
```

其次，我们可以准备一份额外的参数搜索空间文件search_space.yaml，自定义参数空间。例如，在以下文件中，我们将搜索空间限制在预取相关选项上的调优。

```
CodeRegion:
  CodeRegionType: function
  Pass: coarse_option_generate
  Args:
    flag_prefetch_loop_arrays:
      Type: bool
    param_prefetch_latency:
      Min: 100
      Max: 2000
      Type: int
    param_simultaneous_prefetches:
      Min: 1M
      Max: 80
      Type: int
```

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

```
ai4c-autotune autorun test_coarse_tuning.ini \
                      -scf search_space.yaml \
                      --stage-order function \
                      --time-after-convergence=10
```

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

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

- 当前默认支持程序运行时间作为性能值。
- 粗粒度调优暂不支持dump数据库内保存的历史数据。
- 当前的粗粒度调优支持与当前版本的GCC（12.3.1）配套使用，其他编译器版本会出现部分编译选项不支持的问题，可以在path/to/your/AI4C/aiframe/include/option_utils.h中注释编译器未识别的编译选项。
