PandA-2024.02
local_support.c
Go to the documentation of this file.
1 #include "viterbi.h"
2 #include <string.h>
3 
4 int INPUT_SIZE = sizeof(struct bench_args_t);
5 
6 void run_benchmark( void *vargs ) {
7  struct bench_args_t *args = (struct bench_args_t *)vargs;
8  viterbi( args->obs, args->init, args->transition, args->emission, args->path );
9 }
10 
11 /* Input format:
12 %% Section 1
13 tok_t[N_OBS]: observation vector
14 %% Section 2
15 prob_t[N_STATES]: initial state probabilities
16 %% Section 3
17 prob_t[N_STATES*N_STATES]: transition matrix
18 %% Section 4
19 prob_t[N_STATES*N_TOKENS]: emission matrix
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  // Load input string
28  p = readfile(fd);
29 
30  s = find_section_start(p,1);
31  parse_uint8_t_array(s, data->obs, N_OBS);
32 
33  s = find_section_start(p,2);
34  STAC(parse_,TYPE,_array)(s, data->init, N_STATES);
35 
36  s = find_section_start(p,3);
37  STAC(parse_,TYPE,_array)(s, data->transition, N_STATES*N_STATES);
38 
39  s = find_section_start(p,4);
40  STAC(parse_,TYPE,_array)(s, data->emission, N_STATES*N_TOKENS);
41  free(p);
42 }
43 
44 void data_to_input(int fd, void *vdata) {
45  struct bench_args_t *data = (struct bench_args_t *)vdata;
46 
48  write_uint8_t_array(fd, data->obs, N_OBS);
49 
51  STAC(write_,TYPE,_array)(fd, data->init, N_STATES);
52 
54  STAC(write_,TYPE,_array)(fd, data->transition, N_STATES*N_STATES);
55 
57  STAC(write_,TYPE,_array)(fd, data->emission, N_STATES*N_TOKENS);
58 }
59 
60 /* Output format:
61 %% Section 1
62 uint8_t[N_OBS]: most likely state chain
63 */
64 
65 void output_to_data(int fd, void *vdata) {
66  struct bench_args_t *data = (struct bench_args_t *)vdata;
67  char *p, *s;
68  // Zero-out everything.
69  memset(vdata,0,sizeof(struct bench_args_t));
70  // Load input string
71  p = readfile(fd);
72 
73  s = find_section_start(p,1);
74  parse_uint8_t_array(s, data->path, N_OBS);
75  free(p);
76 }
77 
78 void data_to_output(int fd, void *vdata) {
79  struct bench_args_t *data = (struct bench_args_t *)vdata;
80 
82  write_uint8_t_array(fd, data->path, N_OBS);
83 }
84 
85 int check_data( void *vdata, void *vref ) {
86  struct bench_args_t *data = (struct bench_args_t *)vdata;
87  struct bench_args_t *ref = (struct bench_args_t *)vref;
88  int has_errors = 0;
89  int i;
90 
91  for(i=0; i<N_OBS; i++) {
92  has_errors |= (data->path[i]!=ref->path[i]);
93  }
94 
95  // Return true if it's correct.
96  return !has_errors;
97 }
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
int INPUT_SIZE
Definition: local_support.c:4
void data_to_output(int fd, void *vdata)
Definition: local_support.c:63
int write_section_header(int fd)
void output_to_data(int fd, void *vdata)
Definition: local_support.c:49
prob_t transition[N_STATES *N_STATES]
Definition: viterbi.h:33
prob_t emission[N_STATES *N_TOKENS]
Definition: viterbi.h:34
char * readfile(int fd)
Definition: support.c:34
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
state_t path[N_OBS]
Definition: viterbi.h:35
prob_t init[N_STATES]
Definition: viterbi.h:32
int viterbi(tok_t obs[N_OBS], prob_t init[N_STATES], prob_t transition[N_STATES *N_STATES], prob_t emission[N_STATES *N_TOKENS], state_t path[N_OBS])
Definition: viterbi.c:3
#define N_OBS
Definition: viterbi.h:22
int write_uint8_t_array(int fd, uint8_t *arr, int n)
tok_t obs[N_OBS]
Definition: viterbi.h:31
int parse_uint8_t_array(char *s, uint8_t *arr, int n)
void run_benchmark(void *vargs)
Definition: local_support.c:6
#define N_STATES
Definition: viterbi.h:21
#define N_TOKENS
Definition: viterbi.h:23

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