2

I'm struggling to set number of threads to 1 inside of a parallel region. I put a barrier so that all threads stop at that point and I can freely set number of threads to 1 (and there will be no threads executing). But wherever I placed omp_set_num_threads(1) it always returned 3. Is it possible to change number of threads during runtime? How can I do that?

#import<iostream>
#import<omp.h>
#import<stdio.h>

int main(){

int num_of_threads;
std::cin>>num_of_threads;
omp_set_dynamic(0); 
#pragma omp parallel if(num_of_threads>1) num_threads(3)
{
    int t_id = omp_get_thread_num();
    int t_total = omp_get_num_threads();
    printf("Current thread id: %d \n Total number_of_threads: %d \n",t_id,t_total);
    #pragma omp barrier
    #pragma omp single
    { 
    omp_set_num_threads(1);
    t_id = omp_get_thread_num();
    t_total = omp_get_num_threads();
    printf("Single section \n Current thread id: %d \n Total number_of_threads: %d \n",t_id,t_total);
    }
}


}
  • 1
    What are you even trying to achieve here? If you have 3 threads and only want 1 from some point on, why not simply exit the parallel region? What exactly do you expect to happen when you try to set the number of threads? – Qubit Sep 25 '18 at 07:38
  • @Qubit that's my task on OpenMP course at the university. I must not exit the parallel region. It should somehow set number of threads to 1 after the first block of code. – Karina Garipova Sep 25 '18 at 07:43
  • And perhaps to answer why it appears not to be working. According to https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/parallel/openmp/3-1-1-omp-set-num-threads-function.md, `omp_set_num_threads` sets the number of threads for **subsequent** parallel regions. So, if you create a new parallel region after this one, the value should be set, but it can not modify the number of threads in the current region. – Qubit Sep 25 '18 at 07:44
  • @Qubit that's what I was looking for. Thanks a lot :) – Karina Garipova Sep 25 '18 at 07:49
  • In practice it sounds like you just need to use `#pragma omp single` or `#pragma omp master` if that part of the code should only be executed by a single thread. But if you actually want `omp_get_num_threads()` to return 1 while still inside the parallel region, that might be more problematic, if not impossible. – Qubit Sep 25 '18 at 07:49
  • @Qubit makes sense because all threads except the master thread are destroyed after execution outside the parallel region – Karina Garipova Sep 25 '18 at 07:51
  • In the future see official source [openmp-4.5.pdf](https://www.openmp.org/wp-content/uploads/openmp-4.5.pdf). In your case page 231. – Z boson Sep 26 '18 at 07:05

1 Answers1

0

TL;DR You can't change the number of threads in a parallel region.

Remember this is a pool of threads, which get forked at the beginning of the parallel region. Inside they are not even synchronized (if you dont tell them too), thus OpenMP would need to terminate some of them at an unknown position - obviously a bad idea.

Your #pragma omp single makes the following code section be executed by a single thread, thus no need to set it via omp_set_num_threads. BUT it doesnt change your pool, it just advises the compiler to schedule the following section to one thread - while the rest ignores it.

To show this behavior e.g. for university purposes i would suggest to print out only the thread id in parallel and single part. That way you can already tell it's working or not.

Zacharias
  • 576
  • 3
  • 6