我在MSVC 9 SP1下使用OpenMP v2遇到了一个有趣的问题。当从主执行线程中调用omp_set_num_threads
然后使用omp_get_num_threads
来检查金额集时,一切正常并且检出。
然而,在GUI应用程序中,我调用相同的东西,但它自己的线程(使用CreateThread
创建),以防止UI变得无响应,看起来omp_set_num_threads
似乎没有从线程调用时工作,因为omp_get_num_threads
总是报告1,而从测试中我只能看到一个线程正在运行。
摘要:从一个不是进程主线程的线程调用时,omp_set_num_threads
是否有问题/限制?
代码:
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);
//..
答案 0 :(得分:0)
使用了哪些编译器选项(openmp)?请在功能中查看此代码:
#ifndef _OPENMP
printf("No OpenMP.");
#else
printf("OpenMP OK.");
#endif