I have the following code

 int main()
 {
     compute();
     return 0;
 }
 void compute()
 {
     double Ptr;
     vector <double> Vector;

     for (int u=0; u<N; u++)
     {
         computeFor(u, Vector, Ptr);
         Vector.push_back(EnergyPtr);
     }
 }

 void computeFor(int i, vector<double> &List, int u, vector<double> &Vector, double &Ptr)
 {
      int Num = List.size();
      int pos[2][Num];
      for ( int i=0; i<Num; i++)
      {
           pos[0][i] = List[i]->position[0];
           pos[1][i] = List[i]->position[1];
      }

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

      for (int i=0; i<Num; i++) 
      {
         trg[i] +=3*tmp[i];
         tmp[i]=0.0;
      }
     double total = 0.0;
     for (int i=0; i<Num; i++) 
     {
         trg[i] +=3*List[i]->getDec();
         total +=trg[i];
     }
     std::stringstream ss;
     ss << "out" << u << ".txt";
     std::ofstream fout(ss.str().c_str());
     fout<<"Total is "<<total<<endl;
     Ptr = total;
 }

I want to parallelize the above, which computes total and then prints the value in a for loop to txt files out0.txt, out1.txt, out2.txt, etc. I want to just create one txt file that prints out all the values by storing the values in a vector

How would I convert this to OpenMPI? I think I understand the pragma omp parallel and pragma omp critical part, but I'm confused as to how to use pragma for since there are multiple for loops:

 void compute()
 {
     double Ptr;
     vector <double> Vector;

     int my_rank = omp_get_thread_num();
     int thread_count = omp_get_num_threads();
     #pragma omp parallel num_threads(thread_count)
     {
     compute(List, v, Vector, Ptr);
     printf("Hello from thread %d of %d\n", my_rank, thread_count);
     #pragma omp critical
     Vector.push_back(Ptr);
     }

     /*for (int u=0; u<N; u++)
     {
         compute(u, Vector, Ptr);
         Vector.push_back(EnergyPtr);
     }*/
    ofstream foutp("VectorParallel.txt");
    for (vector<double>::iterator i = Vector.begin(); i < Vector.end(); i++)
    {
             foutp<<*i<<endl;
    }
 }

For the computeFor function, do I just enter # pragma omp parallel for numthreads(threadcount) once? I am also confused as to how it works. If I have 8 threads, I believe only one thread will execute computeFor as specified in #pragma omp parallel num_threads(thread_count), right? If so, are new threads created in # pragma omp for? Or do the 8 threads carry out the work in # pragma omp for for each call to the computeFor function?

void computeFor(int i, vector<double> &List, int u, vector<double> &Vector, double &Ptr)
 {
      int Num = List.size();
      int pos[2][Num];
      # pragma omp for
      for ( int i=0; i<Num; i++)
      {
           pos[0][i] = List[i]->position[0];
           pos[1][i] = List[i]->position[1];
      }

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

      for (int i=0; i<Num; i++) 
      {
         trg[i] +=3*tmp[i];
         tmp[i]=0.0;
      }
     double total = 0.0;
     for (int i=0; i<Num; i++) 
     {
         trg[i] +=3*List[i]->getDec();
         total +=trg[i];
     }
     std::stringstream ss;
     ss << "out" << u << ".txt";
     std::ofstream fout(ss.str().c_str());
     fout<<"Total is "<<total<<endl;
     Ptr = total;
 }

Sorry, but I haven't used OpenMP for about 20 years... Try the OpenMP user forums, assuming there are some. FWIW, OpenMP is good to distribute computations over multiple discrete systems, but given today's multi-processor, multi-core systems, threads work a lot better, at least up to a point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.