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

  • Home
  • SEARCH
  • 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 6548307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:56:08+00:00 2026-05-25T11:56:08+00:00

I’m looking for a way to execute sections of code in parallel using multiple

  • 0

I’m looking for a way to execute sections of code in parallel using multiple threads for each section. For example, if I have 16 threads and two tasks, I want 8 threads each to simultaneously execute those two tasks. OpenMP has several constructs (section, task) that execute general code in parallel, but they are single-threaded. In my scenario, using section or task would result in one thread executing each of the two tasks, while 14 threads sit idly by.

Is something like that even possible with OpenMP? If so, how do I do it, and if not, what can I use for that purpose?

Thanks for your time!

edit 2:

Let me expand on this question with an example code:

class some_class{
    void task(){
        cout<<"Entering the task method"<<endl;
        #pragma openmp parallel for
            for(int i=0; i < large_matrix.rows(); i++){
                 perform_thread_safe_operation(large_matrix.getRow(i));
            }
    }

    matrix large_matrix;
};


void main(){
    //I have 16 cores, so I want to spawn 16 threads
     some_class o1;
     some_class o2;
    // I want 8 of the 16 threads to execute this line:
    o1.task();
    // and 8 remaining threads to execute this line:
    o2.task();
}
  • 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-05-25T11:56:08+00:00Added an answer on May 25, 2026 at 11:56 am

    You can do this using nested parallel regions.

    omp_set_nested(1);
    
    #pragma omp parallel num_threads(2)
    {
        if (omp_get_thread_num() == 0){
    #pragma omp parallel num_threads(8)
            {
    
                //  Task 0
    
            }
        }else{
    #pragma omp parallel num_threads(8)
            {
    
                //  Task 1
    
            }
        }
    }
    

    Alternatively, you could do it like this:

    #pragma omp parallel num_threads(16)
    {
        if (omp_get_thread_num() < 8){
            //  Task 0
        }else{
            //  Task 1
        }
    }
    

    Note, this code will not work if OpenMP decides to use fewer than 16 threads. You will have to insert your own cleanup code for that.

    EDIT: In response to your update:

    class some_class{
        void task(){
            cout<<"Entering the task method"<<endl;
    
    #pragma omp parallel for num_threads(8)
            for(int i=0; i < large_matrix.rows(); i++){
                perform_thread_safe_operation(large_matrix.getRow(i));
            }
        }
    
        matrix large_matrix;
    };
    
    
    void main(){
    
        omp_set_nested(1);
    
        //I have 16 cores, so I want to spawn 16 threads
         some_class o1;
         some_class o2;
    
    #pragma omp parallel num_threads(2)
       {
           if (omp_get_thread_num() == 0){
               // I want 8 of the 16 threads to execute this line:
               o1.task();
           }else{
               // and 8 remaining threads to execute this line:
               o2.task();
           }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.