# Calling a class from cuda-kernel

**URL:** <https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220>\
**Category:** CUDA Programming and Performance\
**Created:** [July 29, 2009, 10:49am UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220 "2009-07-29T10:49:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrDX](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MrDX](https://forums.developer.nvidia.com/u/MrDX)\
**Post date:** [July 29, 2009, 10:49am UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220/1 "2009-07-29T10:49:07Z")

</div>

Hello,

i am doing my first steps with CUDA. My goal is to use CUDA in my project to use the CPU and GPU for the calculation. The first kernel was successfully compiled and runs perfect. I ‘improved’ that kernel by using a simple class. So, the problem begins because only inline methods can be called from the kernel. I used the following code:

[codebox]#include \<stdio.h\>

#include \<cuda.h\>

#include “math/TestClass.h”

**global** void square\_array(float \*a, int N)

{

```
int idx = blockIdx.x * blockDim.x + threadIdx.x;

if (idx<N)

{

	math::TestClass test(39.0f);

	a[idx] = test.doSomething();

}

```

}[/codebox]

[codebox]#ifndef _TESTCLASS\_H_

#define _TESTCLASS\_H_

#include \<cuda.h\>

#ifdef GPU\_COMPILATION

```
#ifndef GPU_DEVICE

	#define GPU_DEVICE __device__  __host__

#endif

```

#else

```
#ifndef GPU_DEVICE

	#define GPU_DEVICE

#endif

```

#endif

namespace math

{

class TestClass

{

private:

```
float myVar;

```

public:

```
TestClass(float var)

	: myVar(var)

{

}	

GPU_DEVICE float doSomething();

GPU_DEVICE float doSomethingInline()

{

	return myVar;

}

```

};

}

#endif[/codebox]

[codebox]#include “TestClass.h”

namespace math

{

float TestClass::doSomething()

{

```
return myVar;

```

}

}[/codebox]

I can use doSomethingInline() without any problems, but doSomething() produces the following error:

[codebox]warning: function “math::TestClass::doSomething” was referenced but not defined

Error: External calls are not supported (found non-inlined call to \_ZN4math9TestClass11doSomethingEv)[/codebox]

Of course, I can only write inline-methods. But there are already a lot of complete classes in my project, splitted in \*.cpp and .h.

Can anyone tell me how i can use classes (without inheritance, …) in the CUDA-kernel? :-)

---

<div class="post-metadata">

**Author:** ![\_Big\_Mac](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_Big\_Mac](https://forums.developer.nvidia.com/u/_Big_Mac)\
**Post date:** [July 29, 2009, 3:48pm UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220/2 "2009-07-29T15:48:33Z")

</div>

Ha, I’m surprised inline methods work :)

CUDA supports only the C subset of C++, classes are technically illegal in kernels. Templates are the only C+±ish part that’s allowed in device code.

---

<div class="post-metadata">

**Author:** ![Jeff\_Gullett](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jeff\_Gullett](https://forums.developer.nvidia.com/u/Jeff_Gullett)\
**Post date:** [September 10, 2009, 3:06pm UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220/3 "2009-09-10T15:06:09Z")

</div>

> [@](#):
>
> Ha, I’m surprised inline methods work :)
> 
> CUDA supports only the C subset of C++, classes are technically illegal in kernels. Templates are the only C+±ish part that’s allowed in device code.

Ok, substitute “struct” for “class” in the OP’s code.

Does anyone know how to go about doing this? I have the exact same question, as I have a number of classes that I can easily convert to structs, but they are defined in sets of .h files and .cpp files. I am unable to get it to work with the struct definition in a separate .cu file from the struct declaration (when only including the header file). Thanks to anyone who can shed some light on this,

-Jeff

---

<div class="post-metadata">

**Author:** ![MrDX](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MrDX](https://forums.developer.nvidia.com/u/MrDX)\
**Post date:** [September 10, 2009, 4:20pm UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220/4 "2009-09-10T16:20:52Z")

</div>

> [@](#):
>
> Ok, substitute “struct” for “class” in the OP’s code.
> 
> Does anyone know how to go about doing this? I have the exact same question, as I have a number of classes that I can easily convert to structs, but they are defined in sets of .h files and .cpp files. I am unable to get it to work with the struct definition in a separate .cu file from the struct declaration (when only including the header file). Thanks to anyone who can shed some light on this,
> 
> -Jeff

My simple but effective trick: Include the .cpp at the end of the header via preprocessor.

[codebox]#ifndef _TESTCLASS\_H_

#define _TESTCLASS\_H_

#include “../gpu/GPUDefines.h”

namespace math

{

class TestClass

{

private:

```
float myVar;

```

public:

```
TestClass(float var)

	: myVar(var)

{

}	

GPU_HOST_AND_DEVICE float doSomething();

GPU_HOST_AND_DEVICE float doSomethingInline()

{

	return myVar;

}

```

};

}

#ifdef COMPILE\_FOR\_GPU

#include “TestClass.cpp”

#endif

#endif[/codebox]

[codebox]#ifndef COMPILE\_FOR\_GPU

```
#include "TestClass.h"

```

#endif

namespace math

{

float TestClass::doSomething()

{

```
return myVar * 10;

```

}

}[/codebox]

[codebox]#ifndef _GPUDEFINES\_H_

#define _GPUDEFINES\_H_

#ifdef COMPILE\_FOR\_GPU

# include \<cuda.h\>

# ifndef GPU\_DEVICE

# define GPU\_DEVICE **device**

# endif

# ifndef GPU\_HOST

# define GPU\_HOST **host**

# endif

# ifndef GPU\_HOST\_AND\_DEVICE

# define GPU\_HOST\_AND\_DEVICE **device**  **host**

# endif

#else

# ifndef GPU\_DEVICE

# define GPU\_DEVICE

# endif

# ifndef GPU\_HOST

# define GPU\_HOST

# endif

# ifndef GPU\_HOST\_AND\_DEVICE

# define GPU\_HOST\_AND\_DEVICE

# endif

#endif

#endif[/codebox]

Works perfect with our classes. All things, that are not cuda-compatible (functions using the stl, …) can be ‘removed’ for nvcc with #ifndef COMPILE\_FOR\_GPU. All code can be used for CPU and in CUDA.

---

<div class="post-metadata">

**Author:** ![Jeff\_Gullett](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jeff\_Gullett](https://forums.developer.nvidia.com/u/Jeff_Gullett)\
**Post date:** [September 10, 2009, 4:32pm UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220/5 "2009-09-10T16:32:58Z")

</div>

Unfortunately, like most “features” of CUDA, this is not the answer I was hoping for. Including the \*.cu files using the pre-processor is what I have currently implemented. This technique has the unfortunate side-effect of preventing incremental builds, but at least it works. Thanks,

-Jeff

---

<div class="post-metadata">

**Author:** ![aamir](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@aamir](https://forums.developer.nvidia.com/u/aamir)\
**Post date:** [February 28, 2010, 1:06am UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220/6 "2010-02-28T01:06:07Z")

</div>

> [@](#):
>
> Unfortunately, like most “features” of CUDA, this is not the answer I was hoping for. Including the \*.cu files using the pre-processor is what I have currently implemented. This technique has the unfortunate side-effect of preventing incremental builds, but at least it works. Thanks,
> 
> -Jeff

Hi Jeff and others,

Recently, I also ran into the same problem. I have many structs and standalone functions that I like to compile separately and then link to the kernel, but I was getting the “External calls are not supported” error while compiling (not linking) the kernel. It’s very frustrating!! If you have figured out a way to achieve incremental compilation, please share.

Thanks

---

<div class="post-metadata">

**Author:** ![Jeff\_Gullett](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jeff\_Gullett](https://forums.developer.nvidia.com/u/Jeff_Gullett)\
**Post date:** [March 1, 2010, 3:01am UTC](https://forums.developer.nvidia.com/t/calling-a-class-from-cuda-kernel/11220/7 "2010-03-01T03:01:25Z")

</div>

> [@](#):
>
> Hi Jeff and others,
> 
> Recently, I also ran into the same problem. I have many structs and standalone functions that I like to compile separately and then link to the kernel, but I was getting the “External calls are not supported” error while compiling (not linking) the kernel. It’s very frustrating!! If you have figured out a way to achieve incremental compilation, please share.
> 
> Thanks

Nope, no other solution from my end… although it sounds like the new Fermi architecture being released later this month may solve many of the coding problems developers faced in the past when using CUDA, since it allegedly supports the full C++ standard (from what I have heard… I leave it to you to find a reliable source for this information.)
