Use openmp in map iteration

I am trying to iterate over a map in C ++ using openMP, but I got three error messages saying that the initialization, completion and increase of my loop are irregular in shape and I am completely new to using openmp, is there any way get around this problem, getting the same results as the serial ones? the following code i used

map< int,string >::iterator datIt; #pragma omp parallel for for(datIt=dat.begin();datIt!=dat.end();datIt++) //construct the distance matrix { ............... } 
+4
source share
4 answers

Your OpenMP implementation is probably not compatible with STL iterators. While some have changed the standard to make OMP more STL compatible , I think you will find that your implementation does not support such behavior. Most of the OpenMP implementations I came across are no more than version 2.5, Microsoft C ++ - 2.0. The only compiler I know about supports 3.0 is the Intel C ++ compiler.

A few other points, you should use std :: begin and std :: end. In addition, you need to either declare your loop invariant as closed, or have an OpenMP value like this:

 #pragma omp parallel for for(map< int,string >::iterator datIt = std::begin(dat); datIt != std::end(dat); datIt++) { //construct the distance matrix... } 

But without support for 3.0, this does not apply to the point.

+3
source

This can also be done using a simple loop-based index combined with std::advance to get to a specific map element. OpenMP 2.0 supports loop-based indexes 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 datIt iterator points to a map element and can be used to perform operations on it.

+1
source

OpenMP 3.0, currently available in gcc and the Intel compiler, has a task directive that allows a thread to delegate a task to a thread.

Inspired by this answer and this course , I wrote these code that works great for me:

 map< int,string >::iterator datIt; ... #pragma omp parallel for #pragma omp single nowait { for(datIt=dat.begin();datIt!=dat.end();datIt++) //construct the distance matrix { #pragma omp task firstprivate(datIt) { ............... } } } 

One cycle (one directive) throughout the map and puts each task for each element of the map in pulling tasks. Other tasks of the OMP stream process remain in this tension. For other OMP tasks, there is no need to wait for the end of the for loop to start processing the task (nowait). Each task has an element pointer on the map for processing (firstprivate (datIt)).

Limitation: each task must be independent, and the map must not be changed to the end.

0
source

Try this if it is helpful.

 #pragma omp parallel for shared(dat) private(datIt) for(map< int,string >::iterator datIt=dat.begin();datIt!=dat.end();datIt++) { ............... } 
-1
source

Source: https://habr.com/ru/post/1390798/


All Articles