Build AVX2 C++ Program using CMake

Build AVX2 C++ Program using CMake

In the realm of high-performance computing, leveraging the capabilities of modern CPU architectures is crucial for achieving optimal performance. Modern CPUs support SIMD instruction sets like AVX2 (Advanced Vector Extensions 2), which can significantly accelerate certain types of computations. This tutorial explains how to build AVX2 C++ program using CMake.

Prepare environment

Before you begin, make sure you have installed both CMake and a C++ compiler (such as g++) on your system, and confirm that your CPU supports AVX2 instructions.

Build AVX2 C++ program

  • Step 1: Create a new C++ project

Create a new directory for your C++ project, naming it test_avx2. After creating the directory, navigate into it.

mkdir test_openmp && cd test_avx2
  • Step 2: Create CMakeLists.txt file

Inside the project directory, create a file named CMakeLists.txt. This file will contain the instructions for CMake to configure and generate the required build scripts.

test_avx2/CMakeLists.txt

cmake_minimum_required(VERSION 3.29)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

if (MSVC)
    add_compile_options(/arch:AVX2)
else ()
    add_compile_options(-mavx2)
endif ()

add_executable(${PROJECT_NAME} main.cpp)

The script includes a conditional statement to add architecture-specific compile options: if the compiler is Microsoft Visual C++ (MSVC), it adds the /arch:AVX2 option to enable AVX2 instructions; otherwise, it adds the -mavx2 option for GCC or Clang.

  • Step 3: Create source file

Inside the project directory, create a source file named main.cpp.

test_avx2/main.cpp

#include <iostream>
#include <immintrin.h>

int main() {
    __m256 a = _mm256_setr_ps(1, 2, 3, 4, 5, 6, 7, 8);
    __m256 b = _mm256_setr_ps(10, 11, 12, 13, 14, 15, 16, 17);
    __m256 result = _mm256_add_ps(a, b);

    auto values = reinterpret_cast<float *>(&result);
    for (int i = 0; i < 8; ++i) {
        std::cout << values[i] << " ";
    }

    return 0;
}

This C++ code demonstrates the use of AVX2 instructions to perform vectorized floating-point addition.

  • Step 4: Configure the project with CMake

In the project directory, run the following CMake command to generate the build scripts and configure the project.

make -S . -B build

The -S option indicates the source directory, which in this case is the current directory. The -B option specifies the build directory, which is named build in this instance.

  • Step 5: Build the project with CMake

Run the following CMake command to build a project.

cmake --build build

The --build option instructs CMake to perform the build in the specified directory.

  • Step 6: Run the program

Once the project has been successfully built, run the program using the following command:

./build/myapp

Output:

11 13 15 17 19 21 23 25

Leave a Comment

Cancel reply

Your email address will not be published.