We will use the unmodified source code from Chapter 3, Detecting External Libraries and Programs, Recipe 5, Detecting the OpenMP parallel environment. The example code sums up all natural numbers up to N (example.cpp):
#include <iostream>#include <omp.h>#include <string>int main(int argc, char *argv[]) { std::cout << "number of available processors: " << omp_get_num_procs() << std::endl; std::cout << "number of threads: " << omp_get_max_threads() << std::endl; auto n = std::stol(argv[1]); std::cout << "we will form sum of numbers from 1 to " << n << std::endl; // start timer auto t0 = omp_get_wtime(); auto s = 0LL;#pragma omp parallel for reduction(+ : s) for (auto i = 1; i <= n; i++) { s += i; } // stop timer auto t1 = omp_get_wtime(); ...