在GPU上进行OpenCL RK4集成

时间:2016-09-12 17:07:17

标签: c++ opencl integration gpu physics

使用OpenCL并行化积分器时 - 将整个循环放在内核中是不好的做法?

我正在尝试将我用C ++编写的RK4集成器移植到OpenCL中,这样我就可以在GPU上运行 - 目前它使用的是OpenMP。

我需要运行1000万次+独立集成运行,每次运行大约有700次循环迭代。我目前已经将循环写入内核并且具有停止条件,但它的表现不如我预期的那么好。

当前CL Kernel代码段:

`
while (inPos.z > -1.0f){
        cnt++;
        //Eval 1

        //Euler Velocity
        vel1 = inVel + (inAcc * 0.0f);
        //Euler Position
        pos1 = inPos + (vel1 * 0.0f) + ((inAcc * 0.0f)*0.5f);

        //Drag and accels
        combVel = sqrt(pow(vel1.x, 2)+pow(vel1.y, 2)+pow(vel1.z, 2));
        //motionUtils::drag(netForce, combVel, mortSigma, outPos.z);
        dragForce = mortSigma*1.225f*pow(combVel, 2);
        //Normalise vector
        normVel = vel1 / combVel;
        //Drag Components
        drag = (normVel * dragForce)*-1.0f;
        //Add Gravity force
        drag.z+=((mortMass*9.801f)*-1.0f);
        //Acceleration components
        acc1 = drag/mortMass;

        ...

        //Taylor Expansion
        tayVel = (vel1+((vel2+vel3)*2.0f)+vel4) * (1.0f/6.0f);
        inAcc = (acc1+((acc2+acc3)*2.0f)+acc4) * (1.0f/6.0f);
        tayPos = (pos1+((pos2+pos3)*2.0f)+pos4) * (1.0f/6.0f);

        //Swap ready for next iteration
        inPos = inPos + (tayVel * timeStep);
        inVel = inVel + (inAcc * timeStep);

` 任何想法/建议,非常感谢。

1 个答案:

答案 0 :(得分:1)

尝试更慢(和更不精确)的慢功能版本:

sqrt(pow(vel1.x, 2)+pow(vel1.y, 2)+pow(vel1.z, 2))

native_rsqrt(vel1.x*vel1.x+vel1.y*vel1.y+vel1.z*vel1.z)
 normVel = vel1 / combVel;

 normVel = vel1 * combVel;
 dragForce = mortSigma*1.225f*pow(combVel, 2);

 dragForce = mortSigma*1.225f*(combVel*combVel);
    drag = (normVel * dragForce)*-1.0f;
    //Add Gravity force
    drag.z+=((mortMass*9.801f)*-1.0f);

    drag = -normVel * dragForce;
    //Add Gravity force
    drag.z-=mortMass*9.801f;
    tayVel = (vel1+((vel2+vel3)*2.0f)+vel4) * (1.0f/6.0f);
    inAcc = (acc1+((acc2+acc3)*2.0f)+acc4) * (1.0f/6.0f);
    tayPos = (pos1+((pos2+pos3)*2.0f)+pos4) * (1.0f/6.0f);

    tayVel = (vel1+((vel2+vel3)*2.0f)+vel4) * (0.166666f);
    inAcc = (acc1+((acc2+acc3)*2.0f)+acc4) * (0.166666f);
    tayPos = (pos1+((pos2+pos3)*2.0f)+pos4) * (0.166666f);

如果使用太多变量,请尝试将本地工作组大小从256减少到128或64,如果它们没有在循环中使用,请将它们的声明放在循环中,以便在同一时间发出更多线程。 / p>