[SOLVED] Code not compiling for mysterious reason

Hi,

I’m a beginner with CUDA, so maybe I forgot something important to do …

However I think this problem is very strange because I didn’t find anything similar on google.

When I start compiling it fails and returns me something strange :

[codebox]

nvcc --compiler-bindir=/usr/bin -I/opt/cuda/include -L/opt/cuda/lib64 -o training ./training.cpp

./training.cpp: In function ‘void MatrixProductKernel(float*, float*, float*, int)’:

./training.cpp:38: error: ‘threadIdx’ was not declared in this scope

./training.cpp:40: error: ‘threadIdx’ was not declared in this scope

./training.cpp: In function ‘void MatrixProductOnGPU(float*, float*, float*, int)’:

./training.cpp:63: error: expected primary-expression before ‘<’ token

./training.cpp:63: error: expected primary-expression before ‘>’ token

make: *** [all] Error 255

[/codebox]

I really think there is a strange problem somewhere because threadIdx should be found there, but I got this output instead.

But maybe it’s due to something else : line 63.

However I keep reading my code and I don’t find anything unusual for a cuda code.

Makefile :

[codebox]

all :

    nvcc --compiler-bindir=/usr/bin -I/opt/cuda/include -L/opt/cuda/lib64 -o training ./training.cpp

clean :

    rm -f ./training

[/codebox]

My tiny and unfinished source code :

[codebox]

#include

#include <cuda.h>

#include <cuda_runtime.h>

#include <cuda_runtime_api.h>

global void MatrixProductKernel(float *a_device, float *b_device, float *c_device, int width) {

int i;

float c_value = 0.0;

for(i=0 ; i < width ; i++)

            c_value += a_device[threadIdx.y * width + i] + b_device[i * width + threadIdx.x];

c_device[threadIdx.y * width + threadIdx.x] = c_value;

}

void MatrixProductOnGPU(float *a_host, float *b_host, float *c_host, int width) {

size_t size = width * width * sizeof(float);

float *a_device, *b_device, *c_device;

cudaMalloc((void **)a_device, size);

    cudaMemcpy(a_device, a_host, size, cudaMemcpyHostToDevice);

cudaMalloc((void **)b_device, size);

    cudaMemcpy(b_device, b_host, size, cudaMemcpyHostToDevice);

cudaMalloc((void **)c_device, size);

dim3 dim_grid(1, 1);

    dim3 dim_block(width, width);

MatrixProductKernel<<< dim_grid, dim_block, 0, 0 >>>(a_device, b_device, c_device, width);

cudaMemcpy(c_host, c_device, size, cudaMemcpyDeviceToHost);

cudaFree(a_device);

    cudaFree(b_device);

    cudaFree(c_device);

}

int main(int argc, char **argv) {

std::cout << “Test 1” << std::endl;

return 0;

}

[/codebox]

Could you please tell me what’s wrong with my code ?

Thanks,

Alef B.

Change the filename to .cu. NVCC will only parse device code out of files with .cu extensions. This is discussed in the nvcc manual.

My bad :-/

Thank you

What is this?
I compiled examples in NVIDIA_CUDA-9.0_Samples by
make
command. In spite of errors almost all examples were build successfully e.g. ./bin/deviceQuery

/usr/local/cuda-9.0/bin/nvcc -ccbin g++ -I…/…/common/inc -m64 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_70,code=compute_70 -o eglstrm_common.o -c eglstrm_common.cpp
eglstrm_common.cpp: In function ‘void PrintEGLStreamState(EGLint)’:
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
};
^
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
eglstrm_common.cpp:61:5: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
eglstrm_common.cpp: In function ‘int EGLStreamInit()’:
eglstrm_common.cpp:76:53: error: ‘EGL_SUPPORT_REUSE_NV’ was not declared in this scope
static const EGLint streamAttrMailboxMode = { EGL_SUPPORT_REUSE_NV, EGL_FALSE, EGL_NONE };