我在推力cuda中使用float4时遇到了内存问题
将 float4“buggyVariable”作为成员添加到仿函数似乎会导致 float 据左移 1 个浮点。
在 CUDA_animateParticles 中,我明确将 Y 设置为 0,Z 设置为 1
然而,当运行仿函数并在 OpenGL 中绘制它时。我得到 Xposition=1 的粒子,这表明仿函数内的 Y 为 1。
我还使用 float2 和 float3 进行了测试,它们似乎工作正常。
所以这似乎是内存对齐问题或错误。
有人能解释一下吗?感谢您的帮助。
#include <thrust/sort.h>
#include <thrust/random.h>
#include <thrust/device_vector.h>
#include "cutil_math.h"
struct animateParticles_functor
{
float4 buggyVariable; //why does adding this variable cause following floats to get wrong values???
float pex, pey, pez, pew;
__host__ __device__
animateParticles_functor( float x, float y, float z, float w) :
pex(x), pey(y), pez(z), pew(w)
{
}
template <typename Tuple>
__host__ __device__
void operator()(Tuple t)
{
if(pey > 0)
thrust::get<0>(t) = make_float4(1, 0, 0, 0); //true if y is bugged
else
thrust::get<0>(t) = make_float4(0, 0, 0, 0); //false if its not bugged
return;
}
}
void CUDA_animateParticles(float4* cuda_devicePointer_vboPosition, float3* cuda_devicePointer_particleVelocitys, unsigned int numParticles, float4 particleEmitter)
{
thrust::device_ptr<float4> d_pos(cuda_devicePointer_vboPosition);
thrust::device_ptr<float3> d_vel(cuda_devicePointer_particleVelocitys);
thrust::for_each(
thrust::make_zip_iterator(thrust::make_tuple(d_pos, d_vel)),
thrust::make_zip_iterator(thrust::make_tuple(d_pos + numParticles, d_vel + numParticles)),
animateParticles_functor(0, 0, 1, 0) //notice that i set Z to 1 and not Y to 0
);
}
最佳答案
我认为在 MSVC 的某些风格中,nvcc
和 cl.exe
无法在 sizeof(float4)
上达成一致。
尝试将 float4
替换为 my_float4
:
struct my_float4
{
float x, y, z, w;
};
关于cuda - Thrust cuda中使用float4时出现内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15485992/