編譯第5行時,我在CUDA C編程中遇到內核問題。我得到了"expected an identifier"
錯誤。這是爲什麼發生?如何在CUDA中解決「預期的標識符」錯誤
我的內核函數如下:
__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) ;
}
我不熟悉cuda,但是'threadIdx','blockIdx'和'blockDim',全局變量?函數原型之前什麼是正確的? – macduff
錯誤在哪裏發生?我建議你展示更多的代碼,以便任何人都可以重現此... – KiaMorot
爲什麼讓我們搜索,請給行##。 –