skip to Main Content

I want to write a simple code that will print "Hello world" 4 times using 4 threads:

#include <stdio.h>
#include <stdlib.h>
#include "omp.h"

int main(void) {
    #pragma omp parallel num_threads(4)
    {
        #pragma omp critical
        {
            //i temporalily changed it to print thread number for debug
            printf("Thread rank: %dn", omp_get_thread_num());
        }
    }
    return 0;
}

…but my code prints thread number only one time:

output

As i understand #pragma omp parallel num_threads(4) should create 4 threads so probably no mistakes here.

#pragma omp critical so that program will not print text at the same time and produce garbage.

I use visual studio and i turned on openmp support:

Project properties

And as i understand from vis studio console threads are created but don’t print anything for some reason.

Output:

'Programm.exe' (Win32): Loaded 'mypath hereProgramm.exe'. Symbols loaded.
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32ntdll.dll'. 
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32kernel32.dll'. 
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32KernelBase.dll'. 
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32vcomp140d.dll'. 
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32vcruntime140d.dll'. 
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32ucrtbased.dll'. 
The thread 0x5f9c has exited with code 0 (0x0).
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32kernel.appcore.dll'. 
'Programm.exe' (Win32): Loaded 'C:WindowsSystem32msvcrt.dll'. 
The thread 0x30c8 has exited with code 0 (0x0).
The thread 0x4794 has exited with code 0 (0x0).
The program '[5548] Programm.exe' has exited with code 0 (0x0).

2

Answers


  1. Chosen as BEST ANSWER

    Indeed, how dewaffled and Minxin Yu pointed out, i turned on openmp in release configuration but used debug. My bad, thanks everyone for help.


  2. There might be a chance that remaining threads are ignored by console in Visual Studio Community. So try it on VS Code instead. Put the same code in VS Code naming your file as "filename.c" or filename.cpp" then compile it.
    Use the following command "gcc -fopenmp your_program.c -o your_program.exe"
    Just replace "gcc" with whichever compiler you are using and the additional thing is to add "-fopenmp" attribute. It will surely work I have tried it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search