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 8367179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:05:35+00:00 2026-06-09T13:05:35+00:00

I have this parallel for loop struct p { int n; double *l; }

  • 0

I have this parallel for loop

struct p
{
    int n;
    double *l;
}

#pragma omp parallel for default(none) private(i) shared(p)
for (i = 0; i < p.n; ++i)
{
    DoSomething(p, i);
}

Now, it is possible that inside DoSomething(), p.n is increased because new elements are added to p.l. I’d like to process these elements in a parallel fashion. OpenMP manual states that parallel for can’t be used with lists, so DoSomething() adds these p.l‘s new elements to another list which is processed sequentially and then it is joined back with p.l. I don’t like this workaround. Anyone knows a cleaner way to do this?

  • 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-09T13:05:37+00:00Added an answer on June 9, 2026 at 1:05 pm

    A construct to support dynamic execution was added to OpenMP 3.0 and it is the task construct. Tasks are added to a queue and then executed as concurrently as possible. A sample code would look like this:

    #pragma omp parallel private(i)
    {
        #pragma omp single
        for (i = 0; i < p.n; ++i)
        {
           #pragma omp task
           DoSomething(p, i);
        }
    }
    

    This will spawn a new parallel region. One of the threads will execute the for loop and create a new OpenMP task for each value of i. Each different DoSomething() call will be converted to a task and will later execute inside an idle thread. There is a problem though: if one of the tasks add new values to p.l, it might happen after the creator thread has already exited the for loop. This could be fixed using task synchronisation constructs and an outer loop like this:

    #pragma omp single
    {
       i = 0;
       while (i < p.n)
       {
          for (; i < p.n; ++i)
          {
             #pragma omp task
             DoSomething(p, i);
          }
          #pragma omp taskwait
          #pragma omp flush
       }
    }
    

    The taskwait construct makes for the thread to wait until all queued tasks are executed. If new elements were added to the list, the condition of the while would become true again and a new round of tasks creation will happen. The flush construct is supposed to synchronise the memory view between threads and e.g. update optimised register variables with the value from the shared storage.

    OpenMP 3.0 is supported by all modern C compilers except MSVC, which is stuck at OpenMP 2.0.

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

Sidebar

Related Questions

I have a loop in my C++/OpenMP code that looks like this: #pragma omp
I want to do this: omp_set_nested(1); #pragma omp parallel for private(j) for (i =
I have an OpenMP parallelized program that looks like that: [...] #pragma omp parallel
I have a simple question, i have following simple Parallel for loop. this for
I am experimenting with parallel programming. I have a normal loop: for (int i
I have the following code: Parallel.ForEach(this.listView2.CheckedItems, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (CheckedItem)
I have a method like this: public IOrganizationService GetConnection(bool multi) { if(!multi) { Parallel.For(0,
I have a time consuming loop I would like to execute in parallel. Pseudocode:
I have method called SColl. This method invokes a webservice. When using parallel.invoke I
This is some sample test app i trying to learn Parallel.Foreach Loop functionality static

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.