运行内核后从CLBuffer读取JavaCL

时间:2013-07-18 08:43:58

标签: java opencl

我正在编写一个计算N-Body模拟的程序,但是我在运行后得到内核结果时遇到了问题。速度数组是由内核计算的,但问题是我不知道如何使用JavaCL包装器再次读取数据。

//CLBuffer generator convenience method
private CLBuffer<FloatBuffer> GenBuff(float[] dest)
{
    FloatBuffer ptrLX = ByteBuffer.allocate(numParticles * 4).asFloatBuffer();
    ptrLX.put(dest);
    return context.createBuffer(CLMem.Usage.InputOutput, ptrLX);
}

private void CalculateTimeStep(float e, float DT) {
    float[] params = { DT, e, this.particleMass, (float) this.numParticles };

    //Generates CLBuffers with a convenience method 
    CLBuffer<FloatBuffer> pLXBuffer = this.GenBuff(pLX);
    CLBuffer<FloatBuffer> pLYBuffer = this.GenBuff(pLY);
    CLBuffer<FloatBuffer> pVXBuffer = this.GenBuff(pVX);
    CLBuffer<FloatBuffer> pVYBuffer = this.GenBuff(pVY);
    CLBuffer<FloatBuffer> ParamsBuffer = this.GenBuff(params);
    //sets the kernel arguments
    this.NBodyKernel.setArg(0, pLXBuffer);
    this.NBodyKernel.setArg(1, pLYBuffer);
    this.NBodyKernel.setArg(2, pVXBuffer);
    this.NBodyKernel.setArg(3, pVYBuffer);
    this.NBodyKernel.setArg(4, ParamsBuffer);
    //enqueue the kernel 
    CLEvent evt = this.NBodyKernel.enqueueNDRange(que, new int[]{numParticles});

这是我的问题,因为这不会实际更新数组,但保留原样。

    pVXBuffer.readBytes(que, 0, this.numParticles,evt).asFloatBuffer().get(pVX);
    pVYBuffer.readBytes(que, 0, this.numParticles,evt).asFloatBuffer().get(pVY);

上面的问题区域

    //And I release the temerary openCL components manualy
    pVXBuffer.release();
    pVYBuffer.release();
    pLXBuffer.release();
    pLYBuffer.release();
    ParamsBuffer.release();
    }

和openCL内核

__kernel void NBody(__global float *posX,__global float *posY, __global float *volX , __global float *volY, __global float *params)
{

//params
// DT -> E -> PM -> NP    
float DeltaT = params[0];
float E = params[1];
float mass = params[2];
int numPart = as_int(params[3]);

int x = get_global_id(0);
//create local variables from global variables that are used often to lower memeroy overhead
float mPosX = posX[x];
float mPosY = posY[x];
float mVolX = volX[x];
float mVolY = volY[x];
for(int i = 0; i < numPart; i++)
{
    if(i != x)
    {
        float sx = posX[i] - mPosX;
        float sy = posY[i] - mPosY;
        float r2 = sx*sx + sy*sy;
        float r = sqrt(r2);
        float r3 = r2*r;
        mVolX += sx*mass*DeltaT/(r3+E);
        mVolX += sy*mass*DeltaT/(r3+E);
    }
} 
volX[x] = mVolX;
volY[x] = mVolY;
}

1 个答案:

答案 0 :(得分:0)

看起来您正在使用JFS版本的JavaCL。那个旧的,请使用BridJ版本 - 请参阅Migrating from JNA to BridJ

你真的打算在这一行中将float重新解释为int吗?

int numPart = as_int(params[3]);

这不会将1.0f转换为整数1,它将直接解释该float中的位,就好像它们是一个整数,给出一些数字。我想你想看看第6.2.4.2节:重新解释类型。我想你实际上想要convert_int函数,它是将任何东西转换为整数的显式类型转换函数。