Missing OpenMP feature: thread priority

Everyone thinks about it. OpenMP capabilities for customizing cpu muscles for dumbbel processing. In my research for openmp, we are unable to prioritize a thread to execute block code with powerful muscle. Only one way (_beginthreadex or CreateThread with parameters 5.) creates threads with the highest priority.

Here is the code for this problem:

This is a manual setting.

int numberOfCore = ( execute __cpuid to obtain number of cores on your cpu ).

HANDLES* hThreads = new HANDLES[ numberOfCore ];
hThreads[0] = _beginthreadex( NULL, 0, someThreadFunc, NULL, 0, NULL );

SetThreadPriority( hThreads[0], HIGH_PRIORITY_CLASS );

WaitForMultipleObjects(...); 

      

I want to see this part:

#pragma omp parallel
{
#pragma omp for ( threadpriority:HIGH_PRIORITY_CLASS )
 for( ;; ) { ... }
}

      

Or

#pragma omp parallel
{
// Generally this function greatly appreciativable.
_omp_set_priority( HIGH_PRIORITY_CLASS );
#pragma omp for
 for( ;; ) { ... }
}

      

I don't know if there was a way to set the priority using openmp pls.

0


source to share


2 answers


You can do SetThreadPriority

in the body of the loop without requiring special support from OpenMP:



for (...)
{
  DWORD priority=GetThreadPriority(...);
  SetThreadPriority(...);
  // stuff
  SetThreadPriority(priority);
}

      

+3


source


A simple test shows unexpected results: I ran a simple test in Visual Studio 2010 (Windows 7):

    #include <stdio.h>
    #include <omp.h>
    #include <windows.h>

    int main()
    {
        int tid, nthreads;

        SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);

        #pragma omp parallel private(tid) num_threads(4)
        {
            tid = omp_get_thread_num();
            printf("Thread %d: Priority = %d\n", tid, GetThreadPriority(GetCurrentThread()));
        }

        printf("\n");

        #pragma omp parallel private(tid) shared(nthreads) num_threads(4)
        {
            tid = omp_get_thread_num();

            #pragma omp master
            {
                printf("Master Thread %d: Priority = %d\n", tid, GetThreadPriority(GetCurrentThread()));
            }
        }

        #pragma omp parallel num_threads(4)
        {
            SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
        }

        printf("\n");

        #pragma omp parallel private(tid) num_threads(4)
        {
            tid = omp_get_thread_num();
            printf("Thread %d: Priority = %d\n", tid, GetThreadPriority(GetCurrentThread()));
        }

        return 0;
    }

      

Output:



    Thread 1: Priority = 0
    Thread 0: Priority = 1
    Thread 2: Priority = 0
    Thread 3: Priority = 0

    Master Thread 0: Priority = 1

    Thread 0: Priority = 1
    Thread 1: Priority = 1
    Thread 3: Priority = 1
    Thread 2: Priority = 1

      

Explanation: OpenMP main threads run at the priority of the main thread. Other OpenMP threads remain at normal priority. When manually setting the thread priority in OpenMP streams, the streams remain with this priority.

0


source







All Articles