You can implement a class that stays consistent across all classes, while allowing the data types to 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.
- Template type parameters can be any valid window, stream, or RTP datatypes.
- Only a value that has an integral type is supported for template non-type parameters.
- MATLAB variables can be used to specify non-type template parameters.
Template Specialization
If 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 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.
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 updates, and you can select from the list (see the following figure).
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.
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.