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 6030589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:03:30+00:00 2026-05-23T05:03:30+00:00

I’ve been trying to create template kernels but I’m been having some trouble calling

  • 0

I’ve been trying to create template kernels but I’m been having some trouble calling them in my program. I have a Matrix<T> template class, and some methods defined inside it

Matrix.h:

template <typename T> class Matrix {
    ...
    void sum(Matrix<T>& m1, Matrix<T>& m2, Matrix<T>& sum);
    ...
}

#include "Matrix.cu"

Matrix.cu:

#include "MatrixKernel.h"

template<typename T> void Matrix<T>::sum(const Matrix<T>& m, Matrix<T>& sum) {
    ...
    sumKernel<T><<<dimGrid, dimBlock>>>(Matrix<T> m1, Matrix<T> m2, Matrix<T> sum)
    ...
}

MatrixKernel.h:

template<typename T> __global__ void sumKernel(const Matrix<T> m1, const Matrix<T> m2, Matrix<T> sum) {
...
}

The problem is that when I call sumKernel from inside of sum, the compiler gives me the following error:

error C2059: syntax error : '<'

Does somebody know what’s going on? The code compiled fine just before I included the sumKernel call.

Thanks.

  • 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-23T05:03:31+00:00Added an answer on May 23, 2026 at 5:03 am

    So, it seems you do have a strange #include, leading to code getting compiled by the wrong compiler. Make a distinction between gpu headers and cpu headers by using .cu.h for cuda headers. Make sure only NVCC compiles .cu and .cu.h files. Cuda files should never be included in cpp files. The kernel and kernel call should be in a .cu or .cu.h files, and those files shouldn’t be included anywhere in cpps.

    Because your .cu is being included in a header which is being compiled by the host compiler, the host compiler ends up hitting the token <<< – which it doesn’t recognise. It probably does understand the token << so it consumes that, leaving an unexpected <.

    Here’s an alternative way of doing things that should work (not tried it but it’s similar to code we use)

    (note, this might work but it also might not be the right way to solve the problem. My boss doesn’t like it as a solution and would prefer to add an implementation per variation)

    The underlying problem seems to be lack of distinction between host and device code. I’m leaving the detail out in my solution – things like copying results to and from the device, sum implementation, etc.

    The problem I’m trying to solve is, given a construct, how can you template it for use both on the host and the device?

    I’ll template Matrix.h on both the type and the implementation detail.

     template <typename T, typename Implementation<T> > class Matrix {
         void sum(Matrix<T>& m1, Matrix<T>& m2, Matrix<T>& sum)
         {
             Implementation.sumImp(m1, m2, sum);
         }
     }
    

    The host implementation, HostMatrixSum.h will do things the on the cpu:

     #include "Matrix.h"
    
     template <typename T> struct HostMatrixSum
     {
         void sumImp(Matrix<T>& m1, Matrix<T>& m2, Matrix<T>& sum)
         {
             ...
         }
     }
    

    While GpuMatrixSum.cu.h will upload the matrix, do the sum and recover the results:

     #include "Matrix.h"
    
     template <typename T> struct GpuMatrixSum
     {   
         template<typename T> __global__ void sumKernel(const Matrix<T> m1, const Matrix<T> m2, Matrix<T> sum)
         {
             ...
         }
    
         void sumImp(Matrix<T>& m1, Matrix<T>& m2, Matrix<T>& sum)
         {
             ...
             sumKernel<T> <<< dimGrid, dimBlock >>> (m1,m2);
             ...
         }
     }
    

    Then when we come to use Matrix from host code we template on the host sum implementation and never need to see any cuda specifics:

     #include "Matrix.h"
     #include "HostMatrixSum.h"
    
     Matrix<int, HostMatrixSum> m1 = Matrix<int>(...);
     Matrix<int, HostMatrixSum> m2 = Matrix<int>(...);
     Matrix<int, HostMatrixSum> result;
     Matrix.sum(m1,m2,result);
    

    And if we’re working on the gpu we can use the accelerated gpu implementation of sum:

     #include "Matrix.h"
     #include "GpuMatrixSum.cu.h"
    
     Matrix<int, GpuMatrixSum> m1 = Matrix<int>(...);
     Matrix<int, GpuMatrixSum> m2 = Matrix<int>(...);
     Matrix<int, GpuMatrixSum> result;
     Matrix.sum(m1,m2,result);
    

    Hope that works for you!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.