cuda - Thrust cuda中使用float4时出现内存问题

标签 cuda thrust

我在推力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 的某些风格中,nvcccl.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/

相关文章:

c++ - 推力结构 vector 的迭代器

c++ - 推力:不支持运算符 '*'

c++ - cuda数组排序推力,内存不足

windows - CUDA、Win7、Qt 创建者 - LNK1104 : cannot open file '<cuda file>.obj'

c++ - 编译 .cu 与 .cpp : Compiler errors even without any CUDA code

c++ - Cuda 写入设备上的数组不会更改值

c++ - 如何使用 CUDA Thrust 删除嵌套循环以进行全对距离检查?

optimization - CUDA 32 位整数运算在开普勒上比麦克斯韦更快?

c++ - 具有推力内部状态的仿函数

c++ - 从 C++ 访问 device_vector 的最佳方式