Skip to content

Commit b2cbb16

Browse files
committed
Review C++ optimization Learning Path
1 parent ab0177f commit b2cbb16

File tree

5 files changed

+168
-99
lines changed

5 files changed

+168
-99
lines changed

content/learning-paths/servers-and-cloud-computing/cplusplus_compilers_flags/1.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
2-
title: Basics of Compilers
2+
title: Compiler basics
33
weight: 2
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

9-
## Introduction to C++ and Compilers
9+
## Introduction to C++ and compilers
1010

11-
The C++ language gives the programmer the freedom to be expressive in the way they write code - allowing low-level manipulation of memory and data structures. Compared to managed languages, such as Java, C++ source code is generally less portable, requiring recompilation to the target Arm architecture. In the context of optimizing C++ workloads on Arm, significant performance improvements can be achieved without modifying the source code, simply by using the compiler correctly.
11+
The C++ language gives you the freedom to be expressive in the way you write code - allowing low-level manipulation of memory and data structures. Compared to managed languages, such as Java, C++ source code is generally less portable, requiring recompilation to the target Arm architecture. In the context of optimizing C++ workloads on Arm, significant performance improvements can be achieved without modifying the source code, simply by using the compiler correctly.
1212

13-
Writing performant C++ code is a topic in itself and out of scope for this learning path. Instead we will focus on how to effectively use the compiler to target Arm instances in a cloud environment.
13+
Writing performant C++ code is a complex topic, but you can learn how to effectively use the compiler to target the Arm architecture for a Linux application.
1414

15-
## Purpose of a Compiler
15+
## What is the purpose of a compiler?
1616

17-
The g++ compiler is part of the GNU Compiler Collection (GCC), which is a set of compilers for various programming languages, including C++. The primary objective of the g++ compiler is to translate C++ source code into machine code that can be executed by a computer. This process involves several high-level stages:
17+
The G++ compiler is part of the GNU Compiler Collection (GCC), which is a set of compilers for various programming languages, including C++. The primary objective of the g++ compiler is to translate C++ source code into machine code that can be executed by a computer. This process involves several high-level stages:
1818

1919
- Preprocessing: In this initial stage, the preprocessor handles directives that start with a # symbol, such as `#include`, `#define`, and `#if`. It expands included header files, replaces macros, and processes conditional compilation statements.
2020

@@ -24,19 +24,25 @@ The g++ compiler is part of the GNU Compiler Collection (GCC), which is a set of
2424

2525
- Linking: The final stage involves linking the object code with necessary libraries and other object files. The linker merges multiple object files and libraries, resolves external references, allocates memory addresses for functions and variables, and generates an executable file that can be run on the target platform.
2626

27-
An interesting fact about the g++ compiler is that it is designed to optimize both the performance and the size of the generated code. The compiler performs various optimizations based on the knowledge it has of the program, and it can be configured to prioritize reducing the size of the generated executable.
27+
An interesting fact about the GNU compiler is that it is designed to optimize both the performance and the size of the generated code. The compiler performs various optimizations based on the knowledge it has of the program, and it can be configured to prioritize reducing the size of the generated executable.
2828

29+
### Compiler versions
2930

30-
### Compiler Versioning
31+
Two popular compilers of C++ are the GNU Compiler Collection (GCC) and LLVM - both of which are open-source compilers and have contributions from Arm engineers to support the latest architectures. Proprietary or vendor-specific compilers, such as `nvcc` for compiling for NVIDIA GPUs, are often based on these open-source compilers. Alternative proprietary compilers are often designed for specific use cases. For example, safety-critical applications may need to comply with various ISO standards, which also include the compiler. The functional safety [Arm Compiler for Embedded](https://developer.arm.com/Tools%20and%20Software/Arm%20Compiler%20for%20Embedded%20FuSa) is an example of a C/C++ compiler.
3132

32-
Two popular compilers of C++ are the GNU Compiler Collection (GCC) and LLVM - both of which are open-source compilers and have contributions from Arm engineers to support the latest architectures. Proprietary or vendor-specific compilers, such as `nvcc` for compiling for NVIDIA GPUs, are often based on these open-source compilers. Alternative proprietary compilers are often designed for specific use cases. For example, safety-critical applications may need to comply with various ISO standards, which also include the compiler. The functional safety [Arm Compiler for Embedded](https://developer.arm.com/Tools%20and%20Software/Arm%20Compiler%20for%20Embedded%20FuSa) is such an example of a C/C++ compiler.
33+
If you are an application developer who is not working in the safety qualification domain, you can use the open-source GCC/G++ compiler.
3334

34-
Most application developers are not in this safety qualification domain so we will be using the open-source GCC/G++ compiler for this learning path.
35+
There are multiple Linux distributions available to choose from. Each Linux distribution has a default compiler.
3536

36-
There are multiple Linux distribtions available to choose from. Each Linux distribution and operating system has a default compiler. For example after installing the default g++ on an `r8g` AWS instance, the default g++ compiler as of January 2025 is below.
37+
Print the version information for your compiler:
3738

38-
``` output
39+
```bash
3940
g++ --version
41+
```
42+
43+
For example, after installing `g++` on Ubuntu 24.04, the default compiler as of January 2025 is shown below.
44+
45+
```output
4046
g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
4147
Copyright (C) 2023 Free Software Foundation, Inc.
4248
This is free software; see the source for copying conditions. There is NO
@@ -59,9 +65,9 @@ Red Hat EL8 | 8*, 9, 10 | 10
5965
SUSE Linux ES15 | 7*, 9, 10 | 7
6066

6167

62-
The biggest and most simple performance gain can be achieved by using the most recent compiler available. The most recent optimisations and support will be available through the latest compiler.
68+
The easiest way to achieve a performance gain is by using the most recent compiler available. The most recent optimizations and support are available through the latest compiler.
6369

64-
Looking at the g++ documentation as an example, the most recent version of GCC available at the time of writing, version 14.2, has the following support and optimisations listed on their website [change note](https://gcc.gnu.org/gcc-14/changes.html).
70+
Looking at the G++ documentation as an example, the most recent version of GCC, version 14.2, has the following support and optimizations listed in the [release notes](https://gcc.gnu.org/gcc-14/changes.html).
6571

6672
```output
6773
A number of new CPUs are supported through the -mcpu and -mtune options (GCC identifiers in parentheses).
@@ -70,14 +76,13 @@ A number of new CPUs are supported through the -mcpu and -mtune options (GCC ide
7076
- Arm Cortex-A720 (cortex-a720).
7177
- Arm Cortex-X4 (cortex-x4).
7278
- Microsoft Cobalt-100 (cobalt-100).
73-
...
7479
```
7580

76-
Sufficient due diligence should be taken when updating your C++ compiler because the process may reveal bugs in your source code. These bugs are often undefined behaviour caused by not adhering to the C++ standard. It is rare that the compiler itself will introduced a bug. However, in such events known bugs are made publicly available in the compiler documentation.
81+
Sufficient due diligence should be taken when updating your C++ compiler because the process may reveal bugs in your source code. These bugs are often undefined behavior caused by not adhering to the C++ standard. It is rare that the compiler itself will introduced a bug. However, in such events known bugs are made publicly available in the compiler documentation.
7782

78-
## Basic g++ Optimisation Levels
83+
## Basic G++ optimization levels
7984

80-
Using the g++ compiler as an example, the most course-grained dial you can adjust is the optimisation level, denoted with `-O<x>`. This adjusts a variety of lower-level optimsation flags at the expense of increased computation time, memory use and debuggability. When aggresive optimisation is used, the optimised binary may not show expected behaviour when hooked up to a debugger such as `gdb`. This is because the generated code may not match the original source code or program order, for example from loop unrolling and vectorisation.
85+
Using the G++ compiler as an example, the most course-grained dial you can adjust is the optimization level, denoted with `-O<x>`. This adjusts a variety of lower-level optimization flags at the expense of increased computation time, memory use and debuggability. When aggressive optimization is used, the optimized binary may not show expected behavior when hooked up to a debugger such as `gdb`. This is because the generated code may not match the original source code or program order, for example from loop unrolling and vectorization.
8186

8287
A few of the most common optimization levels are in the table below.
8388

@@ -90,4 +95,4 @@ A few of the most common optimization levels are in the table below.
9095
| `-Os` | Optimizes code size, reducing the overall binary size. |
9196
| `-Ofast` | Enables optimizations that may not strictly adhere to standard compliance. |
9297

93-
Please refer to your compiler documentation for full details on the optimisation level, for example [GCC](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Optimize-Options.html).
98+
Please refer to your compiler documentation for full details on the optimization level, for example you can review the G++ [Options That Control Optimization ](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Optimize-Options.html).

content/learning-paths/servers-and-cloud-computing/cplusplus_compilers_flags/2.md

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
---
2-
title: Setup Your Environment
2+
title: Set up Your Environment
33
weight: 3
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

9-
If you are new to cloud computing, please refer to our learning path on [Getting started with Servers and Cloud Computing](https://learn.arm.com/learning-paths/servers-and-cloud-computing/intro/).
9+
If you are new to cloud computing, please refer to [Getting started with Servers and Cloud Computing](https://learn.arm.com/learning-paths/servers-and-cloud-computing/intro/). It provides an introduction to the Arm servers available from various cloud service providers.
1010

11-
## Connect to an AWS Arm-based Instance
11+
## Connect to an AWS Arm-based instance
1212

13-
In this example we will be building and running our C++ application on an AWS Graviton 4 (`r8g.xlarge`) instance running Ubuntu 24.04 LTS. Once connected run the following commands to confirm the operating system and archiecture version.
13+
In this example you will build and run a C++ application on an AWS Graviton 4 (`r8g.xlarge`) instance running Ubuntu 24.04 LTS.
14+
15+
Create the AWS instance using your AWS account. Connect to the instance using SSH or AWS Session Manager so you can enter shell commands.
16+
17+
Once connected, run the following commands to confirm the operating system and architecture.
1418

1519
```bash
1620
cat /etc/*lsb*
1721
```
1822

19-
You will see an output such as the following:
23+
You see output similar to:
2024

2125
```output
2226
DISTRIB_ID=Ubuntu
@@ -25,58 +29,65 @@ DISTRIB_CODENAME=noble
2529
DISTRIB_DESCRIPTION="Ubuntu 24.04.1 LTS"
2630
```
2731

28-
Next, we will confirm we are using a 64-bit Arm-based system using the following command
32+
Next, confirm we are using a 64-bit Arm-based system using the following command
2933

3034
```bash
3135
uname -m
3236
```
3337

34-
You will see the following output.
38+
You see the following output:
3539

3640
```output
3741
aarch64
3842
```
3943

40-
## Enable Environment modules
44+
## Enable environment modules
45+
46+
Environment modules is a tool to quickly modify your shell configuration and environment variables. For this activity, it allows you to quickly switch between different compiler versions to demonstrate potential improvements.
4147

42-
Environment modules are a tool to quickly modify your shell configuration and environment variables. For this learning path, it allows us to quickly switch between different compiler versions to demonstrate potential improvements.
48+
First, you need to install the environment modules package.
4349

44-
Install Environment Modules
50+
In your terminal and run the following command:
4551

46-
First, you need to install the environment modules package. Open your terminal and run the following command:
47-
```bash
48-
sudo apt update
49-
sudo apt install environment-modules
50-
```
52+
```bash
53+
sudo apt update
54+
sudo apt install environment-modules
55+
```
5156

52-
Load environment modules after the package is installed.
57+
Load environment modules after the package is installed:
5358

5459
```bash
5560
sudo chmod 755 /usr/share/modules/init/bash
5661
source /usr/share/modules/init/bash
5762
```
58-
Reload your shell configuration.
63+
64+
Reload your shell configuration:
5965

6066
```bash
6167
source ~/.bashrc
6268
```
6369

64-
Install various compiler version on your Ubuntu system. For this example we will install version 9 of the gcc/g++ compiler to demonstrate potential improvements your application could achieve.
70+
Install multiple compiler versions on your Ubuntu system. For this example you can install GCC version 9 to demonstrate potential improvements your application could achieve.
71+
72+
Install GCC version 9:
6573

6674
```bash
6775
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
6876
sudo apt update
69-
sudo apt install gcc-9 g++-9
77+
sudo apt install gcc-9 g++-9 -y
7078
```
7179

7280
Create a module file for each compiler installed.
7381

7482
```bash
7583
mkdir -p ~/modules/gcc
76-
nano ~/modules/gcc/9
7784
```
78-
Copy and paste the text below into the nano text editor and save the file
79-
```ouput
85+
86+
Use a text editor to modify the file `~/modules/gcc/9`
87+
88+
Copy and paste the text below into the file and save it.
89+
90+
```console
8091
#%Module1.0
8192
prepend-path PATH /usr/bin/gcc-9
8293
prepend-path PATH /usr/bin/g++-9

0 commit comments

Comments
 (0)