Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

How to explicitly make a thread

Anupam_Dev1
Beginner
260 Views
I want to make a thread explicitly. Is there any OpenMP construct or way to do this.

Suppose at every loop iteration i want to make another team of threads after checking a condition in the loop body. can i do this....or it is illegal!!! how???
0 Kudos
3 Replies
robert-reed
Valued Contributor II
260 Views
Why? Making new threads takes a lot of instructions. Doing so for every loop iteration could be really slow. And if the work the threads have to do has any duration, the number of threads will quickly explode and potentially lock up the system with oversubscription.

Perhaps you could talkmore about what it is you would like to do, and someone might be able to suggest a useful approach.
0 Kudos
Grant_H_Intel
Employee
260 Views

Here is the pseudocode that I think would do what you want:

[bash]for (int i=0; i!=n; ++i) {
     int my_condition = ...
     #pragma omp parallel num_threads(2) if(my_condition)
     {
          do_something();
     }
}
[/bash]


When the variable my_condition is TRUE, you will get two threads executing do_something(). When my_condition is FALSE, you will get only a single thread executing do_something(). This is assuming you want all of the iterations of the loop to happen serially.

Please be aware that forking a new thread at the pragma could take up to a couple thousand cycles, so only do this if you have enough work in do_something() that this cost is small.

0 Kudos
Anupam_Dev
Beginner
260 Views
Is nested parallelism possible using if conditions?
0 Kudos
Reply