1

Anyone think about it. OpenMP features to adjust cpu muscles to handle dumbbel. In my research for openmp we cannot set thread priority to execute block code with powerfull muscle. Only one way(_beginthreadex or CreateThread function with 5. parameters) to create threads with highest priority.

Here some code for this issue:

This is 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(...); 

Here is 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 dont know if there was a way to setup priority with openmp pls inform us.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Stack overflow is kind of unfriendly to new users. When voting someone down, please leave a comment explaining why you did it and some hint as to what they should do to make it better. – 1800 INFORMATION Mar 23 '09 at 00:52

2 Answers2

3

You can do SetThreadPriority in the body of the loop without requiring special support from OpenMP:

for (...)
{
  DWORD priority=GetThreadPriority(...);
  SetThreadPriority(...);
  // stuff
  SetThreadPriority(priority);
}
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
1

Simple test reveals unexpected results: I have run 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;
    }

The output is:

    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: The OpenMP master threads is executed with the thread priority of the main. The other OpenMP threads are left in Normal priority. When manually setting the thread priority of OpenMP threads, the threads remains with that priority.

Rotem
  • 30,366
  • 4
  • 32
  • 65