Kernel with Class Templates - 2024.1 English

Vitis Model Composer User Guide (UG1483)

Document ID
UG1483
Release Date
2024-05-30
Version
2024.1 English

You may require a class implementation that remains the same for all classes but the data types vary. Vitis Model Composer supports importing the kernels with class templates using the AIE Class Kernel block. Consider the following example which declares the class template in kernel.h.

kernel.h

#ifndef _AIE_CLASS_KERNELS_H_
#define _AIE_CLASS_KERNELS_H_
#include <adf.h>
 
template<typename T, int N>
class MyKernel {
   int m_count;
public:
   MyKernel();
   void myFunc(input_stream<T>  *i1,
               output_stream<T> *o1,
               output_stream<int> *o2);
 
    static void registerKernelClass()
    {
        REGISTER_FUNCTION(MyKernel::myFunc);
    }
    
};
#endif

In this case, the default constructor initializer m_count(N) initializes m_count with template parameter N as shown in te following kernel.cpp code.

kernel.cpp

#include "kernel.h"
 
template<typename T, int N>
MyKernel<T,N>::MyKernel()
   : m_count(N)
{
}
 
template<typename T, int N>
void
MyKernel<T,N>::myFunc(input_stream<T>  *i1,
                      output_stream<T> *o1,
                      output_stream<int> *o2)
{
   put_ms(0, get_ss(0) * N);
   ++m_count;
   writeincr(o2, m_count);
}

After successfully importing the kernel with class template using the AIE Class Kernel block, the Function tab displays. Here you can enter appropriate values in the user-editable configuration parameters. Click Apply to see the updated interface in the Block Parameters dialog box the AIE Class Kernel block.

Redirect to the Kernel Class tab in the Block Parameters dialog box to review the template class declaration from the kernel class variant. In the Kernel Class tab, you can enter the value of a template type parameter 'T' and a template non-type parameter of integral type as shown.

Figure 1. Kernel Class Template
Important:
  1. Template type parameters can be any valid window, stream, or RTP datatypes.
  2. Only a value that has an integral type is supported for template non-type parameters.
  3. MATLAB variables can be used to specify non-type template parameters.

Template Specialization

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 Vitis Model Composer using the AIE Class kernel block, the Kernel Class tab in block GUI parameters looks as shown.

Figure 2. 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 3. Kernel Class Constructors
Note: MATLAB variables can be used to specify the values of Kernel Class Constructor Parameters.

Template Partial Specialization

For cases where you write a template that specializes one template parameter and still allows some parameterization, you can use the template partial specialization. Vitis Model Composer allows you to import the class kernels with partial specialization using the AIE Class Kernel block. Consider the following example where a class class_a is partially specialized with a non-type template parameter.

partial_specialization.h

#include <adf.h>
template<typename T,int N>
class class_a
{
};
 
template<typename T>
class class_a<T,2> {
   int m_count;
   T var;
public:
   class_a(T q_var1);
       
   void func_mem(input_stream<T>  *i1,
                      output_stream<T> *o1,
                      output_stream<int> *o2);
 
    static void registerKernelClass()
    {
        REGISTER_FUNCTION(class_a::func_mem);
    }
    
};

Notice that the function func_mem is registered in registerKernelClass() method. When you try to import the kernel function as a block into Model Composer using the AIE Class Kernel block, the Kernel Class tab in the Block Parameters dialog box looks as shown.

Figure 4. Block Parameters: AIE Class Kernel

Because the class is partially specialized with a non-type template parameter, you cannot edit the parameter 'N' from the Kernel Class Template Parameters. However, the value of the template type parameter can be a modified to any valid datatype.