Note that there are some explanatory texts on larger screens.

plurals
  1. POc++: OpenMP and non-random-access STL containers - a possible workaround
    primarykey
    data
    text
    <p>So on SO, and the Internets in general, there is much confusion and frustration about how to make OpenMP's easy-to-use <code>#pragma</code> directives cooperate with C++'s equally easy-to-use STL containers.</p> <p>Everyone talks about work-arounds for STL <code>vector</code>, but what about non-random access / bi-directional containers, like <code>map</code>, <code>list</code>, <code>set</code>, etc. ?</p> <p>I encountered this problem and devised a very simple, obvious workaround. I present it here for STL <code>map</code>, but it is clearly generalizable.</p> <p>Serial version:</p> <pre><code>for (std::map&lt;A,B&gt;::iterator it = my_map.begin(); it != my_map.end(); ++it) { /* do work with it */ } </code></pre> <p>My proposed solution to use OpenMP with STL <code>map</code>:</p> <pre><code> //make an array of iterators. int loop_length = my_map.size(); std::map&lt;A,B&gt;::iterator loop_array[ loop_length ]; std::map&lt;A,B&gt;::iterator allocate_it = my_map.begin(); for (int j=0; j&lt;loop_length; ++j) loop_array[j] = allocate_it++; // now you can use OpenMP as usual: #pragma omp parallel for for (uint j=0; j&lt;loop_length; ++j) { /* do work with loop_array[j] */ } </code></pre> <p>I am far from an expert on OpenMP, however, so <strong>I would like to know if my proposed work-around is efficient and good practice.</strong></p> <p>Please assume that the programmer is responsible for thread-safe handling of the STL container within the for loop.</p> <p>Finally, is my proposed solution more efficient than the following commonly-proposed solution <a href="https://stackoverflow.com/questions/2513988/iteration-through-std-containers-in-openmp">(see answer to this SO Question)</a>, because, in my solution,each thread does not iterate over the whole container?</p> <pre><code>#pragma omp parallel { for (std::map&lt;A,B&gt;::iterator it = my_map.begin(); it != my_map.end(); ++it) #pragma single nowait { /* do work */ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload