Skip to content

Commit a7d50c2

Browse files
Merge pull request #1595 from kieranhejmadi01/LP-optimised-libraries
LP - Learn to migrate applications that with Arm Performance Libraries
2 parents 9650c0a + d19de47 commit a7d50c2

File tree

6 files changed

+447
-0
lines changed

6 files changed

+447
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Introduction to Performance Libraries
3+
weight: 2
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Introduction to Performance Libraries
10+
11+
The C++ Standard Library provides a collection of classes and functions that are essential for everyday programming tasks, such as data structures, algorithms, and input/output operations. It is designed to be versatile and easy to use, ensuring compatibility and portability across different platforms. However as a result of this portability, standard libraries introduces some limitations. Performance sensitive applications may wish to take maximum advantage of the hardware's capabilities - this is where performance libraries come in.
12+
13+
Performance libraries are specialized for high-performance computing tasks and are often tailored to the microarchitecture of a specific processor. These libraries are optimized for speed and efficiency, often leveraging hardware-specific features such as vector units to achieve maximum performance. Performance libraries are crafted through extensive benchmarking and optimization, and can be domain-specific, such as genomics libraries, or produced by Arm for general-purpose computing. For example, OpenRNG focuses on generating random numbers quickly and efficiently, which is crucial for simulations and scientific computations, whereas the C++ Standard Library offers a more general-purpose approach with functions like std::mt19937 for random number generation.
14+
15+
Performance libraries for Arm CPUs, such as the Arm Performance Libraries (APL), provide highly optimized mathematical functions for scientific computing. An analogous library for accelerating routines on GPU is cuBLAS for NVIDIA GPUs. These libraries can be linked dynamically at runtime or statically during compilation, offering flexibility in deployment. They are designed to support multiple versions of the Arm architecture, including those with NEON and SVE extensions. Generally, minimal source code changes are required to support these libraries, making them simple for porting and optimising.
16+
17+
### Choosing the right version of a library
18+
19+
Performance libraries are often distributed with the following formats to support various use cases.
20+
21+
- **ILP64** use 64 bits for representing integers, which are often used for indexing large arrays in scentific computing. In C++ source code we use the `long long` type to specify 64-bit integers.
22+
23+
- **LP64** use 32 bits to present integers which are more common in general purpose applications.
24+
25+
- **Open Multi-process** (OpenMP) is a programming interface for paralleling workloads across many CPU cores on shared memory across multiple platforms (i.e. x86, AArch64 etc.). Programmers would interact primarily through compiler directives, such as `#pragma omp parallel` indicating which section of source code can be run on parallel and which sections require synchronisation. This learning path does not serve to teach you about OpenMP but presumes the reader is familiar.
26+
27+
Arm performance libraries like the x86 equivalent, Open Math Kernel Library (MKL) provide optimised functions for both ILP64 and LP64 as well as OpenMP or single threaded implementations. Further, the interface libraries are available as shared libraries for dynamic linking (i.e. `*.so`) or static linking (i.e. `*.a`).
28+
29+
### Why do multiple performance Libraries exist?
30+
31+
A natural source of confusion stems from the plethora of similar seeming performance libraries, for example OpenBLAS, NVIDIA Performance Libraries (NVPL) which have their own implementations for specific functions, for example basic linear algebra subprograms (BLAS). This begs the question which one should a developer use?
32+
33+
Multiple performance libraries coexist to cater to the diverse needs of different hardware architectures and applications. For instance, Arm performance libraries are optimized for Arm CPUs, leveraging their unique instruction sets and power efficiency. On the other hand, NVIDIA performance libraries for Grace CPU are tailored to maximize the performance of NVIDIA's Grace hardware features specific to their own Neoverse implementation.
34+
35+
- **Hardware Specialization** Some libraries are designed to be cross-platform, supporting multiple hardware architectures to provide flexibility and broader usability. For example, the OpenBLAS library supports both Arm and x86 architectures, allowing developers to use the same library across different systems.
36+
37+
- **Domain-Specific Libraries**: Libraries are often created to handle specific domains or types of computations more efficiently. For instance, libraries like cuDNN are optimized for deep learning tasks, providing specialized functions that significantly speed up neural network training and inference.
38+
39+
- **Commercial Libraries**: Alternatively, some highly performant libraries require a license to use. This is more common in domain specific libraries such as computations chemistry or fluid dynamics.
40+
41+
These factors contribute to the existence of multiple performance libraries, each tailored to meet the specific demands of various hardware and applications.
42+
43+
Invariably, there will be performance differences between each library and the best way to observe it to use the library within your own program. For more information on performance benchmarking please read [this blog](https://community.arm.com/arm-community-blogs/b/servers-and-cloud-computing-blog/posts/arm-performance-libraries-24-10).
44+
45+
### What performance libraries are available on Arm?
46+
47+
For a directory of community-produced libraries we recommend looking at the the Arm Ecosystem Dashboard. Each library may not be available as a binary and may need to be compiled from source. The table below gives and example of such libraries that are available on Arm with a link to the full dashboard at the bottom.
48+
49+
50+
| Package / Library | Domain |
51+
| -------- | ------- |
52+
| Minimap2 | Long-read sequence alignment in genomics |
53+
| HMMER |Bioinformatics library for homologous sequences |
54+
| FFTW | Open-source fast fourier transform library |
55+
|[Please see the Arm Ecosystem Dashboard](https://www.arm.com/developer-hub/ecosystem-dashboard) for the most comprehensive and up-to-date list.||
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Setting Up Your Environment
3+
weight: 2
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Setting Up Your Environment
10+
11+
In this initial example we will use an Arm-based AWS `t4g.2xlarge` instance running Ubuntu 22.04 LTS along with the Arm Performance Libraries. For instructions to connect to an AWS instance, please see our [getting started guide](https://learn.arm.com/learning-paths/servers-and-cloud-computing/intro/).
12+
13+
Once connected via `ssh`, install the required packages with the following commands.
14+
15+
```bash
16+
sudo apt update
17+
sudo apt install gcc make
18+
```
19+
Next, install Arm performance libraries using the following [installation guide](https://learn.arm.com/install-guides/armpl/). Alternatively, use the commands below.
20+
21+
```bash
22+
wget https://developer.arm.com/-/cdn-downloads/permalink/Arm-Performance-Libraries/Version_24.10/arm-performance-libraries_24.10_deb_gcc.tar
23+
tar xvf arm-performance-libraries_24.10_deb_gcc.tar
24+
cd arm-performance-libraries_24.10_deb/
25+
```
26+
27+
Now we need to install environment modules to set the required environment variables, allowing us to quickly build the example applications.
28+
29+
```bash
30+
sudo add-apt-respository universe
31+
sudo apt install environment-modules
32+
source /usr/share/modules/init/bash
33+
export MODULEPATH=$MODULEPATH:/opt/arm/modulefiles
34+
module avail
35+
```
36+
37+
You should see the following `armpl/24.10.0_gcc` available.
38+
```output
39+
------------------------------------------------------------------------------------------------------- /opt/arm/modulefiles -------------------------------------------------------------------------------------------------------
40+
armpl/24.10.0_gcc
41+
```
42+
43+
Load the module with the following command.
44+
45+
```bash
46+
module load armpl/24.10.0_gcc
47+
```
48+
49+
Navigate to the `lp64` C source code examples and compile.
50+
51+
```bash
52+
cd $ARMPL_DIR
53+
cd /examples_lp64/
54+
sudo -E make c_examples // -E is to preserve environment variables
55+
```
56+
57+
Your terminal output should show the examples being compiled, ending with.
58+
59+
```output
60+
...
61+
Test passed OK
62+
```
63+
64+
For more information on all the available function, please refer to the [Arm Performance Libraries Reference Guide](https://developer.arm.com/documentation/101004/latest/).
65+
66+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: Using Optimised Math Library
3+
weight: 4
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Example using Optimised Math library
10+
11+
The `libamath` library from Arm is an optimized subset of the standard library math functions for Arm-based CPUs, providing both scalar and vector functions at different levels of precision. It includes vectorized versions (Neon and SVE) of common math functions found in the standard library, such as those in the `<cmath>` header.
12+
13+
The trivial snippet below uses the `<cmath>` standard cmath header to calculate the base exponential of a scalar value. Copy and paste the code sample below into a file named `basic_math.cpp`.
14+
15+
```c++
16+
#include <iostream>
17+
#include <ctime>
18+
#include <cmath> // Include the standard library
19+
20+
int main() {
21+
std::srand(std::time(0));
22+
double random_number = std::rand() / static_cast<double>(RAND_MAX);
23+
double result = exp(random_number); // Use the standard exponential function
24+
std::cout << "Exponential of " << random_number << " is " << result << std::endl;
25+
return 0;
26+
}
27+
```
28+
29+
Compiling using the following g++ command. We can use the `ldd` command to print the shared objects for dynamic linking. Here we observe the superset `libm.so` is linked.
30+
31+
```bash
32+
g++ basic_math.cpp -o basic_math
33+
ldd basic_math
34+
```
35+
You should see the following output.
36+
37+
```output
38+
linux-vdso.so.1 (0x0000f55218587000)
39+
libstdc++.so.6 => /lib/aarch64-linux-gnu/libstdc++.so.6 (0x0000f55218200000)
40+
libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x0000f55218490000)
41+
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000f55218050000)
42+
/lib/ld-linux-aarch64.so.1 (0x0000f5521854e000)
43+
libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x0000f55218460000)
44+
```
45+
46+
## Updating to use Optimised Library
47+
48+
To use the optimised math library `libamath` requires minimal source code changes for our scalar example. Modify the include statements to point to the correct header file and additional compiler flags.
49+
50+
Libamath routines have maximum errors inferior to 4 ULPs, where ULP stands for Unit in the Last Place, which is the smallest difference between two consecutive floating-point numbers at a specific precision. These routines only support the default rounding mode (round-to-nearest, ties to even). Therefore, switching from libm to libamath results in a small accuracy loss on a range of routines, similar to other vectorized implementations of these functions.
51+
52+
Copy and paste the following C++ snippet into a file named `optimised_math.cpp`.
53+
54+
```c++
55+
#include <iostream>
56+
#include <ctime>
57+
#include <amath.h> // Include the Arm Performance Library header
58+
59+
int main() {
60+
std::srand(std::time(0));
61+
double random_number = std::rand() / static_cast<double>(RAND_MAX);
62+
double result = exp(random_number); // Use the optimized exp function from libamath
63+
std::cout << "Exponential of " << random_number << " is " << result << std::endl;
64+
return 0;
65+
}
66+
```
67+
68+
Compiling using the following g++ command. Again we can use the `ldd` command to print the shared objects for dynamic linking.
69+
70+
```bash
71+
g++ optimised_math.cpp -o optimised_math -lamath -lm
72+
ldd optimised_math
73+
```
74+
Now we can observe the `libamath.so` shared object is linked.
75+
76+
```output
77+
78+
linux-vdso.so.1 (0x0000eb1eb379b000)
79+
libamath.so => /opt/arm/armpl_24.10_gcc/lib/libamath.so (0x0000eb1eb35c0000)
80+
libstdc++.so.6 => /lib/aarch64-linux-gnu/libstdc++.so.6 (0x0000eb1eb3200000)
81+
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000eb1eb3050000)
82+
libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x0000eb1eb3520000)
83+
/lib/ld-linux-aarch64.so.1 (0x0000eb1eb3762000)
84+
libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x0000eb1eb34f0000
85+
```
86+
87+
### What about vector operations?
88+
89+
The naming convention of the Arm Performance Library for scalar operations follows that of `libm`. Hence, we are able to simply update the header file and recompile. For vector operations, we can either rely on the compiler autovectorisation, whereby the compiler generates the vector code for us. This is used in the Arm Compiler for Linux (ACfL). Alternatively, we can use vector routines, which uses name mangling. Mangling is a technique used in computer programming to modify the names of vector functions to ensure uniqueness and avoid conflicts. This is particularly important in compiled languages like C++ and in environments where multiple libraries or modules may be used together.
90+
91+
In the context of Arm's AArch64 architecture, vector name mangling follows the specific convention below to differentiate between scalar and vector versions of functions.
92+
93+
```output
94+
'_ZGV' <isa> <mask> <vlen> <signature> '_' <original_name>
95+
```
96+
97+
- **original_name** : name of scalar libm function
98+
- **ISA** : 'n' for Neon, 's' for SVE
99+
- **Mask** : 'M' for masked/predicated version, 'N' for unmasked. Only masked routines are defined for SVE, and only unmasked for Neon.
100+
- **vlen** : integer number representing vector length expressed as number of lanes. For Neon <vlen>='2' in double-precision and <vlen>='4' in single-precision. For SVE, <vlen>='x'.
101+
- **signature** : 'v' for 1 input floating point or integer argument, 'vv' for 2. More details in AArch64's vector function ABI.
102+
103+
Please refer to the [Arm Performance Library reference guide](https://developer.arm.com/documentation/101004/latest/) for more information.

0 commit comments

Comments
 (0)