1 This folder contains an implementation of [automemcpy: A framework for automatic generation of fundamental memory operations](https://research.google/pubs/pub50338/).
3 It uses the [Z3 theorem prover](https://github.com/Z3Prover/z3) to enumerate a subset of valid memory function implementations. These implementations are then materialized as C++ code and can be [benchmarked](../) against various [size distributions](../distributions). This process helps the design of efficient implementations for a particular environnement (size distribution, processor or custom compilation options).
5 This is not enabled by default, as it is mostly useful when working on tuning the library implementation. To build it, use `LIBC_BUILD_AUTOMEMCPY=ON` (see below).
9 You may need to install `Z3` from source if it's not available on your system.
10 Here we show instructions to install it into `<Z3_INSTALL_DIR>`.
11 You may need to `sudo` to `make install`.
16 git clone https://github.com/Z3Prover/z3.git
17 python scripts/mk_make.py --prefix=<Z3_INSTALL_DIR>
27 cd <LLVM_PROJECT_DIR>/llvm
28 cmake -DCMAKE_C_COMPILER=/usr/bin/clang \
29 -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
30 -DLLVM_ENABLE_PROJECTS="libc" \
31 -DLLVM_ENABLE_Z3_SOLVER=ON \
32 -DLLVM_Z3_INSTALL_DIR=<Z3_INSTALL_DIR> \
33 -DLIBC_BUILD_AUTOMEMCPY=ON \
34 -DCMAKE_BUILD_TYPE=Release \
38 ## Targets and compilation
40 There are three main CMake targets
41 1. `automemcpy_implementations`
42 - runs `Z3` and materializes valid memory functions as C++ code, a message will display its ondisk location.
43 - the source code is then compiled using the native host optimizations (i.e. `-march=native` or `-mcpu=native` depending on the architecture).
45 - the binary that benchmarks the autogenerated implementations.
46 3. `automemcpy_result_analyzer`
47 - the binary that analyses the benchmark results.
49 You may only compile the binaries as they both pull the autogenerated code as a dependency.
52 make -C <BUILD_DIR> -j automemcpy automemcpy_result_analyzer
55 ## Running the benchmarks
57 Make sure to save the results of the benchmark as a json file.
60 <BUILD_DIR>/bin/automemcpy --benchmark_out_format=json --benchmark_out=<RESULTS_DIR>/results.json
63 ### Additional useful options
66 - `--benchmark_min_time=.2`
68 By default, each function is benchmarked for at least one second, here we lower it to 200ms.
70 - `--benchmark_filter="BM_Memset|BM_Bzero"`
72 By default, all functions are benchmarked, here we restrict them to `memset` and `bzero`.
74 Other options might be useful, use `--help` for more information.
76 ## Analyzing the benchmarks
78 Analysis is performed by running `automemcpy_result_analyzer` on one or more json result files.
81 <BUILD_DIR>/bin/automemcpy_result_analyzer <RESULTS_DIR>/results.json
85 1. Gathers all throughput values for each function / distribution pair and picks the median one.\
86 This allows picking a representative value over many runs of the benchmark. Please make sure all the runs happen under similar circumstances.
88 2. For each distribution, look at the span of throughputs for functions of the same type (e.g. For distribution `A`, memcpy throughput spans from 2GiB/s to 5GiB/s).
90 3. For each distribution, give a normalized score to each function (e.g. For distribution `A`, function `M` scores 0.65).\
91 This score is then turned into a grade `EXCELLENT`, `VERY_GOOD`, `GOOD`, `PASSABLE`, `INADEQUATE`, `MEDIOCRE`, `BAD` - so that each distribution categorizes how function perform according to them.
93 4. A [Majority Judgement](https://en.wikipedia.org/wiki/Majority_judgment) process is then used to categorize each function. This enables finer analysis of how distributions agree on which function is better. In the following example, `Function_1` and `Function_2` are rated `EXCELLENT` but looking at the grade's distribution might help decide which is best.
95 | | EXCELLENT | VERY_GOOD | GOOD | PASSABLE | INADEQUATE | MEDIOCRE | BAD |
96 |------------|:---------:|:---------:|:----:|:--------:|:----------:|:--------:|:---:|
97 | Function_1 | 7 | 1 | 2 | | | | |
98 | Function_2 | 6 | 4 | | | | | |
100 The tool outputs the histogram of grades for each function. In case of tie, other dimensions might help decide (e.g. code size, performance on other microarchitectures).
103 EXCELLENT |█▁▂ | Function_0
104 EXCELLENT |█▅ | Function_1
105 VERY_GOOD |▂█▁ ▁ | Function_2
106 GOOD | ▁█▄ | Function_3
107 PASSABLE | ▂▆▄█ | Function_4
108 INADEQUATE | ▃▃█▁ | Function_5
109 MEDIOCRE | █▆▁| Function_6
110 BAD | ▁▁█| Function_7