从WinAPI线程调用omp_set_num_threads的问题 [英] Problem with omp_set_num_threads called from a WinAPI thread

查看:345
本文介绍了从WinAPI线程调用omp_set_num_threads的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MSVC 9 SP1下使用OpenMP v2遇到了一个有趣的问题。当从主执行线程调用 omp_set_num_threads 时,使用 omp_get_num_threads 来检查设置的数量, 。

I've run into a funny problem using OpenMP v2 under MSVC 9 SP1. when calling omp_set_num_threads from the main thread of execution then using omp_get_num_threads to check the amount set, all works well and checks out.

然而,在GUI应用程序中,我调用相同的东西,但是自己的线程(使用 CreateThread 创建) ,为了防止UI变得无响应,当从线程调用时, omp_set_num_threads 似乎不起作用,因为 omp_get_num_threads 总是报告1,从测试我只能看到一个线程在操作。

However, in an GUI app, I call the same thing, but its own thread(created with CreateThread), to prevent the UI from becoming unresponsive, how ever it seems that omp_set_num_threads doesn't work when called from a thread, as omp_get_num_threads always reports 1, and from tests I can see only one thread in operation.

总结: > omp_set_num_threads 从进程主线程的线程调用时有问题/限制?

In Summary: does omp_set_num_threads have problems/restrictions when being called from a thread thats not the processes main thread?

代码:

void CalculateDivisionSeriesOMP(unsigned long dwMul, int nType, size_t nOMPThreads)
{
    omp_set_num_threads(nOMPThreads);
    if(nType == 0)
    {
        #pragma omp parallel default(shared)
        {
            #pragma omp master
            {
                Printf("Starting Search For Divisor With Constant: 0x%08X...",dwMul);
                Printf("%d Threads In Use",omp_get_num_threads());
            }

            #pragma omp for
            for(__int64 i = 2; i < 4294967295; i++)
            {
                mu U = magicu2(i);
                if(U.M == dwMul)
                    Printf("Found Unsigned Divisor: %d Series Expansion Stage(Shift): %d Add: %d",unsigned long(i),U.s,U.a);

                ms S = magic(i);
                if(S.M == dwMul)
                    Printf("Found Signed Divisor: %d Series Expansion Stage(Shift): %d",i,S.s);
            }
        }

    }
//more of the same...
}

DWORD WINAPI DivThread(void* p)
{
    //...
    CalculateDivisionSeriesOMP(dwMul,nType,nOMPThreads);
    //...
}

//...
hDivThread = CreateThread(NULL,0,DivThread,NULL,0,NULL);
//..


推荐答案

是否使用(openmp)?请检查函数中的代码:

What compiler options are used(openmp)? Please, check this code in function:

#ifndef _OPENMP
printf("No OpenMP.");
#else
printf("OpenMP OK.");
#endif

这篇关于从WinAPI线程调用omp_set_num_threads的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆