I
I
ivandzemianchyk2014-10-03 18:05:45
GPGPU
ivandzemianchyk, 2014-10-03 18:05:45

How to build a project in C++ with the implementation of part of the code in CUDA?

It is possible that the problem is in the compiler:

I'm using Visual Studio 2010

I looked through several tutorials, they all look about the same, but having taken the steps that they write about there, they have not yet led to success.
If the main() function is in the .cu file, then everything works without problems, but when I try to call it from the .cpp file, then there are problems during compilation.
I'll give an example below:
//test.cu
#include <stdio.h>
#include "hello.cuh"

 int main()
{
  hello();
  system("pause");
  return 0;
}

and
//hello.cuh
#include <cuda.h>
#include <stdio.h>

__global__ void helloWorld(char* str)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  str[idx] += idx;
}

 void hello()
{
  int i;
  char str[] = "Start!";
  for(i = 0; i < 6; i++)
    str[i] -= i;
  char *d_str;
  int size = sizeof(str);
  cudaMalloc((void**)&d_str, size);
  cudaMemcpy(d_str, str, size, cudaMemcpyHostToDevice);
  dim3 dimGrid(2);
  dim3 dimBlock(3);
  helloWorld<<< dimGrid, dimBlock >>>(d_str);
  cudaMemcpy(str, d_str, size, cudaMemcpyDeviceToHost);
  cudaFree(d_str);
  printf("\n%s\n", str);
}

Everything works fine. But if I try to create a new .cpp file and paste the same code from test.cu into it , then the following errors appear:

1> main.cpp
1>e:\inzynierka\v5\hello.cuh(4): error C2144: syntax error : 'void' should be preceded by ';'
1>e:\inzynierka\v5\hello.cuh(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\inzynierka\v5\hello.cuh(7): error C2065: 'blockIdx' : undeclared identifier
1>e:\inzynierka\v5\hello.cuh(7): error C2228: left of '.x' must have class/struct/union
1> type is ''unknown-type''
1>e:\inzynierka\v5\hello.cuh(7): error C2065: 'blockDim' : undeclared identifier
1>e:\inzynierka\v5\hello.cuh(7): error C2228: left of '.x' must have class/struct/union
1> type is ''unknown-type''
1>e:\inzynierka\v5\hello.cuh(7): error C2228: left of '.x' must have class/struct/union
1> type is ''unknown-type''
1>e:\inzynierka\ v5\hello.cuh(20): error C3861: 'cudaMalloc': identifier not found
1>e:\inzynierka\v5\hello.cuh(21): error C2065: 'cudaMemcpyHostToDevice' : undeclared identifier
1>e:\inzynierka \v5\hello.cuh(21): error C3861: 'cudaMemcpy': identifier not found
1>e:\inzynierka\v5\hello.cuh(22): error C2065: 'dim3' : undeclared identifier
1>e:\ inzynierka\v5\hello.cuh(22): error C2146: syntax error : missing ';' before identifier 'dimGrid'
1>e:\inzynierka\v5\hello.cuh(22): error C3861: 'dimGrid': identifier not found
1>e:
1>e:\inzynierka\v5\hello.cuh(23): error C2146: syntax error : missing ';' before identifier 'dimBlock'
1>e:\inzynierka\v5\hello.cuh(23): error C3861: 'dimBlock': identifier not found
1>e:\inzynierka\v5\hello.cuh(24): error C2059: syntax error : '<'
1>e:\inzynierka\v5\hello.cuh(25): error C2065: 'cudaMemcpyDeviceToHost' : undeclared identifier
1>e:\inzynierka\v5\hello.cuh(25): error C3861: 'cudaMemcpy': identifier not found
1>e:\inzynierka\v5\hello.cuh(26): error C3861: 'cudaFree': identifier not found
1>
1>Build FAILED.

In different tutorials, they write about the usertype.dat file, which should remove all underscores of unknown characters (like __global__ or blockIdx.x ), so I did not find this file, but in fact it should not affect anything.
I know that there are a lot of tutorials, but I've been sitting on this problem for n-hours and so far without results, I'm doing something wrong.
PS When I try to run CUDA_VS_Wizard I get an error Can't find Visual Studio install directory

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivandzemianchyk, 2014-10-06
@ivandzemianchyk

(I don't know if this is important) But I divided the CUDA code into .cu and .cuh
But 100% IMPORTANT!
If we create a .cu or .cuh file before we check the Build Customization checkbox, then we need to go to the file settings and select the file type:
Properties -> General -> Item Type -> (Select "CUDA C / C ++)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question