CoreFlow 1.0.0
A modern orchestration and execution runtime
Loading...
Searching...
No Matches
custom_code.cpp

Custom kernel example.

Custom kernel example

Version
0.1
Date
2025-08-10
#include <cstdlib>
#include <iostream>
#include <COREFLOW/all.hpp>
using namespace coreflow;
int main()
{
// 1. Create context
auto context = Context::createContext();
if (Error::getStatus(context) != VX_SUCCESS)
{
std::cerr << "Failed to create context\n";
return EXIT_FAILURE;
}
// 2. Create a graph
auto graph = Graph::createGraph(context);
{
std::cerr << "Failed to create graph\n";
return EXIT_FAILURE;
}
// 3. Create input and output data objects -- in this case, scalars
int32_t a = 7, b = 5;
auto scalar_a = Scalar::createScalar(context, VX_TYPE_INT32, &a);
auto scalar_b = Scalar::createScalar(context, VX_TYPE_INT32, &b);
auto scalar_out = Scalar::createScalar(context, VX_TYPE_INT32, nullptr);
// 4. Register the custom kernel
auto add_kernel = Kernel::registerCustomKernel(
context,
"example.scalar_add",
{
// direction, type, state
},
// Custom kernel function via lambda
[](vx_node node, const vx_reference parameters[], vx_uint32 num) -> vx_status {
(void)node;
(void)num;
int32_t a = 0, b = 0, c = 0;
auto scalar_a = (vx_scalar)parameters[0];
auto scalar_b = (vx_scalar)parameters[1];
auto scalar_out = (vx_scalar)parameters[2];
scalar_a->readValue(&a);
scalar_b->readValue(&b);
c = a + b;
scalar_out->writeValue(&c);
return VX_SUCCESS;
});
if (Error::getStatus(add_kernel) != VX_SUCCESS)
{
std::cerr << "Custom kernel not registered!\n";
return EXIT_FAILURE;
}
// 5. Add node to the graph
auto add_node = Node::createNode(graph, add_kernel, {scalar_a, scalar_b, scalar_out});
if (Error::getStatus(add_node) != VX_SUCCESS)
{
std::cerr << "Failed to create node\n";
return EXIT_FAILURE;
}
// 6. Process the graph
if (graph->process() != VX_SUCCESS)
{
std::cerr << "Graph processing failed\n";
return EXIT_FAILURE;
}
// 7. Read back the result
int32_t result = 0;
scalar_out->readValue(&result);
std::cout << "Result: " << a << " + " << b << " = " << result << std::endl;
// 8. cleanup automatically done when program exits
return EXIT_SUCCESS;
}
CoreVX single-include header for C++ development.
int main()
Definition blur_pipeline.cpp:15
uint32_t vx_uint32
A 32-bit unsigned value.
Definition vx_types.h:85
vx_enum vx_status
A formal status type with known fixed size.
Definition vx_types.h:550
@ VX_TYPE_INT32
A vx_int32.
Definition vx_types.h:440
@ VX_SUCCESS
No error.
Definition vx_types.h:543
static vx_context createContext()
Create a new context.
static vx_status getStatus(vx_reference ref)
Provides a generic API to return status values from Object constructors if they fail.
static vx_graph createGraph(vx_context context)
Create a graph.
static vx_kernel registerCustomKernel(vx_context context, std::string name, const std::vector< Kernel::Param > &params, vx_kernel_f function, vx_kernel_validate_f validate=nullptr, vx_kernel_initialize_f initialize=nullptr, vx_kernel_deinitialize_f deinitialize=nullptr)
Register a custom kernel.
static vx_node createNode(vx_graph graph, vx_kernel kernel)
Create a new node.
static vx_scalar createScalar(vx_context context, vx_enum data_type, const void *ptr)
Create a scalar object.
struct Node * vx_node
An opaque reference to a kernel node.
Definition vx_types.h:253
@ VX_OUTPUT
The parameter is an output only.
Definition vx_types.h:720
@ VX_INPUT
The parameter is an input only.
Definition vx_types.h:718
@ VX_PARAMETER_STATE_REQUIRED
Default. The parameter must be supplied. If not set, during Verify, an error is returned.
Definition vx_types.h:1436
The internal representation of a vx_array.
Definition vx_array.h:34
signed int int32_t
Definition stdint.h:42
struct Reference * vx_reference
Definition vx_types.h:173
struct Scalar * vx_scalar
Definition vx_types.h:208