Chapter 3.3 : The CMakeLists.txt file
Let's write the CMakeLists.txt file.
Ordinary, we call the add_executable function from CMake and we can compile our program easily. But, for this tutorial, we want to compare the different compilation options of the compiler (here GCC/G++).
So, we will compile the same code (main.cpp) several times to compare the different execution performances :
We will test different compilation optimisation option which are ordered from the lower to the higher level of optimisation :
- hadamard_product_O0 to test the -O0 option.
- hadamard_product_O1 to test the -O1 option.
- hadamard_product_O2 to test the -O2 option.
- hadamard_product_O3 to test the -O3 option.
- hadamard_product_Ofast to test the -Ofast option.
And we will call our runExample function to run all the programs (or targets).
We will create all these targets in the CMakeLists.txt :
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 |
project(HadamardProduct) cmake_minimum_required(VERSION 3.0) add_executable(hadamard_product_O0 main.cpp) set_property(TARGET hadamard_product_O0 PROPERTY COMPILE_FLAGS "-O0") target_link_libraries(hadamard_product_O0 asterics_hpc) runExample(hadamard_product_O0) add_executable(hadamard_product_O1 main.cpp) set_property(TARGET hadamard_product_O1 PROPERTY COMPILE_FLAGS "-O1") target_link_libraries(hadamard_product_O1 asterics_hpc) runExample(hadamard_product_O1) add_executable(hadamard_product_O2 main.cpp) set_property(TARGET hadamard_product_O2 PROPERTY COMPILE_FLAGS "-O2") target_link_libraries(hadamard_product_O2 asterics_hpc) runExample(hadamard_product_O2) add_executable(hadamard_product_O3 main.cpp) set_property(TARGET hadamard_product_O3 PROPERTY COMPILE_FLAGS "-O3") target_link_libraries(hadamard_product_O3 asterics_hpc) runExample(hadamard_product_O3) add_executable(hadamard_product_Ofast main.cpp) set_property(TARGET hadamard_product_Ofast PROPERTY COMPILE_FLAGS "-Ofast") target_link_libraries(hadamard_product_Ofast asterics_hpc) runExample(hadamard_product_Ofast) |
Finally, we have to call our plotPerf to compare their performances :
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 |
project(HadamardProduct) cmake_minimum_required(VERSION 3.0) add_executable(hadamard_product_O0 main.cpp) set_property(TARGET hadamard_product_O0 PROPERTY COMPILE_FLAGS "-O0") target_link_libraries(hadamard_product_O0 asterics_hpc) runExample(hadamard_product_O0) add_executable(hadamard_product_O1 main.cpp) set_property(TARGET hadamard_product_O1 PROPERTY COMPILE_FLAGS "-O1") target_link_libraries(hadamard_product_O1 asterics_hpc) runExample(hadamard_product_O1) add_executable(hadamard_product_O2 main.cpp) set_property(TARGET hadamard_product_O2 PROPERTY COMPILE_FLAGS "-O2") target_link_libraries(hadamard_product_O2 asterics_hpc) runExample(hadamard_product_O2) add_executable(hadamard_product_O3 main.cpp) set_property(TARGET hadamard_product_O3 PROPERTY COMPILE_FLAGS "-O3") target_link_libraries(hadamard_product_O3 asterics_hpc) runExample(hadamard_product_O3) add_executable(hadamard_product_Ofast main.cpp) set_property(TARGET hadamard_product_Ofast PROPERTY COMPILE_FLAGS "-Ofast") target_link_libraries(hadamard_product_Ofast asterics_hpc) runExample(hadamard_product_Ofast) plotPerf("hadamardBase" hadamard_product_O0 hadamard_product_O1 hadamard_product_O2 hadamard_product_O3 hadamard_product_Ofast) |