简体   繁体   中英

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;
}`

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; 
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM