Kernel with Class Templates - 2022.2 English

Vitis Model Composer User Guide (UG1483)

Document ID
UG1483
Release Date
2023-01-13
Version
2022.2 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.