Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop for iterator openMP

I use OpenMP to parallelize my code. I try to parallelize a loop for with two iterator. I would like to know if my implementation is the best way to parallelize this sequential code:

#include <iostream>
#include <vector>
#include <omp.h>

using namespace std;

int main(int argc, char *argv[])
{
  vector<float> v = {1, 2, 3, 4};
  vector<float> d = {0, 0, 0, 0};   
  vector<float>::iterator iterV, iterD; 

  for(iterV = v.begin(), iterD = d.begin(); iterV < v.end(); ++iterV, ++iterD)
    {
      *iterD =  *iterV; 
    }


  for(iterD = d.begin(); iterD < d.end(); ++iterD)
    cout << *iterD << endl;  

  return 0;
}

My parallel version of this code :

#include <iostream>
#include <vector>
#include <omp.h>

using namespace std;

int main(int argc, char *argv[])
{
  vector<float> v = {1, 2, 3, 4};
  vector<float> d = {0, 0, 0, 0};   
  vector<float>::iterator iterV, iterD; 

  iterV = v.begin();
  iterD = d.begin();

 #pragma omp parallel for
  for(int i = 0; i < v.size(); ++i)
    {
      *(iterD + i) =  *(iterV + i) ; 
    }


  for(iterD = d.begin(); iterD < d.end(); ++iterD)
    cout << *iterD << endl;  

  return 0;
}`
like image 878
axel Avatar asked Nov 21 '22 06:11

axel


1 Answers

Your example is very simple which hardly need any performance optimization. You just copy the memory (which could be optimized using std::copy instead).

The code you have written is correct and there is hardly any other way to write it to gain performance. But however, to maintain cleaner code, I try to maintain loop iterators private to each thread. This makes code clean (personal preference).

  vector<float> v = {1, 2, 3, 4};
  vector<float> d = {0, 0, 0, 0};   
  #pragma omp parallel for
  for(auto it_v = v.begin(),it_d = d.begin(); it_v!=v.end();++it_v,++it_d)
    {
      *it_d =  *it_v; 
    }

EDIT

OpenMP 3.1 doesn't allow multiple loop initialization in this case. It doesn't adhere to their specification. So one way to do is as follows:

  #pragma omp parallel
  {
    auto it_v = v.begin(),it_d = d.begin();
    #pragma openmp for
    for(; it_v!=v.end();++it_v)
    {
      *it_d =  *it_v; 
    }
  }
like image 173
Rishit Sanmukhani Avatar answered Nov 22 '22 20:11

Rishit Sanmukhani