cuda and C++

Hello everyone.

For some time now I’ve been experimenting with cuda. So I had wrote some myVector/myMatrix/myTensor structs for my experiments. But I thought to pass it on to C++ classes. Well I had some time to program on C++, so it took me a day to resolve my compilation errors.

Now I try to create a test_kernel and call it through myVector class. But I can’t resolve these compilation errors. So If you could give a hint, would be much appreciated.

Here is my code ::

cudalaLIB++.cuh

#include "laLIB++.h"

#include <cuda.h>

#include <cutil_inline.h>

extern "C" void trialKernel(float* array, int elements);

template <class T>

class cuVector : public iVector<T>

{

public: 

  cuVector() :

	iVector<T>()

  {

	cout << "A cuVector was created." << endl;

  }

cuVector(int _nElements) :

	iVector<T>(_nElements),

	size(_nElements*sizeof(T))

  {

	cout << "cuVector consc2." << endl;

  }

cuVector(int _nElements, T _element) :

	iVector<T>(_nElements, _element)

  {

	cout << "cuVector consc3." << endl;

  }

void allocVectorOnDev()

  {

	cudaMalloc((void **) &this->deviceCopyOfElements, this->size);   // Allocate array on device

  }

void copyVectorOnDev()

  {

	cudaMemcpy(this->deviceCopyOfElements, this->elements, size, cudaMemcpyHostToDevice);

  }

void allocAndCopyVectorOnDev()

  {

	allocVectorOnDev();

	copyVectorOnDev();

  }

void copyVectorBackOnHost()

  {

	cudaMemcpy(this->elements, this->deviceCopyOfElements, size, cudaMemcpyDeviceToHost);	// Copy memory on device.

  }

T* getDCOE()

  {

	return this->deviceCopyOfElements;

  }

void callTrialKernel()

  {

	trialKernel((float*) this->deviceCopyOfElements, this->nElements);

  }

private:

  size_t size;

  T* deviceCopyOfElements;

};

cudalaLIB++.cu

#include "cudalaLIB++.cuh"

__global__ void trial_kernel(float* array)

{

  int x = threadIdx.x;

  array[x] = 0;

}

extern "C" void trialKernel(float* array, int elements)

{

  dim3 grid(elements);

  dim3 block(elements);

  trial_kernel<<<grid, block>>>(array);

}

main.cpp

#include <iostream>

#include "cudalaLIB++.cuh"

using namespace std;

int main(int argc, char** argv)

{

  cout << "Hello World!" << endl;

  cuVector<float> vec1(10, .3);

  vec1.print();

vec1.allocAndCopyVectorOnDev();

  vec1.callTrialKernel();

vec1.copyVectorBackOnHost();

  vec1.print();

  return 0;

}

So when I try to compile it I get ::

obj/x86_64/release/myCUDAlaLIB.cpp.o: In function `main':

main.cpp:(.text+0x416): undefined reference to `trialKernel'

If you have any idea how to solve this, would be really helpfull !!

You could try to leave out the extern “C” part in the trialKernel() declaration, as it refers to an ordinary C function. I think you need the extern “C” only when exposing the kernel itself (trial_kernel) for other sourcefiles, but you already wrapped it.

I might be wrong, though.

You could try to leave out the extern “C” part in the trialKernel() declaration, as it refers to an ordinary C function. I think you need the extern “C” only when exposing the kernel itself (trial_kernel) for other sourcefiles, but you already wrapped it.

I might be wrong, though.

Jan, thank you for your time.

After trying to change the names of my source code from .cu/cuh to .cpp/h, I found out that I had mispelled my source files in the CUDA Makefile. Thus even though I was making changes the compiler couldn’t read my source files properly.

Jan, thank you for your time.

After trying to change the names of my source code from .cu/cuh to .cpp/h, I found out that I had mispelled my source files in the CUDA Makefile. Thus even though I was making changes the compiler couldn’t read my source files properly.