Exemple #1
0
/* Create a sorted string_list with one entry per author. The util-field
 * for each author is another string_list which is used to calculate the
 * number of commits per time-interval.
 */
void cgit_show_stats(struct cgit_context *ctx)
{
	struct string_list authors;
	struct cgit_period *period;
	int top, i;
	const char *code = "w";

	if (ctx->qry.period)
		code = ctx->qry.period;

	i = cgit_find_stats_period(code, &period);
	if (!i) {
		cgit_print_error(fmt("Unknown statistics type: %c", code));
		return;
	}
	if (i > ctx->repo->max_stats) {
		cgit_print_error(fmt("Statistics type disabled: %s",
				     period->name));
		return;
	}
	authors = collect_stats(ctx, period);
	qsort(authors.items, authors.nr, sizeof(struct string_list_item),
		cmp_total_commits);

	top = ctx->qry.ofs;
	if (!top)
		top = 10;
	htmlf("<h2>Commits per author per %s", period->name);
	if (ctx->qry.path) {
		html(" (path '");
		html_txt(ctx->qry.path);
		html("')");
	}
	html("</h2>");

	html("<form method='get' action='' style='float: right; text-align: right;'>");
	cgit_add_hidden_formfields(1, 0, "stats");
	if (ctx->repo->max_stats > 1) {
		html("Period: ");
		html("<select name='period' onchange='this.form.submit();'>");
		for (i = 0; i < ctx->repo->max_stats; i++)
			htmlf("<option value='%c'%s>%s</option>",
				periods[i].code,
				period == &periods[i] ? " selected" : "",
				periods[i].name);
		html("</select><br/><br/>");
	}
	html("Authors: ");
	html("");
	html("<select name='ofs' onchange='this.form.submit();'>");
	htmlf("<option value='10'%s>10</option>", top == 10 ? " selected" : "");
	htmlf("<option value='25'%s>25</option>", top == 25 ? " selected" : "");
	htmlf("<option value='50'%s>50</option>", top == 50 ? " selected" : "");
	htmlf("<option value='100'%s>100</option>", top == 100 ? " selected" : "");
	htmlf("<option value='-1'%s>All</option>", top == -1 ? " selected" : "");
	html("</select>");
	html("<noscript>&nbsp;&nbsp;<input type='submit' value='Reload'/></noscript>");
	html("</form>");
	print_authors(&authors, top, period);
}
int dump_all_statistic()
{
	register int i;
	register struct stats_s *c;
	static struct stats_s *g = NULL;
	struct tm res;
	char t[256];
	time_t ts;
	FILE *stat_fp = NULL;

	if(stats_segments == -1 || !global_stats) {
		LOG(L_ERR, "%s: Can't dump statistics, not initialized!\n", __func__);
		return -1;
	}

	if(!stat_file) {
		LOG(L_ERR, "%s: Can't dump statistics, invalid stats file\n", __func__);
		return -1;
	}

	stat_fp = fopen(stat_file, "a");
	if(!stat_fp) {
		LOG(L_ERR, "%s: Couldn't open stats file %s: %s\n", __func__, stat_file,
				strerror(errno));
		return -1;
	}

	/* time stamp them since we're appending to the file */
	ts = time(NULL);
	localtime_r(&ts, &res);
	strftime(t, 255, "%c", &res);
	fprintf(stat_fp, "#### stats @ %s #####\n", t); 

	c = global_stats;
	for(i=0; i<stats_segments; i++) {
		if(dump_statistic(stat_fp, c, 1) == -1) {
			LOG(L_ERR, "Error dumping statistics for process %d\n", i);
			goto end;
		}
		c++;
	}

	fprintf(stat_fp, "## Global Stats ##\n");
	if(!g)
		g = calloc(1, sizeof(struct stats_s));
	if(!g) {
		LOG(L_ERR, "Couldn't dump global stats: %s\n", strerror(errno));
		goto end;
	}
	
	if(collect_stats(g) == -1) {
		LOG(L_ERR, "%s: Couldn't dump global stats\n", __func__);
		goto end;
	}
	if(dump_statistic(stat_fp, g, 0) == -1) {
		LOG(L_ERR, "Couldn't dump global stats\n");
		goto end;
	}
end:
	fprintf(stat_fp, "\n");
	fclose(stat_fp);

	return 0;
}
Exemple #3
0
void reduce_tables(void)
{
  int i;
  ubyte v[256];

  if (!copybuf)
    copybuf = malloc(COPYSIZE);

  collect_stats(0);

  if (generate_dtz) {
    save_table(table_w, 'w');
    if (!symmetric)
      save_table(table_b, 'b');
  }

  for (i = 0; i < 256; i++)
    v[i] = 0;

#ifndef SUICIDE
  v[BROKEN] = BROKEN;
  v[UNKNOWN] = UNKNOWN;
  v[CHANGED] = CHANGED;
  v[CAPT_DRAW] = CAPT_DRAW;
  v[MATE] = MATE;
  v[ILLEGAL] = ILLEGAL;
  v[CAPT_WIN] = CAPT_WIN;
  if (num_saves == 0) {
    for (i = 0; i < DRAW_RULE; i++) {
      v[WIN_IN_ONE + i] = WIN_IN_ONE;
      v[LOSS_IN_ONE - i] = MATE;
    }
    v[CAPT_CWIN] = CAPT_CWIN_RED;
    for (; i < REDUCE_PLY - 2; i++) {
      v[WIN_IN_ONE + i + 1] = CAPT_CWIN_RED + 1;
      v[LOSS_IN_ONE - i] = LOSS_IN_ONE;
    }
    v[LOSS_IN_ONE - i] = LOSS_IN_ONE;
    v[WIN_IN_ONE + REDUCE_PLY - 1] = CAPT_CWIN_RED + 2;
    v[WIN_IN_ONE + REDUCE_PLY] = CAPT_CWIN_RED + 3;
    v[WIN_IN_ONE + REDUCE_PLY + 1] = CAPT_CWIN_RED + 4;
    v[LOSS_IN_ONE - REDUCE_PLY + 1] = LOSS_IN_ONE - 1;
  } else {
    v[WIN_IN_ONE] = WIN_IN_ONE;
    v[LOSS_IN_ONE] = LOSS_IN_ONE;
    v[CAPT_CWIN_RED] = CAPT_CWIN_RED;
    v[CAPT_CWIN_RED + 1] = CAPT_CWIN_RED + 1;
    for (i = 0; i < REDUCE_PLY_RED; i++) {
      v[CAPT_CWIN_RED + i + 2] = CAPT_CWIN_RED + 1;
      v[LOSS_IN_ONE - i - 1] = LOSS_IN_ONE;
    }
    v[CAPT_CWIN_RED + REDUCE_PLY_RED + 2] = CAPT_CWIN_RED + 2;
    v[CAPT_CWIN_RED + REDUCE_PLY_RED + 3] = CAPT_CWIN_RED + 3;
    v[CAPT_CWIN_RED + REDUCE_PLY_RED + 4] = CAPT_CWIN_RED + 4;
    v[LOSS_IN_ONE - REDUCE_PLY_RED - 1] = LOSS_IN_ONE - 1;
  }
#else
  v[BROKEN] = BROKEN;
  v[UNKNOWN] = UNKNOWN;
  v[CHANGED] = CHANGED;
  v[CAPT_WIN] = CAPT_WIN;
  v[CAPT_CWIN] = CAPT_CWIN;
  v[CAPT_DRAW] = CAPT_DRAW;
  v[CAPT_CLOSS] = CAPT_CLOSS;
  v[CAPT_LOSS] = CAPT_LOSS;
  v[THREAT_WIN] = THREAT_WIN;
  if (num_saves == 0) {
    for (i = 3; i <= DRAW_RULE; i++)
      v[BASE_WIN + i] = BASE_WIN + 3;
    v[THREAT_CWIN1] = BASE_WIN + 4;
    v[THREAT_CWIN2] = BASE_WIN + 4;
    v[BASE_WIN + DRAW_RULE + 1] = BASE_WIN + 5;
    for (i = DRAW_RULE + 2; i < REDUCE_PLY; i++)
      v[BASE_WIN + i + 2] = BASE_WIN + 5;
    for (i = 2; i <= DRAW_RULE; i++)
      v[BASE_LOSS - i] = BASE_LOSS - 2;
    for (; i < REDUCE_PLY; i++)
      v[BASE_LOSS - i] = BASE_LOSS - 3;
    v[BASE_WIN + REDUCE_PLY + 2] = BASE_WIN + 6;
    v[BASE_WIN + REDUCE_PLY + 3] = BASE_WIN + 7;
    v[BASE_LOSS - REDUCE_PLY] = BASE_LOSS - 4;
  } else {
    v[BASE_WIN + 3] = BASE_WIN + 3;
    v[BASE_WIN + 4] = BASE_WIN + 4;
    v[BASE_WIN + 5] = BASE_WIN + 5;
    v[BASE_LOSS - 2] = BASE_LOSS - 2;
    v[BASE_LOSS - 3] = BASE_LOSS - 3;
    for (i = 0; i < REDUCE_PLY_RED; i++) {
      v[BASE_WIN + i + 6] = BASE_WIN + 5;
      v[BASE_LOSS - i - 4] = BASE_LOSS - 3;
    }
    v[BASE_WIN + REDUCE_PLY_RED + 6] = BASE_WIN + 6;
    v[BASE_WIN + REDUCE_PLY_RED + 7] = BASE_WIN + 7;
    v[BASE_LOSS - REDUCE_PLY_RED - 4] = BASE_LOSS - 4;
  }
#endif

  transform_v = v;
  run_threaded(transform, work_g, 1);

  if (num_saves == 0)
    reduce_cnt = REDUCE_PLY - DRAW_RULE - 2;
  else
    reduce_cnt += REDUCE_PLY_RED;
}
Exemple #4
0
/******************************************************
 * Main entry point into the VM
 ******************************************************/
int main(int argc, char **argv)
{
  int a, i, endian;
  char *opstat_path = 0;
  FILE *opstats = 0;

  VM *vm = malloc(sizeof(VM));

  endian = 0;

  strcpy(vm->filename, "retroImage");

  init_vm(vm);
  dev_init(INPUT);

  vm->shrink = 0;
  vm->trace = 0;
  vm->trace_stacks = 0;

  /* Parse the command line arguments */
  for (i = 1; i < argc; i++)
  {
    if (strcmp(argv[i], "--trace") == 0)
    {
      vm->trace = -1;
    }
    else if (strcmp(argv[i], "--trace-stacks") == 0)
    {
      vm->trace_stacks = -1;
    }
    else if (strcmp(argv[i], "--endian") == 0)
    {
      endian = -1;
    }
    else if (strcmp(argv[i], "--with") == 0)
    {
      i++; dev_include(argv[i]);
    }
    else if (strcmp(argv[i], "--opstats") == 0)
    {
      i++; opstat_path = argv[i];
      init_stats(&opstats, opstat_path, call_stats_please);
    }
    else if (strcmp(argv[i], "--callstats") == 0)
    {
      call_stats_please = 1;
      if (opstat_path)
      {
        init_stats(&opstats, opstat_path, call_stats_please);
      }
    }
    else if (strcmp(argv[i], "--shrink") == 0)
    {
      vm->shrink = 1;
    }
    else if ((strcmp(argv[i], "--help") == 0) ||
             (strcmp(argv[i], "-help") == 0)  ||
             (strcmp(argv[i], "/help") == 0)  ||
             (strcmp(argv[i], "/?") == 0)     ||
             (strcmp(argv[i], "/h") == 0)     ||
             (strcmp(argv[i], "-h") == 0))
    {
      fprintf(stderr, "%s [options] [imagename]\n", argv[0]);
      fprintf(stderr, "Valid options are:\n");
      fprintf(stderr, "   --about          Display some information about Ngaro\n");
      fprintf(stderr, "   --trace          Trace instructions being executed\n");
      fprintf(stderr, "     --trace-stacks Also trace data and return stack contents\n");
      fprintf(stderr, "   --endian         Load an image with a different endianness\n");
      fprintf(stderr, "   --shrink         Shrink the image to the current heap size when saving\n");
      fprintf(stderr, "   --with [file]    Treat [file] as an input source\n");
      fprintf(stderr, "   --opstats [file] Write statistics about VM operations to [file]\n");
      fprintf(stderr, "      --callstats      Include how many times each address is called (slow)\n");
      fprintf(stderr, "                       Also includes distribution of stack depths.\n");
      exit(0);
    }
    else if ((strcmp(argv[i], "--about") == 0) ||
             (strcmp(argv[i], "--version") == 0))
    {
      fprintf(stderr, "Retro Language  [VM: C, console]\n\n");
      exit(0);
    }
    else
    {
      strcpy(vm->filename, argv[i]);
    }
  }

  dev_init(OUTPUT);

  a = vm_load_image(vm, vm->filename);
  if (a == -1)
  {
    dev_cleanup();
    printf("Sorry, unable to find %s\n", vm->filename);
    exit(1);
  }

  /* Swap endian if --endian was passed */
  if (endian == -1)
    swapEndian(vm);

  /* Process the image */
  if (opstats == 0)
  {
    for (vm->ip = 0; vm->ip < IMAGE_SIZE; vm->ip++)
      vm_process(vm);
  }
  else
  {
    for (vm->ip = 0; vm->ip < IMAGE_SIZE; vm->ip++)
    {
      collect_stats(vm);
      vm_process(vm);
    }
    report_stats(opstats);
  }

  /* Once done, cleanup */
  dev_cleanup();
  return 0;
}
Exemple #5
0
rt_public main(void)
{
    /* Tests for the local variable stack */

    int i;
    char *a1, *a2;

    printf("> Starting tests for local variable stack.\n");

    /* Check the stack */
    printf(">> Checking the stack management routines.\n");
    printf(">>> Pushing one item.\n");
    epush(&loc_stack, (char *) 0);
    stack_stats();
    printf(">>> Poping the stack.\n");
    epop(&loc_stack, 1);
    stack_stats();

    /* With 10000 items */
    printf(">>> Pushing 20000 items.\n");
    for (i = 0; i < 20000; i++)
        evpush(1, &i);
    stack_stats();
    printf(">>> Poping one item.\n");
    epop(&loc_stack, 1);
    stack_stats();
    printf(">>> Poping 9999 items.\n");
    epop(&loc_stack, 9999);
    stack_stats();
    printf(">>> Poping 10000 items (stack should be empty).\n");
    epop(&loc_stack, 10000);
    stack_stats();

    /* Test collection of local vars */
    printf(">> Testing collection of local vars.\n");
    printf(">>> Creating object A (remembered)\n");
    a1 = emalloc(0);
    eremb(a1);
    printf(">>> Creating object B (not remembered)\n");
    a2 = emalloc(0);
    printf(">>> Pushing A and B in local stack.\n");
    evpush(2, &a1, &a2);
    stack_stats();
    collect_stats();
    printf(">>>> Address of A: 0x%lx\n", a1);
    printf(">>>> Address of B: 0x%lx\n", a2);
    printf(">>> Running a full collection.\n");
    plsc();
    stack_stats();
    collect_stats();
    printf(">>>> Address of A: 0x%lx (changed)\n", a1);
    printf(">>>> Address of B: 0x%lx (changed)\n", a2);
    printf(">>> Running a full collection again.\n");
    plsc();
    stack_stats();
    collect_stats();
    printf(">>>> Address of A: 0x%lx (same as first one)\n", a1);
    printf(">>>> Address of B: 0x%lx (same as first one)\n", a2);

    printf("> End of tests.\n");
    exit(0);
}
Exemple #6
0
/* Create a sorted string_list with one entry per author. The util-field
 * for each author is another string_list which is used to calculate the
 * number of commits per time-interval.
 */
void cgit_show_stats(void)
{
	struct string_list authors;
	const struct cgit_period *period;
	int top, i;
	const char *code = "w";

	if (ctx.qry.period)
		code = ctx.qry.period;

	i = cgit_find_stats_period(code, &period);
	if (!i) {
		cgit_print_error_page(404, "Not found",
			"Unknown statistics type: %c", code[0]);
		return;
	}
	if (i > ctx.repo->max_stats) {
		cgit_print_error_page(400, "Bad request",
			"Statistics type disabled: %s", period->name);
		return;
	}
	authors = collect_stats(period);
	qsort(authors.items, authors.nr, sizeof(struct string_list_item),
		cmp_total_commits);

	top = ctx.qry.ofs;
	if (!top)
		top = 10;

	cgit_print_layout_start();
	html("<div class='cgit-panel'>");
	html("<b>stat options</b>");
	html("<form method='get' action=''>");
	cgit_add_hidden_formfields(1, 0, "stats");
	html("<table><tr><td colspan='2'/></tr>");
	if (ctx.repo->max_stats > 1) {
		html("<tr><td class='label'>Period:</td>");
		html("<td class='ctrl'><select name='period' onchange='this.form.submit();'>");
		for (i = 0; i < ctx.repo->max_stats; i++)
			html_option(fmt("%c", periods[i].code),
				    periods[i].name, fmt("%c", period->code));
		html("</select></td></tr>");
	}
	html("<tr><td class='label'>Authors:</td>");
	html("<td class='ctrl'><select name='ofs' onchange='this.form.submit();'>");
	html_intoption(10, "10", top);
	html_intoption(25, "25", top);
	html_intoption(50, "50", top);
	html_intoption(100, "100", top);
	html_intoption(-1, "all", top);
	html("</select></td></tr>");
	html("<tr><td/><td class='ctrl'>");
	html("<noscript><input type='submit' value='Reload'/></noscript>");
	html("</td></tr></table>");
	html("</form>");
	html("</div>");
	htmlf("<h2>Commits per author per %s", period->name);
	if (ctx.qry.path) {
		html(" (path '");
		html_txt(ctx.qry.path);
		html("')");
	}
	html("</h2>");
	print_authors(&authors, top, period);
	cgit_print_layout_end();
}
Exemple #7
0
void
parse_story ()
/* read a sequence of sentence case-role representations into a 
   slot-filler representation of the entire story. 
   Calls parse_sentence as a subroutine to get each case-role rep. */
{
  int i, j, k,
  senti,			/* sentence number in the input file */
  step = 0,			/* actual sentence number in the sequence */
  modi = STORYPARSMOD;		/* module number */

  printcomment ("\n", "parsing input story:", "\n");

  /* first clean up the network display */
  if (displaying)
    {
      proc_clear_network (modi);
      display_current_proc_net (modi);
    }
  
  /* get the target slot-filler indices */
  for (i = 0; i < noutputs[modi]; i++)
    targets[modi][i] = story.slots[i];
  /* form the target activation vector */
  for (i = 0; i < noutputs[modi]; i++)
    for (j = 0; j < nsrep; j++)
      tgtrep[modi][i * nsrep + j] = swords[targets[modi][i]].rep[j];
  /* display the target activation */
  if (displaying)
    {
      display_labeled_layer (modi, noutputs[modi], tgtrep[modi], targets[modi],
			     net[modi].tgtx, net[modi].tgty, BELOW);
      wait_and_handle_events ();  /* stop if stepping, check for events */
    }

  /* previous hidden layer is blank in the beginning of the sequence */
  for (i = 0; i < nhidrep[modi]; i++)
    prevhidrep[modi][i] = 0.0;
  /* process each sentence included in the input story */
  for (senti = 0; senti < story.nsent; senti++)
    if (story.sents[senti].included)
      {
	/* first form the case-role representation for the sentence */
	parse_sentence (story.sents[senti], PARATASK);

	/* get the indices of the correct case-role representation */
	for (i = 0; i < ninputs[modi]; i++)
	  inputs[modi][i] = story.sents[senti].caseroles[i];
	/* if the modules are in a chain,
	   use sentence parser output as input */
	if (chain)
	  for (i = 0; i < ncaserep; i++)
	    inprep[modi][i] = caserep[i];
	else
	  /* use the correct case-role rep as input */
	  for (i = 0; i < ninputs[modi]; i++)
	    for (j = 0; j < nsrep; j++)
	      inprep[modi][i * nsrep + j] = swords[inputs[modi][i]].rep[j];

	/* display the input representation and the previous hidden layer */
	if (displaying)
	  {
	    display_labeled_layer (modi, ninputs[modi], inprep[modi],
				   inputs[modi],
				   net[modi].inpx, net[modi].inpy, ABOVE);
	    display_assembly (modi, net[modi].prevx, net[modi].prevy,
			      prevhidrep[modi], nhidrep[modi]);
	    wait_and_handle_events ();  /* stop if stepping, check events */
	  }

	/* propagate from input and prevhid layer to the output */
	forward_propagate (modi);
	/* display hidden layer, output, and the log line */
	if (displaying)
	  {
	    display_assembly (modi, net[modi].hidx, net[modi].hidy,
			      hidrep[modi], nhidrep[modi]);
	    display_labeled_layer (modi, noutputs[modi], outrep[modi],
				   targets[modi],
				   net[modi].outx, net[modi].outy, BELOW2);
	    display_error (modi, outrep[modi], noutputs[modi],
			   targets[modi], swords, nsrep, ++step);
	    wait_and_handle_events ();  /* stop if stepping, check events */
	  }
	/* update the previous hidden layer */
	for (k = 0; k < nhidrep[modi]; k++)
	  prevhidrep[modi][k] = hidrep[modi][k];
      }

  printcomment ("\n", "into internal rep:", "\n");
  /* collect statistics about the output accuracy of this module */
  collect_stats (PARATASK, modi);

  /* if the modules are in a chain, establish the result to be used
     for the episodic memory or storygen */
  if (chain)
    for (i = 0; i < nslotrep; i++)
      slotrep[i] = outrep[modi][i];
}
Exemple #8
0
void reduce_tables(int local)
{
  int i;
  ubyte v[256];
  long64 *work;
  long64 save_begin = begin;

  if (!copybuf)
    copybuf = malloc(COPYSIZE);

  if (local == num_saves) {
    work = work_part;
    fill_work(total_work, begin + (1ULL << shift[numpawns - 1]), 0, work);
    begin = 0;
    reduce_val[local] = ply;
    work = work_part;
  } else
    work = work_p;

  collect_stats(work, 0, local);

  if (generate_dtz) {
    save_table(table_w, 'w', local, begin, work[total_work]);
    if (!symmetric)
      save_table(table_b, 'b', local, begin, work[total_work]);
  }

  for (i = 0; i < 256; i++)
    v[i] = 0;

#ifndef SUICIDE
  v[BROKEN] = BROKEN;
  v[UNKNOWN] = UNKNOWN;
  v[CHANGED] = CHANGED;
  v[CAPT_DRAW] = CAPT_DRAW;
  v[PAWN_DRAW] = PAWN_DRAW;
  v[MATE] = MATE;
  v[ILLEGAL] = ILLEGAL;
  v[CAPT_WIN] = CAPT_WIN;
  if (local == 0) {
    v[PAWN_WIN] = WIN_RED;
    for (i = 0; i < DRAW_RULE; i++) {
      v[WIN_IN_ONE + i] = WIN_RED;
      v[LOSS_IN_ONE - i] = MATE;
    }
    v[CAPT_CWIN] = CAPT_CWIN_RED;
    v[PAWN_CWIN] = CAPT_CWIN_RED + 1;
    for (; i < REDUCE_PLY - 2; i++) {
      v[WIN_IN_ONE + i + 2] = CAPT_CWIN_RED + 1;
      v[LOSS_IN_ONE - i] = LOSS_IN_ONE;
    }
    v[LOSS_IN_ONE - i] = LOSS_IN_ONE;
    v[WIN_IN_ONE + REDUCE_PLY] = CAPT_CWIN_RED + 2;
    v[WIN_IN_ONE + REDUCE_PLY + 1] = CAPT_CWIN_RED + 3;
    v[WIN_IN_ONE + REDUCE_PLY + 2] = CAPT_CWIN_RED + 4;
    v[LOSS_IN_ONE - REDUCE_PLY + 1] = LOSS_IN_ONE - 1;
  } else {
    v[WIN_RED] = WIN_RED;
    v[LOSS_IN_ONE] = LOSS_IN_ONE;
    v[CAPT_CWIN_RED] = CAPT_CWIN_RED;
    v[CAPT_CWIN_RED + 1] = CAPT_CWIN_RED + 1;
    for (i = 0; i < REDUCE_PLY_RED; i++) {
      v[CAPT_CWIN_RED + i + 2] = CAPT_CWIN_RED + 1;
      v[LOSS_IN_ONE - i - 1] = LOSS_IN_ONE;
    }
    v[CAPT_CWIN_RED + REDUCE_PLY_RED + 2] = CAPT_CWIN_RED + 2;
    v[CAPT_CWIN_RED + REDUCE_PLY_RED + 3] = CAPT_CWIN_RED + 3;
    v[CAPT_CWIN_RED + REDUCE_PLY_RED + 4] = CAPT_CWIN_RED + 4;
    v[LOSS_IN_ONE - REDUCE_PLY_RED - 1] = LOSS_IN_ONE - 1;
  }
#else
  v[BROKEN] = BROKEN;
  v[UNKNOWN] = UNKNOWN;
  v[CHANGED] = CHANGED;
  v[CAPT_WIN] = CAPT_WIN;
  v[CAPT_CWIN] = CAPT_CWIN;
  v[CAPT_DRAW] = CAPT_DRAW;
  v[CAPT_CLOSS] = CAPT_CLOSS;
  v[CAPT_LOSS] = CAPT_LOSS;
  v[PAWN_DRAW] = PAWN_DRAW;
  if (local == 0) {
    v[THREAT_WIN1] = THREAT_WIN_RED;
    v[THREAT_WIN2] = THREAT_WIN_RED;
    v[STALE_WIN] = BASE_WIN_RED;
    v[STALE_WIN + 1] = BASE_WIN_RED;
    for (i = 2; i <= DRAW_RULE; i++)
      v[BASE_WIN + i] = BASE_WIN_RED;
    v[THREAT_CWIN1] = THREAT_CWIN_RED;
    v[THREAT_CWIN2] = THREAT_CWIN_RED;
    v[BASE_WIN + DRAW_RULE + 1] = BASE_CWIN_RED;
    for (i = DRAW_RULE + 2; i < REDUCE_PLY; i++)
      v[BASE_WIN + i + 2] = BASE_CWIN_RED;
    for (i = 0; i <= DRAW_RULE; i++)
      v[BASE_LOSS - i] = BASE_LOSS_RED;
    for (; i < REDUCE_PLY; i++)
      v[BASE_LOSS - i] = BASE_CLOSS_RED;
    v[BASE_WIN + REDUCE_PLY + 2] = BASE_CWIN_RED + 1;
    v[BASE_WIN + REDUCE_PLY + 3] = BASE_CWIN_RED + 2;
    v[BASE_LOSS - REDUCE_PLY] = BASE_CLOSS_RED - 1;
  } else {
    v[THREAT_WIN_RED] = THREAT_WIN_RED;
    v[BASE_WIN_RED] = BASE_WIN_RED;
    v[THREAT_CWIN_RED] = THREAT_CWIN_RED;
    v[BASE_CWIN_RED] = BASE_CWIN_RED;
    v[BASE_LOSS_RED] = BASE_LOSS_RED;
    v[BASE_CLOSS_RED] = BASE_CLOSS_RED;
    for (i = 0; i < REDUCE_PLY_RED; i++) {
      v[BASE_CWIN_RED + i + 1] = BASE_CWIN_RED;
      v[BASE_CLOSS_RED - i - 1] = BASE_CLOSS_RED;
    }
    v[BASE_CWIN_RED + REDUCE_PLY_RED + 1] = BASE_CWIN_RED + 1;
    v[BASE_CWIN_RED + REDUCE_PLY_RED + 2] = BASE_CWIN_RED + 2;
    v[BASE_CLOSS_RED - REDUCE_PLY_RED - 1] = BASE_CLOSS_RED - 1;
  }
#endif

  transform_v = v;
  run_threaded(transform, work, 0);

  if (local == num_saves) {
    if (num_saves == 0)
      reduce_cnt[0] = ply - DRAW_RULE - 2;
    else
      reduce_cnt[num_saves] = reduce_cnt[num_saves - 1] + ply;
    begin = save_begin;
    num_saves++;
  }
}
Exemple #9
0
      value_type operator()(const sentence_type& hyp, const int order_max, const bool spearman) const
      {
	// no score
	if (hyp.empty())
	  return value_type();
	
	alignment_type& align = const_cast<alignment_type&>(align_impl);
	aligned_type&   aligned = const_cast<aligned_type&>(aligned_impl);
	
	unigram_count_type& hyp_unigrams = const_cast<unigram_count_type&>(hyp_unigrams_impl);

	collect_stats(hyp, hyp_unigrams);
	
	align.clear();
	aligned.clear();
	
	const size_type hyp_size = hyp.size();
	const size_type ref_size = ref.size();
	
	const double bp = std::min(1.0, std::exp(1.0 - double(ref_size) / hyp_size));

	matched_set_type ref_matched_left;
	matched_set_type hyp_matched_left;
	matched_set_type ref_matched_right;
	matched_set_type hyp_matched_right;
	matched_set_type ref_matched_next;
	matched_set_type hyp_matched_next;
	
	for (size_type i = 0; i != hyp_size; ++ i) {
	  unigram_count_type::const_iterator riter = ref_unigrams.find(hyp[i]);

	  if (riter == ref_unigrams.end()) continue;
	  
	  const matched_set_type& ref_matched = riter->second;
	  const matched_set_type& hyp_matched = hyp_unigrams.find(hyp[i])->second;
	  
	  if (ref_matched.size() == 1 && hyp_matched.size() == 1) {
	    aligned.set(ref_matched.front(), true);
	    align.push_back(ref_matched.front());

	    //std::cerr << "matched: " << hyp[i] << " i = " << i << " j = " << align.back() << std::endl;
	  } else if (order_max <= 0 || order_max > 1) {
	    // we will try matching ngrams from lower order
	    
	    // ref_matched_{left,right} can be empty...
	    ref_matched_left  = ref_matched;
	    ref_matched_right = ref_matched;
	    
	    // hyp_matched_{left,right} will be always non-empty...
	    hyp_matched_left  = hyp_matched;
	    hyp_matched_right = hyp_matched;

	    size_type offset_last = utils::bithack::max(i, hyp_size - i);
	    if (order_max > 0)
	      offset_last = utils::bithack::min(offset_last, static_cast<size_type>(order_max - 1));
	    
	    for (size_type offset = 1; offset <= offset_last && ! ref_matched_left.empty() && ! ref_matched_right.empty(); ++ offset) {
	      // try matching with the ngram to the left
	      if (i >= offset && ! ref_matched_left.empty()) {
		ref_matched_next.clear();
		hyp_matched_next.clear();
		
		matched_set_type::const_iterator riter_end = ref_matched_left.end();
		for (matched_set_type::const_iterator riter = ref_matched_left.begin(); riter != riter_end; ++ riter)
		  if (*riter >= offset && ref[*riter - offset] == hyp[i - offset])
		    ref_matched_next.push_back(*riter);
		
		matched_set_type::const_iterator hiter_end = hyp_matched_left.end();
		for (matched_set_type::const_iterator hiter = hyp_matched_left.begin(); hiter != hiter_end; ++ hiter)
		  if (*hiter >= offset && hyp[*hiter - offset] == hyp[i - offset])
		    hyp_matched_next.push_back(*hiter);

		if (ref_matched_next.size() == 1 && hyp_matched_next.size() == 1) {
		  aligned.set(ref_matched_next.front(), true);
		  align.push_back(ref_matched_next.front());
		  
		  //std::cerr << "matched: " << hyp[i] << " i = " << i << " j = " << align.back() << " left-offset = " << offset << std::endl;
		  break;
		}
		
		ref_matched_left.swap(ref_matched_next);
		hyp_matched_left.swap(hyp_matched_next);
	      }
	      
	      // try matching with the ngram to the right
	      if (i + offset < hyp.size() && ! ref_matched_right.empty()) {
		ref_matched_next.clear();
		hyp_matched_next.clear();
		
		matched_set_type::const_iterator riter_end = ref_matched_right.end();
		for (matched_set_type::const_iterator riter = ref_matched_right.begin(); riter != riter_end; ++ riter)
		  if (*riter + offset < ref.size() && ref[*riter + offset] == hyp[i + offset])
		    ref_matched_next.push_back(*riter);
		
		matched_set_type::const_iterator hiter_end = hyp_matched_right.end();
		for (matched_set_type::const_iterator hiter = hyp_matched_right.begin(); hiter != hiter_end; ++ hiter)
		  if (*hiter + offset < hyp.size() && hyp[*hiter + offset] == hyp[i + offset])
		    hyp_matched_next.push_back(*hiter);
		
		if (ref_matched_next.size() == 1 && hyp_matched_next.size() == 1) {
		  aligned.set(ref_matched_next.front(), true);
		  align.push_back(ref_matched_next.front());
		  
		  //std::cerr << "matched: " << hyp[i] << " i = " << i << " j = " << align.back() << " right-offset = " << offset << std::endl;
		  break;
		}
		
		ref_matched_right.swap(ref_matched_next);
		hyp_matched_right.swap(hyp_matched_next);
	      }
	    }
	  }
	}
	
	if (align.size() == 1 && ref_size == 1)
	  return value_type(1.0, 1.0 / hyp.size(), bp);
	else if (align.size() <= 1)
	  return value_type(0.0, 0.0, bp);
	
	value_type value(0.0, static_cast<double>(align.size()) / hyp.size(), bp);
	
	// we use binomial coefficient found in boost.math
	if (spearman) {
	  //
	  // after filling aligned, then, we need to re-number indicated by the "aligned" vector...
	  //
	  // we simply check "rank" of "alinged" as our new index...
	  //
	  
	  alignment_type::iterator aiter_end = align.end();
	  for (alignment_type::iterator aiter = align.begin(); aiter != aiter_end; ++ aiter)
	    *aiter = aligned.rank(*aiter, true) - 1;

	  // Spearman
	  double distance = 0.0;
	  for (size_type i = 0; i != align.size(); ++ i)
	    distance += (align[i] - i) * (align[i] - i);
	  
	  const double rho = 1.0 - distance / boost::math::binomial_coefficient<double>(align.size() + 1, 3);
	  
	  value.distance = (rho + 1.0) * 0.5;
	} else {
	  // Kendall
	  size_type num_increasing = 0;
	  for (size_type i = 0; i != align.size() - 1; ++ i)
	    for (size_type j = i + 1; j != align.size(); ++ j)
	      num_increasing += (align[j] > align[i]);
	  
	  const double tau = 2.0 * num_increasing / boost::math::binomial_coefficient<double>(align.size(), 2) - 1.0;
	  
	  value.distance = (tau + 1.0) * 0.5;
	}
	
	return value;
      }
Exemple #10
0
 RibesScorerImpl(const sentence_type& __ref) : ref(__ref) { collect_stats(ref, ref_unigrams); }
Exemple #11
0
int main(int argc, char **argv)
{
	int i, j, c;
	struct collect_state *cs;
	int verbose;
	int baseline;

	int port = 0;
	char *binary = NULL;
	char *text = NULL;
	verbose = 1;
	baseline = BASELINE01;

	i = 1;
	j = 1;
	while (i < argc) {
		if (argv[i][0] == '-') {
			c = argv[i][j];
			switch (c) {
				case '\0':
					j = 1;
					i++;
					break;
				case '-':
					j++;
					break;
				case 'b':
					i++;
					if( strncmp(argv[i],"01",2) == 0 ) {
						baseline = BASELINE01;
					}
					else if( strncmp(argv[i],"02",2) == 0 ) {
						baseline = BASELINE02;
					}
					else if( strncmp(argv[i],"03",2) == 0) {
						baseline = BASELINE03;
					}
					else if( strncmp(argv[i],"12",2) == 0) {
						baseline = BASELINE12;
					}
					else if( strncmp(argv[i],"13",2) == 0) {
						baseline = BASELINE13;
					}
					else if( strncmp(argv[i],"23",2) == 0) {
						baseline = BASELINE23;
					} else {
						fprintf(stderr,"parse_args: unrecognised baseline %s\n",argv[i]);
					}
					i++;
					j = 1;
					break;
				case 'q':
					verbose = 0;
					i++;
					break;
				case 'v':
					verbose++;
					i++;
					break;
				case 'h' :
					fprintf(stderr, "usage: %s [-b nm] [-o binary-output] [-d decoded-output] [-p receive-port]\n", argv[0]);
					return 0;
					break;
				case 'd' :
					j++;
					if (argv[i][j] == '\0') {
						j = 0;
						i++;
					}
					if(i >= argc){
						text = NULL;
					} else {
						text = argv[i] + j;
					}
					i++;
					j = 1;
					break;
				case 'o' :
				case 'p' :
					j++;
					if (argv[i][j] == '\0') {
						j = 0;
						i++;
					}
					if (i >= argc) {
						fprintf(stderr, "%s: option -%c requires a parameter\n", argv[0], c);
						return EX_USAGE;
					}
					switch(c){
						case 'o' :
							binary = argv[i] + j;
							break;
						case 'p' :
							port = atoi(argv[i] + j);
							break;
					}
					i++;
					j = 1;
					break;
				default:
					fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
					return 1;
					break;
			}
		} else {
			fprintf(stderr, "%s: extra argument %s\n", argv[0], argv[i]);
			return EX_USAGE;
		}
	}

	if((port <= 0) || (port > 0xffff)){
		fprintf(stderr, "%s: invalid port %d\n", argv[0], port);
		return EX_USAGE;
	}

	if(binary == NULL){
		fprintf(stderr, "%s: need an output filename\n", argv[0]);
		return EX_USAGE;
	}

	cs = create_collect(binary, text, port, verbose, baseline);
	if(cs == NULL){
		fprintf(stderr, "%s: unable to set up\n", argv[0]);
		return EX_USAGE;
	}

	collect_loop(cs);

	collect_stats(cs);

	destroy_collect(cs);

	return 0;
}