IBM XL C/C++ Compiler for AIX level can improve performance
by user
Comments
Transcript
IBM XL C/C++ Compiler for AIX level can improve performance
IBM XL C/C++ Compiler for AIX IBM XL C/C++ Compilers for AIX how increasing optimization level can improve performance By: Raymond Kwong Jaideep Bajwa Level: Introductory May 2012 How increasing optimization level can improve performance, Page 2 of 11 Contents IBM XL C/C++ Compiler for AIX ................................................................................. 1 About this series ...................................................................................................... 3 About this Tutorial ................................................................................................... 3 Objectives .............................................................................................................. 3 Prerequisites ........................................................................................................... 3 System Requirements............................................................................................... 3 Glossary ................................................................................................................. 4 Introducing Optimization........................................................................................... 4 Start the Terminal Emulator to AIX System.................................................................. 5 Get Started with IBM XL C/C++ Compiler optimization .................................................. 5 Conclusion ............................................................................................................ 11 Trademarks........................................................................................................... 11 Resources ............................................................................................................. 11 Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 3 of 11 Before you start About this series Walk through this scenario and others online as part of the IBM XL C/C++ Compiler for AIX About this Tutorial This tutorial introduces optimizing C code at level 2 using the XL C compiler. You will learn about the compiler options you can use that will help you optimize your code at level 2. You will also learn how to make code changes that will address the common code problems uncovered at this level of optimization. Objectives • • • • Explain what you need to do before optimizing code Increase optimization level to achieve higher performance with C/C++ program Address common C/C++ code problems using compiler options and rewriting code Total time: 30 minutes Prerequisites • • • • Basic Basic Basic Basic Unix skills Source code compile and build experience knowledge of time UNIX utilities knowledge of C programming language System Requirements http://www.ibm.com/software/awdtools/xlcpp/aix/sysreq/ Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 4 of 11 Glossary IBM XL C/C++ Compiler: IBM® XL C and C++ compilers offer advanced compiler and optimization technologies and are built on a common code base for easier porting of your applications between platforms. They comply with the latest C/C++ international standards and industry specifications and support a large array of common language features. Introducing Optimization The optimizer includes five base optimization levels: -O0, -O2, -O3, -O4, and -O5 to generate faster, more efficient applications. These levels allow you to choose from minimal optimization to intense program analysis that provides benefits even across programming languages. Optimization analyses range from local basic block to subprogram to file-level to whole-program analysis. The higher the optimization level, the more intense the program analysis becomes as increasingly sophisticated optimization techniques are applied to your code. Getting the best optimization is a process of moving forward, then slowly backing off when you encounter problems. With any combination of code and compiler, either the code or compiler can contain problems, which prevents the code from reaching the highest optimization level offered by the compiler. This is why always begin at level 0 and make sure any code issues are fixed and the code is as efficient as possible, in order to get the most of high level optimizations. This document will focus on the base optimization levels available, including high-order transformations. Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 5 of 11 Getting Started Start the Terminal Emulator to AIX System Figure 1 Get Started Double click the “Launch AIX” icon on the desktop (See Figure 1) to start the character terminal to AIX system. Get Started with IBM XL C/C++ Compiler optimization Successful login will result with user presented with a menu of demo hosted on the server. Type 7 and press Enter to select the “Increasing Optimization Level for performance” demo. Figure 2 Demo Prepared On the terminal window you will see important information and directory path to compiler install directory (See Figure 2 Demo Prepared oval red). Note: Starting another command window will start the demonstration setup of your environment. This will result in loss of any work done in your home directory. This will impact any progress you have made on demo steps going forward. This demo does not require more than one terminal window. However, if you prefer Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 6 of 11 more than one terminal window then you may open them before going forward. Terminal window is now ready for commands (See Figure 2 Demo Prepared arrow). Your home directory contains necessary source code to perform the tutorial. Type ls command to see the directory content (See Figure 3 Contents). Command: ls Figure 3 Contents Directory contents input.dat Data for the programs Game.c John Conway’s Game of Life source code Introducing Game of Life is a cellular automation invented by John Conway in 1970. Cells are arranged in a rectangular shape, with each cell being a square that has a maximum of 8 and a minimum of 3 neighboring cells. There are three rules that govern this automation. First, if a live cell has 2 or 3 live neighbors it stays live. Second, if a live cell has less than 2 or more than 3 live neighbors, it dies in the next generation. Third, if a dead cell has exactly 3 live neighbors it becomes live in the next generation. The rules are applied to each cell simultaneously, so the transition from one state to the other is a one step process which results in a new generation. To learn more about John Conway’s Game of Life visit Wikipedia page http://en.wikipedia.org/wiki/Conway's_Game_of_Life Steps: Suppose you don’t have a lot of time to implement this automation. Assume also that the initial state is given by a set of points that are stored in a file called input.dat in a form of one row per point, where x and y are separated by a whitespace. To minimize the effect of I/O delays and variability during timing steps, this version of the game does not produce output to the console. 1. Compile the game with no options (this is optimization level 0 by default) as shown in Figure 4 No optimization. Command: xlc –o game-noopt game.c Figure 4 No optimization Time the executable produced to set a base line for comparison. See figure 5 Time baseline. Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 7 of 11 Command: time game-noopt Figure 5 Time baseline 2. Now, try to compile the game with optimization level 2. See figure 6 Optimized O2 Command: xlc –O2 –o game-o2 game.c Figure 6 Optimized O2 Time the executable produced by optimization level 2. See figure 7 Time O2 Command: time game-o2 Figure 7 Time O2 3. Compile the game with a higher optimization level of 3. See figure 8 Optimized O3 Command: xlc –O3 –o game-o3 game.c Figure 8 Optimized O3 Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 8 of 11 Time the executable produced by optimization level 3. See figure 9 Time O3 Command: time game-o3 Figure 9 Time O3 4. Compile the game with optimization level 3 and enable high-order transformations. See figure 10 Optimized O3 Hot Command: xlc –O3 –qhot –o game-o3hot game.c Figure 10 Optimized O3 Hot Time the executable produced by optimization level 3 and high-order transformations. See figure 11 Time O3 Hot Command: time game-o3hot Figure 11 Time O3 Hot 5. Compile the game with optimization level 4 (this invokes inter-procedural analysis). See figure 12 Optimized O4 Command: xlc –O4 –o game-o4 game.c Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 9 of 11 Figure 12 Optimized O4 Time the executable produced by optimization level 4. See figure 13 Time O4 Command: time game-o4 Figure 13 Time O4 6. Compile the game with optimization level 5 (this invokes more complex interprocedural analysis). See figure 14 Optimized O5 Command: xlc –O5 –o game-o5 game.c Figure 14 Optimized O5 Time the executable produced by optimization level 5. See figure 15 Time O5 Command: time game-o5 Figure 15 Time O5 Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 10 of 11 As you can see, optimization can have a large effect on the runtime performance of the program. For example, when run on an isolated single user system, the run-time of the game decreased roughly five times at optimization level 5 compared to no optimization. Keep in mind the complexity of the program being optimized can influence the effectiveness of which optimizations are applied and that the levels optimizations can scale differently to individual programs. In our example here, our test program is not large or particularly complex, and as a result we can see the largest gain happens moving from no optimization to level 2 optimization, then modest gains going to level 3, enabling high order transformations and so on. As you notice with this example, as we progress through the optimizations, the compiler is steadily exhausting more of the opportunities as we increase the optimization level and experiences diminishing returns at the highest optimization levels, this behavior can change with the complexity and size of the program being optimized. Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®. How increasing optimization level can improve performance, Page 11 of 11 What you have learned In this • • • • exercise you learnt how to: Use IBM XL C/C++ compiler for AIX to build source code. Use optimization flags to optimize the program. Your code can be optimized to varying degrees of optimization, with higher levels yielding better performance. Size and complexity of a program can affect the degree it is optimized at each optimization level. Conclusion This concludes the tutorial on using IBM XL C/C++ compiler with Optimization. This tutorial has shown how to compile programs with IBM XL C/C++ compiler for AIX and use the base optimization level options. It has also shows that increasing the level of optimization during compile-time will result in an increase of run-time performance. Trademarks IBM and the IBM logo are trademarks of International Business Machines Corporation in the United States, other countries or both. Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. Other company, product and service names may be trademarks or service marks of others. Resources XL C/C++ for AIX, Library : http://www.ibm.com/software/awdtools/xlcpp/aix/library/ Community Cafe : http://www.ibm.com/software/rational/cafe/community/ccpp Copyright © 2012, IBM® Corporation. Published by IBM® developerWorks®.