Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I assign multiple threads to a code section in OpenMP?

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();
}
like image 215
John Avatar asked Sep 06 '11 16:09

John


People also ask

How many threads can I use in OpenMP?

First of all,you can't run more than 8 threads. Second,resort to nested parallelism if nothing else works as openmp has to improve a lot in this aspect.

Which instruction in OpenMP is used for creating multiple threads?

Thread creation The pragma omp parallel is used to fork additional threads to carry out the work enclosed in the construct in parallel.

Can multiple threads run the same function?

A thread can execute a function in parallel with other threads. Each thread shares the same code, data, and files while they have their own stack and registers.


1 Answers

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();
       }
   }
}
like image 196
Mysticial Avatar answered Nov 14 '22 22:11

Mysticial