Exemplo n.º 1
0
/* fadster algorithm for random distribution */
static void _random2_fill (VsgPRTree2d *tree, guint np)
{
  gint i;
  Pt *pt;
  VsgVector2d lb, ub;
  GRand *rand = g_rand_new_with_seed (_random_seed);

  vsg_prtree2d_get_bounds (tree, &lb, &ub);

  pt = pt_alloc (TRUE, NULL);

  for (i=0; i< np; i++)
    {
      gdouble x1, y1;
      gint c;

      x1 = g_rand_double_range (rand, lb.x, ub.x);
      y1 = g_rand_double_range (rand, lb.y, ub.y);
      c = i+1;

      pt->vector.x = x1;
      pt->vector.y = y1;
      pt->weight = c;

      _ref_count += c;

      if (vsg_prtree2d_insert_point_local (tree, pt))
        {
          if (i % 10000 == 0 && _verbose)
            g_printerr ("%d: insert %dth point\n", rk, i);

          pt = pt_alloc (TRUE, NULL);
        }

      if (i%(_flush_interval*10) == 0)
        {
          if (_verbose && rk == 0)
            g_printerr ("%d: contiguous dist before %dth point\n", rk, i);
          _distribute (tree);
        }
    }

  pt_destroy (pt, TRUE, NULL);

  _distribute (tree);

  g_rand_free (rand);
}
Exemplo n.º 2
0
static void grid_fill (guint *npoints, GPtrArray *array)
{
  gint i, j, n;

  n = ceil (sqrt (*npoints));

  for (i=0; i<n; i++)
    {
      gdouble x = (2.*i)/(n) - 1.;

      for (j=0; j<n; j++)
        {
          Pt *point = pt_alloc (TRUE, array);
          gdouble y = (2.*j)/(n) - 1.;


          point->vector.x = x;
          point->vector.y = y;

          point->weight = 1;
          /* vsg_vector2d_write (point, stderr); */
          /* g_printerr ("\n"); */
        }
    }

  *npoints = n*n;
}
Exemplo n.º 3
0
OPTION *root_prediction(APPROX_PARAMS *ap, MODEL_PARAMS *mp, SENTENCE *sent) {

    //opt to predict shift
    STACK st; st.size = 0;
    STACK srl_st; srl_st.size = 0;
    OPTION *opt = create_option(SRL_STEP, 1, -1, -1, NULL, 1, &st, &srl_st, 1, pt_alloc(sent->len), 
            ps_alloc(sent->len), &mp->out_link_act, ACTION_NUM);
    set_links(ap, mp, opt, sent);
    return opt;
}
Exemplo n.º 4
0
void _circle2_fill (VsgPRTree2d *tree, guint np)
{
  gint i;
  Pt *pt;
  const gdouble r = 0.95;
  gdouble dtheta = 2. * G_PI / (np-1);
  gdouble theta0 = 0.;

  pt = pt_alloc (TRUE, NULL);

  for (i=0; i<np; i++)
    {
      gint c;

      c = i+1;

      _ref_count += c;

      pt->vector.x = r * cos (theta0 + i * dtheta);
      pt->vector.y = r * sin (theta0 + i * dtheta);
      pt->weight = c;

      if (vsg_prtree2d_insert_point_local (tree, pt))
        {
          if (i % 10000 == 0 && _verbose)
            g_printerr ("%d: insert %dth point\n", rk, i);

          pt = pt_alloc (TRUE, NULL);
        }

      if (i%(_flush_interval*10) == 0)
        {
          if (_verbose && rk == 0)
            g_printerr ("%d: contiguous dist before %dth point\n", rk, i);
          _distribute (tree);
        }
    }

  pt_destroy (pt, TRUE, NULL);

  _distribute (tree);
}
Exemplo n.º 5
0
static void random_fill (VsgPRTree3d *tree, guint np)
{
  gint i;
  VsgVector3d *pt;
  Sphere *c;
  VsgVector3d lb, ub;
  GRand *rand = g_rand_new_with_seed (_random_seed);

  vsg_prtree3d_get_bounds (tree, &lb, &ub);

  for (i=0; i< np; i++)
    {
      gdouble x1, x2, y1, y2, z1, z2, r;

      x1 = g_rand_double_range (rand, lb.x, ub.x);
      y1 = g_rand_double_range (rand, lb.y, ub.y);
      z1 = g_rand_double_range (rand, lb.z, ub.z);
      x2 = g_rand_double_range (rand, lb.x, ub.x);
      y2 = g_rand_double_range (rand, lb.y, ub.y);
      z2 = g_rand_double_range (rand, lb.z, ub.z);
      r = g_rand_double_range (rand, 0., 0.1);

      if (i%_flush_interval == 0) vsg_prtree3d_migrate_flush (tree);

      if (i%sz != rk) continue;

      if (i % 10000 == 0 && _verbose) g_printerr ("%d: insert %dth point\n", rk, i);

      pt = pt_alloc (TRUE, NULL);
      pt->x = x1;
      pt->y = y1;
      pt->z = z1;
      vsg_prtree3d_insert_point (tree, pt);

      if (_put_regions)
        {
          c = rg_alloc (TRUE, NULL);
          c->center.x = x2;
          c->center.y = y2;
          c->center.z = z2;
          c->radius = r;
          vsg_prtree3d_insert_region (tree, c);
        }
/*       g_printerr ("%d: %d\n", rk, i); */
    }

  vsg_prtree3d_migrate_flush (tree);

  g_rand_free (rand);
}
Exemplo n.º 6
0
/* faster algorithm for circle distribution */
void _circle_fill (VsgPRTree2d *tree, guint np)
{
  gint i;
  Pt *pt;
  const gdouble r = 0.95;
  gdouble dtheta = 2. * G_PI / (np-1) / sz;
  gdouble theta0 = 2. * rk * G_PI / sz;

  for (i=0; i<np; i++)
    {
      gint c;

      c = i+1;

      _ref_count += c;

      if (i%_flush_interval == 0)
        {
          vsg_prtree2d_migrate_flush (tree);
          if (i%(_flush_interval*10) == 0)
            {
              if (_verbose && rk == 0)
                g_printerr ("%d: contiguous dist before %dth point\n", rk, i);
              _distribute (tree);
            }
        }
      if (i%sz != rk) continue;

      if (i % 10000 == 0 && _verbose)
        g_printerr ("%d: insert %dth point\n", rk, i);

      pt = pt_alloc (TRUE, NULL);

      pt->vector.x = r * cos (theta0 + i * dtheta);
      pt->vector.y = r * sin (theta0 + i * dtheta);
      pt->weight = c;

      vsg_prtree2d_insert_point (tree, pt);
    }

  vsg_prtree2d_migrate_flush (tree);
  _distribute (tree);
}
Exemplo n.º 7
0
static void _exterior_points (VsgPRTree3d *tree)
{
  VsgVector3d lb, ub;
  VsgVector3d *pt;

  if (_verbose) g_printerr ("%d: exterior points\n", rk);

  vsg_prtree3d_get_bounds (tree, &lb, &ub);

  pt = pt_alloc (TRUE, NULL);
  pt->x = ub.x+rk+1.;
  pt->y = ub.y+rk+1.;
  pt->z = ub.z+rk+1.;

  vsg_prtree3d_insert_point (tree, pt);
  
  vsg_prtree3d_migrate_flush (tree);

  if (_verbose) g_printerr ("%d: exterior points ok\n", rk);
}
Exemplo n.º 8
0
gint main (gint argc, gchar ** argv)
{
  gint ret = 0;

  VsgPRTree3d *tree;
  gint i;

  VsgVector3d lb;
  VsgVector3d ub;


  MPI_Init (&argc, &argv);

  MPI_Comm_size (MPI_COMM_WORLD, &sz);
  MPI_Comm_rank (MPI_COMM_WORLD, &rk);

  if (argc > 1 && g_ascii_strncasecmp (argv[1], "--version", 9) == 0)
    {
      if (rk == 0)
        g_print ("%s\n", PACKAGE_VERSION);
      return 0;
    }

  if (argc > 1 && g_ascii_strncasecmp (argv[1], "--write", 7) == 0)
    {
      _do_write = TRUE;
    }

  vsg_init_gdouble ();

  points = g_ptr_array_new ();
  regions = g_ptr_array_new ();

  if (rk == 0)
    {
      VsgVector3d *pt;
      Sphere *c;

      lb.x = -1.; lb.y = -1.; lb.z = -1.;
      ub.x = 0.; ub.y = 0.; ub.z = 0.;

      pt = pt_alloc (TRUE, NULL);
      pt->x = -0.5; pt->y = -0.5; pt->z = -0.5;

      c = rg_alloc (TRUE, NULL);
      c->center.x = -0.6; c->center.y = -0.6; c->center.z = -0.6;
      c->radius = 0.1;
    }
  else
    {
      VsgVector3d *pt;
      Sphere *c;

      lb.x = 0.; lb.y = 0.; lb.z = 0.;
      ub.x = 1.*rk; ub.y = 1.*rk; ub.z = 1.*rk;

      pt = pt_alloc (TRUE, NULL);
      pt->x = 0.5*rk; pt->y = 0.5*rk; pt->z = 0.5*rk;

      pt = pt_alloc (TRUE, NULL);
      pt->x = 0.60*rk; pt->y = 0.65*rk; pt->z = 0.70*rk;

      pt = pt_alloc (TRUE, NULL);
      pt->x = 0.15*rk; pt->y = 0.75*rk; pt->z = 0.80*rk;

      c = rg_alloc (TRUE, NULL);
      c->center.x = 0.6*rk; c->center.y = 0.6*rk; c->center.z = 0.6*rk;
      c->radius = 0.11;
    }

  /* create the tree */
  tree =
    vsg_prtree3d_new_full (&lb, &ub,
                           (VsgPoint3dLocFunc) vsg_vector3d_vector3d_locfunc,
                           (VsgPoint3dDistFunc) vsg_vector3d_dist,
                           (VsgRegion3dLocFunc) _sphere_loc3, 2);

  /* insert the points */
  for (i=0; i<points->len; i++)
    {
      vsg_prtree3d_insert_point (tree, g_ptr_array_index (points, i));
    }

  /* insert the regions */
  for (i=0; i<regions->len; i++)
    {
      vsg_prtree3d_insert_region (tree, g_ptr_array_index (regions, i));
    }

  /* count total created points and regions */
  init_total_points_count ();
  init_total_regions_count ();

/*   MPI_Barrier (MPI_COMM_WORLD); */
/*   g_printerr ("%d: set_parallel begin\n", rk); */

  vsg_prtree3d_set_parallel (tree, &pconfig);

/*   MPI_Barrier (MPI_COMM_WORLD); */
/*   g_printerr ("%d: set_parallel ok\n", rk); */

  ret += check_points_number (tree);
  ret += check_regions_number (tree);

/*   MPI_Barrier (MPI_COMM_WORLD); */
/*   g_printerr ("%d: before migrate_flush ok\n", rk); */
  {
    VsgVector3d *pt;
    Sphere *c;

    pt = pt_alloc (TRUE, NULL);
    pt->x = 0.5*rk; pt->y = 0.75*rk; pt->z = 0.75*rk;
    vsg_prtree3d_insert_point (tree, pt);

    c = rg_alloc (TRUE, NULL);
    c->center.x = 1.; c->center.y = 0.6*rk; c->center.z = 0.6*rk;
    c->radius = 0.1;
    vsg_prtree3d_insert_region (tree, c);
  }

  /* update total points and regions count */
  init_total_points_count ();
  init_total_regions_count ();

/*   MPI_Barrier (MPI_COMM_WORLD); */
/*   g_printerr ("%d: migrate_flush begin\n", rk); */

  vsg_prtree3d_migrate_flush (tree);

/*   MPI_Barrier (MPI_COMM_WORLD); */
/*   g_printerr ("%d: migrate_flush ok\n", rk); */

  ret += check_points_number (tree);
  ret += check_regions_number (tree);

/*   MPI_Barrier (MPI_COMM_WORLD); */
/*   g_printerr ("%d: distribute_nodes begin\n", rk); */

  for (i=0; i<sz; i++)
    {
      gint dst = (i+1) % sz;

/*       MPI_Barrier (MPI_COMM_WORLD); */
/*       g_printerr ("%d: move to %d\n", rk, dst); */

      vsg_prtree3d_distribute_concentrate (tree, dst);

      ret += check_points_number (tree);
      ret += check_regions_number (tree);
    }

/*   MPI_Barrier (MPI_COMM_WORLD); */
/*   g_printerr ("%d: split between nodes\n", rk); */

  vsg_prtree3d_distribute_scatter_leaves (tree);

  ret += check_points_number (tree);
  ret += check_regions_number (tree);

/* /\*   MPI_Barrier (MPI_COMM_WORLD); *\/ */
/* /\*   g_printerr ("%d: distribute_nodes ok\n", rk); *\/ */

  if (_do_write)
    {
      MPI_Barrier (MPI_COMM_WORLD);
      _tree_write (tree);
    }

  if (_do_write)
    {
      gchar fn[1024];
      FILE *f;

      g_sprintf (fn, "prtree3parallel-%03d.txt", rk);
      f = fopen (fn, "w");
      vsg_prtree3d_write (tree, f);
      fclose (f);
    }

  /* destroy the points */
  g_ptr_array_foreach (points, empty_array, NULL);
  g_ptr_array_free (points, TRUE);

  /* destroy the spheres */
  g_ptr_array_foreach (regions, empty_array, NULL);
  g_ptr_array_free (regions, TRUE);

  /* destroy the tree */
  vsg_prtree3d_free (tree);

  MPI_Finalize ();

  return ret;
}
Exemplo n.º 9
0
//processes sentence and creates derivations
//returns tail of the derivation
OPTION *get_derivation(APPROX_PARAMS *ap, MODEL_PARAMS *mp, SENTENCE *sent, int do_free_partials) {

	/*    if (ap->intern_synt_deproj == DEPROJ_EXHAUSTIVE && !ap->is_synt_early_reduce) {
        fprintf(stderr, "Error: exhaustive internal syntactic projectivization with late reduce strategy is not supported\n");
        exit(EXIT_FAILURE);
    }
    if (ap->intern_srl_deproj == DEPROJ_EXHAUSTIVE && !ap->is_srl_early_reduce) {
        fprintf(stderr, "Error: exhaustive internal SRL projectivization with late reduce strategy is not supported\n");
        exit(EXIT_FAILURE);
    } */

	//Debug
	//    printf("Next sentence ============\n");

	ASSERT(ap->input_offset == 0 || ap->input_offset == 1);

	STACK st; st.size = 0;
	STACK srl_st; srl_st.size = 0;
	//OPTION *head = create_option(SYNT_STEP, 1, -1, -1, NULL, 1, &st, &srl_st, 1, pt_alloc(sent->len), ps_alloc(sent->len), &mp->out_link_act, ACTION_NUM);
	OPTION *head = create_option(SRL_STEP, 1, -1, -1, NULL, 1, &st, &srl_st, 1, pt_alloc(sent->len), ps_alloc(sent->len), &mp->out_link_act, ACTION_NUM);
	///st_push(&head->srl_stack, 0); //JH allow 0 as an SRL argument
	set_links(ap, mp, head, sent);

	///printf("new sentence\n");///
	OPTION *opt = head;
	while (opt->queue != sent->len + 1) {
		//get next possible actions
		ACTION_SET as = get_next_actions(ap, mp, opt, sent);

		set_mask(mp, opt, &as);

		//=====================  SYNTAX ======================================================
		if (ap->is_synt_early_reduce && as.acts[RED]) {
			int s = st_peek(&opt->stack);
			if (everything_complete(opt->pt, sent, s)) {
				if (ap->parsing_mode != 3 || sent->head[s] != 0 || sent->deprel[s] != ROOT_DEPREL) {
					opt = _action_synt_red(ap, mp, sent, do_free_partials, opt);
					//DEBUG
					//printf("RED\n");

					continue;
				}
			}
		}


		//check LA
		if (as.acts[LA]) {
			int d = st_peek(&opt->stack);
			int h = opt->queue;
			if (sent->head[d] == h) {
				//create sequence of LA operations
				//update queue and stack
				opt = create_option(SYNT_STEP, 0, opt->previous_act, LA, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
						&mp->out_link_la_label, mp->deprel_num);
				TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
				//opt->previous_option->pt = NULL;
				set_links(ap, mp, opt, sent);

				opt = create_option(SYNT_STEP, 1, LA, sent->deprel[d], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
						&mp->out_link_act, ACTION_NUM);
				TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
				//opt->previous_option->pt = NULL;
				pt_add_link(opt->pt, h, d, sent->deprel[d]);
				opt->pt->right_connections_num[d]++;
				if (ap->intern_synt_deproj == DEPROJ_NO) {
					st_pop(&opt->stack);
				} else {
					// Do Nothing!!!
				}
				set_links(ap, mp, opt, sent);


				//DEBUG
				//printf("LA\n");

				continue;
			}
		}

		//check RA
		if (as.acts[RA]) {
			int d = opt->queue;
			int h = st_peek(&opt->stack);
			//when agrees
			if (sent->head[d] == h) {
				//means if stack is not empty or  we can make RA with empty stack
				if (h != 0 ||
						(ap->parsing_mode >= 3 && sent->deprel[d] != ROOT_DEPREL
								// can do only once
								&& opt->pt->nodes[d].deprel == ROOT_DEPREL))
				{
					//create sequence of RA operations
					//update queue and stack
					opt = create_option(SYNT_STEP, 0, opt->previous_act, RA, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt),  ps_clone(opt->ps),
							&mp->out_link_ra_label, mp->deprel_num);
					TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
					set_links(ap, mp, opt, sent);


					opt = create_option(SYNT_STEP, 1, RA, sent->deprel[d], opt, opt->period, &opt->stack,  &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
							&mp->out_link_act, ACTION_NUM);
					TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
					pt_add_link(opt->pt, h, d, sent->deprel[d]);
					if (h != 0) {
						opt->pt->right_connections_num[h]++;
					}
					set_links(ap, mp, opt, sent);

					//DEBUG
					//printf("RA\n");

					continue;
				}
			}
		}
		// check FLIP
		if (as.acts[FLIP] && ap->intern_synt_deproj == DEPROJ_EXHAUSTIVE) {
			int s = st_peek(&opt->stack);
			int s_already_conns =  opt->pt->right_connections_num[s];
			int s_under = st_look(&opt->stack, 1);
			int s_under_already_conns = opt->pt->right_connections_num[s_under];

			int top_is_usl = (sent->synt_right_degree[s] == s_already_conns);
			int under_top_is_usl = (sent->synt_right_degree[s_under] == s_under_already_conns);

			if (ap->is_synt_early_reduce || (!top_is_usl && !under_top_is_usl)  )  {
				// s_under will be active earlier than s
				if (lexicograph_ignore_double_entries_comp(
						sent->synt_right_indices[s_under] + s_under_already_conns,
						sent->synt_right_degree[s_under] - s_under_already_conns,
						sent->synt_right_indices[s] + s_already_conns,
						sent->synt_right_degree[s] - s_already_conns) < 0) {
					//create sequence of reduce operation
					//update queue and stack
					opt = _action_synt_flip(ap, mp, sent, do_free_partials, opt);
					//DEBUG
					printf("(SYNT) FLIP\n");
					continue;
				}

			} else {
				// if a top word can be reduce from the top
				if (top_is_usl) {
					int first_usf = get_useful(&opt->stack, 1, opt->pt->right_connections_num, sent->synt_right_degree);
					if (first_usf > 0) {
						// first 'useful' word in the stack should be attached to front of queue -- start remioving useless words
						// next words will be removed on the next rounds
						int next_connection = sent->synt_right_indices[first_usf][opt->pt->right_connections_num[first_usf]];
						if (next_connection  == opt->queue) {
							ASSERT(as.acts[RED]);
							opt = _action_synt_red(ap, mp, sent, do_free_partials, opt);
							//DEBUG
							//printf("RED\n");
							continue;
						}
					}
				}  else {
					// if
					int second_usf = get_useful(&opt->stack, 1, opt->pt->right_connections_num, sent->synt_right_degree);

					ASSERT(second_usf != s_under);
					if (second_usf > 0) {
						int second_usf_already_conns = opt->pt->right_connections_num[second_usf];

						if (lexicograph_ignore_double_entries_comp(
								sent->synt_right_indices[second_usf] + second_usf_already_conns,
								sent->synt_right_degree[second_usf] - second_usf_already_conns,
								sent->synt_right_indices[s] + s_already_conns,
								sent->synt_right_degree[s] - s_already_conns) < 0) {

							opt = _action_synt_flip(ap, mp, sent, do_free_partials, opt);
							as = get_next_actions(ap, mp, opt, sent);
							set_mask(mp, opt, &as);
							ASSERT(as.acts[RED]);
							opt = _action_synt_red(ap, mp, sent, do_free_partials, opt);
							// DEBUG
							printf("FLIP, followed by RED\n");
							//printf("RED, preceded by FLIP\n");
							continue;
						}
					}

				}   // !top_is_usl
			} // !ap->is_synt_early_reduce
		}  // FLIP


		//check SWITCH
		if (as.acts[SWITCH]) {
			int q = opt->queue;
			if (left_part_complete(opt->pt, sent, q)) {
				//if atached to root with not ROOT_DEPREL and parsing_mode >= 3 then RA, not SHIFT should be peformed
				//option sent->deprel[q] == opt->pt->nodes[q]->deprel - relates to RA- + SHIFT sequence  and means that RA- is actually preformed
				//and q is already attached
				if (sent->head[q] != 0 || ap->parsing_mode < 3 ||
						(ap->parsing_mode >= 3 && (sent->deprel[q] == ROOT_DEPREL || sent->deprel[q] == opt->pt->nodes[q].deprel))) {

					opt = create_option(SRL_STEP, 1, SWITCH, SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue,
							pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM);
					/*                    opt = create_option(SYNT_STEP, 1, SWITCH, SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue,
                            pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM); */
					TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
					set_links(ap, mp, opt, sent);

					//DEBUG
					//printf("SWITCH\n");
					continue;
				}
			}
		}

		//check RED
		if (!ap->is_synt_early_reduce && as.acts[RED]) {
			int s = st_peek(&opt->stack);
			if (everything_complete(opt->pt, sent, s)) {
				if (ap->parsing_mode != 3 || sent->head[s] != 0 || sent->deprel[s] != ROOT_DEPREL) {

					opt = _action_synt_red(ap, mp, sent, do_free_partials, opt);

					//DEBUG
					//printf("RED\n");

					continue;
				}
			}
		}

		// check FLIP
		if (as.acts[FLIP] && ap->intern_synt_deproj != DEPROJ_EXHAUSTIVE) {
			opt = _action_synt_flip(ap, mp, sent, do_free_partials, opt);

			//DEBUG
			printf("(SYNT) FLIP\n");

			continue;
		}

		//=====================  WORD PREDICTION =============================================
		if (as.acts[SHIFT]) {
			//create sequence of shift operation
			//update queue and stack
			opt = create_option(SYNT_STEP, 0, opt->previous_act, SHIFT, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
					&mp->out_link_pos,  get_pos_out_num());
			TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
			set_links(ap, mp, opt, sent);

			opt = create_word_prediction_seq(ap, mp, sent, opt, do_free_partials);

			//DEBUG
			//printf("SHIFT\n");
			continue;
		}




		//=====================  SRL  ======================================================

		if (ap->is_srl_early_reduce && as.acts[SRL_RED]) {
			int srl_s = st_peek(&opt->srl_stack);
			if (srl_everything_complete(opt->ps, sent, srl_s)) {

				opt = _action_srl_red(ap, mp, sent, do_free_partials, opt);

				//DEBUG
				//                    printf("SRL_RED\n");

				continue;
			}
		}

		//check SRL_LA
		int q_bank = sent->bank[opt->queue];
		///if (q_bank >= 0)///
		///  printf("srl_la 0: %d %d; %d %d\n", q_bank, as.acts[SRL_LA[q_bank]], st_peek(&opt->srl_stack), opt->queue);///
		if (q_bank >= 0 && as.acts[SRL_LA[q_bank]]) {
			int d = st_peek(&opt->srl_stack);
			int h = opt->queue;
			int role = next_srl_role(0, sent, h, d);
			///printf("srl_la 1: %d %d %d\n", d, h, role);///
			if (role >= 0) {
				int i = 0;
				for (; role >= 0; role = next_srl_role(i, sent, h, d)) {
					i++;
					//create sequence of SRL_LA operations
					//update queue and stack
					opt = create_option(SRL_STEP, 0, opt->previous_act, SRL_LA[q_bank], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
							&mp->out_link_sem_la_label[q_bank], mp->role_num[q_bank]);
					TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
					set_links(ap, mp, opt, sent);

					opt = create_option(SRL_STEP, 1, SRL_LA[q_bank], role, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
							&mp->out_link_act, ACTION_NUM);
					TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
					ps_add_link(opt->ps, h, d, role);
					opt->ps->right_connections_num[d]++;
					set_links(ap, mp, opt, sent);

					//DEBUG
					//printf("SRL_LA[%d]\n", q_bank);
				}

				continue;
			}
		}
		q_bank = -1;

		//check SRL_RA
		int s_bank = sent->bank[st_peek(&opt->srl_stack)];
		if (s_bank >= 0 && as.acts[SRL_RA[s_bank]]) {
			int d = opt->queue;
			int h = st_peek(&opt->srl_stack);
			int role =  next_srl_role(0, sent, h, d);
			///printf("srl_ra: %d %d %d\n", d, h, role);///
			//when agrees
			if (role >= 0) {
				int i = 0;
				for (; role >= 0; role = next_srl_role(i, sent, h, d)) {
					ASSERT(i == 0);
					i++;
					//create sequence of RA operations
					//update queue and stack
					opt = create_option(SRL_STEP, 0, opt->previous_act, SRL_RA[s_bank], opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt),  ps_clone(opt->ps),
							&mp->out_link_sem_ra_label[s_bank], mp->role_num[s_bank]);
					TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
					set_links(ap, mp, opt, sent);


					opt = create_option(SRL_STEP, 1, SRL_RA[s_bank], role, opt, opt->period, &opt->stack,  &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
							&mp->out_link_act, ACTION_NUM);
					TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
					ps_add_link(opt->ps, h, d, role);
					opt->ps->right_connections_num[h]++;
					set_links(ap, mp, opt, sent);

					//DEBUG
					//printf("SRL-RA[%d]\n", s_bank);
				}  // role

				continue;
			}
		}

		if (as.acts[SRL_FLIP] && ap->intern_srl_deproj == DEPROJ_EXHAUSTIVE) {
			int srl_s = st_peek(&opt->srl_stack);
			int srl_s_already_conns = opt->ps->right_connections_num[srl_s];

			int srl_s_under = st_look(&opt->srl_stack, 1);
			int srl_s_under_already_conns = opt->ps->right_connections_num[srl_s_under];


			int top_is_usl = (sent->srl_right_degree[srl_s] == srl_s_already_conns);
			int under_top_is_usl = (sent->srl_right_degree[srl_s_under] == srl_s_under_already_conns);

			if (ap->is_srl_early_reduce || (!top_is_usl && !under_top_is_usl)  )  {
				// srl_s_under will be active earlier than srl_s
				if (lexicograph_ignore_double_entries_comp(
						sent->srl_right_indices[srl_s_under] + srl_s_under_already_conns,
						sent->srl_right_degree[srl_s_under] - srl_s_under_already_conns,
						sent->srl_right_indices[srl_s] + srl_s_already_conns,
						sent->srl_right_degree[srl_s] - srl_s_already_conns) < 0) {

					opt = _action_srl_flip(ap, mp, sent, do_free_partials, opt);

					//DEBUG
					//TMP printf("SRL_FLIP\n");
					continue;
				}
			} else {
				// if a top word can be reduce from the top
				if (top_is_usl) {
					int first_usf = get_useful(&opt->srl_stack, 1, opt->ps->right_connections_num, sent->srl_right_degree);
					if (first_usf > 0) {
						// first 'useful' word in the stack should be attached to front of queue -- start remioving useless words
						// next words will be removed on the next rounds
						int next_connection = sent->srl_right_indices[first_usf][opt->ps->right_connections_num[first_usf]];
						if (next_connection  == opt->queue) {
							ASSERT(as.acts[SRL_RED]);
							opt = _action_srl_red(ap, mp, sent, do_free_partials, opt);
							continue;
							//DEBUG
							//printf("SRL_RED, useless reduction\n");
						}
					}
				}  else {
					// if
					int second_usf = get_useful(&opt->srl_stack, 1, opt->ps->right_connections_num, sent->srl_right_degree);

					ASSERT(second_usf != srl_s_under);

					if (second_usf > 0) {
						int second_usf_already_conns = opt->ps->right_connections_num[second_usf];

						if (lexicograph_ignore_double_entries_comp(
								sent->srl_right_indices[second_usf] + second_usf_already_conns,
								sent->srl_right_degree[second_usf] - second_usf_already_conns,
								sent->srl_right_indices[srl_s] + srl_s_already_conns,
								sent->srl_right_degree[srl_s] - srl_s_already_conns) < 0) {

							opt = _action_srl_flip(ap, mp, sent, do_free_partials, opt);
							as = get_next_actions(ap, mp, opt, sent);
							set_mask(mp, opt, &as);
							ASSERT(as.acts[SRL_RED]);
							opt = _action_srl_red(ap, mp, sent, do_free_partials, opt);
							// DEBUG
							printf("SRL_FLIP, followed by SRL_RED\n");
							//printf("SRL_RED, preceded by SRL_FLIP\n");

							continue;
						}
					}
				}   // !top_is_usl
			} // !ap->is_synt_early_reduce
		}  // FLIP

		//check SRL_SWITCH
		if (as.acts[SRL_SWITCH]) {
			int q = opt->queue;
			if (srl_left_part_complete(opt->ps, sent, q)) {

				opt = create_option(SYNT_STEP, 1, SRL_SWITCH, SRL_SWITCH, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue,
						pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM);
				TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
				set_links(ap, mp, opt, sent);

				//DEBUG
				//printf("SRL_SWITCH\n");
				continue;
			}
		}

		//check SRL_RED
		if (!ap->is_srl_early_reduce  && as.acts[SRL_RED]) {
			int srl_s = st_peek(&opt->srl_stack);
			if (srl_everything_complete(opt->ps, sent, srl_s)) {
				opt = _action_srl_red(ap, mp, sent, do_free_partials, opt);
				//DEBUG
				//printf("SRL_RED\n");

				continue;
			}
		}

		if (as.acts[PREDIC_NO]) {
			int q = opt->queue;
			if (sent->sense[q] < 0) {
				opt = create_option(SRL_STEP, 1, PREDIC_NO, PREDIC_NO, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue,
						pt_clone(opt->pt), ps_clone(opt->ps), &mp->out_link_act, ACTION_NUM);
				TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));

				set_links(ap, mp, opt, sent);

				//DEBUG
				//printf("PREDIC_NO\n");
				continue;
			}
		}
		if (as.acts[PREDIC_YES]) {
			int q = opt->queue;
			int q_sense = sent->sense[q];
			if (q_sense >= 0) {

				int q_bank = sent->bank[q];
				int q_lemma = sent->lemma[q];

				//create sequence of predicate prediction operations
				opt = create_option(SRL_STEP, 0, opt->previous_act, PREDIC_YES, opt, opt->period, &opt->stack, &opt->srl_stack, opt->queue, pt_clone(opt->pt),  ps_clone(opt->ps),
						&mp->out_link_sense[q_bank][q_lemma][0], mp->sense_num[q_bank][q_lemma]);
				TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
				set_links(ap, mp, opt, sent);


				opt = create_option(SRL_STEP, 1, PREDIC_YES, q_sense, opt, opt->period, &opt->stack,  &opt->srl_stack, opt->queue, pt_clone(opt->pt), ps_clone(opt->ps),
						&mp->out_link_act, ACTION_NUM);
				TRY_FREE_PARTIALS(&(opt->previous_option->pt), &(opt->previous_option->ps));
				ps_set_sense(opt->ps, q, q_sense);
				set_links(ap, mp, opt, sent);

				//DEBUG
				//printf("PREDIC_YES\n");
				continue;
			}
		}
		if (as.acts[SRL_FLIP] && ap->intern_srl_deproj != DEPROJ_EXHAUSTIVE) {
			opt = _action_srl_flip(ap, mp, sent, do_free_partials, opt);
			//DEBUG
			//printf("SRL_FLIP\n");

			continue;
		}

		// if stalled in syntactic part
		if (IS_NOW_SYNTAX(opt->previous_act)) {
			//DEBUG
			printf("STALLED IN SYNTAX\n");

		} else {
			//DEBUG
			printf("STALLED IN SRL\n");
		}

		ASSERT(0);
		if (IS_NOW_SYNTAX(opt->previous_act)) {
			fprintf(stderr, "(Syntax) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n");
			printf("(Syntax) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n");
		} else {
			//TMP fprintf(stderr, "(SRL) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n");
			//TMP printf("(SRL) Problem: wrong parsing order or in input file (e.g. NON-PROJECTIVITY): can't make an action\n");
		}
		fprintf(stderr, "Try changing PARSING_ORDER in the configuration file to a larger value\n");
		//        printf("%s\n", print_sent(sent, 1));
		//TODO this should be a fatal error
		fprintf(stderr, "Warning: returning partial derivation\n");
		DEF_ALLOC(as_final, ACTION_SET);
		bzero(as_final, sizeof(ACTION_SET));
		set_mask(mp, opt, as_final);
		free(as_final);

		TRY_FREE_PARTIALS(&(opt->pt), &(opt->ps));
		return opt;
		//TODO restore!
		//exit(1);
	}

	//DEBUG
	//TMP printf("==== SENT FINISHED ===== \n");

	if (!check_pt_t_equality(opt->pt, sent)) {
		fprintf(stderr, "Presumably: bug in parsing or in input file (e.g. NON-PROJECTIVITY): resulting syntactic trees do not match\n");
		exit(1);
	}
	if (!check_ps_t_equality(opt->ps, sent)) {
		fprintf(stderr, "Presumably: bug in SRL parsing or in input file (e.g. NON-PROJECTIVITY): resulting predicate argument structures do not match\n");

	}


	DEF_ALLOC(as, ACTION_SET);
	bzero(as, sizeof(ACTION_SET));
	set_mask(mp, opt, as);
	free(as);

	TRY_FREE_PARTIALS(&(opt->pt), &(opt->ps));



	return opt;
}
Exemplo n.º 10
0
gint main (gint argc, gchar ** argv)
{
  gint ret = 0;

  VsgVector2d lb = {-1., -1.};
  VsgVector2d ub = {1., 1.};

  VsgPRTree2d *tree;
  VsgPRTree2d *treeref;
  gint i;
  gint ref_far_count;

  vsg_init_gdouble ();

  parse_args (argc, argv);

#ifdef VSG_HAVE_MPI
  if (_mpi)
    {
      MPI_Init (&argc, &argv);

      MPI_Comm_size (MPI_COMM_WORLD, &sz);
      MPI_Comm_rank (MPI_COMM_WORLD, &rk);

      if (counts != NULL)
        {
          gint i = 0;
          gint tmp = 0;

          while (counts[i+1] != NULL && i<rk) i ++;

	  if (sscanf (counts[i], "%u", &tmp) == 1)
            _expect_far_count = tmp;
	  else
	    test_printerr ("Invalid expected far count list (--expect-far-counts)\n");

          g_strfreev (counts);
        }
    }
#endif

  points_array = g_ptr_array_new ();

  if (rk == 0) _fill (&_npoints, points_array);
  else _npoints = 0;

  pointsref_array = g_ptr_array_sized_new (_npoints);
  for (i=0; i<_npoints; i++)
    {
      Pt *ptref = pt_alloc (TRUE, pointsref_array);
      memcpy (ptref, (Pt *) g_ptr_array_index (points_array, i), sizeof (Pt));
    }

  /* create the trees */
  tree =
    vsg_prtree2d_new_full (&lb, &ub,
                           (VsgPoint2dLocFunc) vsg_vector2d_vector2d_locfunc,
                           (VsgPoint2dDistFunc) vsg_vector2d_dist,
                           NULL, _maxbox);
  treeref = vsg_prtree2d_clone (tree);

  vsg_prtree2d_set_nf_isleaf (tree, _nf_isleaf_virtual_maxbox,
                              &_virtual_maxbox);

  vsg_parallel_vtable_set (&pconfig.node_data,
                           node_counter_alloc, NULL,
                           node_counter_destroy, NULL);
  vsg_prtree2d_set_node_data_vtable (tree, &pconfig.node_data);

  vsg_parallel_vtable_set (&prefconfig.node_data,
                           node_counter_alloc, NULL,
                           node_counter_destroy, NULL);
  vsg_prtree2d_set_node_data_vtable (treeref, &prefconfig.node_data);

#ifdef VSG_HAVE_MPI

  if (_mpi)
    {
      vsg_parallel_vtable_set (&pconfig.point,
                               pt_alloc, points_array,
                               pt_destroy, points_array);

      vsg_parallel_vtable_set (&prefconfig.point,
                               pt_alloc, pointsref_array,
                               pt_destroy, pointsref_array);

      vsg_parallel_vtable_set_parallel (&pconfig.point,
                                        pt_migrate_pack, NULL,
                                        pt_migrate_unpack, NULL,
                                        NULL, NULL,
                                        pt_visit_fw_pack, NULL,
                                        pt_visit_fw_unpack, NULL,
                                        NULL, NULL,
                                        pt_visit_bw_pack, NULL,
                                        pt_visit_bw_unpack, NULL,
                                        pt_visit_bw_reduce, NULL);

      vsg_parallel_vtable_set_parallel (&pconfig.node_data,
                                        nc_migrate_pack, NULL,
                                        nc_migrate_unpack, NULL,
                                        NULL, NULL,
                                        nc_visit_fw_pack, NULL,
                                        nc_visit_fw_unpack, NULL,
                                        nc_visit_fw_reduce, NULL,
                                        nc_visit_bw_pack, NULL,
                                        nc_visit_bw_unpack, NULL,
                                        nc_visit_bw_reduce, NULL);

      vsg_parallel_vtable_set_parallel (&prefconfig.point,
                                        pt_migrate_pack, NULL,
                                        pt_migrate_unpack, NULL,
                                        NULL, NULL,
                                        pt_visit_fw_pack, NULL,
                                        pt_visit_fw_unpack, NULL,
                                        NULL, NULL,
                                        pt_visit_bw_pack, NULL,
                                        pt_visit_bw_unpack, NULL,
                                        pt_visit_bw_reduce, NULL);

      vsg_parallel_vtable_set_parallel (&prefconfig.node_data,
                                        nc_migrate_pack, NULL,
                                        nc_migrate_unpack, NULL,
                                        NULL, NULL,
                                        nc_visit_fw_pack, NULL,
                                        nc_visit_fw_unpack, NULL,
                                        nc_visit_fw_reduce, NULL,
                                        nc_visit_bw_pack, NULL,
                                        nc_visit_bw_unpack, NULL,
                                        nc_visit_bw_reduce, NULL);

      vsg_prtree_parallel_config_set_communicator (&pconfig, MPI_COMM_WORLD);
      vsg_prtree_parallel_config_set_communicator (&prefconfig, MPI_COMM_WORLD);

      vsg_prtree2d_set_parallel (tree, &pconfig);

      vsg_prtree2d_set_parallel (treeref, &prefconfig);
    }
#endif

  /* insert points */
  for (i=0; i<_npoints; i++)
    {
      if (_verbose)
	{
	  test_printerr ("points[%d]: ", i);
	  vsg_vector2d_write (g_ptr_array_index (points_array, i), stderr);
	  g_printerr ("\n");
	}

      vsg_prtree2d_insert_point (tree, g_ptr_array_index (points_array, i));
      vsg_prtree2d_insert_point (treeref,
                                 g_ptr_array_index (pointsref_array, i));
    }

#ifdef VSG_HAVE_MPI
  if (_mpi)
    {
      vsg_prtree2d_migrate_flush (tree);
      vsg_prtree2d_migrate_flush (treeref);

      vsg_prtree2d_distribute_contiguous_leaves (tree);
      vsg_prtree2d_distribute_contiguous_leaves (treeref);
    }
#endif

  if (_write)
    {
      gchar fn[128];
      FILE *f;

      g_sprintf (fn, "tree-%03d.txt", rk);
      f = fopen (fn, "w");
      vsg_prtree2d_write (tree, f);
      fclose (f);
      g_sprintf (fn, "treeref-%03d.txt", rk);
      f = fopen (fn, "w");
      vsg_prtree2d_write (treeref, f);
      fclose (f);
    }

  /* compute neaf/far interactions for treeref */
  _far_count = 0;
  _do_upward_pass (treeref);
  vsg_prtree2d_near_far_traversal (treeref, (VsgPRTree2dFarInteractionFunc) _far,
                                   (VsgPRTree2dInteractionFunc) _near,
                                   &ret);
  vsg_prtree2d_traverse (treeref, G_PRE_ORDER, (VsgPRTree2dFunc) _down, NULL);
  ref_far_count = _far_count;

  /* compute neaf/far interactions for tree */
  _far_count = 0;
  _do_upward_pass (tree);
  vsg_prtree2d_near_far_traversal (tree, (VsgPRTree2dFarInteractionFunc) _far,
                                   (VsgPRTree2dInteractionFunc) _near,
                                   &ret);
  vsg_prtree2d_traverse (tree, G_PRE_ORDER, (VsgPRTree2dFunc) _down, NULL);

#ifdef VSG_HAVE_MPI
  if (_mpi)
    {
      /* migrate points back to processor 0 before checking */
      vsg_prtree2d_distribute_concentrate (tree, 0);
      vsg_prtree2d_distribute_concentrate (treeref, 0);
    }
#endif

  /* check results */
  for (i=0; i<points_array->len; i++)
    {
      Pt *pt = g_ptr_array_index (points_array, i);
      /* FIXME: what if pointsref and points are no longer in the same order? */
      Pt *ptref = g_ptr_array_index (pointsref_array, i);

      if (pt->count != ptref->count)
        {
          test_printerr ("error pt[");
          vsg_vector2d_write (&pt->vector, stderr);
          g_printerr ("]=%ld ptref[", pt->count);
          vsg_vector2d_write (&ptref->vector, stderr);
          g_printerr ("]=%ld\n", ptref->count);
        }
      else if (_verbose)
        {
          test_printerr ("correct comparison [");
          vsg_vector2d_write (&pt->vector, stderr);
          g_printerr ("]=%ld ptref[", pt->count);
          vsg_vector2d_write (&ptref->vector, stderr);
          g_printerr ("]=%ld\n", ptref->count);
        }
    }

  if ((_expect_far_count >= 0) && (_expect_far_count != _far_count))
    test_printerr ("far_count=%d != expected far_count=%d (ref=%d)\n",
                   _far_count, _expect_far_count, ref_far_count);
  else if (_verbose)
    test_printerr ("far_count=%d == expected far_count=%d (ref=%d)\n",
                   _far_count, _expect_far_count, ref_far_count);

  /* remove the points */
  for (i=0; i<points_array->len; i++)
    {
      vsg_prtree2d_remove_point (tree, g_ptr_array_index (points_array, i));
      vsg_prtree2d_remove_point (treeref,
                                 g_ptr_array_index (pointsref_array, i));
    }

  /* destroy the trees */
  vsg_prtree2d_free (tree);
  vsg_prtree2d_free (treeref);

  for (i=0; i<points_array->len; i++)
    pt_destroy (g_ptr_array_index (points_array, i), FALSE, points_array);

  for (i=0; i<pointsref_array->len; i++)
    pt_destroy (g_ptr_array_index (pointsref_array, i), FALSE, pointsref_array);


  g_ptr_array_free (points_array, TRUE);
  g_ptr_array_free (pointsref_array, TRUE);

#ifdef VSG_HAVE_MPI
  if (_mpi)
    MPI_Finalize ();
#endif

  return ret;
}