缺少 OpenMP 功能:线程优先级

发布于 2024-07-15 01:14:22 字数 883 浏览 3 评论 0原文

任何人都想想吧。 OpenMP 具有调整 CPU 能力来处理哑铃的功能。 在我对 openmp 的研究中,我们无法设置线程优先级来以强大的力量执行块代码。 5. 只有一种方法(_beginthreadex 或带参数的 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 设置优先级,请通知我们。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-07-22 01:14:22

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

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

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

for (...)
{
  DWORD priority=GetThreadPriority(...);
  SetThreadPriority(...);
  // stuff
  SetThreadPriority(priority);
}
对风讲故事 2024-07-22 01:14:22

简单的测试揭示了意想不到的结果:
我在 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 线程的线程优先级时,线程将保持该优先级。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文