在编译第 5 行时,我在 CUDA C 编程中遇到了内核问题。我遇到了一个"expected an identifier"
错误。为什么会这样?
我的内核函数如下:
__global__ void txz_kernel(float *txz,float *vz)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x; --> error
txz[offset]=txz[offset]+vz[offset];
}
这是完整的代码:
#include "../common/book.h"
#include "conio.h"
#include "cuda.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <assert.h>
#include "../common/book.h"
#include <fstream>
#define DIMX 320
#define DIMZ 320
#define PI 3.1415926535897932f
__global__ void txz_kernel(float *txz,float *vz)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x; --> error
txz[offset]=txz[offset]+vz[offset];
}
int main( void ) {
float *txz;
float *vz;
HANDLE_ERROR( cudaMalloc( (void**)&txz, DIMX * DIMZ * sizeof(float)));
HANDLE_ERROR( cudaMalloc( (void**)&vz, DIMX * DIMZ * sizeof(float)));
float *tempvz = (float*)malloc( sizeof(float)*(DIMX*DIMZ));
float *temptxz = (float*)malloc( sizeof(float)*(DIMX*DIMZ));
for (int i=0; i<DIMX; i++) {
for (int j=0; j<DIMZ; j++) {
int ij=DIMX*j + i;
tempvz[ij]=200.0;
temptxz[ij]=100.0;
}
}
for (int i=0; i<DIMX; i++) {
for (int j=(121); j<DIMZ; j++) {
int ij=DIMX*j + i;
tempvz[ij]=250.0;
temptxz[ij]=150.0;
}
}
HANDLE_ERROR( cudaMemcpy( vz, tempvz,sizeof(float)*(DIMX*DIMZ),cudaMemcpyHostToDevice ) );
HANDLE_ERROR( cudaMemcpy( txz, temptxz,sizeof(float)*(DIMX*DIMZ),cudaMemcpyHostToDevice ) );
dim3 blocks(DIMX/16,DIMZ/16);
dim3 threads(16,16);
txz_kernel<<<blocks,threads>>>(txz,vz) ;
}