aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/tests/seektest.c
blob: cc59874aeaa230d851f5f4a3ea6818610e3bfc37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <gfsm.h>
#include <gfsmIndexed2.h>
#include <stdio.h>
#include <stdlib.h>
#include "labprobs.h"

/*======================================================================
 * Globals
 */
const char *prog = "seektest";

const char *fsmfile   = "tagh-chopped.gfst";
//const char *xfsmfile  = "tagh-lo.gfstx";

gfsmStateId qid_test = 0;
guint       out_degree_test = 0;
gulong count_test = 
//1024
//1048576
4194304
//16777216
;

//#define BENCH_SORTED 1
#undef BENCH_SORTED

/*======================================================================
 * bench_seek_vanilla()
 */
double bench_seek_vanilla(gfsmAutomaton *fsm) {
  guint i;
  double elapsed;
  GPtrArray *ary = g_ptr_array_sized_new(gfsm_automaton_out_degree(fsm,qid_test));
  GTimer *timer  = g_timer_new();

  g_timer_start(timer);
  for (i=0; i < count_test; i++) {
    //-- BEGIN TEST CODE
    gfsmStateId qid = qid_test;
    gfsmLabelId lab = g_array_index(seekus,gfsmLabelId,i);
    gfsmArcIter ai;
    ary->len=0;
    for (gfsm_arciter_open(&ai,fsm,qid), gfsm_arciter_seek_lower(&ai,lab);
	 gfsm_arciter_ok(&ai);
	 gfsm_arciter_next(&ai), gfsm_arciter_seek_lower(&ai,lab))
      {
	gfsmArc *a = gfsm_arciter_arc(&ai);
	if (fsm->flags.sort_mode==gfsmASMLower && a->lower!=lab) break;
	g_ptr_array_add(ary, a);
      }
    //-- END TEST CODE
  }
  elapsed = g_timer_elapsed(timer,NULL);

  //-- cleanup
  g_ptr_array_free(ary,TRUE);
  g_timer_destroy(timer);

  return elapsed;
}

/*======================================================================
 * indexed_lower_lb()
 */
gfsmArcId indexed_lower_lb(gfsmIndexedAutomaton *fsm,
			   gfsmLabelId lab,
			   gfsmArcId aid_lo,
			   gfsmArcId aid_hi)

{
  /*
  gfsmArcId aid_mid;
  gfsmArc   *a;
  */

  //if (!gfsm_indexed_automaton_has_state(fsm,qid)) return gfsmNoArc;
  /*
  aid_lo = g_array_index(fsm->state_first_arc, gfsmArcId, qid);
  aid_hi = g_array_index(fsm->state_first_arc, gfsmArcId, qid+1);
  */

  while (aid_lo < aid_hi) {
    gfsmArcId aid_mid = (aid_lo+aid_hi)/2;
    gfsmArc        *a = &g_array_index(fsm->arcs, gfsmArc, g_array_index(fsm->arcix_lower, gfsmArcId, aid_mid));
    if (a->lower < lab) { aid_lo = aid_mid+1; }
    else                { aid_hi = aid_mid; }
  }
  //return aid_lo <= aid_hi ? aid_lo : gfsmNoArc;
  return aid_lo;
}


/*======================================================================
 * bench_seek_indexed()
 */
#ifndef SEEK_INDEXED_BINSEARCH_CUTOFF
//#define SEEK_INDEXED_BINSEARCH_CUTOFF 0
//#define SEEK_INDEXED_BINSEARCH_CUTOFF 4
//#define SEEK_INDEXED_BINSEARCH_CUTOFF 8
//#define SEEK_INDEXED_BINSEARCH_CUTOFF 16
//#define SEEK_INDEXED_BINSEARCH_CUTOFF 32
#define SEEK_INDEXED_BINSEARCH_CUTOFF 64
#endif
double bench_seek_indexed(gfsmIndexedAutomaton *fsm) {
#if 1
  guint i;
  double elapsed;
  GPtrArray *ary = g_ptr_array_sized_new(gfsm_indexed_automaton_out_degree(fsm,qid_test));
  GTimer *timer  = g_timer_new();

  g_timer_start(timer);
  for (i=0; i < count_test; i++) {
    //-- BEGIN TEST CODE
    gfsmStateId qid = qid_test;
    gfsmLabelId lab = g_array_index(seekus,gfsmLabelId,i);
    gfsmArcId aid_lo = g_array_index(fsm->state_first_arc, gfsmArcId, qid);
    gfsmArcId aid_hi = g_array_index(fsm->state_first_arc, gfsmArcId, qid+1);
    gfsmArcId aid;
    gfsmArc *a;
    ary->len=0;
    if (aid_hi-aid_lo >= SEEK_INDEXED_BINSEARCH_CUTOFF) {
      for (aid=indexed_lower_lb(fsm,lab,aid_lo,aid_hi); aid<aid_hi; aid++) {
	a = &g_array_index(fsm->arcs, gfsmArc, g_array_index(fsm->arcix_lower, gfsmArcId, aid));
	if (a->lower!=lab) break;
	g_ptr_array_add(ary, a);
      }
    } else {
      for (a=((gfsmArc*)fsm->arcs->data)+aid_lo; a < ((gfsmArc*)fsm->arcs->data)+aid_hi; a++) {
	if (a->lower==lab) g_ptr_array_add(ary,a);
      }
    }
    //-- END TEST CODE
  }
  elapsed = g_timer_elapsed(timer,NULL);

  //-- cleanup
  g_ptr_array_free(ary,TRUE);
  g_timer_destroy(timer);

  return elapsed;
#else
  return 1e38; //-- dummy
#endif
}


/*======================================================================
 * indexed2_lower_lb()
 */
gfsmArcId indexed2_lower_lb(gfsmIndexedAutomaton2 *fsm,
			    gfsmLabelId lab,
			    gfsmArcId aid_lo,
			    gfsmArcId aid_hi)

{
  while (aid_lo < aid_hi) {
    gfsmArcId aid_mid = (aid_lo+aid_hi)/2;
    gfsmArc        *a = g_ptr_array_index(fsm->arcix_lower, aid_mid);
    if (a->lower < lab) { aid_lo = aid_mid+1; }
    else                { aid_hi = aid_mid; }
  }
  //return aid_lo <= aid_hi ? aid_lo : gfsmNoArc;
  return aid_lo;
}


/*======================================================================
 * bench_seek_indexed2()
 */
double bench_seek_indexed2(gfsmIndexedAutomaton2 *fsm) {
  guint i;
  double elapsed;
  GPtrArray *ary = g_ptr_array_sized_new(gfsm_indexed_automaton2_out_degree(fsm,qid_test));
  GTimer *timer  = g_timer_new();

  g_timer_start(timer);
  for (i=0; i < count_test; i++) {
    //-- BEGIN TEST CODE
    gfsmStateId qid  = qid_test;
    gfsmLabelId lab  = g_array_index(seekus,gfsmLabelId,i);
    gfsmArcId aid_lo = g_array_index(fsm->state_first_arc, gfsmArcId, qid);
    gfsmArcId aid_hi = g_array_index(fsm->state_first_arc, gfsmArcId, qid+1);
    gfsmArcId aid;
    gfsmArc **app;
    ary->len=0;
    if (aid_hi-aid_lo >= SEEK_INDEXED_BINSEARCH_CUTOFF) {
      for (app = ((gfsmArc**)(fsm->arcix_lower->pdata)) + indexed2_lower_lb(fsm,lab,aid_lo,aid_hi);
	   app < ((gfsmArc**)(fsm->arcix_lower->pdata)) + aid_hi && (*app)->lower==lab;
	   app++)
	{
	  g_ptr_array_add(ary, (*app));
	}
    } else {
      for (app = ((gfsmArc**)(fsm->arcix_lower->pdata)) + aid_lo;
	   app < ((gfsmArc**)(fsm->arcix_lower->pdata)) + aid_hi;
	   app++)
	{
	  if ((*app)->lower==lab) g_ptr_array_add(ary,(*app));
	}
    }
    //-- END TEST CODE
  }
  elapsed = g_timer_elapsed(timer,NULL);

  //-- cleanup
  g_ptr_array_free(ary,TRUE);
  g_timer_destroy(timer);

  return elapsed;
}


/*======================================================================
 * Report
 */
GString *dat_header=NULL;
GString *dat_data=NULL;
gint     dat_row=0;
gint     dat_col=0;

void report_new_row(void) {
  fprintf(stderr, "%s: qid=%u, out_degree=%u\n", prog, qid_test, out_degree_test);
  //
  //-- save data for gnuplot output
  dat_col=0;
  if (!dat_header) dat_header = g_string_new("");
  if (!dat_data)   dat_data   = g_string_new("");
  if (dat_row==0) {
    g_string_append(dat_header,"#1:out_deg");
  }
  g_string_append_printf(dat_data, "%u", out_degree_test);
}

void report_column(char *label, double elapsed) {
  double iters_per_sec = ((double)count_test)/elapsed;
  //
  //-- to stderr
  fprintf(stderr, "BENCH[%16s]: %ld iters in %.2g sec: %.2g iters/sec\n",
	  label, count_test, elapsed, iters_per_sec);
  fflush(stderr);
  //
  //-- to data strings
  if (dat_row==0) {
    g_string_append_printf(dat_header, "\t%d:%s_secs\t%d:%s_ips", (2*dat_col+2),label, (2*dat_col+3),label);
  }
  g_string_append_c(dat_data,'\t');
  g_string_append_printf(dat_data,"\t%g\t%g", elapsed,iters_per_sec);
  ++dat_col;
}

void report_end_row(void) {
  ++dat_row;
  g_string_append(dat_data,"\n");
}

void report_gnuplot(void) {
  fflush(stderr);
  printf("%s\n%s", dat_header->str, dat_data->str);
}


/*======================================================================
 * Main
 */
int main(int argc, char **argv)
{
  char *qid_str="0";
  gfsmError *err=NULL;
  int argi;
  //
  gfsmAutomaton *fsm=NULL;
  double elapsed_vanilla;
#ifdef BENCH_SORTED
  gfsmAutomaton *fsm_sorted=NULL;
  double elapsed_sorted;
#endif
  gfsmIndexedAutomaton *xfsm=NULL;
  double elapsed_indexed;
  gfsmIndexedAutomaton2 *xfsm2=NULL;
  double elapsed_indexed2;

  //-- sanity check
  if (argc < 2) {
    fprintf(stderr, "Usage: %s [QID(s)...]\n", prog);
    exit(1);
  }

  //-- load probabilities & initialize
  load_label_probs();
  populate_seek_labels();

  //-- report
  fprintf(stderr, "%s: count=%lu\n", prog, count_test);
  fflush(stderr);

  //-- load/create: vanilla
  fprintf(stderr, "%s: loading vanilla automaton '%s'... ", prog, fsmfile); fflush(stderr);
  fsm = gfsm_automaton_new();
  if (!gfsm_automaton_load_bin_filename(fsm,fsmfile,&err)) {
    fprintf(stderr,"%s: load failed for '%s': %s\n", prog, fsmfile, (err ? err->message : "?"));
    exit(3);
  }
  fsm->flags.sort_mode = gfsmASMNone;
  fprintf(stderr, "loaded.\n"); fflush(stderr);

#ifdef BENCH_SORTED
  //-- load/create: sorted
  fprintf(stderr, "%s: sorting... ", prog); fflush(stderr);
  fsm_sorted = gfsm_automaton_clone(fsm);
  gfsm_automaton_arcsort(fsm_sorted,gfsmASMLower);
  fprintf(stderr, "sorted.\n"); fflush(stderr);
#endif

  //-- load/create: indexed
  fprintf(stderr, "%s: indexing... ", prog); fflush(stderr);
  xfsm = gfsm_automaton_to_indexed(fsm,NULL);
  fprintf(stderr, "indexed.\n"); fflush(stderr);

  //-- load/create: indexed2
  fprintf(stderr, "%s: indexing(2)... ", prog); fflush(stderr);
  xfsm2 = gfsm_automaton_to_indexed2(fsm,NULL);
  fprintf(stderr, "indexed.\n"); fflush(stderr);

  //-- main loop
  for (argi=1; argi < argc; argi++) {
    qid_str  = argv[argi];
    qid_test = strtol(qid_str,NULL,0);
    out_degree_test = gfsm_automaton_out_degree(fsm,qid_test);

    report_new_row();

    //-- benchmark: vanilla
    elapsed_vanilla = bench_seek_vanilla(fsm);
    report_column("vanilla", elapsed_vanilla);

#ifdef BENCH_SORTED
    //-- benchmark: vanilla+sorted
    gfsm_automaton_arcsort(fsm,gfsmASMLower);
    elapsed_sorted  = bench_seek_vanilla(fsm);
    report_column("sorted", elapsed_sorted);
#endif

    //-- benchmark: indexed
    elapsed_indexed = bench_seek_indexed(xfsm);
    report_column("indexed", elapsed_indexed);

    //-- benchmark: indexed2
    elapsed_indexed2 = bench_seek_indexed2(xfsm2);
    report_column("indexed2", elapsed_indexed2);

    report_end_row();
  }

  //-- gnuplot output
  report_gnuplot();

  //-- cleanup
  if (fsm) gfsm_automaton_free(fsm);
#ifdef BENCH_SORTED
  if (fsm_sorted) gfsm_automaton_free(fsm_sorted);
#endif
  if (xfsm) gfsm_indexed_automaton_free(xfsm);

  return 0;
}