Search⌘ K

Parallel & Vectorized Execution

Explore the mechanics of parallel and vectorized execution in C++ Standard Template Library algorithms. Understand how CPU, OS, and compiler optimizations influence execution, and learn about common concurrency hazards like data races and how to avoid them.

Parallel & Vectorized Execution

Whether an algorithm runs in a parallel and vectorized way depends on many factors. For example, it depends on whether the CPU and the operating system support SIMD instructions. Additionally, it also depends on the compiler and​ the optimization level that you used to translate your code.

The following example shows a simple loop for creating a new vector.

C++
const int SIZE= 8;
int vec[] = {1, 2, 3, 4, 5, 6, 7, 8};
int res[] = {0, 0, 0, 0, 0, 0, 0, 0};
int main(){
for (int i= 0; i < SIZE; ++i) {
res[i]= vec[i]+5;
}
}

​Line 8 is the key line in this small example. Thanks to the compiler explorer, we can have a closer look at the ...