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

  • Home
  • SEARCH
  • 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 7525331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:30:51+00:00 2026-05-30T03:30:51+00:00

I have a filter which is supposed to work on arbitrary tensors. For my

  • 0

I have a filter which is supposed to work on arbitrary tensors. For my case it is sufficient when the filter works on rank 1,2 and 3 tensors which are lists, matrices and 3d-matrices or volumes respectively. Additionally, the filter can be applied in each possible direction. For a list this is only one, for matrices exist 2 possible directions (namely X-direction and y-direction) and for volumes exist 3 possible direction.

Before I go into detail let me ask my question first: Is my layout of the filter ok or did I forgot something important which maybe gives me a hard time later? I’m not new to C++ templates, but it’s not that I feel like a fish in water. Is it possible to compress this layout further (maybe there’s a way around the dummy XDirection classes or a shorter Type2Type)?

The basic procedure of the filter is for every tensor-rank and for every direction the same. There is just a few lines of code, the function callKernel, which are different. To make the overloaded operator() call the right callKernel function is the only interesting part in the code below. Since partial specialization for templates does not work for class-methods, you can convert the template arguments into a real class-type and give this as dummy argument to callKernel.

The following code is they layout up to rank 2. It is compileable with g++ and can be tried.

template <class DataType, int Rank>
class Tensor { };

class XDirection;
class YDirection;

template <class TensorType, class Direction>
struct Type2Type {
  typedef TensorType TT;
  typedef Direction D;
};

template <class TensorType, class Direction>
struct Filter {
  Filter(const TensorType &t){}
  TensorType operator()(){
    /* much code here */
    callKernel(Type2Type<TensorType,Direction>());
    /* more code */
    TensorType result;
    return result;
  } 
  void callKernel(Type2Type<Tensor<double,1>, XDirection>) {} 
  void callKernel(Type2Type<Tensor<double,2>, XDirection>) {}
  void callKernel(Type2Type<Tensor<double,2>, YDirection>) {}
};

int main(void) {
  Tensor<double, 2> rank_two_tensor;
  Filter<Tensor<double,2>,XDirection> f(rank_two_tensor);
  f();
}

Let me add some important things: It is necessary that the filter-logic is in the operator() because what you see here is going used with the Intel Threading Building Blocks which require this structure. It is very important, that the callKernel is inlined. From everything I know, this should be the case.

Thanks in advance for any helpful and critical comment.

  • 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-30T03:30:52+00:00Added an answer on May 30, 2026 at 3:30 am

    First, not bad for a first try on templates.

    If you have a recent version of GCC you can simplify like this, there’s a better way to execute code conditionally on a type using std::is_same<>. It will return true if the types are identical. It also makes your intent more explict.

    #include <type_traits>
    
    template <class TensorType, class Direction> 
    struct Filter { 
      Filter(const TensorType &t) { } 
    
      TensorType operator()(){ 
        /* much code here */ 
        callKernel(); 
        /* more code */ 
        TensorType result; 
        return result; 
      }  
    
      void callKernel() {
          // Check which type is called at compile time (the if will be simplified by the compiler)
          if (std::is_same<TensorType, Tensor<double, 2> >::value) {
              if (std::is_same<Direction, XDirection>::value) {
                  // do stuff
              } else {
                  ...
              }
          } else if (...) {
              ...
          }
      }  
    }; 
    

    Edit: If you wish you can even move it to op() to make sure the code is inlined.

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

Sidebar

Related Questions

I have a filter function which is supposed to check two fields in an
I have an Extension method which is supposed to filter a Queryable object (IQueryable)
I have a text file which I want to filter using awk. The text
I have made a sample application which constructs a filter graph to capture audio
Im having some conflicts with the Searchable plugin. I have a filter which fetches
I have the following code which is supposed to increment a field value by
I have a Service class and an main Acitivity class which is supposed to
I have a filter attribute that I'd like to be applied to every action
I have a Filter Method in my User Class, that takes in a list
I have some filter and one grid.And grid have some buttons like modify/edit.I am

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.