Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:44:51+00:00 2026-06-07T13:44:51+00:00

I wanted to parallelize this code with the help of OpenMP with something like

  • 0

I wanted to parallelize this code with the help of OpenMP with something like
#pragma omp parallel for to divide the work amongst the different threads.

What would be an efficient way? Here level is shared between the varioud threads.

Here make is a set.

for(iter=make.at(level).begin();iter!=make.at(level).end();iter++)
{
    Function(*iter);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:44:52+00:00Added an answer on June 7, 2026 at 1:44 pm

    If the type returned by make.at(level) has random-access iterators with constant access time and if your compiler supports recent enough OpenMP version (read: it is not MSVC++) then you can directly use the parallel for worksharing directive:

    obj = make.at(level);
    #pragma omp parallel for
    for (iter = obj.begin(); iter != obj.end(); iter++)
    {
        Function(*iter);
    }
    

    If the type does not provide radom-access iterators but still your compiler supports OpenMP 3.0 or newer, then you can use OpenMP tasks:

    #pragma omp parallel
    {
        #pragma omp single
        {
            obj = make.at(level);
            for (iter = obj.begin(); iter != obj.end(); iter++)
            {
                #pragma omp task
                Function(*iter);
            }
        }
    }
    

    Here a single thread executes the for loop and creates a number of OpenMP tasks. Each task will make a single call to Function() using the corresponding value of *iter. Then each idle thread will start picking from the list of unfinished tasks. At the end of the parallel region there is an implicit barrier so the master thread will dutifully wait for all tasks to finish before continuing execution past the parallel region.

    If you are unfortunate enough to use MS Visual C++, then you don’t have much of a choice than to create an array of object pointers and iterate over it using a simple integer loop:

    obj = make.at(level);
    obj_type* elements = new obj_type*[obj.size()];
    for (i = 0, iter = obj.begin(); i < obj.size(); i++)
    {
        elements[i] = &(*iter++);
    }
    
    #pragma omp parallel for
    for (i = 0; i < obj.size(); i++)
    {
        Function(*elements[i]);
    }
    
    delete [] elements;
    

    It’s not the most elegant solution but it should work.

    Edit: If I understand correctly from the title of your question, you are working with sets. That rules out the first algorithm since sets do not support random-access iterators. Use either the second or the third algorithm depending on your compiler’s supports for OpenMP tasks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just wanted to add this Color Picker ( http://code.google.com/p/devmil-android-color-picker/source/browse/ ) to my app and
I wanted to know if this is possible? I would like to create a
I wanted to ask about the best way to write this JQUERY code. I
Wanted to stop a thread doing some downloads. The code below work fine when
Wanted to know if someone had a suggestion on code or maybe there's a
I wanted to use 6 different textures on a cube, one per side, but
Wanted to try something new, using the awesome twitter bootstrap and their Less CSS
Wanted some help with a problem with mapkit I am facing. Should be a
I wanted to see if I can better organize / optimize my code, and
I wanted to post this question and answer because I wasn't able to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.