0

I'm studying Parallel Programming in Visual Studio using C++. So, I get the practice work in which I should to set up number of threads using environment variable - OMP_NUM_THREADS. But how its using I don't know. Here's my code:

#include <omp.h>
#include <iostream>
#include <thread>
using namespace std;
int main()
{
    
#pragma omp parallel 
        {
        cout << "Hello World\n";
           
        }
        return 0;

}

So, how I can get use it? What should I do?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
KillReal
  • 3
  • 2
  • 1) build your program, 2) set `OMP_NUM_THREADS`, 3) run the program. What particularly you don't understand about it? – Daniel Langr Sep 13 '21 at 14:38
  • @DanielLangr I build the program, use "set OMP_NUM_THREADS=4", but when I'm running program cout print only once, not four times – KillReal Sep 13 '21 at 14:45
  • Did you enable OpenMP in project properties? In my case, it's under _Configuration Properties -> C/C++ -> Language -> Open MP Support_. It was disabled by default (and the program build did not fail). – Daniel Langr Sep 13 '21 at 14:55
  • @DanielLangr of course. If I use functions of OpenMP it's working – KillReal Sep 13 '21 at 14:57
  • Functions may be called, but `#pragma omp` directives may be ignored. Even without `OMP_NUM_THREADS` set, program should run in parallel with OpenMP support enabled (that's how VS behaves on my system). – Daniel Langr Sep 13 '21 at 14:59
  • @DanielLangr yes, you right, MVS telling me that OpenMP is ignoring, code: C6993, how I can solve this problem? – KillReal Sep 13 '21 at 15:02
  • That's likely not the problem. C6993 is a code-analysis error (possibly IntelliSense?), which does not support OpenMP. Again, is really OpenMP support turned on in the project configuration? – Daniel Langr Sep 13 '21 at 15:04
  • @DanielLangr Yes – KillReal Sep 13 '21 at 15:10
  • Then, I am afraid I can't help. On my machine, it works such that without `OMP_NUM_THREADS` defined, the program creates as many threads as many cores my CPU have. With `OMP_NUM_THREADS`, it creates a desired number of threads. Which is expected behavior. (Last option — is OpenMP enabled in both project configurations, that is, _debug_ and _release_?) – Daniel Langr Sep 13 '21 at 15:18
  • @DanielLangr I set the same setting for relase too, but it doesn't help – KillReal Sep 13 '21 at 15:26
  • Try to set `OMP_DISPLAY_ENV` to `TRUE`. If it does not print anything, it means the problem lies either in the environment variable management or there is no OpenMP runtime. You can print the environment variables in your program to check if the runtime could see it. If this is not due to that, you can check the assembly code and look for OpenMP runtime functions. If there is such functions, then the only rational explanation is that the runtime used is totally bogus. – Jérôme Richard Sep 13 '21 at 18:22

1 Answers1

3

Try to use #pragma omp parallel num_threads(4). I tested and it did print four times. FYI, OpenMP set_num_threads() is not working.

Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14