-1

I have a templated static CUDA library which I want to include into a common c++ project. When I include the headers of the library the compiler crashes and says It cannot resolve the CUDA-specific symbols. Of course the g++ compiler cannot interpret these symbols. I know the problem, but I do not know how to fix this problem using the nsight IDE.

I'm using nsight for both, the cuda/nvcc library and the c++/g++ project.

Console output:

make all 
Building file: ../src/MedPrak.cpp
Invoking: GCC C++ Compiler
g++ -I/home/voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/MedPrak.d" -MT"src/MedPrak.d" -o "src/MedPrak.o" "../src/MedPrak.cpp"

In file included from ../src/cudaWrapper.cu:8:0,
                 from ../src/MedPrak.cpp:3:

/home/voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src/kernel.h:15:23: error: ‘__global__’ does not name a type
 template <typename T> __global__ void squareVector(T *input, T *output, int size) {

Edit: Forgot to mention that I have a cuda project with the same files as in the library. The cuda project compiles fine and runs properly, so I think there is not a huge error within my code.

Edit2: To avoid the "template library" idea. I have a wrapper around the actual template classes. So there is no "empty" library.

6
  • Exactly what errors are you getting? What are your compile settings? Post both the exact error message and the compiler settings as passed to g++ in the question.
    – shuttle87
    Jul 4, 2014 at 13:43
  • Got the exakt same error multiple times for different lines in my code.
    – VoodooCode
    Jul 4, 2014 at 13:49
  • It looks like you aren't actually linking to the library. If the library is not header-only then you will run into some problems. __global__ is a CUDA extension keyword, do you compile that other project with different command line arguments and with different headers? I suspect at the very least you have to use the compile flag -lcuda here.
    – shuttle87
    Jul 4, 2014 at 14:02
  • The other project, the cuda one, is compiled using the nvcc compiler which recognises these commands.
    – VoodooCode
    Jul 4, 2014 at 14:26
  • 2
    You library should not expose any CUDA-related stuff in its interface (header files). Compile CUDA library with nvcc and then link it with the rest of your c/c++ code as usual with gcc.
    – Archie
    Jul 4, 2014 at 14:37

1 Answer 1

5

Here's a set of instructions that should help:

A. Create library project:

  1. select File...New...CUDA C/C++ Project
  2. Select Static Library...Empty Project and give the project a name (test8)
  3. Next...Next...Finish to finish creating the project
  4. right click on project name in Project Explorer window, select New...Header File, give it a name (test8lib.h)
  5. edit test8lib.h (with contents from below), save it
  6. create another new header file for cuda template, (test8.cuh)
  7. edit test8.cuh (with contents from below), save it
  8. create a new source file, (test8.cu)
  9. edit test8.cu (with contents from below), save it
  10. select Project...Build Project (libtest8.a is now built)

test8lib.h:

#ifndef TEST8LIB_H_
#define TEST8LIB_H_

void calc_square_vec_float(float *in_data, float *out_data, int size);


#endif /* TEST8LIB_H_ */

test8.cuh:

#ifndef TEST8_CUH_
#define TEST8_CUH_

template <typename T> __global__ void squareVector(T *input, T *output, int size) {
    int idx = threadIdx.x+blockDim.x*blockIdx.x;
    if (idx < size) output[idx]=input[idx]*input[idx];
}


#endif /* TEST8_CUH_ */

test8.cu:

#include "test8lib.h"
#include "test8.cuh"
#define nTPB 256

void calc_square_vec_float(float *in_data, float *out_data, int size){
    float *d_in_data, *d_out_data;
    cudaMalloc(&d_in_data,  size*sizeof(float));
    cudaMalloc(&d_out_data, size*sizeof(float));
    cudaMemcpy(d_in_data, in_data, size*sizeof(float),cudaMemcpyHostToDevice);
    squareVector<<<(size+nTPB-1)/nTPB, nTPB>>>(d_in_data, d_out_data, size);
    cudaMemcpy(out_data, d_out_data, size*sizeof(float),cudaMemcpyDeviceToHost);
}

B. Create main project:

  1. File...new...C++ project...empty project...Linux GCC toolchain, give it a name (test9)
  2. Next...Finish to finish creating the project
  3. File...New Source File...Default C++ source template, give it a name (test9.cpp)
  4. edit the file with contents from below, save it.
  5. add the include path: Project...Properties...Build...Settings...Tool Settings...GCC C++ Compiler...Includes...Include Paths...Add and add the directory where test8lib.h is located.
  6. add the lib: Tool Settings...GCC C++ Linker...Libraries...Libraries...Add and add the name of the previously built library (test8)
  7. also add CUDA runtime library (cudart)
  8. add the lib path: Tool Settings...GCC C++ Linker...Libraries...Library Paths...Add and add the path to the previously built library (e.g. /path/to/cuda-workspace/test8/Debug)
  9. also add the path to cudart (e.g. /usr/local/cuda/lib64)
  10. Build Project
  11. Run Project

test9.cpp:

#include <stdio.h>
#include <stdlib.h>
#include "test8lib.h"
#define DSIZE 4
#define TEST_VAL 2.0f

int main(){
    float *in, *out;
    in = (float *)malloc(DSIZE*sizeof(float));
    out = (float *)malloc(DSIZE*sizeof(float));
    for (int i=0; i<DSIZE; i++){
        in[i] = TEST_VAL;
        out[i] = 0.0f;
    }
    calc_square_vec_float(in, out, DSIZE);
    for (int i=0; i<DSIZE; i++)
        if (out[i] != (float)(TEST_VAL*TEST_VAL)){
            printf("mismatch at %d, was: %f, should be: %f\n", i, out[i], (float)(TEST_VAL*TEST_VAL));
            return 1;
        }
    printf("Success!\n");
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.