PandA-2024.02
local_support.c
Go to the documentation of this file.
1 #include "backprop.h"
2 #include <string.h>
3 
4 int INPUT_SIZE = sizeof(struct bench_args_t);
5 
6 #define EPSILON (1.0e-6)
7 
8 void run_benchmark( void *vargs ) {
9  struct bench_args_t *args = (struct bench_args_t *)vargs;
10  backprop( args->weights1, args->weights2, args->weights3,
11  args->biases1, args->biases2, args->biases3,
12  args->training_data, args->training_targets );
13 }
14 
15 /* Input format:
16 %% Section 1
17 TYPE[row_size*col_size]: input matrix
18 %% Section 2
19 TYPE[f_size]: filter coefficients
20 */
21 
22 void input_to_data(int fd, void *vdata) {
23  struct bench_args_t *data = (struct bench_args_t *)vdata;
24  char *p, *s;
25  // Zero-out everything.
26  memset(vdata,0,sizeof(struct bench_args_t));
27 
28  // Load input string
29  p = readfile(fd);
30 
31  s = find_section_start(p,1);
32  STAC(parse_,TYPE,_array)(s, data->weights1, input_dimension*nodes_per_layer);
33 
34  s = find_section_start(p,2);
35  STAC(parse_,TYPE,_array)(s, data->weights2, nodes_per_layer*nodes_per_layer);
36 
37  s = find_section_start(p,3);
38  STAC(parse_,TYPE,_array)(s, data->weights3, nodes_per_layer*possible_outputs);
39 
40  s = find_section_start(p,4);
41  STAC(parse_,TYPE,_array)(s, data->biases1, nodes_per_layer);
42 
43  s = find_section_start(p,5);
44  STAC(parse_,TYPE,_array)(s, data->biases2, nodes_per_layer);
45 
46  s = find_section_start(p,6);
47  STAC(parse_,TYPE,_array)(s, data->biases3, possible_outputs);
48 
49  s = find_section_start(p,7);
50  STAC(parse_,TYPE,_array)(s, data->training_data, training_sets*input_dimension);
51 
52  s = find_section_start(p,8);
53  STAC(parse_,TYPE,_array)(s, data->training_targets, training_sets*possible_outputs);
54  free(p);
55 }
56 
57 void data_to_input(int fd, void *vdata) {
58  struct bench_args_t *data = (struct bench_args_t *)vdata;
59 
61  STAC(write_,TYPE,_array)(fd, data->weights1, input_dimension*nodes_per_layer);
62 
64  STAC(write_,TYPE,_array)(fd, data->weights2, nodes_per_layer*nodes_per_layer);
65 
67  STAC(write_,TYPE,_array)(fd, data->weights3, nodes_per_layer*possible_outputs);
68 
70  STAC(write_,TYPE,_array)(fd, data->biases1, nodes_per_layer);
71 
73  STAC(write_,TYPE,_array)(fd, data->biases2, nodes_per_layer);
74 
76  STAC(write_,TYPE,_array)(fd, data->biases3, possible_outputs);
77 
79  STAC(write_,TYPE,_array)(fd, data->training_data, training_sets*input_dimension);
80 
82  STAC(write_,TYPE,_array)(fd, data->training_targets, training_sets*possible_outputs);
83 }
84 
85 /* Output format:
86 %% Section 1
87 TYPE[row_size*col_size]: solution matrix
88 */
89 
90 void output_to_data(int fd, void *vdata) {
91  struct bench_args_t *data = (struct bench_args_t *)vdata;
92  char *p, *s;
93  // Zero-out everything.
94  memset(vdata,0,sizeof(struct bench_args_t));
95  // Load input string
96  p = readfile(fd);
97 
98  s = find_section_start(p,1);
99  STAC(parse_,TYPE,_array)(s, data->weights1, input_dimension*nodes_per_layer);
100 
101  s = find_section_start(p,2);
102  STAC(parse_,TYPE,_array)(s, data->weights2, nodes_per_layer*nodes_per_layer);
103 
104  s = find_section_start(p,3);
105  STAC(parse_,TYPE,_array)(s, data->weights3, nodes_per_layer*possible_outputs);
106 
107  s = find_section_start(p,4);
108  STAC(parse_,TYPE,_array)(s, data->biases1, nodes_per_layer);
109 
110  s = find_section_start(p,5);
111  STAC(parse_,TYPE,_array)(s, data->biases2, nodes_per_layer);
112 
113  s = find_section_start(p,6);
114  STAC(parse_,TYPE,_array)(s, data->biases3, possible_outputs);
115  free(p);
116 
117 }
118 
119 void data_to_output(int fd, void *vdata) {
120  struct bench_args_t *data = (struct bench_args_t *)vdata;
121 
123  STAC(write_,TYPE,_array)(fd, data->weights1, input_dimension*nodes_per_layer);
124 
126  STAC(write_,TYPE,_array)(fd, data->weights2, nodes_per_layer*nodes_per_layer);
127 
129  STAC(write_,TYPE,_array)(fd, data->weights3, nodes_per_layer*possible_outputs);
130 
132  STAC(write_,TYPE,_array)(fd, data->biases1, nodes_per_layer);
133 
135  STAC(write_,TYPE,_array)(fd, data->biases2, nodes_per_layer);
136 
138  STAC(write_,TYPE,_array)(fd, data->biases3, possible_outputs);
139 
140 }
141 
142 int check_data( void *vdata, void *vref ) {
143  struct bench_args_t *data = (struct bench_args_t *)vdata;
144  struct bench_args_t *ref = (struct bench_args_t *)vref;
145  int has_errors = 0;
146  int i, j;
147  TYPE diff;
148 
149  for(i=0; i<input_dimension; i++) {
150  for(j=0; j<nodes_per_layer; j++) {
151  diff = data->weights1[i*nodes_per_layer + j] - ref->weights1[i*nodes_per_layer + j];
152  has_errors |= (diff<-EPSILON) || (EPSILON<diff);
153  }
154  }
155  for(i=0; i<nodes_per_layer; i++) {
156  for(j=0; j<nodes_per_layer; j++) {
157  diff = data->weights2[i*nodes_per_layer + j] - ref->weights2[i*nodes_per_layer + j];
158  has_errors |= (diff<-EPSILON) || (EPSILON<diff);
159  }
160  }
161  for(i=0; i<nodes_per_layer; i++) {
162  for(j=0; j<possible_outputs; j++) {
163  diff = data->weights3[i*possible_outputs + j] - ref->weights3[i*possible_outputs + j];
164  has_errors |= (diff<-EPSILON) || (EPSILON<diff);
165  }
166  }
167  for(i=0; i<nodes_per_layer; i++) {
168  diff = data->biases1[i] - ref->biases1[i];
169  has_errors |= (diff<-EPSILON) || (EPSILON<diff);
170  }
171  for(i=0; i<nodes_per_layer; i++) {
172  diff = data->biases2[i] - ref->biases2[i];
173  has_errors |= (diff<-EPSILON) || (EPSILON<diff);
174  }
175  for(i=0; i<possible_outputs; i++) {
176  diff = data->biases3[i] - ref->biases3[i];
177  has_errors |= (diff<-EPSILON) || (EPSILON<diff);
178  }
179  // Return true if it's correct.
180  return !has_errors;
181 }
#define input_dimension
Definition: backprop.h:7
void data_to_input(int fd, void *vdata)
Definition: local_support.c:34
int check_data(void *vdata, void *vref)
Definition: local_support.c:70
#define TYPE
Definition: backprop.h:21
#define STAC(f_pfx, t, f_sfx)
Definition: support.h:51
TYPE weights1[input_dimension *nodes_per_layer]
Definition: backprop.h:38
int INPUT_SIZE
Definition: local_support.c:4
#define training_sets
Definition: backprop.h:9
TYPE biases3[possible_outputs]
Definition: backprop.h:43
#define possible_outputs
Definition: backprop.h:8
void data_to_output(int fd, void *vdata)
Definition: local_support.c:63
int write_section_header(int fd)
void backprop(TYPE weights1[input_dimension *nodes_per_layer], TYPE weights2[nodes_per_layer *nodes_per_layer], TYPE weights3[nodes_per_layer *possible_outputs], TYPE biases1[nodes_per_layer], TYPE biases2[nodes_per_layer], TYPE biases3[possible_outputs], TYPE training_data[training_sets *input_dimension], TYPE training_targets[training_sets *possible_outputs])
Definition: backprop.c:239
void output_to_data(int fd, void *vdata)
Definition: local_support.c:49
TYPE biases2[nodes_per_layer]
Definition: backprop.h:42
TYPE training_targets[training_sets *possible_outputs]
Definition: backprop.h:45
TYPE weights2[nodes_per_layer *nodes_per_layer]
Definition: backprop.h:39
char * readfile(int fd)
Definition: support.c:34
TYPE biases1[nodes_per_layer]
Definition: backprop.h:41
void input_to_data(int fd, void *vdata)
Definition: local_support.c:18
char * find_section_start(char *s, int n)
Definition: support.c:56
TYPE weights3[nodes_per_layer *possible_outputs]
Definition: backprop.h:40
#define nodes_per_layer
Definition: backprop.h:10
#define EPSILON
Definition: local_support.c:6
TYPE training_data[training_sets *input_dimension]
Definition: backprop.h:44
void run_benchmark(void *vargs)
Definition: local_support.c:6

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13