C++ 是否可以创建一个线程组,然后只创建;使用「;等会儿再说?
所以我有一些OpenMP代码:C++ 是否可以创建一个线程组,然后只创建;使用「;等会儿再说?,c++,multithreading,parallel-processing,openmp,C++,Multithreading,Parallel Processing,Openmp,所以我有一些OpenMP代码: for(unsigned int it = 0; it < its; ++it) { #pragma omp parallel { /** * Run the position integrator, reset the * acceleration, update the acceleration, update the velocity. */ #
for(unsigned int it = 0; it < its; ++it)
{
#pragma omp parallel
{
/**
* Run the position integrator, reset the
* acceleration, update the acceleration, update the velocity.
*/
#pragma omp for schedule(dynamic, blockSize)
for(unsigned int i = 0; i < numBods; ++i)
{
Body* body = &bodies[i];
body->position += (body->velocity * timestep);
body->position += (0.5 * body->acceleration * timestep * timestep);
/**
* Update velocity for half-timestep, then reset the acceleration.
*/
body->velocity += (0.5f) * body->acceleration * timestep;
body->acceleration = Vector3();
}
/**
* Calculate the acceleration.
*/
#pragma omp for schedule(dynamic, blockSize)
for(unsigned int i = 0; i < numBods; ++i)
{
for(unsigned int j = 0; j < numBods; ++j)
{
if(j > i)
{
Body* body = &bodies[i];
Body* bodyJ = &bodies[j];
/**
* Calculating some of the subsections of the acceleration formula.
*/
Vector3 rij = bodyJ->position - body->position;
double sqrDistWithEps = rij.SqrMagnitude() + epsilon2;
double oneOverDistCubed = 1.0 / sqrt(sqrDistWithEps * sqrDistWithEps * sqrDistWithEps);
double scalar = oneOverDistCubed * gravConst;
body->acceleration += bodyJ->mass * scalar * rij;
bodyJ->acceleration -= body->mass * scalar * rij; //Newton's Third Law.
}
}
}
/**
* Velocity for the full timestep.
*/
#pragma omp for schedule(dynamic, blockSize)
for(unsigned int i = 0; i < numBods; ++i)
{
bodies[i].velocity += (0.5 * bodies[i].acceleration * timestep);
}
}
/**
* Don't want I/O to be parallel
*/
for(unsigned int index = 1; index < bodies.size(); ++index)
{
outFile << bodies[index] << std::endl;
}
}
for(unsigned int it=0;it位置+=(车身->速度*时间步);
车身->位置+=(0.5*车身->加速度*时间步长*时间步长);
/**
*更新半个时间步的速度,然后重置加速度。
*/
车身->速度+=(0.5f)*车身->加速度*时间步长;
车身->加速度=矢量3();
}
/**
*计算加速度。
*/
#计划的pragma omp(动态、块大小)
for(无符号整数i=0;ii)
{
Body*Body=&Body[i];
Body*bodyJ=&bodys[j];
/**
*计算加速度公式的某些小节。
*/
向量3 rij=bodyJ->position-body->position;
双sqrDistWithEps=rij.SqrMagnitude()+epsilon2;
双1超差=1.0/sqrt(带EPS的SQRDist*带EPS的SQRDist*带EPS的SQRDist);
双标量=一个超差立方*重力常数;
车身->加速度+=bodyJ->质量*标量*rij;
bodyJ->加速度-=物体->质量*标量*rij;//牛顿第三定律。
}
}
}
/**
*整个时间步的速度。
*/
#计划的pragma omp(动态、块大小)
for(无符号整数i=0;i outFile据我所知,这是最合乎逻辑的方法,线程池已经创建,每次线程到达并行构造函数时,它都会从池中请求一组线程。因此,它不会每次到达并行区域构造函数时都创建线程池,但是,如果要重用相同的线程,为什么ot只需将并行构造函数推出循环,并使用单个pragma
处理顺序代码,如下所示:
#pragma omp parallel
{
for(unsigned int it = 0; it < its; ++it)
{
...
...
/**
* Don't want I/O to be parallel
*/
#pragma omp single
{
for(unsigned int index = 1; index < bodies.size(); ++index)
{
outFile << bodies[index] << std::endl;
}
} // threads will wait in the internal barrier of the single
}
}
#pragma omp并行
{
for(无符号int it=0;it outFileOpenMP模型通常显示为fork-join范例。但出于性能原因,线程不会在连接结束时终止。在某些实现中,例如Intel OpenMP,线程在睡眠前在连接结束时等待一段特定时间的自旋锁(请参阅KMP_BLOCKTIME on)