To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
Parallel notes N4 - continuing to study…

Parallel notes N4 - continuing to study OpenMP constructs

Mar 11 2010
Author:

In this post we will continue to introduce you into OpenMP technology and tell you about some functions and new directives.

OpenMP has some auxiliary functions. Do not forget to enable the header file <omp.h> to be able to use them..

Execution environment functions

These functions allow you to request for and set various parameters of the OpenMP environment:

  • omp_get_num_procs returns the number of the computational nodes (processors/cores) in the computer.
  • omp_in_parallel allows a thread to know if it is processing a parallel region at the moment.
  • omp_get_num_threads returns the number of threads included into the current thread team.
  • omp_set_num_thread defines the number of threads to execute the next parallel region the current executed thread will meet. The function may help you distribute resources. For example, if you are simultaneously processing sound and video on one processor with four cores, you may create one thread to process the sound and three threads to process the video.
  • omp_get_max_threads returns the maximum possible number of threads to be used in the next parallel region.
  • omp_set_nested permits or forbids nested parallelism. If nested parallelism is permitted, each thread that has a description of a parallel region will spawn a new thread team to execute this region and will become the master-thread of the team.
  • omp_get_nested tells you if nested parallelism is enabled or disabled.

If the name of a function begins with omp_set_, it may be called only outside parallel regions. All the rest functions may be used both inside and outside parallel regions.

Synchronization/lock functions

OpenMP allows you to build parallel code without these functions because there are directives that implement some synchronization types. But in some cases these functions are convenient and even necessary.

OpenMP has two types of locks: simple and nested. The locks of the latter type have the suffix "nest". Locks may be in one of the three states - non-initialized, locked and unlocked.

  • omp_init_lock/omp_init_nest_lock serves to initialize a variable of the type omp_lock_t/omp_nest_lock_t. It is equivalent to InitializeCriticalSection.
  • omp_destroy_lock/omp_destroy_nest_lock serves to unlock a variable of the type omp_lock_t/omp_nest_lock_t. It is equivalent to DeleteCriticalSection.
  • omp_set_lock/omp_set_nest_lock - when this lock is used, one thread sets the lock while the others wait for it to unlock the variable with the function omp_unset_lock(). It is equivalent to EnterCriticalSection.
  • omp_unset_lock/omp_unset_nest_lock is used to release the lock. It is equivalent to LeaveCriticalSection.
  • omp_test_lock/omp_test_nest_lock is a non-locking attempt to capture a lock. This function will try to capture the specified lock. If it succeeds, it will return 1 for a simple lock. If it fails, it will return 0. It is equivalent to TryEnterCriticalSection.

Simple locks cannot be set more than once even by the same thread. Nested locks are nearly the same as simple ones with the exception that a thread is not locked when trying to set a nested lock already belonging to it.

Here is an example of code where these functions are used. All the threads being created will by turn print the messages "Begin work" and "End work". Between these two messages generated by one thread there may appear messages from the other threads generated when they fail to enter a locked section.

omp_lock_t lock;
int n;
omp_init_lock(&lock);
#pragma omp parallel private (n)
{
  n=omp_get_thread_num();
  while (!omp_test_lock (&lock))
  {
    printf("Wait..., thread %d\n", n);
    Sleep(3);
  }
  printf("Begin work, thread %d\n", n);
  Sleep(5); // Work...
  printf("End work, thread %d\n", n);
  omp_unset_lock(&lock);
}
omp_destroy_lock(&lock);

You may expect the following result on a computer with four cores:

Begin work, thread 0

Wait..., thread 1

Wait..., thread 2

Wait..., thread 3

Wait..., thread 2

Wait..., thread 3

Wait..., thread 1

End work, thread 0

Begin work, thread 2

Wait..., thread 3

Wait..., thread 1

Wait..., thread 3

Wait..., thread 1

End work, thread 2

Begin work, thread 3

Wait..., thread 1

Wait..., thread 1

End work, thread 3

Begin work, thread 1

End work, thread 1

Timer functions

  • omp_get_wtime returns the astronomical time that has passed since some moment in past in seconds in the thread that has called it (a real number of double precision - double). If you surround some code fragment with calls of this function, the remainder of the values being returned will show the time of this fragment's execution.
  • omp_get_wtick() returns the resolution of the timer in seconds in the thread that has called it, i.e. shows the timer's precision.

Let us finish here with the functions and consider a couple of new directives. These directives may be called options of the parallel regions being created.

if (condition)

Execution of a parallel region by a condition. Several threads are created only if some condition is fulfilled. Otherwise the code continues to execute in serial mode.

For example:

void test(bool x)
{
  #pragma omp parallel if (x)
  if (omp_in_parallel())
  {
    #pragma omp single
    printf_s("parallelized with %d threads\n",
             omp_get_num_threads());
  }
  else
  {
    printf_s("single thread\n");
  }
}
int _tmain(int argc, _TCHAR* argv[])
{
  test(false);
  test(true);
  return 0;
}

The result:

single thread
parallelized with 4 threads

num_threads

It serves to explicitly define the number of threads to execute a parallel region. By default, the last value is selected that was returned by the function omp_set_num_threads().

If we modify the example above in the following way:

...
#pragma omp parallel if (x) num_threads(3)
...

the result will be the following:

single thread
parallelized with 3 threads

To be continued in the next issue of "Parallel Notes"...

Popular related articles
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept