Function-level Coarse-grained Tuning
The current function-level coarse-grained tuning module consists of three parts:
- Application tuning configuration file (.ini): handles the compilation and execution processes of applications.
- Parameter search space configuration file (YAML): configures the parameter search space for option tuning during the Autotuner stage, which can replace the default parameter search space.
- Full compilation options file (YAML): contains the pre-configured full compilation options in the search space. The default file is located at path/to/your/python<version>/site-packages/ai4c/autotuner/yaml/coarse_options.yaml.
Currently, function-level coarse-grained tuning is implemented based on Autotuner, which enables each function to use different compilation option combinations for compilation tuning. The tuning principles are consistent with fine-grained tuning. Since there are many tunable compilation options for each function, the option space can be pre-trimmed.
The usage of coarse-grained tuning is essentially consistent with that of fine-grained tuning. In the following test example, we optimize the compilation options for each function in test_coarse_tuning.cc. First, we will prepare the tuning configuration file test_coarse_tuning.ini for test_coarse_tuning.cc. We need to:
- Provide the application path, as well as the compile and run commands for the application.
- Add the dynamic library -fplugin=%(PluginPath)s/coarse_option_tuning_plugin_gcc12.so for coarse-grained tuning and the full compilation options file -fplugin-arg-coarse_option_tuning_plugin_gcc12-yaml=<YAML_FILE> to the basic compile command.
- In the generate and autotune stages, add the input files corresponding to -fplugin-arg-coarse_option_tuning_plugin_gcc12-<stage>, separately.
- You can customize the path for the tuning opportunity configuration file (./opp/*.yaml) and the path for the compiler input file generated by the Autotuner (input.yaml).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [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 |
Next, we can prepare an additional search space file search_space.yaml to customize the parameter space. For example, in the following file, we limit the search space to tuning related to prefetch options.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
Finally, place the test_coarse_tuning.cc, test_coarse_tuning.ini, and search_space.yaml files in the same folder and run the following script:
1 2 3 4 | ai4c-autotune autorun test_coarse_tuning.ini \ -scf search_space.yaml \ --stage-order function \ --time-after-convergence=10 |
Where, the parameter time-after-convergence represents the number of seconds after the historical best value, during which no new optimal configuration is found. If no new optimal configuration is discovered within this time, the tuning process will end early.
After the tuning is complete, the optimal tuning configuration is saved in function.yaml. You can reproduce the performance value of the tuning combination by re-calling the compile command in the autotune stage and modifying the input file of the autotune option (for example, -fplugin-arg-coarse_option_tuning_plugin_gcc12-autotune=function.yaml).
- By default, the program runtime is used as the performance value.
- Coarse-grained tuning currently does not support dumping historical data stored in the database.
- Currently, coarse-grained tuning can be used with GCC 12.3.1. Other compiler versions may encounter issues where certain compilation options are unsupported. In such cases, you can comment out the unsupported compilation options in path/to/your/AI4C/aiframe/include/option_utils.h.