I just read this intro to parallel processing with openMP.
I tried the following simple code
#include <iostream>
#include <ctime>
#include <vector>
int main()
{
// Create an object just to allow the following loops to do something
std::vector<int> a;
a.reserve(2000);
// First single threaded loop
std::clock_t begin;
std::clock_t end;
begin = std::clock();
double elapsed_secs;
for(int n=0; n<1000000000; ++n)
{
if (n%100000000 == 0) a.push_back(n);
}
end = std::clock();
elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
std::cout << "Time for single thread loop: " << elapsed_secs << std::endl;
// Second multithreaded loop
begin = std::clock();
#pragma omp parallel for
for(int n=0; n<1000000000; ++n)
{
if (n%100000000 == 0) a.push_back(n);
}
end = std::clock();
elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
std::cout << "Time for multi thread loop: " << elapsed_secs << std::endl;
return 0;
}
which has been compiled with g++ -std=c++11 -o a a.cpp -fopenmp
which outputs
Time for single thread loop: 3.9438
Time for multi thread loop: 3.94977
- Do I misunderstand how to parallelize in C++
- Do I misunderstand how to compile?
- Is the code parallelize but the improvement in speed is not-noticable for whatever reason?
Note that I have 12 cores (and no big process currently running) on my machine.
Aucun commentaire:
Enregistrer un commentaire