When I was compiling cuda code , it awalys shows " error: identifier "_device_float" is undefined"

My setup for cuda
linux ubuntu 16.04+cuda 8.0+NIVIDIA GEFORCE GTX850M

My cuda code

#include <stdio.h>
#define N 64
#define TPB 32

_device_float scale(int i, int n)
return ((float)i)/(n - 1);

_device_float distance(float x1, float x2)
return sqrt((x2 - x1)*(x2 - x1));

_global_void distanceKernel(float d_out, float ref, int len)
const int i = blockIdx.x
blockDim.x + threadIdx.x;
const float x = scale(i, len);
d_out[i] = distance(x, ref);
printf(“i = %2d: dist from %f to %f is %f.\n”, i, ref, x, d_out[i]);

int main()
const float ref = 0.5f;

float *d_out = 0;

cudaMalloc(&d_out, N*sizeof(float));

distanceKernel<<<N/TPB, TPB>>>(d_out, ref, N);

return 0;

and Makefile code
NVCC = /usr/local/cuda/bin/nvcc
NVCC_FLAGS = -g -G -Xcompiler -Wall

main.exe: kernel.cu
(NVCC) (NVCC_FLAGS) ^ -o @

When I was compiling cuda code , it awalys shows
kernel.cu(5): error: identifier “_device_float” is undefined

kernel.cu(10): error: identifier “_device_float” is undefined

kernel.cu(15): error: identifier “_global_void” is undefined

kernel.cu(31): error: a host function call cannot be configured

4 errors detected in the compilation of “/tmp/tmpxft_000011b9_00000000-9_kernel.cpp1.ii”.

How could I fix this problem? I will be very grateful if someone can help me !
(PS I just started learning cuda.)

This is not valid CUDA code.



should be this:

device float

that is a double underscore: __ not a single underscore: _ both before and after the device keyword, and there should be a space between the device keyword and float

You should fix all instances of device this way and all instances of global this way. So instead of this:


It should be:

global void

All you have to do is look at a CUDA sample project like vectorAdd (or read just a short section of the CUDA C programming guide) to see the correct syntax.