Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use openmp in iterating over a map

Tags:

c++

I'm trying to iterate over a map in c++ using openMP, but I got three error messages saying
that the initialization, termination and increment of my loop has improper form and I'm quite new in using openmp, so is there any way to get around this problem while getting the same results as the serial ones? the following is the code I used

map< int,string >::iterator datIt;
#pragma omp parallel for
for(datIt=dat.begin();datIt!=dat.end();datIt++) //construct the distance matrix
{
...............
}
like image 990
DOSMarter Avatar asked Jan 13 '12 10:01

DOSMarter


1 Answers

It could be done also by using a simple index based for loop clubbed with std::advance to reach to a particular map element. OpenMP 2.0 supports index based for loops very well.

#pragma omp parallel for
    for(int i = 0; i < dat.size(); i++) {
        auto datIt = dat.begin();
        advance(datIt, i);
        //construct the distance matrix using iterator datIt
    }

In each thread the iterator datIt will point to a map item and can be used to perform operations on it.

like image 147
sabertooth1990 Avatar answered Sep 20 '22 22:09

sabertooth1990