Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:07:22+00:00 2026-05-26T00:07:22+00:00

I have a templated wrapper function that calls a kernel ( __global__ ) defined

  • 0

I have a templated wrapper function that calls a kernel (__global__) defined in a .cu file like this

template<typename T, class M> 
__global__ void compute_kernel(T* input, T* output, n) {
    M m;
    // compute stuff using m
};

template<typename T, class M> 
void compute(T* input, T* output, int n) {
    // ... compute blocks, threads, etc.
    compute_kernel<T,M> <<<dim_grid, dim_block>>>(input, output, n);
    // ...
};

and a header file to be included in host code that has only the declaration

template<typename T, class M> 
void compute(T* input, T* output, int n);

However, calling compute() from the host with arbitrary template parameters, the compilation fails with undefined reference to 'void reduce(...)' and only if I add specialization declarations to the end of the .cu file does the code compile:

template void
compute<int, Method1<int> >(int* input, int* output, int n);

template void
compute<float, Method1<float> >(float* input, float* output, int n);

template void
compute<int, Method2<int> >(int* input, int* output, int n);

template void
compute<float, Method2<float> >(float* input, float* output, int n);

So, is it necessary to specialize every templated function in order to make it callable from the host? (That’s quite a drawback)

Thanks for your comments!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:07:22+00:00Added an answer on May 26, 2026 at 12:07 am

    This is a C++ FAQ, not limited to CUDA.

    If you have a template implementation in a .cpp or .cu file then when you compile that translation unit the compiler cannot possibly know what permutations of template parameters you will need. Therefore when you link you will get the errors.

    You could put the implementation in a header file (in which case you’ll need to instantiate in a .cu file since it contains CUDA) or you will have to explicitly instantiate all required permutations. If you have to do many of these then you could use a macro to instantiate all your permutations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a templated class defined (in part) as template <class T> MyClass {
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
I have template that looks like this: 100 template<size_t A0, size_t A1, size_t A2,
In C++, can you have a templated operator on a class? Like so: class
I have a dll that contains a templated class. Is there a way to
My situation is the following: I have a template wrapper that handles the situation
I have a templated class template<class U, class V, class W> class S {
I have a C library that has types like this: typedef struct { //
I have a class templated on <PIXEL> , assumed to be one of boost::gil
I have some templated function which has different number of arguments due to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.