Prepare the Kernels - 2023.2 English

AI Engine Kernel and Graph Programming Guide (UG1079)

Document ID
UG1079
Release Date
2023-12-04
Version
2023.2 English

Kernels are computation functions that form the fundamental building blocks of the data flow graph specifications. Kernels are declared as ordinary C/C++ functions that return void and can use special data types as arguments. Each kernel must be defined in its own source file. This organization is recommended for reusability and faster compilation. Furthermore, the kernel source files should include all relevant header files to allow for independent compilation.

Note: For the AI Engine kernel to use the AI Engine API, include the following files in the kernel source code:
  • #include "aie_api/aie.hpp"
  • #include "aie_api/aie_adf.hpp"

It is recommended that a header file (kernels.h in this documentation) declares the function prototypes for all kernels used in a graph. An example is as follows.

#ifndef FUNCTION_KERNELS_H
#define FUNCTION_KERNELS_H

void simple(input_buffer<int16> & __restrict in, 
            output_buffer<int16> & __restrict out);

#endif

In the example, the #ifndef and #endif are present to ensure that the include file is only included once, which is good C/C++ practice.

The kernels.cc file is the implementation file for the simple function. The kernel implementation uses two int 16 variables in_t and out_t, which point to the underlying values of input and output buffer respectively using the data() member function..


/* A simple kernel */
#include <adf.h>
#include "kernels.h"

void simple(input_buffer<int16> & __restrict in,
            output_buffer<int16> & __restrict out)
{
    int16* __restrict in_t = in.data();
    int16* __restrict out_t = out.data();
    for (unsigned i=0; i<NUM_SAMPLES; i++) {
        *out_t++ = 1 + *in_t++;
    }
}