OpenMP - Am I using pragma correctly in for loop?

I gave the below code a shot:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  int main()
    {
         Solve* slv = new Solve();
         slv->compute();
         return 0;
    }

   void Solve::compute()
    {
         double Ptr;
         vector <double> valueVector;
         double List;
         default_random_engine generator;
         uniform_real_distribution<double> uDistribution;
         double news;

         omp_set_num_threads(16);
         #pragma omp parallel for num_threads(16)
         for (int u=0; u<3200; u++)
         {   
             double news;
             news = uDistribution(generator);
             List = news;
             computeFor(List, u, valueVector, Ptr); 
         }
    }

    void Solve::computeFor(double List, int u, vector<double> &valueVector, double &Ptr)
    {
        double pos;
        for ( int i=0; i<3200; i++)
        {
           pos = List*List;
        }

        double tmp[3200];
        double trg[3200];
        double cnt[3200];
        for (int i=0; i<3200; i++) 
        {
           tmp[i]=List;
        }

        for (int i=0; i<3200; i++) 
        {
           cnt[i] +=3*tmp[i];
           tmp[i]=0.0;
        }
        double total = 0.0;
        for (int i=0; i<3200; i++) 
        {
           trg[i] +=3*pos - cnt[i];
           total +=trg[i];
        }

        cout<<"Total is "<<total<<endl;
        Ptr = total;
    }


When I run this, the parallel version is SLOWER than the sequential, as the sequential takes about 37 seconds whereas the parallel takes about 117

Am I writing that #pragma omp parallel for num_threads(16) correctly? Should I use a different #pragma instead?
Last edited on
Topic archived. No new replies allowed.