#include<iostream>
#include<fstream>
#include<time.h>
#include<omp.h>
using namespace std;
static long num_steps = 100;
#define NUM 8
double step;
void main()
{
clock_t time =clock();
ofstream result;
result.open ("Result.txt");
int a[100];
double pi, sum=0.0;
step = 1.0/(double) num_steps;
#pragma omp parallel num_threads(NUM)
{
int i, ID;
double x, psum= 0.0;
int nthreads = omp_get_num_threads();
ID = omp_get_thread_num();
for (i=ID;i<= num_steps; i+=nthreads)
{
x = (i+0.5)*step;
psum += 4.0/(1.0+x*x);
}
#pragma omp critical
sum += psum;
}
pi = step * sum;
for (int j=0;j<100;j++)
result<<a[j]<<endl;
time = clock() - time;
result << "Time Elapsed: " << (((double)time)/CLOCKS_PER_SEC) << endl;
result <<"======================================================================================="<<endl;
result.close();
}
问题是:
for (i=ID;i<= num_steps; i+=nthreads)
以下for循环按以下顺序执行线程:
01234567 01234567 01234567等...
赋值是将for循环更改为,以便线程均匀分布并以圆形方式分配。首先是零,然后是两个......然后是七人组
我该怎么改变forloop?
答案 0 :(得分:0)
你必须使用某种线程同步...
你标记Visual Studio所以我假设Windows平台......
最近这成了我的最爱:
// init
CRITICAL_SECTION hnd;
InitializeCriticalSectionAndSpinCount(&hnd,0x00000400);
// start lock
EnterCriticalSection(&hnd);
// stop lock
LeaveCriticalSection(&hnd);
// exit
DeleteCriticalSection(&hnd);
但还有很多其他方法。
如果您错误地使用锁定,则可能会失去多线程加速的任何好处。
如果您只是担心您的解决方案不能同时计算线程
你的情况不是平行但是连续的,而不是由此引起的:
处理时间粒度。
错误的多线程代码
单核
要玩粒度,你可以使用他的:
// obtain OS time capabilities
TIMECAPS tim;
timeGetDevCaps(&tim,sizeof(tim));
// set new granularity
if (timeBeginPeriod(time ms)!=TIMERR_NOERROR) log("time granularity out of range");
// return to previous hranularity
timeEndPeriod(time ms ... must be the same as beginperiod);out of range");
PS。非常好的关于此的事情就在这里:
http://bitflipgames.com/2011/05/09/multithreaded-programming-part-1-the-critical-section-lock/ http://bitflipgames.com/2011/05/17/multithreaded-programming-part-2-multiple-readersingle-writer-lock/ http://bitflipgames.com/2011/05/20/multithreaded-programming-part-2-5-mrsw-lock-code/ http://bitflipgames.com/2011/05/25/multithreaded-programming-part-3-going-lockless/