On some Online Judge platforms, codes are compile and run in such way:
g++ -O3 -std=c++17 a.cpp
./a.out
I want to use OpenMp to parallelize my code, so is it possible to enable OpenMP without -fopenmp flag?
#include <iostream>
using namespace std;
int main() {
#pragma omp parallel for
for (int i = 0; i < 100; i++) {
std::cout << i << std::endl;
}
}
I've tried added this to source code, but it doesn't work:
#pragma GCC optimize("openmp")
update: The code above is a terrible example, here's another program that can be accelerated by parallelization:
#include <iostream>
#include <cmath>
#include <chrono>
int main() {
auto start = std::chrono::steady_clock::now();;
double sum = 0;
int n = 2e9;
//std::cin >> n;
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < n; i++) {
sum += sqrt(i);
}
auto end = std::chrono::steady_clock::now();
std::cout << sum << std::endl;
std::cout << n << std::endl;
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
}
cout<<is a pretty terrible example of something you'd want to parallelize! To preserve the semantics of the program (produce the same output), everycout <<operation has to happen in source order. (And will have to do locking to control access to the I/O buffer for that stream anyway, which a purely single-threaded process might have avoided). GCC wouldn't auto-parallelize this even if you did use-fopenmp.