I have a simple question about using OpenMP (with C++) that I hoped someone could help me with. I've included a small example below to illustrate my problem.
#include<iostream> #include<vector> #include<ctime> #include<omp.h> using namespace std; int main(){ srand(time(NULL));//Seed random number generator vector<int>v;//Create vector to hold random numbers in interval [0,9] vector<int>d(10,0);//Vector to hold counts of each integer initialized to 0 for(int i=0;i<1e9;++i) v.push_back(rand()%10);//Push back random numbers [0,9] clock_t c=clock(); #pragma omp parallel for for(int i=0;i<v.size();++i) d[v[i]]+=1;//Count number stored at v[i] cout<<"Seconds: "<<(clock()-c)/CLOCKS_PER_SEC<<endl; for(vector<int>::iterator i=d.begin();i!=d.end();++i) cout<<*i<<endl; return 0; } The above code creates a vector v that contains 1 billion random integers in the range [0,9]. Then, the code loops through v counting how many instances of each different integer there is (i.e., how many ones are found in v, how many twos, etc.)
Each time a particular integer is encountered, it is counted by incrementing the appropriate element of a vector d. So, d[0] counts how many zeroes, d[6] counts how many sixes, and so on. Make sense so far?
My problem is when I try to make the counting loop parallel. Without the #pragma OpenMP statement, my code takes 20 seconds, yet with the pragma it takes over 60 seconds.
Clearly, I've misunderstood some concept relating to OpenMP (perhaps how data is shared/accessed?). Could someone explain my error please or point me in the direction of some insightful literature with appropriate keywords to help my search?