# template cuda kernel function cannot be called in another template function on vs2013

**URL:** <https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065>\
**Category:** CUDA Programming and Performance\
**Created:** [October 29, 2015, 3:56am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065 "2015-10-29T03:56:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugowangyj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@hugowangyj](https://forums.developer.nvidia.com/u/hugowangyj)\
**Post date:** [October 29, 2015, 3:56am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/1 "2015-10-29T03:56:48Z")

</div>

Hi, all  
I recently found when trying to call a template cuda kernel function in a template host function the same errors happen.  
Codes as follow:

template  
**global** void addKernel()  
{  
}

template  
void hostfun(Dtype \*in, Dtype \*out)  
{  
addKernel \<\<\<1, 512\>\>\>();  
}

int main(int argc, char \*\* argv)  
{  
float \*a, \*b;  
hostfun(a, b);  
}

Could anyone help?  
ps: my system is win7; vs 2013; cuda 7.0

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [October 29, 2015, 6:24am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/2 "2015-10-29T06:24:18Z")

</div>

I fixed missing pointer initializations to get rid of warnings. After that your code compiled without warnings or errors for me (CUDA 7.5, MSVS 2010, Win7 64). What is your exact nvcc command line, and what errors messages are emitted by the compiler?

```auto
template<typename Dtype>
 __global__ void addKernel()
 {
 }

 template<typename Dtype>
 void hostfun(Dtype *in, Dtype *out)
 {
     addKernel<Dtype> <<<1, 512>>>();
 }

 int main(int argc, char ** argv)
 {
     float *a = 0, *b = 0;
     hostfun(a, b);
 }

```

---

<div class="post-metadata">

**Author:** ![hugowangyj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@hugowangyj](https://forums.developer.nvidia.com/u/hugowangyj)\
**Post date:** [October 30, 2015, 2:28am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/3 "2015-10-30T02:28:07Z")

</div>

Hi,njuffa  
points initialization don’t resolve my problem. The error messages are:  
syntax error:missing ‘;’ before ‘constant’  
syntax error:‘\>’  
both on line 9(the same line as code “addKernel\<\<\<1,512\>\>\>()”).

If I initialtize addKernel directly in main function, it is ok.If I compile the above codes on vs2012, it is ok.

The nvcc command lines are as follows:  
error MSB3721: The command ““C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\bin\nvcc.exe” -gencode=arch=compute\_20,code="sm\_20,compute\_20" --use-local-env --cl-version 2013 -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86\_amd64” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\include” -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\include" --keep-dir x64\Release -maxrregcount=0 --machine 64 --compile -cudart static -DWIN32 -DWIN64 -DNDEBUG -D\_CONSOLE -D\_MBCS -Xcompiler “/EHsc /W3 /nologo /O2 /Zi /MD " -o x64\Release\kernel.cu.obj “C:\Users\dmsuvorov\Documents\Visual Studio 2013\Projects\cuda\_test\cuda\_test\kernel.cu”” exited with code 2.

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [October 30, 2015, 2:53am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/4 "2015-10-30T02:53:21Z")

</div>

Your original post stated that you are using CUDA 7.0, but the compilation log above shows that the compiler from CUDA 6.5 is being used.

Is it possible your compiler configuration is corrupted? Mixing files from different CUDA releases can definitely cause weird compiler problems as I know from personal experience.

---

<div class="post-metadata">

**Author:** ![hugowangyj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@hugowangyj](https://forums.developer.nvidia.com/u/hugowangyj)\
**Post date:** [October 30, 2015, 3:31am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/5 "2015-10-30T03:31:16Z")

</div>

I tried both cuda 6.5 and 7.0 without success.

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [October 30, 2015, 3:47am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/6 "2015-10-30T03:47:52Z")

</div>

As I stated, I performed my experiment with CUDA 7.5, so you might want to try that. (I migrated straight from CUDA 6.5 to CUDA 7.5, as CUDA 7.0 seemed to have too many bugs).

---

<div class="post-metadata">

**Author:** ![hugowangyj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@hugowangyj](https://forums.developer.nvidia.com/u/hugowangyj)\
**Post date:** [October 30, 2015, 9:23am UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/7 "2015-10-30T09:23:45Z")

</div>

I will give it a try, but I am afraid it is still not the root reason!  
Anyway, thank you, njuffa.

---

<div class="post-metadata">

**Author:** ![episteme](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/episteme/32/10803_2.png) [@episteme](https://forums.developer.nvidia.com/u/episteme)\
**Post date:** [October 30, 2015, 12:28pm UTC](https://forums.developer.nvidia.com/t/template-cuda-kernel-function-cannot-be-called-in-another-template-function-on-vs2013/40065/8 "2015-10-30T12:28:21Z")

</div>

On my environment (Win10 64bit, Visual Studio 2013), successfully compiled with both CUDA 7.0 & 7.5
