我是CUDA C的新手,我正在尝试将typedef'd结构传递给内核.当我尝试使用仅包含整数的结构时,我的方法工作得很好,但是当我切换到浮点数时,我得到了无意义的数字作为结果.我认为这与对齐有关,我尝试包括__align__我的类型声明,但无济于事.有人能举例说明这是如何完成的,还是提供了另一种方法?我正在尝试设置它,以便我可以轻松地添加或删除字段,而不会更改除结构和内核之外的任何内容.我的代码:
typedef struct __align__(8)
{
float a, b;
} point;
__global__ void testKernel(point *p)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
p[i].a = 1.1;
p[i].b = 2.2;
}
int main(void)
{
// set number of points
int numPoints = 16,
gpuBlockSize = 4,
pointSize = sizeof(point),
numBytes = numPoints * pointSize,
gpuGridSize = numPoints / gpuBlockSize;
// allocate memory
point *cpuPointArray = new point[numPoints],
*gpuPointArray = new point[numPoints];
cpuPointArray = (point*)malloc(numBytes);
cudaMalloc((void**)&gpuPointArray, numBytes);
// launch …Run Code Online (Sandbox Code Playgroud)