本文介绍了OpenCL产生不正确的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用openCL进行一些计算,但是结果不正确.

I've been trying to use openCL to do some calculations, but the results are incorrect.

我输入了三个如下所示的float3:

I input three float3's that look like this:

[300000,0,0]
[300000,300000,0]
[300000,300000,300000]

进入此内核:

__kernel void gravitate(__global const float3 *position,__global const float3 *momentum,__global const float3 *mass,__global float3 *newPosition,__global float3 *newMomentum,unsigned int numBodies,unsigned int seconds)
{
    int gid=get_global_id(0);

    newPosition[gid]=position[gid]*2;
    newMomentum[gid]=momentum[gid]*2;
}

我本来是试图模拟一组物体的引力相互作用的,这就是为什么内核看起来像这样的原因.

I was originally trying to simulate the gravitational interaction of a group of bodies which is why the kernel looks like that.

对于输出,我得到:

[600000.0,0.0,0.0]
[x,600000.0,0.0]
[600000.0,x,600000.0]

其中x是一个介于NaN到0到10 ^ -44之间的值,并且每次运行内核时都不同.newPosition和newMomentum都具有相同的错误输出模式.

Where x is a value that ranges from NaN to between 0 and 10^-44 and is different every time the kernel is run.Both newPosition and newMomentum have the same incorrect output pattern.

我正在使用的python代码如下:

The python code that I'm using looks like this:

def __init__(self):
        self.context=cl.create_some_context()
        self.queue=cl.CommandQueue(self.context)

        f=open("gravitate.cl")
        self.program=cl.Program(self.context,f.read()).build()

def simulate(self,seconds):
        bodyPosition=[]
        bodyMomentum=[]
        bodyMass=[]

        for body in self.objects:
            bodyPosition+=[body.position.x,body.position.y,body.position.z]
            bodyMomentum+=[body.momentum.x,body.momentum.y,body.momentum.z]
            bodyMass+=[body.mass]

        bodyPosition=numpy.array(bodyPosition).astype(numpy.float32)
        bodyMomentum=numpy.array(bodyMomentum).astype(numpy.float32)
        bodyMass=numpy.array(bodyMass).astype(numpy.float32)

        bodyPositionCl=cl.Buffer(self.context,cl.mem_flags.READ_ONLY|cl.mem_flags.COPY_HOST_PTR,hostbuf=bodyPosition)
        bodyMomentumCl=cl.Buffer(self.context,cl.mem_flags.READ_ONLY|cl.mem_flags.COPY_HOST_PTR,hostbuf=bodyMomentum)
        bodyMassCl=cl.Buffer(self.context,cl.mem_flags.READ_ONLY|cl.mem_flags.COPY_HOST_PTR,hostbuf=bodyMass)

        newBodyPosition=numpy.zeros(len(self.objects)*3).astype(numpy.float32)
        newBodyMomentum=numpy.zeros(len(self.objects)*3).astype(numpy.float32)

        newBodyPositionCl=cl.Buffer(self.context,cl.mem_flags.WRITE_ONLY,newBodyPosition.nbytes)
        newBodyMomentumCl=cl.Buffer(self.context,cl.mem_flags.WRITE_ONLY,newBodyMomentum.nbytes)

        self.program.gravitate(self.queue,(3,),None,bodyPositionCl,bodyMomentumCl,bodyMassCl,newBodyPositionCl,newBodyMomentumCl,numpy.uint32(len(self.objects)),numpy.uint32(seconds))
        cl.enqueue_read_buffer(self.queue,newBodyPositionCl,newBodyPosition).wait()
        cl.enqueue_read_buffer(self.queue,newBodyMomentumCl,newBodyMomentum).wait()

推荐答案

float3是16字节对齐的.请参阅OpenCL 1.1规范6.1.5.

float3 are 16-byte aligned. See OpenCL 1.1 spec, 6.1.5.

这篇关于OpenCL产生不正确的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:41