问题
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.
回答1:
You can do SetThreadPriority
in the body of the loop without requiring special support from OpenMP:
for (...)
{
DWORD priority=GetThreadPriority(...);
SetThreadPriority(...);
// stuff
SetThreadPriority(priority);
}
回答2:
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.
来源:https://stackoverflow.com/questions/671902/missing-openmp-feature-thread-priority