Graph Framework
Loading...
Searching...
No Matches
Embedding in C code

Documentation for linking into a C code base.

Introduction

This section assumes the reader is already familar with developing C codes. The simplist method to link framework code into a C code is to create a C++ function with

extern "C"

First create a header file c_callable.h

extern "C" {
void c_callable_function();
}

Next create a source file c_callable.c and add the framework. This example uses the equation of a line example from the making workflows turorial.

// Include the necessary framework headers.
extern "C" {
void c_callable_function() {
auto x = graph::variable(3, "x");
// Define explicit constant.
auto m = graph::constant<T> (0.4);
// Define implicit constant.
const T b = 0.6;
// Equation of a line
auto y = m*x + b;
// Auto differentiation.
auto dydx = y->df(x);
x->set({1.0, 2.0, 3.0});
// Create a workflow manager.
work.add_item({
}, {
y, dydx
}, {}, NULL, "my_first_kernel", 3);
work.compile();
work.run();
work.print(0, {x, y, dydx});
work.print(1, {x, y, dydx});
work.print(2, {x, y, dydx});
}
}
Class representing a workflow manager.
Definition workflow.hpp:171
constexpr shared_leaf< T, SAFE_MATH > zero()
Forward declare for zero.
Definition node.hpp:994
shared_variable< T, SAFE_MATH > variable_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a variable node.
Definition node.hpp:1746
shared_leaf< T, SAFE_MATH > variable(const size_t s, const std::string &symbol)
Construct a variable.
Definition node.hpp:1674

C Binding Interface

An alternative is to use the C Language interface. The C binding interface can be enabled as one of the cmake conifgure options. As an example, we will convert the making workflows turorial to use the C language bindings.

void c_binding() {
const bool use_safe_math = 0;
struct graph_c_context *c_context = graph_construct_context(DOUBLE, use_safe_math);
graph_node x = graph_variable(c_context, 3, "x");
graph_node m = graph_constant(c_context, 0.4);
graph_node b = graph_constant(c_context, 0.6);
graph_node y = graph_add(c_context, graph_mul(c_context, m, x), b);
graph_node dydx = graph_df(c_context, y, x);
double temp[3];
temp[0] = 1.0;
temp[1] = 2.0;
temp[2] = 3.0;
graoh_set_variable(c_context, x, temp);
graph_set_device_number(c_context, 0);
graph_node inputs[1];
inputs[0] = x;
graph_node outputs[2];
outputs[0] = y;
outputs[1] = dydx;
graph_add_item(c_context, inputs, 1, outputs, 2, NULL, NULL, 0, NULL,
"x", 3);
graph_compile(c_context);
graph_run(c_context);
graph_node inputs2[3];
inputs2[0] = x;
inputs2[1] = y;
inputs2[2] = dydx;
graph_print(c_context, 0, inputs2, 3);
graph_print(c_context, 1, inputs2, 3);
graph_print(c_context, 2, inputs2, 3);
}
graph_node graph_constant(STRUCT_TAG graph_c_context *c, const double value)
Create constant node.
Definition graph_c_binding.cpp:164
graph_node graph_add(STRUCT_TAG graph_c_context *c, graph_node left, graph_node right)
Create add node.
Definition graph_c_binding.cpp:498
void graph_print(STRUCT_TAG graph_c_context *c, const size_t index, graph_node *nodes, const size_t num_nodes)
Print a value from nodes.
Definition graph_c_binding.cpp:3118
void graph_compile(graph_c_context *c)
Compile the work items.
Definition graph_c_binding.cpp:2813
void graph_add_item(STRUCT_TAG graph_c_context *c, graph_node *inputs, size_t num_inputs, graph_node *outputs, size_t num_outputs, graph_node *map_inputs, graph_node *map_outputs, size_t num_maps, graph_node random_state, const char *name, const size_t size)
Add workflow item.
Definition graph_c_binding.cpp:2127
graph_node graph_mul(STRUCT_TAG graph_c_context *c, graph_node left, graph_node right)
Create Multiply node.
Definition graph_c_binding.cpp:630
void graph_set_device_number(STRUCT_TAG graph_c_context *c, const size_t num)
Choose the device number.
Definition graph_c_binding.cpp:1729
graph_node graph_variable(STRUCT_TAG graph_c_context *c, const size_t size, const char *symbol)
Create variable node.
Definition graph_c_binding.cpp:99
graph_c_context * graph_construct_context(const enum graph_type type, const bool use_safe_math)
Construct a C context.
Definition graph_c_binding.cpp:40
void graph_run(graph_c_context *c)
Run work items.
Definition graph_c_binding.cpp:2911
graph_node graph_df(STRUCT_TAG graph_c_context *c, graph_node fnode, graph_node xnode)
Take derivative ∂f∂x.
Definition graph_c_binding.cpp:3204
void graph_destroy_context(graph_c_context *c)
Destroy C context.
Definition graph_c_binding.cpp:87
Header file for the c binding library.
void * graph_node
Graph node type for C interface.
Definition graph_c_binding.h:147
graph_c_context type.
Definition graph_c_binding.h:162