diff --git a/quad/computation_graph/src/computation_graph.c b/quad/computation_graph/src/computation_graph.c index d64fa753f1e1d287e8bc55717a8f3b7ce6d27435..ac02dc324a23598cc879d36ee6e62fb4b1d3dba2 100644 --- a/quad/computation_graph/src/computation_graph.c +++ b/quad/computation_graph/src/computation_graph.c @@ -121,18 +121,23 @@ void graph_compute_node(struct computation_graph *graph, int node_id) { int export_dot(const struct computation_graph* graph, FILE* of) { fprintf(of, "digraph G {\n"); // Header + fprintf(of, "rankdir=\"LR\"\n"); // Horizontal layout // Draw all the nodes and their inputs int i; for (i = 0; i < graph->n_nodes; i++) { struct graph_node *node = &graph->nodes[i]; // Create node - fprintf(of, "\"%s\"[label=\"", node->name); - fprintf(of, "Name: %s\n", node->name); // Node name first + fprintf(of, "\"%s\"[shape=record\nlabel=\"\n", node->name); + fprintf(of, "<f0> %s\n", node->name); // Node name is port 0 int j; - // Add parameters as labels + // Create ports for inputs + for (j = 0; j < node->type->n_inputs; j++) { + fprintf(of, "|<f%d> --\\>%s\n", j+1, node->type->input_names[j]); + } + // Create ports for parameters for (j = 0; j < node->type->n_params; j++) { - fprintf(of, "%s=%.3f\n", node->type->param_names[j], node->param_values[j]); + fprintf(of, "|<f%d> [%s=%.3f]\n", j+1+node->type->n_inputs, node->type->param_names[j],node->param_values[j]); } fprintf(of, "\"]\n"); // Close label bracket // @@ -140,26 +145,9 @@ int export_dot(const struct computation_graph* graph, FILE* of) { struct graph_node* src_node = &graph->nodes[node->input_srcs[j].controller_id]; int output_id = node->input_srcs[j].controller_output; const char* output_name = src_node->type->output_names[output_id]; - fprintf(of, "\"%s\" -> \"%s\" [label=\"%s=%.3f\"]\n", src_node->name, node->name, output_name, src_node->output_values[output_id]); - } - } - /* - // Draw the links between nodes - for (i = 0; i < graph->n_nodes; i++) { - struct graph_node *node = &graph->nodes[i]; - int j; - for (j = 0; j < node->n_inputs; j++) { - // Input is connected if controller ID is not -1 - if (node->input_srcs[j].controller_id != -1) { - struct graph_node* src_node = &graph->nodes[node->input_srcs[j].controller_id]; - int output_id = node->input_srcs[j].controller_output; - const char* output_name = src_node->output_names[output_id]; - // Draws link from source controller to node input block. Also labels the value of the link - fprintf(of, "\"%s\" -> \"%s[%s]\" [label=\"%s=%.3f\"]\n", src_node->name, node->name, node->input_names[j], output_name, src_node->output_values[output_id]); - } + fprintf(of, "\"%s\" -> \"%s\":f%d [label=\"%s=%.3f\"]\n", src_node->name, node->name, j+1, output_name, src_node->output_values[output_id]); } } - */ - fprintf(of, "}"); // Close graph1 + fprintf(of, "}"); // Close graph return 0; } \ No newline at end of file diff --git a/quad/computation_graph/src/main.c b/quad/computation_graph/src/main.c index 761a1ac515d36423276fb0908b73a1ffe82567f3..8be7d30ecdd22650d37e8dfecceebc7baa4f14d0 100644 --- a/quad/computation_graph/src/main.c +++ b/quad/computation_graph/src/main.c @@ -3,7 +3,7 @@ #include "node_add.h" #include "node_mult.h" #include "node_constant.h" -#include "node_pow.h" +#include "node_gain.h" int main() { struct computation_graph *graph = create_graph(); @@ -17,17 +17,21 @@ int main() { graph_set_source(graph, add1_id, ADD_SUMMAND1, const1, CONST_VAL); graph_set_source(graph, add1_id, ADD_SUMMAND2, const2, CONST_VAL); - int pow1_id = graph_add_node_pow(graph, "Pow"); - graph_set_param_val(graph, pow1_id, POW_EXP, 3); - graph_set_source(graph, pow1_id, POW_BASE, add1_id, ADD_SUM); + int gain1_id = graph_add_node_gain(graph, "Gain"); + graph_set_param_val(graph, gain1_id, GAIN_GAIN, 3); + graph_set_source(graph, gain1_id, GAIN_INPUT, add1_id, ADD_SUM); - graph_compute_node(graph, pow1_id); + int mult1_id = graph_add_node_mult(graph, "Mult"); + graph_set_source(graph, mult1_id, MULT_MULTIPLICAND1, gain1_id, GAIN_RESULT); + graph_set_source(graph, mult1_id, MULT_MULTIPLICAND2, const1, CONST_VAL); + + graph_compute_node(graph, mult1_id); FILE* dot_fp; dot_fp = fopen("..\\comp_graph.dot", "w"); export_dot(graph, dot_fp); fclose(dot_fp); - printf("Sum is %f\n", graph_get_output(graph, pow1_id, POW_RESULT)); + printf("Sum is %f\n", graph_get_output(graph, mult1_id, GAIN_RESULT)); fflush(stdout); return 0; } diff --git a/quad/computation_graph/src/node_gain.c b/quad/computation_graph/src/node_gain.c new file mode 100644 index 0000000000000000000000000000000000000000..f89c6f9cb068249af6fc5b958156dd5ae558b4bb --- /dev/null +++ b/quad/computation_graph/src/node_gain.c @@ -0,0 +1,25 @@ +#include "node_gain.h" +#include <stdlib.h> + +static void scale_nodes(void *state, const double* params, const double *inputs, double *outputs) { + outputs[GAIN_RESULT] = inputs[GAIN_INPUT] * params[GAIN_GAIN]; +} +static void reset(void *state) {} + +static const char* const in_names[1] = {"Input"}; +static const char* const out_names[1] = {"Amplified"}; +static const char* const param_names[1] = {"Gain"}; +const struct graph_node_type node_gain_type = { + .input_names = in_names, + .output_names = out_names, + .param_names = param_names, + .n_inputs = 1, + .n_outputs = 1, + .n_params = 1, + .execute = scale_nodes, + .reset = reset +}; + +int graph_add_node_gain(struct computation_graph *graph, const char* name) { + return graph_add_node(graph, name, &node_gain_type, NULL); +} diff --git a/quad/computation_graph/src/node_gain.h b/quad/computation_graph/src/node_gain.h new file mode 100644 index 0000000000000000000000000000000000000000..dd42d97f56711df1b32cfc523de48e2aea0e15ba --- /dev/null +++ b/quad/computation_graph/src/node_gain.h @@ -0,0 +1,20 @@ +#ifndef __NODE_GAIN_H__ +#define __NODE_GAIN_H__ +#include "computation_graph.h" + +int graph_add_node_gain(struct computation_graph *graph, const char* name); + +const extern struct graph_node_type node_gain_type; + +enum graph_node_pow_inputs { + GAIN_INPUT +}; + +enum graph_node_pow_params { + GAIN_GAIN +}; + +enum graph_node_gain_outputs { + GAIN_RESULT +}; +#endif // __NODE_GAIN_H__ \ No newline at end of file diff --git a/quad/computation_graph/src/node_pow.h b/quad/computation_graph/src/node_pow.h index be98299548aaf5147687e4c248383038d1d56f14..cfc8959e2900b2a77c021f69140daeea0dc590fd 100644 --- a/quad/computation_graph/src/node_pow.h +++ b/quad/computation_graph/src/node_pow.h @@ -14,7 +14,7 @@ enum graph_node_pow_params { POW_EXP }; -enum graph_node_add2_outputs { +enum graph_node_pow_outputs { POW_RESULT }; #endif // __NODE_POW_H__ \ No newline at end of file