5

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;
}`
axel
  • 77
  • 1
  • 9
  • 10
    _"to paralyze my code"_ Oh, no you shouldn't do this. No code deserves that. You probably meant to _parallelize_ it. – πάντα ῥεῖ May 05 '16 at 14:31
  • Usually you don't parallelize just for the sake of it, but for performance optimization. Unfortunately your example is too synthetic to give valuable performance optimization advice. I can tell that the serial version should use `std::copy.` – Zulan May 05 '16 at 17:14
  • I synthesize an example to have a good understanding at my needs. I just would like to know if I have a loop for with two iterators how the best way to parallelize it with openMP. – axel May 05 '16 at 17:56

1 Answers1

1

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; 
    }
  }
Rishit Sanmukhani
  • 2,159
  • 16
  • 26
  • Thanks ! But I try your solution and I received this error : `expected ‘;’ before ‘,’ token for(auto itV = v.begin(), itD = d.begin(); itV!=v.end();++itV,++itD)` Which version of openMP did you use ? – axel May 06 '16 at 08:35
  • Did you use -std=c++11 flag? `auto` is keyword in c++11 – Rishit Sanmukhani May 06 '16 at 08:36
  • Yes I used -stdd=c++11 flag – axel May 06 '16 at 08:37
  • Which version of openMP are you using? – Rishit Sanmukhani May 06 '16 at 08:38
  • I used gcc 4.8 so I think the version of openMP is 3.1 – axel May 06 '16 at 08:39
  • @axel Refer edit. OpenMP 3.1 specification doesn't allow multiple loop variable initialization. – Rishit Sanmukhani May 06 '16 at 08:46
  • Ok ! I just install gcc 4.9 but I think the version of openMP still the 3.1. How can I check my version of openMP ? – axel May 06 '16 at 08:46
  • Refer this question: http://stackoverflow.com/questions/1304363/how-to-check-the-version-of-openmp-on-linux – Rishit Sanmukhani May 06 '16 at 08:48
  • Ok I still have the version 3.1 at openMP. – axel May 06 '16 at 08:54
  • I have the same error than axel with openmp 5 for your first loop. it seems, from the specification (https://www.openmp.org/wp-content/uploads/OpenMP-API-Specification-5.0.pdf, pages 96) that your code is not possible. The second loop is strange : what is pragma openmp ? when did you increment ```ìt_d``` iterator ? – Kafka Jul 28 '20 at 22:12
  • I wouldn't consider it as a good answer for the reasons explained here: https://stackoverflow.com/questions/8691459/how-do-i-parallelize-a-for-loop-through-a-c-stdlist-using-openmp – Joachim Sep 10 '20 at 05:55