Fine-grained Tuning
The fine-grained tuning of the GCC loop unrolling optimization pass is used as an example to describe the usage flow of the tuning tool.
The current fine-grained tuning module consists of two 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.
Fine-grained tuning is currently implemented based on Autotuner.
- In the generate phase of the compiler, a set of tuning compilation data structures and tuning coefficient sets are generated and stored in opp/*.yaml.
- Based on the additional compilation parameter search space (search_space.yaml) and the tunable data structures, Autotuner uses tuning algorithms to generate the next set of tuning coefficients for each tuning data structure, which are saved in input.yaml.
- During the autotune phase of the compiler, the tuning coefficients are marked in the corresponding data structures based on the hash values of the data structures in input.yaml to complete the tuning process.
In the following test example, we will tune the loop unrolling parameters of CoreMark. First, prepare the CoreMark tuning configuration file coremark_sample.ini. The following information must be provided:
- Application path, compilation command, and running command.
- Add the dynamic library -fplugin=%(PluginPath)s/rtl_unroll_autotune_plugin_gcc12.so for fine-grained tuning to the basic compile command.
- In the generate and autotune stages, add the input files corresponding to -fplugin-arg-rtl_unroll_autotune_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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | [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 |
Next, we can prepare an additional search space file search_space.yaml to customize the parameter space to be reduced. For example, the dynamic library by default selects the loop unrolling coefficient space as:
1 2 3 4 5 6 7 | CodeRegion: CodeRegionType: loop Pass: loop2_unroll Args: UnrollCount: Value: [0, 1, 2, 4, 8, 16, 32] Type: enum |
Finally, place the coremark, coremark_sample.ini, and search_space.yaml files in the same folder and run the following script:
1 2 3 | ai4c-autotune autorun coremark_sample.ini \ -scf search_space.yaml --stage-order loop \ --time-after-convergence=100 |
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 loop.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-rtl_unroll_autotune_plugin_gcc12-autotune=loop.yaml).
Users can retrieve the historical tuning configuration file (autotune_config.csv) and performance data file (autotune_data.csv) as follows:
1 | ai4c-autotune dump -c coremark/input.yaml \ --database=opentuner.db/localhost.localdomain.db -o autotune |
By default, the program runtime is used as the performance value.