1

任何人都可以考虑一下。OpenMP 功能可调整 cpu 肌肉以处理哑铃。在我对 openmp 的研究中,我们无法设置线程优先级来执行具有强大肌肉的块代码。只有一种方法(_beginthreadex 或带有 5 个参数的 CreateThread 函数)来创建具有最高优先级的线程。

这里有一些关于这个问题的代码:

这是手动设置。

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

这是我想看到这部分:

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

或者

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

我不知道是否有办法使用 openmp 设置优先级,请告知我们。

4

2 回答 2

3

您可以SetThreadPriority在循环体中执行此操作,而无需 OpenMP 的特殊支持:

for (...)
{
  DWORD priority=GetThreadPriority(...);
  SetThreadPriority(...);
  // stuff
  SetThreadPriority(priority);
}
于 2009-03-23T00:35:13.177 回答
1

简单的测试揭示了意想不到的结果:我在 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;
    }

输出是:

    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

说明:OpenMP 主线程以主线程优先级执行。其他 OpenMP 线程保持正常优先级。手动设置 OpenMP 线程的线程优先级时,线程保持该优先级。

于 2014-03-08T22:14:17.840 回答