Admin Production ni-theme
Current Publication

Troubleshooting OpenMP Issues

LabWindows/CVI

Troubleshooting OpenMP Issues

The following are some common issues that can occur and cause you to see unexpected results from your OpenMP application.

Note Note  This topic is a partial list of possible issues. Consult outside resources for additional assistance troubleshooting common OpenMP issues. For an example of one such resource, visit ni.com/info and enter the following Info Code: OpenMPTraps.

OpenMP Not Enabled

  • If you do not enable OpenMP, LabWindows/CVI ignores all OpenMP directives, clauses, and functions. To enable OpenMP, select Enable OpenMP Support from the C Language Options section of the Build Options dialog box.

Threading Issues

  • Set the number of threads that execute the parallel region before the start of the parallel region. For example, you can set the number of threads that execute a parallel region in the following ways:
  • If you explicitly set the number of threads that execute a parallel region, make sure to use the appropriate mechanism. For example, using the OMP_NUM_THREAD environment variable can vary from one machine to another.
  • If a thread in a parallel region calls a function that posts a synchronous message to another thread in the same region, a deadlock might occur preventing all threads of the region from exiting the implicit barrier at the end of that region. Common functions that cause a deadlock are functions that create user interface objects or perform user interface operations on user interface objects. To avoid this scenario, perform user interface operations only from the master thread or the thread that owns the user interface object. Use the master directive inside a parallel region to limit the user interface operations to the master thread.

    In the event that you are unable to modify the source code or the above mechanism does not work, you can work around this issue by forcing the master thread to process the Windows message pump by setting the OMP_CVI_PROC_MSGS_MASTER environment variable to 1. Note that using this environment option should be done with caution; setting the variable can lead to unexpected reentrancy issues and because the environment variable affects the entire application, it can break code in ways that might not be immediately apparently.

Keyword Issues

  • Every OpenMP directive must be preceded by the omp keyword.
    #pragma omp master

    not

    #pragma master
  • Every OpenMP parallel region must be indicated by the parallel keyword and the parallel keyword should not be used again in the parallel region.
    #pragma omp parallel
       {
          printf("Hello World from thread = %d\n", omp_get_thread_num());
          #pragma omp master
          printf("Number of threads in the team = %d\n", omp_get_num_threads());
       }

    not

    #pragma omp parallel
       {
          printf("Hello World from thread = %d\n", omp_get_thread_num());
          #pragma omp parallel master
          printf("Number of threads in the team = %d\n", omp_get_num_threads());
       }
  • If you have a for loop in a parallel region and you want the work of the loop to be divided among threads, you must specify this with the for keyword prior to the beginning of the for loop.