Template Specialization - 2022.1 English

Vitis Model Composer User Guide (UG1483)

Document ID
UG1483
Release Date
2022-05-26
Version
2022.1 English

For cases when you need to override the default template implementation to handle a particular type in a different way, Vitis Model Composer supports template specialization. Consider the following example where a class MyClass has two different interfaces than the generic MyClass. One specialized version is declared to implement the cint16 datatype and other version to implement the uint32 datatype.

template_specialization.h

#include <adf.h>
template<typename T,int N>
class MyClass
{
};
 
template<>
class MyClass<cint16,1> {
   int m_count;
   int16 var_1;
   int16 var_2;
   int16 var_3;
   uint16 var_4;
public:
   MyClass();
   MyClass(int16 q_var1,int16 q_var2,int16 q_var3,uint16 q_var4);
   MyClass(int16 q_var1,int16 q_var2);
   MyClass(int16 q_var1,int16 q_var2,int16 q_var3);
    
   void func_mem(input_stream<cint16>  *i1,
                 output_stream<cint16> *o1,
                 output_stream<int> *o2);
 
    static void registerKernelClass()
    {
        REGISTER_FUNCTION(MyClass::func_mem);
    }
    
};
 
template<>
class MyClass<uint32,2> {
   int m_count;
   int16 var;
public:
   MyClass(uint32 q_var1);
       
   void func_mem(input_stream<uint32>  *i1,
                 output_stream<uint32> *o1,
                 output_stream<int> *o2);
 
    static void registerKernelClass()
    {
        REGISTER_FUNCTION(MyClass::func_mem);
    }
    
};

You can see that two functions are registered separately in two specialized classes. When you try to import the kernel func_mem as a block into Model Composer using the AIE Class kernel block, the Kernel Class tab in block GUI parameters looks as shown.

Figure 1. Class Variant

After selecting one of the Kernel Class Variants from the list, the Class Template Parameters update accordingly. The list of Kernel Class Constructors for the corresponding class variant, is updated and you can select from the list (see the following figure).

Figure 2. Kernel Class Constructors
Note: MATLAB variables can be used to specify the values of Kernel Class Constructor Parameters.