Sure! Here's an example of a kernel that operates over a vector for FPGA using oneAPI and sycl:
kernel.cc
#pragma once
#include "sycl/sycl.hpp"
using namespace sycl;
__global__ void my_vector_operation(const std::vector<float>& input, std::vector<float>& output, int size) {
int index = get_global_id(0);
if (index < size) {
output[index] = input[index] * input[index];
}
}
kernel.h
#ifndef MY_VECTOR_OPERATION_H
#define MY_VECTOR_OPERATION_H
#include <vector>
void my_vector_operation(const std::vector<float>& input, std::vector<float>& output, int size);
#endif // MY_VECTOR_OPERATION_H
main.cc
#include "sycl/sycl.hpp"
#include "kernel.h"
int main() {
int size = 1024;
std::vector<float> input(size, 1.0f);
std::vector<float> output(size, 0.0f);
queue q(device);
my_vector_operation<float, float, int>(input, output, size);
q.submit();
q.wait();
for (int i = 0; i < size; ++i) {
std::cout << input[i] << " * " << input[i] << " = " << output[i] << std::endl;
}
return 0;
}
In this example, we have a kernel my_vector_operation that takes in a vector input, a vector output, and the size of the vector size. The kernel applies an element-wise operation to the input vector and stores the result in the output vector.
The kernel is defined in kernel.cc using the __global__ directive from OpenCL, which indicates that the function should be compiled as a kernel. The kernel takes in the input and output vectors as global memory arrays, and uses the global ID to determine which element of the input vector to process.
The kernel is included in the kernel.h header file, which also defines a function pointer to the kernel.
In the main.cc file, we create a vector input with some initial values, and a vector output with the same size, but all elements set to zero. We create a sycl queue and submit the kernel to the FPGA using the submit function. We wait for the kernel to finish executing using the wait function.
Finally, we print out the results of the kernel operation by iterating over the input and output vectors and printing out the values.