cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MicroZed Chronicles: OpenCL: Creating the Kernel and Host Integration

xtech-blogs
Xilinx Employee
Xilinx Employee
0 0 330

Editor’s Note: This content is republished from the MicroZed Chronicles, with permission from the author.

 

Last week we looked at how to create a Vitis application that would detect the OpenCL platform and identify the OpenCL devices available within the platform. In this blog, we are going to examine how we can create a simple kernel application and then complete the rest of the host application so that we get an executable application.

To get started creating the kernel, we need to select the src directory under the kernels folder in the explorer and create a new file.

The code to be created is simple. For instructional purposes, it is simple input which is multiplied by the iteration of the loop and returned. All of the source will be made available on my GitHub.

377_Fig1.png

Once this is completed, we need to update the host application in order to load and execute the kernel. Last week we identified the platform and devices in addition to creating the context and the command queue.

Now that we have a kernel, we can start developing the remainder of the host application. To complete the application, we need to do the following.

  1. The kernel program is precomputed and not compiled on the fly due to the long compilation times of programmable logic, so we need to load in the precompiled XCL binary. The XCL binary to load can be an argument passed to the host program at run time.
  2. 377_Step1.pngOnce the XCL binary is loaded, we need to create a program object and the XCL binary is then loaded into the program object377_Step2.png
  3. Create a kernel which uses the program object previously loaded.
  4. 377_Step3.pngCreate the buffers to be able to transfer data to and from the kernel. These buffers need to be correctly sized because we use the size of the arguments. We create two buffers, one for each argument passed to the kernel.
  5. 377_Step4.pngWith the kernel and the buffers defined, the next step is to associate the kernel arguments with a buffer.
  6. 377_Step5.pngWe need to map them to pointers to be able to read and write the buffers.
  7. 377_Step6.pngWe can then use the pointer to create the input data using a simple loop to initialize all the input values.
  8. With the input data ready, we are now able to move the ip data from the host memory to the kernel, run the kernel, and move the results back from the kernel to the host.
  9. 377_Step8.pngFinally, we need to unmap the pointers from the buffers and analyze the results to make sure the algorithm behaved as expected.

377_Step9.png

 

The completed application for both the host and kernel is available on my GitHub. Once these are in place, we need to make sure we have set up the kernel and linker projects settings to include the kernel in the binary container.

377_Fig2.png

 

377_Fig3.png

We are now ready to build the application and run the application on the host. This will load the kernel on the Alveo U50 card and execute it. Once completed, we should see if the example has run successfully or not in the terminal window.

377_Fig4.png

Now we know exactly how we can create a kernel and integrate it within a host application using OpenCL and the Vitis acceleration flow. This will be a useful reference when we develop applications using Vitis on both acceleration cards and edge-based heterogeneous SoCs.