Exemplo n.º 1
0
void make_root_frames(void) {
#ifdef YAPOR
    /* root or frame */
    or_fr_ptr or_fr;

    ALLOC_OR_FRAME(or_fr);
    INIT_LOCK(OrFr_lock(or_fr));
    OrFr_alternative(or_fr) = NULL;
    BITMAP_copy(OrFr_members(or_fr), GLOBAL_bm_present_workers);
    SetOrFr_node(or_fr, (choiceptr) Yap_LocalBase);
    OrFr_nearest_livenode(or_fr) = NULL;
    OrFr_depth(or_fr) = 0;
    Set_OrFr_pend_prune_cp(or_fr, NULL);
    OrFr_nearest_leftnode(or_fr) = or_fr;
    OrFr_qg_solutions(or_fr) = NULL;
#ifdef TABLING_INNER_CUTS
    OrFr_tg_solutions(or_fr) = NULL;
#endif /* TABLING_INNER_CUTS */
#ifdef TABLING
    OrFr_owners(or_fr) = number_workers;
    OrFr_next_on_stack(or_fr) = NULL;
    OrFr_suspensions(or_fr) = NULL;
    OrFr_nearest_suspnode(or_fr) = or_fr;
#endif /* TABLING */
    OrFr_next(or_fr) = NULL;
    GLOBAL_root_or_fr = or_fr;
#endif /* YAPOR */

#ifdef TABLING
    /* root dependency frame */
    if (!GLOBAL_root_dep_fr) {
        new_dependency_frame(GLOBAL_root_dep_fr, FALSE, NULL, NULL, NULL, NULL, NULL);
    }
#endif /* TABLING */
}
Exemplo n.º 2
0
void resume_suspension_frame(susp_fr_ptr resume_fr, or_fr_ptr top_or_fr) {
  CACHE_REGS
  or_fr_ptr or_frame;
  sg_fr_ptr sg_frame;

  /* copy suspended stacks */
  memmove(SuspFr_global_reg(resume_fr),
         SuspFr_global_start(resume_fr),
         SuspFr_global_size(resume_fr));
  memmove(SuspFr_local_reg(resume_fr),
         SuspFr_local_start(resume_fr),
         SuspFr_local_size(resume_fr));
  memmove(SuspFr_trail_reg(resume_fr),
         SuspFr_trail_start(resume_fr),
         SuspFr_trail_size(resume_fr));

  OPTYAP_ERROR_CHECKING(resume_suspension_frame, DepFr_cons_cp(SuspFr_top_dep_fr(resume_fr))->cp_h != SuspFr_global_reg(resume_fr) + SuspFr_global_size(resume_fr));
  OPTYAP_ERROR_CHECKING(resume_suspension_frame, DepFr_cons_cp(SuspFr_top_dep_fr(resume_fr))->cp_tr != SuspFr_trail_reg(resume_fr) + SuspFr_trail_size(resume_fr));
  OPTYAP_ERROR_CHECKING(resume_suspension_frame, DepFr_cons_cp(SuspFr_top_dep_fr(resume_fr)) != SuspFr_local_reg(resume_fr));
  OPTYAP_ERROR_CHECKING(resume_suspension_frame, (void *)Get_LOCAL_top_cp() < SuspFr_local_reg(resume_fr) + SuspFr_local_size(resume_fr));

  /* update shared nodes */
  or_frame = top_or_fr;
  while (or_frame != LOCAL_top_or_fr) {
    LOCK_OR_FRAME(or_frame);
    OrFr_owners(or_frame)++;
    UNLOCK_OR_FRAME(or_frame);
    or_frame = OrFr_next_on_stack(or_frame);
  }  
  or_frame = top_or_fr;
  while (or_frame != LOCAL_top_or_fr) {
    LOCK_OR_FRAME(or_frame);
    BITMAP_insert(OrFr_members(or_frame), worker_id);
    BRANCH(worker_id, OrFr_depth(or_frame)) = 1;
    UNLOCK_OR_FRAME(or_frame);
    or_frame = OrFr_next(or_frame);
  }

  /* adjust top pointers */
  LOCAL_top_or_fr = top_or_fr;
  SetOrFr_node(top_or_fr, Get_LOCAL_top_cp());
  LOCAL_top_sg_fr = SuspFr_top_sg_fr(resume_fr);
  LOCAL_top_dep_fr = SuspFr_top_dep_fr(resume_fr);
  Set_LOCAL_top_cp_on_stack( GetOrFr_node(SuspFr_top_or_fr_on_stack(resume_fr)) );
  sg_frame = LOCAL_top_sg_fr;
  while (sg_frame && YOUNGER_CP(SgFr_gen_cp(sg_frame), Get_LOCAL_top_cp_on_stack())) {
    SgFr_gen_worker(sg_frame) = worker_id;
    sg_frame = SgFr_next(sg_frame);
  }

  /* adjust freeze registers */
  adjust_freeze_registers();

  /* free suspension frame */
  FREE_SUSPENSION_FRAME(resume_fr);

  return;
}
Exemplo n.º 3
0
void complete_suspension_frames(or_fr_ptr or_fr) {
  CACHE_REGS
  dep_fr_ptr chain_dep_fr;
  or_fr_ptr chain_or_fr;
  susp_fr_ptr susp_fr;
  qg_sol_fr_ptr solutions;

  /* complete suspension frames */
  chain_dep_fr = NULL;
  chain_or_fr = NULL;
  susp_fr = OrFr_suspensions(or_fr);
  do {
    susp_fr_ptr next_susp_fr;
    complete_suspension_branch(susp_fr, GetOrFr_node(or_fr), &chain_or_fr, &chain_dep_fr);
    next_susp_fr = SuspFr_next(susp_fr);
    FREE_SUSPENSION_FRAME(susp_fr);
    susp_fr = next_susp_fr;
  } while (susp_fr);
  OrFr_suspensions(or_fr) = NULL;
  OrFr_nearest_suspnode(or_fr) = or_fr;

  /* release dependency frames */
  while (chain_dep_fr) {
    dep_fr_ptr next_dep_fr;
    next_dep_fr = DepFr_next(chain_dep_fr);
    FREE_DEPENDENCY_FRAME(chain_dep_fr);
    chain_dep_fr = next_dep_fr;
  }

  /* release or frames */
  solutions = NULL;
  while (chain_or_fr) {
    or_fr_ptr next_or_fr;
    qg_sol_fr_ptr aux_solutions;
    aux_solutions = OrFr_qg_solutions(chain_or_fr);
    if (aux_solutions) {
      CUT_join_answers_in_an_unique_frame(aux_solutions);
      SolFr_next(aux_solutions) = solutions;
      solutions = aux_solutions;
    }
    next_or_fr = OrFr_next_on_stack(chain_or_fr);
    FREE_OR_FRAME(chain_or_fr);
    chain_or_fr = next_or_fr;
  }
  if (solutions) {
    CUT_join_answers_in_an_unique_frame(solutions);
    SolFr_next(solutions) = OrFr_qg_solutions(or_fr);
    OrFr_qg_solutions(LOCAL_top_or_fr) = solutions;
  }

  return;
}
Exemplo n.º 4
0
void Yap_init_root_frames(void) {
  CACHE_REGS

#ifdef YAPOR
  /* root or frame */
  or_fr_ptr or_fr = GLOBAL_root_or_fr;
  INIT_LOCK(OrFr_lock(or_fr));
  OrFr_alternative(or_fr) = NULL;
  BITMAP_copy(OrFr_members(or_fr), GLOBAL_bm_present_workers);
  SetOrFr_node(or_fr, (choiceptr)LOCAL_LocalBase);
  OrFr_nearest_livenode(or_fr) = NULL;
  OrFr_depth(or_fr) = 0;
  Set_OrFr_pend_prune_cp(or_fr, NULL);
  OrFr_nearest_leftnode(or_fr) = or_fr;
  OrFr_qg_solutions(or_fr) = NULL;
#ifdef TABLING_INNER_CUTS
  OrFr_tg_solutions(or_fr) = NULL;
#endif /* TABLING_INNER_CUTS */
#ifdef TABLING
  OrFr_owners(or_fr) = GLOBAL_number_workers;
  OrFr_next_on_stack(or_fr) = NULL;
  OrFr_suspensions(or_fr) = NULL;
  OrFr_nearest_suspnode(or_fr) = or_fr;
#endif /* TABLING */
  OrFr_next(or_fr) = NULL;
#endif /* YAPOR */

#ifdef TABLING
  /* root global trie node */
  new_global_trie_node(GLOBAL_root_gt, 0, NULL, NULL, NULL);
/* root dependency frame */
#ifdef YAPOR
  DepFr_cons_cp(GLOBAL_root_dep_fr) = B; /* with YAPOR, at that point,
                                            LOCAL_top_dep_fr shouldn't be the
                                            same as GLOBAL_root_dep_fr ? */
#else
  new_dependency_frame(LOCAL_top_dep_fr, FALSE, NULL, NULL, B, NULL, FALSE,
                       NULL);
#endif /* YAPOR */
#endif /* TABLING */
}
Exemplo n.º 5
0
static
void share_private_nodes(int worker_q) {
  choiceptr sharing_node = B;

#ifdef DEBUG_OPTYAP
  OPTYAP_ERROR_CHECKING(share_private_nodes, YOUNGER_CP(LOCAL_top_cp, LOCAL_top_cp_on_stack));
  { choiceptr aux_cp = B;
    while (aux_cp != LOCAL_top_cp) {
      OPTYAP_ERROR_CHECKING(share_private_nodes, YOUNGER_CP(LOCAL_top_cp, aux_cp));
      OPTYAP_ERROR_CHECKING(share_private_nodes, EQUAL_OR_YOUNGER_CP(LOCAL_top_cp_on_stack, aux_cp));
      aux_cp = aux_cp->cp_b;
    }
  }
#endif /* DEBUG_OPTYAP */

#ifdef TABLING
  /* check if the branch is already shared */
  if (EQUAL_OR_YOUNGER_CP(LOCAL_top_cp_on_stack, sharing_node)) {
    or_fr_ptr or_frame;
    sg_fr_ptr sg_frame;
    dep_fr_ptr dep_frame;

#ifdef DEBUG_OPTYAP
    { or_fr_ptr aux_or_fr;
      aux_or_fr = LOCAL_top_or_fr;
      while (aux_or_fr != REMOTE_top_or_fr(worker_q)) {
	OPTYAP_ERROR_CHECKING(share_private_nodes, YOUNGER_CP(OrFr_node(REMOTE_top_or_fr(worker_q)), OrFr_node(aux_or_fr)));
        aux_or_fr = OrFr_next_on_stack(aux_or_fr);
      }
    }
#endif /* DEBUG_OPTYAP */

    /* update old shared nodes */
    or_frame = LOCAL_top_or_fr;
    while (or_frame != REMOTE_top_or_fr(worker_q)) {
      LOCK_OR_FRAME(or_frame);
      BRANCH(worker_q, OrFr_depth(or_frame)) = BRANCH(worker_id, OrFr_depth(or_frame));
      OrFr_owners(or_frame)++;
      if (BITMAP_member(OrFr_members(or_frame), worker_id))
        BITMAP_insert(OrFr_members(or_frame), worker_q);
      UNLOCK_OR_FRAME(or_frame);
      or_frame = OrFr_next_on_stack(or_frame);
    }

    /* update worker Q top subgoal frame */
    sg_frame = LOCAL_top_sg_fr;
    while (sg_frame && YOUNGER_CP(SgFr_gen_cp(sg_frame), sharing_node)) {
      sg_frame = SgFr_next(sg_frame);
    }
    REMOTE_top_sg_fr(worker_q) = sg_frame;

    /* update worker Q top dependency frame */
    dep_frame = LOCAL_top_dep_fr;
    while (YOUNGER_CP(DepFr_cons_cp(dep_frame), sharing_node)) {
      dep_frame = DepFr_next(dep_frame);
    }
    REMOTE_top_dep_fr(worker_q) = dep_frame;

    /* update worker Q top shared nodes */
    REMOTE_top_cp_on_stack(worker_q) = REMOTE_top_cp(worker_q) = LOCAL_top_cp;
    REMOTE_top_or_fr(worker_q) = LOCAL_top_or_fr;
  } else
#endif /* TABLING */
  {
    int depth;
    bitmap bm_workers;
    or_fr_ptr or_frame, previous_or_frame;
#ifdef TABLING
    choiceptr consumer_cp, next_node_on_branch;
    dep_fr_ptr dep_frame;
    sg_fr_ptr sg_frame;
    CELL *stack, *stack_limit;

    /* find top dependency frame above current choice point */
    dep_frame = LOCAL_top_dep_fr;
    while (EQUAL_OR_YOUNGER_CP(DepFr_cons_cp(dep_frame), sharing_node)) {
      dep_frame = DepFr_next(dep_frame);
    }
    /* initialize tabling auxiliary variables */ 
    consumer_cp = DepFr_cons_cp(dep_frame);
    next_node_on_branch = NULL;
    stack_limit = (CELL *)TR;
    stack = (CELL *)LOCAL_TrailTop;
#endif /* TABLING */

    /* initialize auxiliary variables */
    BITMAP_clear(bm_workers);
    BITMAP_insert(bm_workers, worker_id);
    BITMAP_insert(bm_workers, worker_q);
    previous_or_frame = NULL;
    depth = OrFr_depth(LOCAL_top_or_fr);

    /* sharing loop */
#ifdef TABLING
    while (YOUNGER_CP(sharing_node, LOCAL_top_cp_on_stack)) {
#else
    while (sharing_node != LOCAL_top_cp) {
#endif /* TABLING */

#ifdef DEBUG_OPTYAP
      if (next_node_on_branch) {
        choiceptr aux_cp = B;
        while (aux_cp != next_node_on_branch) {
	  OPTYAP_ERROR_CHECKING(share_private_nodes, sharing_node == aux_cp);
	  OPTYAP_ERROR_CHECKING(share_private_nodes, YOUNGER_CP(next_node_on_branch, aux_cp));
          aux_cp = aux_cp->cp_b;
        }
      } else {
        choiceptr aux_cp = B;
        while (aux_cp != sharing_node) {
	  OPTYAP_ERROR_CHECKING(share_private_nodes, YOUNGER_CP(sharing_node, aux_cp));
          aux_cp = aux_cp->cp_b;
        }
      }
#endif /* DEBUG_OPTYAP */

      ALLOC_OR_FRAME(or_frame);
      if (previous_or_frame) {
#ifdef TABLING
        OrFr_next_on_stack(previous_or_frame) =
#endif /* TABLING */
        OrFr_nearest_livenode(previous_or_frame) = OrFr_next(previous_or_frame) = or_frame;
      }
      previous_or_frame = or_frame;
      depth++;
      INIT_LOCK(OrFr_lock(or_frame));
      OrFr_node(or_frame) = sharing_node;
      OrFr_alternative(or_frame) = sharing_node->cp_ap;
      OrFr_pend_prune_cp(or_frame) = NULL;
      OrFr_nearest_leftnode(or_frame) = LOCAL_top_or_fr;
      OrFr_qg_solutions(or_frame) = NULL;
#ifdef TABLING_INNER_CUTS
      OrFr_tg_solutions(or_frame) = NULL;
#endif /* TABLING_INNER_CUTS */
#ifdef TABLING
      OrFr_suspensions(or_frame) = NULL;
      OrFr_nearest_suspnode(or_frame) = or_frame;
      OrFr_owners(or_frame) = 2;
      if (next_node_on_branch)
        BITMAP_clear(OrFr_members(or_frame));
      else
#endif /* TABLING */
        OrFr_members(or_frame) = bm_workers;

      YAPOR_ERROR_CHECKING(share_private_nodes, sharing_node->cp_ap == GETWORK || sharing_node->cp_ap == GETWORK_SEQ);
      if (sharing_node->cp_ap && YAMOP_SEQ(sharing_node->cp_ap)) {
        sharing_node->cp_ap = GETWORK_SEQ;
      } else {
        sharing_node->cp_ap = GETWORK;
      }
      sharing_node->cp_or_fr = or_frame;
      sharing_node = sharing_node->cp_b;

#ifdef TABLING
      /* when next_node_on_branch is not NULL the **
      ** sharing_node belongs to a frozen branch. */   
      if (YOUNGER_CP(consumer_cp, sharing_node)) {
        /* frozen stack segment */
        if (! next_node_on_branch)
          next_node_on_branch = sharing_node;
        STACK_PUSH_UP(or_frame, stack);
        STACK_CHECK_EXPAND(stack, stack_limit);
        STACK_PUSH_UP(sharing_node, stack);
        STACK_CHECK_EXPAND(stack, stack_limit);
        sharing_node = consumer_cp;
        dep_frame = DepFr_next(dep_frame);
        consumer_cp = DepFr_cons_cp(dep_frame);
      } else if (consumer_cp == sharing_node) {
        dep_frame = DepFr_next(dep_frame);
        consumer_cp = DepFr_cons_cp(dep_frame);
      }
      if (next_node_on_branch == sharing_node)
        next_node_on_branch = NULL;
#endif /* TABLING */
      OPTYAP_ERROR_CHECKING(share_private_nodes, next_node_on_branch && YOUNGER_CP(next_node_on_branch, sharing_node));
    }

    /* initialize last or-frame pointer */
    or_frame = sharing_node->cp_or_fr;
    if (previous_or_frame) {
#ifdef TABLING
      OrFr_next_on_stack(previous_or_frame) =
#endif /* TABLING */
      OrFr_nearest_livenode(previous_or_frame) = OrFr_next(previous_or_frame) = or_frame;
    }

#ifdef TABLING
    /* update or-frames stored in auxiliary stack */
    while (STACK_NOT_EMPTY(stack, (CELL *)LOCAL_TrailTop)) {
      next_node_on_branch = (choiceptr) STACK_POP_DOWN(stack);
      or_frame = (or_fr_ptr) STACK_POP_DOWN(stack);
      OrFr_nearest_livenode(or_frame) = OrFr_next(or_frame) = next_node_on_branch->cp_or_fr;
    }
#endif /* TABLING */

    /* update depth */
    if (depth >= MAX_BRANCH_DEPTH)
      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "maximum depth exceded (share_private_nodes)");
    or_frame = B->cp_or_fr;
#ifdef TABLING
    previous_or_frame = LOCAL_top_cp_on_stack->cp_or_fr;
    while (or_frame != previous_or_frame) {
#else
    while (or_frame != LOCAL_top_or_fr) {
#endif /* TABLING */
      unsigned int branch;
      if (OrFr_alternative(or_frame)) {
        branch = YAMOP_OR_ARG(OrFr_alternative(or_frame)) + 1;
      } else {
        branch = 1;
      }
      branch |= YAMOP_CUT_FLAG;  /* in doubt, assume cut */
      BRANCH(worker_id, depth) = BRANCH(worker_q, depth) = branch;
      OrFr_depth(or_frame) = depth--;
      or_frame = OrFr_next_on_stack(or_frame);
    }

    YAPOR_ERROR_CHECKING(share_private_nodes, depth != OrFr_depth(LOCAL_top_or_fr));

#ifdef DEBUG_OPTYAP
    { or_fr_ptr aux_or_fr = B->cp_or_fr;
      choiceptr aux_cp;
      while (aux_or_fr != LOCAL_top_cp_on_stack->cp_or_fr) {
        aux_cp = OrFr_node(aux_or_fr);
	OPTYAP_ERROR_CHECKING(share_private_nodes, OrFr_next(aux_or_fr) != aux_cp->cp_b->cp_or_fr);
	OPTYAP_ERROR_CHECKING(share_private_nodes, OrFr_nearest_livenode(aux_or_fr) != aux_cp->cp_b->cp_or_fr);
        aux_or_fr = OrFr_next_on_stack(aux_or_fr);
      }
      aux_or_fr = B->cp_or_fr;
      while (aux_or_fr != LOCAL_top_cp_on_stack->cp_or_fr) {
        or_fr_ptr nearest_leftnode = OrFr_nearest_leftnode(aux_or_fr);
        aux_cp = OrFr_node(aux_or_fr);
        while (OrFr_node(nearest_leftnode) != aux_cp) {
	  OPTYAP_ERROR_CHECKING(share_private_nodes, YOUNGER_CP(OrFr_node(nearest_leftnode), aux_cp));
          aux_cp = aux_cp->cp_b;
        }
        aux_or_fr = OrFr_next_on_stack(aux_or_fr);
      }
    }
#endif /* DEBUG_OPTYAP */

    /* update old shared nodes */
    while (or_frame != REMOTE_top_or_fr(worker_q)) {
      LOCK_OR_FRAME(or_frame);
      BRANCH(worker_q, OrFr_depth(or_frame)) = BRANCH(worker_id, OrFr_depth(or_frame));
#ifdef TABLING
      OrFr_owners(or_frame)++;
      if (BITMAP_member(OrFr_members(or_frame), worker_id))
#endif /* TABLING */
        BITMAP_insert(OrFr_members(or_frame), worker_q);
      UNLOCK_OR_FRAME(or_frame);
      or_frame = OrFr_next_on_stack(or_frame);
    }

    LOCK_OR_FRAME(REMOTE_top_or_fr(worker_q));
    or_fr_ptr old_top = REMOTE_top_or_fr(worker_q);
    Set_REMOTE_top_cp(worker_q,B);
    Set_LOCAL_top_cp(B);
    REMOTE_top_or_fr(worker_q) = LOCAL_top_or_fr = Get_LOCAL_top_cp()->cp_or_fr;
    UNLOCK_OR_FRAME(old_top);  

#ifdef TABLING
    /* update subgoal frames in the maintained private branches */
    sg_frame = LOCAL_top_sg_fr;
    while (sg_frame && YOUNGER_CP(SgFr_gen_cp(sg_frame), B)) {
      choiceptr top_cp_on_branch;
      top_cp_on_branch = SgFr_gen_cp(sg_frame);
      while (YOUNGER_CP(top_cp_on_branch, B)) {
        top_cp_on_branch = top_cp_on_branch->cp_b;
      }
      SgFr_gen_top_or_fr(sg_frame) = top_cp_on_branch->cp_or_fr;
      sg_frame = SgFr_next(sg_frame);
    }
    /* update worker Q top subgoal frame */
    REMOTE_top_sg_fr(worker_q) = sg_frame;
    /* update subgoal frames in the recently shared branches */
    while (sg_frame && YOUNGER_CP(SgFr_gen_cp(sg_frame), LOCAL_top_cp_on_stack)) {
      SgFr_gen_worker(sg_frame) = MAX_WORKERS;
      SgFr_gen_top_or_fr(sg_frame) = SgFr_gen_cp(sg_frame)->cp_or_fr;
      sg_frame = SgFr_next(sg_frame);
    }

    /* update dependency frames in the maintained private branches */
    dep_frame = LOCAL_top_dep_fr;
    while (YOUNGER_CP(DepFr_cons_cp(dep_frame), B)) {
      choiceptr top_cp_on_branch;
      top_cp_on_branch = DepFr_cons_cp(dep_frame);
      while (YOUNGER_CP(top_cp_on_branch, B)) {
        top_cp_on_branch = top_cp_on_branch->cp_b;
      }
      DepFr_top_or_fr(dep_frame) = top_cp_on_branch->cp_or_fr;
      dep_frame = DepFr_next(dep_frame);
    }
    /* update worker Q top dependency frame */
    REMOTE_top_dep_fr(worker_q) = dep_frame;
    /* update dependency frames in the recently shared branches */
    while (YOUNGER_CP(DepFr_cons_cp(dep_frame), LOCAL_top_cp_on_stack)) {
      DepFr_top_or_fr(dep_frame) = DepFr_cons_cp(dep_frame)->cp_or_fr;
      dep_frame = DepFr_next(dep_frame);
    }
#endif /* TABLING */

#ifdef DEBUG_OPTYAP
    { dep_fr_ptr aux_dep_fr = LOCAL_top_dep_fr;
      while(aux_dep_fr != GLOBAL_root_dep_fr) {
        choiceptr top_cp_on_branch;
        top_cp_on_branch = DepFr_cons_cp(aux_dep_fr);
        while (YOUNGER_CP(top_cp_on_branch, B)) {
          top_cp_on_branch = top_cp_on_branch->cp_b;
        }
	OPTYAP_ERROR_CHECKING(share_private_nodes, top_cp_on_branch->cp_or_fr != DepFr_top_or_fr(aux_dep_fr));
        aux_dep_fr = DepFr_next(aux_dep_fr);
      }
    }
#endif /* DEBUG_OPTYAP */

    /* update top shared nodes */
#ifdef TABLING
    REMOTE_top_cp_on_stack(worker_q) = LOCAL_top_cp_on_stack = 
#endif /* TABLING */
    REMOTE_top_cp(worker_q) = LOCAL_top_cp = B;
    REMOTE_top_or_fr(worker_q) = LOCAL_top_or_fr = LOCAL_top_cp->cp_or_fr;
  }

#ifdef TABLING_INNER_CUTS
  /* update worker Q pruning scope */
  if (LOCAL_pruning_scope && EQUAL_OR_YOUNGER_CP(LOCAL_top_cp, LOCAL_pruning_scope)) {
    REMOTE_pruning_scope(worker_q) = LOCAL_pruning_scope;
    PUT_IN_PRUNING(worker_q);
  } else {
    PUT_OUT_PRUNING(worker_q);
    REMOTE_pruning_scope(worker_q) = NULL;
  }
#endif /* TABLING_INNER_CUTS */

  /* update worker Q prune request */
  if (LOCAL_prune_request) {
    CUT_send_prune_request(worker_q, LOCAL_prune_request);
  }

  /* update load and return */
  REMOTE_load(worker_q) = LOCAL_load = 0;
  return;
}
Exemplo n.º 6
0
static void complete_suspension_branch(susp_fr_ptr susp_fr, choiceptr top_cp, or_fr_ptr *chain_or_fr, dep_fr_ptr *chain_dep_fr) {
  or_fr_ptr aux_or_fr;
  sg_fr_ptr aux_sg_fr;
  dep_fr_ptr aux_dep_fr;

  /* complete all subgoals */
  aux_dep_fr = SuspFr_top_dep_fr(susp_fr);
  aux_sg_fr = SuspFr_top_sg_fr(susp_fr);
  if (DepFr_leader_dep_is_on_stack(aux_dep_fr)) {
    while (aux_sg_fr && 
	   /* continue if the subgoal was early completed */ 
           /* SgFr_state(aux_sg_fr) == evaluating && */
           (SgFr_state(aux_sg_fr) == evaluating || SgFr_first_answer(aux_sg_fr) == SgFr_answer_trie(aux_sg_fr)) &&
           EQUAL_OR_YOUNGER_CP(SgFr_gen_cp(aux_sg_fr), top_cp)) {
      mark_as_completed(aux_sg_fr);
      aux_sg_fr = SgFr_next(aux_sg_fr);
    }
  } else {
    while (aux_sg_fr && 
	   /* continue if the subgoal was early completed */ 
           /* SgFr_state(aux_sg_fr) == evaluating && */
           (SgFr_state(aux_sg_fr) == evaluating || SgFr_first_answer(aux_sg_fr) == SgFr_answer_trie(aux_sg_fr)) &&
           YOUNGER_CP(SgFr_gen_cp(aux_sg_fr), top_cp)) {
      mark_as_completed(aux_sg_fr);
      aux_sg_fr = SgFr_next(aux_sg_fr);
    }
  }

  /* chain dependency frames to release (using DepFr_next) */
  while (IS_UNLOCKED_DEP_FR(aux_dep_fr) && 
         YOUNGER_CP(DepFr_cons_cp(aux_dep_fr), top_cp)) {
    dep_fr_ptr next_dep_fr;
    LOCK_DEP_FR(aux_dep_fr);
    next_dep_fr = DepFr_next(aux_dep_fr);
    DepFr_next(aux_dep_fr) = *chain_dep_fr;
    *chain_dep_fr = aux_dep_fr;
    aux_dep_fr = next_dep_fr;
  }

  /* chain or-frames to release (using OrFr_next_on_stack)    **
  ** we use the OrFr_next_on_stack field instead of OrFr_next **
  ** to avoid conflicts with the 'find_dependency_node' macro */
  aux_or_fr = SuspFr_top_or_fr_on_stack(susp_fr);
  while (IS_UNLOCKED(OrFr_lock(aux_or_fr))) {
    susp_fr_ptr aux_susp_fr;
    or_fr_ptr next_or_fr_on_stack;
    OPTYAP_ERROR_CHECKING(complete_suspension_branch, YOUNGER_CP(top_cp, GetOrFr_node(aux_or_fr)));
    LOCK_OR_FRAME(aux_or_fr);
    aux_susp_fr = OrFr_suspensions(aux_or_fr);
    while (aux_susp_fr) {
      susp_fr_ptr next_susp_fr;
      complete_suspension_branch(aux_susp_fr, GetOrFr_node(aux_or_fr), chain_or_fr, chain_dep_fr);
      next_susp_fr = SuspFr_next(aux_susp_fr);
      FREE_SUSPENSION_FRAME(aux_susp_fr);
      aux_susp_fr = next_susp_fr;
    }
    next_or_fr_on_stack = OrFr_next_on_stack(aux_or_fr);
    OrFr_next_on_stack(aux_or_fr) = *chain_or_fr;
    *chain_or_fr = aux_or_fr;
    aux_or_fr = next_or_fr_on_stack;
  }

  return;
}
Exemplo n.º 7
0
void suspend_branch(void) {
  CACHE_REGS
  or_fr_ptr or_frame;

  /* suspension only occurs in shared nodes that **
  **   are leaders with younger consumer nodes   */
#ifdef DEBUG_OPTYAP
  OPTYAP_ERROR_CHECKING(suspend_branch, Get_LOCAL_top_cp()->cp_or_fr != LOCAL_top_or_fr);
  OPTYAP_ERROR_CHECKING(suspend_branch, B_FZ == Get_LOCAL_top_cp());
  OPTYAP_ERROR_CHECKING(suspend_branch, YOUNGER_CP(Get_LOCAL_top_cp(), Get_LOCAL_top_cp_on_stack()));
  OPTYAP_ERROR_CHECKING(suspend_branch, Get_LOCAL_top_cp()->cp_or_fr != LOCAL_top_or_fr);
  or_frame = Get_LOCAL_top_cp_on_stack()->cp_or_fr;
  while (or_frame != LOCAL_top_or_fr) {
    OPTYAP_ERROR_CHECKING(suspend_branch, YOUNGER_CP(Get_LOCAL_top_cp(), GetOrFr_node(or_frame)));
    or_frame = OrFr_next_on_stack(or_frame);
  }
#endif /* DEBUG_OPTYAP */

  or_frame = Get_LOCAL_top_cp_on_stack()->cp_or_fr;
  LOCK_OR_FRAME(or_frame);
  if (B_FZ == Get_LOCAL_top_cp_on_stack() && OrFr_owners(or_frame) > 1) {
    /* there are other workers sharing the whole branch **
    **         --> we can avoid suspension <--          */

    /* update shared nodes */
    OrFr_owners(or_frame)--;
    UNLOCK_OR_FRAME(or_frame);
    or_frame = OrFr_next_on_stack(or_frame);
    while (or_frame != LOCAL_top_or_fr) {
      LOCK_OR_FRAME(or_frame);
      OrFr_owners(or_frame)--;
      UNLOCK_OR_FRAME(or_frame);
      or_frame = OrFr_next_on_stack(or_frame);
    }
  } else {
    /* the branch has private parts **
    **    --> suspend branch <--    */
    susp_fr_ptr new_susp_fr;
    long h_size, b_size, tr_size;
    UNLOCK_OR_FRAME(or_frame);

    /* alloc suspension frame */
    h_size = (unsigned long) H_FZ - (unsigned long) Get_LOCAL_top_cp()->cp_h;
    b_size = (unsigned long) Get_LOCAL_top_cp() - (unsigned long) B_FZ;
    tr_size = (unsigned long) TR_FZ - (unsigned long) Get_LOCAL_top_cp()->cp_tr;
    new_suspension_frame(new_susp_fr, Get_LOCAL_top_cp_on_stack()->cp_or_fr, LOCAL_top_dep_fr, LOCAL_top_sg_fr,
                         Get_LOCAL_top_cp()->cp_h, B_FZ, Get_LOCAL_top_cp()->cp_tr, h_size, b_size, tr_size);

    /* store suspension frame in current top or-frame */
    LOCK_OR_FRAME(LOCAL_top_or_fr);
    if (OrFr_nearest_suspnode(LOCAL_top_or_fr) == LOCAL_top_or_fr)
      OrFr_nearest_suspnode(LOCAL_top_or_fr) = NULL;
    SuspFr_next(new_susp_fr) = OrFr_suspensions(LOCAL_top_or_fr);
    OrFr_suspensions(LOCAL_top_or_fr) = new_susp_fr;
    UNLOCK_OR_FRAME(LOCAL_top_or_fr);
  }

  /* adjust top pointers */
  while (LOCAL_top_sg_fr && YOUNGER_CP(SgFr_gen_cp(LOCAL_top_sg_fr), Get_LOCAL_top_cp_on_stack())) {
    SgFr_gen_worker(LOCAL_top_sg_fr) = MAX_WORKERS;
    LOCAL_top_sg_fr = SgFr_next(LOCAL_top_sg_fr);
  }
  while (LOCAL_top_sg_fr && YOUNGER_CP(SgFr_gen_cp(LOCAL_top_sg_fr), Get_LOCAL_top_cp())) {
    LOCAL_top_sg_fr = SgFr_next(LOCAL_top_sg_fr);
  }
  while (YOUNGER_CP(DepFr_cons_cp(LOCAL_top_dep_fr), Get_LOCAL_top_cp())) {
    LOCAL_top_dep_fr = DepFr_next(LOCAL_top_dep_fr);
  }
  Set_LOCAL_top_cp_on_stack( Get_LOCAL_top_cp() );

  /* adjust freeze registers */
  adjust_freeze_registers();

  return;
}
Exemplo n.º 8
0
void public_completion(void) {
  CACHE_REGS
  dep_fr_ptr chain_dep_fr, next_dep_fr;
  or_fr_ptr chain_or_fr, top_or_fr, next_or_fr;
  susp_fr_ptr susp_fr, next_susp_fr;
  qg_sol_fr_ptr solutions, aux_solutions;

  if (YOUNGER_CP(Get_LOCAL_top_cp(), B_FZ)) {
    /* the current node is a generator node without younger consumer **
    ** nodes --> we only have the current node to complete           */
    sg_fr_ptr top_sg_fr;

    /* complete subgoals */
#ifdef DETERMINISTIC_TABLING
    if (IS_DET_GEN_CP(Get_LOCAL_top_cp()))
      top_sg_fr = SgFr_next(DET_GEN_CP(Get_LOCAL_top_cp())->cp_sg_fr);
    else
#endif /* DETERMINISTIC_TABLING */
      top_sg_fr = SgFr_next(GEN_CP(Get_LOCAL_top_cp())->cp_sg_fr);
    do {
      mark_as_completed(LOCAL_top_sg_fr);
      LOCAL_top_sg_fr = SgFr_next(LOCAL_top_sg_fr);
    } while (LOCAL_top_sg_fr != top_sg_fr);

    /* no dependency frames to release */
    chain_dep_fr = NULL;

    /* no need to adjust freeze registers */
  } else {
    /* the current node is a leader node with younger consumer **
    ** nodes ---> we need to complete all dependent subgoals   */

    /* complete subgoals */
    if (DepFr_leader_dep_is_on_stack(LOCAL_top_dep_fr)) {
      while (LOCAL_top_sg_fr && 
             EQUAL_OR_YOUNGER_CP(SgFr_gen_cp(LOCAL_top_sg_fr), Get_LOCAL_top_cp())) {
        mark_as_completed(LOCAL_top_sg_fr);
        LOCAL_top_sg_fr = SgFr_next(LOCAL_top_sg_fr);
      }
    } else {
      while (LOCAL_top_sg_fr && 
             YOUNGER_CP(SgFr_gen_cp(LOCAL_top_sg_fr), Get_LOCAL_top_cp())) {
        mark_as_completed(LOCAL_top_sg_fr);
        LOCAL_top_sg_fr = SgFr_next(LOCAL_top_sg_fr);
      }
    }

    /* chain dependency frames to release */
    chain_dep_fr = NULL;
    while (YOUNGER_CP(DepFr_cons_cp(LOCAL_top_dep_fr), Get_LOCAL_top_cp())) {
      LOCK_DEP_FR(LOCAL_top_dep_fr);
      next_dep_fr = DepFr_next(LOCAL_top_dep_fr);
      DepFr_next(LOCAL_top_dep_fr) = chain_dep_fr;
      chain_dep_fr = LOCAL_top_dep_fr;
      LOCAL_top_dep_fr = next_dep_fr;
    }

    /* adjust freeze registers */
    adjust_freeze_registers();
  }

  /* chain or-frames to release */
  chain_or_fr = NULL;
  top_or_fr = Get_LOCAL_top_cp_on_stack()->cp_or_fr;
  while (top_or_fr != LOCAL_top_or_fr) {
    or_fr_ptr next_or_fr_on_stack;
    LOCK_OR_FRAME(top_or_fr);
    susp_fr = OrFr_suspensions(top_or_fr);
    while (susp_fr) {
      complete_suspension_branch(susp_fr, GetOrFr_node(top_or_fr), &chain_or_fr, &chain_dep_fr);
      next_susp_fr = SuspFr_next(susp_fr);
      FREE_SUSPENSION_FRAME(susp_fr);
      susp_fr = next_susp_fr;
    }
    next_or_fr_on_stack = OrFr_next_on_stack(top_or_fr);
    OrFr_next_on_stack(top_or_fr) = chain_or_fr;
    chain_or_fr = top_or_fr;
    top_or_fr = next_or_fr_on_stack;
  }
  LOCK_OR_FRAME(top_or_fr);
  susp_fr = OrFr_suspensions(top_or_fr);
  while (susp_fr) {
    complete_suspension_branch(susp_fr, GetOrFr_node(top_or_fr), &chain_or_fr, &chain_dep_fr);
    next_susp_fr = SuspFr_next(susp_fr);
    FREE_SUSPENSION_FRAME(susp_fr);
    susp_fr = next_susp_fr;
  }
  OrFr_suspensions(top_or_fr) = NULL;
  OrFr_nearest_suspnode(top_or_fr) = top_or_fr;
  UNLOCK_OR_FRAME(top_or_fr);

  /* release dependency frames */
  while (chain_dep_fr) {
    next_dep_fr = DepFr_next(chain_dep_fr);
    FREE_DEPENDENCY_FRAME(chain_dep_fr);
    chain_dep_fr = next_dep_fr;
  }

  /* release or frames */
  solutions = NULL;
  while (chain_or_fr) {
    aux_solutions = OrFr_qg_solutions(chain_or_fr);
    if (aux_solutions) {
      CUT_join_answers_in_an_unique_frame(aux_solutions);
      SolFr_next(aux_solutions) = solutions;
      solutions = aux_solutions;
    }
    next_or_fr = OrFr_next_on_stack(chain_or_fr);
    FREE_OR_FRAME(chain_or_fr);
    chain_or_fr = next_or_fr;
  }
  if (solutions) {
    CUT_join_answers_in_an_unique_frame(solutions);
    SolFr_next(solutions) = OrFr_qg_solutions(LOCAL_top_or_fr);
    OrFr_qg_solutions(LOCAL_top_or_fr) = solutions;
  }

  /* adjust top register */
  Set_LOCAL_top_cp_on_stack( Get_LOCAL_top_cp() );

  return;
}
Exemplo n.º 9
0
static
void share_private_nodes(int worker_q) {
  int depth;
  choiceptr AuxB;
  or_fr_ptr or_frame, previous_or_frame;

  /* initialize auxiliary variables */
  AuxB = B;
  previous_or_frame = NULL;
  depth = OrFr_depth(LOCAL_top_or_fr);
  /* sharing loop */
  while (AuxB != LOCAL_top_cp) {
    depth++;
    ALLOC_OR_FRAME(or_frame);
    INIT_LOCK(OrFr_lock(or_frame));
    SetOrFr_node(or_frame, AuxB);
    OrFr_alternative(or_frame) = AuxB->cp_ap;
    OrFr_pend_prune_cp(or_frame) = NULL;
    OrFr_nearest_leftnode(or_frame) = LOCAL_top_or_fr;
    OrFr_qg_solutions(or_frame) = NULL;
    BITMAP_clear(OrFr_members(or_frame));
    BITMAP_insert(OrFr_members(or_frame), worker_id);
    BITMAP_insert(OrFr_members(or_frame), worker_q);
    if (AuxB->cp_ap && YAMOP_SEQ(AuxB->cp_ap)) {
      AuxB->cp_ap = GETWORK_SEQ;
    } else {
      AuxB->cp_ap = GETWORK;
    }
    AuxB->cp_or_fr = or_frame;
    AuxB = AuxB->cp_b;
    if (previous_or_frame) {
      OrFr_nearest_livenode(previous_or_frame) = OrFr_next(previous_or_frame) = or_frame;
    }
    previous_or_frame = or_frame;
  }
  /* initialize last or-frame pointer */
  or_frame = AuxB->cp_or_fr;
  if (previous_or_frame) {
    OrFr_nearest_livenode(previous_or_frame) = OrFr_next(previous_or_frame) = or_frame;
  }
  /* update depth */
  if (depth >= MAX_BRANCH_DEPTH)
    Yap_Error(INTERNAL_ERROR, TermNil, "maximum depth exceded (share_private_nodes)");
  or_frame = B->cp_or_fr;
  while (or_frame != LOCAL_top_or_fr) {
    unsigned int branch;
    if (OrFr_alternative(or_frame)) {
      branch = YAMOP_OR_ARG(OrFr_alternative(or_frame)) + 1;
    } else {
      branch = 1;
    }
    branch |= YAMOP_CUT_FLAG;  /* in doubt, assume cut */
    BRANCH(worker_id, depth) = BRANCH(worker_q, depth) = branch;
    OrFr_depth(or_frame) = depth--;
    or_frame = OrFr_next_on_stack(or_frame);
  }
  /* update old shared nodes */
  while (or_frame != REMOTE_top_or_fr(worker_q)) {
    LOCK_OR_FRAME(or_frame);
    BRANCH(worker_q, OrFr_depth(or_frame)) = BRANCH(worker_id, OrFr_depth(or_frame));
    BITMAP_insert(OrFr_members(or_frame), worker_q);
    UNLOCK_OR_FRAME(or_frame);
    or_frame = OrFr_next_on_stack(or_frame);
  }
  /* update top shared nodes */
  REMOTE_top_cp(worker_q) = LOCAL_top_cp = B;
  REMOTE_top_or_fr(worker_q) = LOCAL_top_or_fr = LOCAL_top_cp->cp_or_fr;
  /* update prune request */
  if (LOCAL_prune_request) {
    CUT_send_prune_request(worker_q, LOCAL_prune_request);
  }
  /* update load and return */
  REMOTE_load(worker_q) = LOCAL_load = 0;
  return;
}
Exemplo n.º 10
0
static
void share_private_nodes(int worker_q) {
  int depth;
  choiceptr AuxB;
  or_fr_ptr or_frame, previous_or_frame;

  /* initialize auxiliary variables */
  AuxB = B;
  previous_or_frame = NULL;
  depth = OrFr_depth(LOCAL_top_or_fr);
  /* sharing loop */
  while (AuxB != LOCAL_top_cp) {
    depth++;
    ALLOC_OR_FRAME(or_frame);
    INIT_LOCK(OrFr_lock(or_frame));
    OrFr_node(or_frame) = AuxB;
    OrFr_alternative(or_frame) = AuxB->cp_ap;
    OrFr_pend_prune_cp(or_frame) = NULL;
    OrFr_nearest_leftnode(or_frame) = LOCAL_top_or_fr;
    OrFr_qg_solutions(or_frame) = NULL;
    BITMAP_clear(OrFr_members(or_frame));
    BITMAP_insert(OrFr_members(or_frame), worker_id);
    BITMAP_insert(OrFr_members(or_frame), worker_q);
    if (AuxB->cp_ap && YAMOP_SEQ(AuxB->cp_ap)) {
      AuxB->cp_ap = GETWORK_SEQ;
    } else {
      AuxB->cp_ap = GETWORK;
    }
    AuxB->cp_or_fr = or_frame;
    AuxB = AuxB->cp_b;
    if (previous_or_frame) {
      OrFr_nearest_livenode(previous_or_frame) = OrFr_next(previous_or_frame) = or_frame;
    }
    previous_or_frame = or_frame;
  }
  /* initialize last or-frame pointer */
  or_frame = AuxB->cp_or_fr;
  if (previous_or_frame) {
    OrFr_nearest_livenode(previous_or_frame) = OrFr_next(previous_or_frame) = or_frame;
  }
  /* update depth */
  if (depth >= MAX_BRANCH_DEPTH)
    Yap_Error(INTERNAL_ERROR, TermNil, "maximum depth exceded (share_private_nodes)");
  or_frame = B->cp_or_fr;

  while (or_frame != LOCAL_top_or_fr) {
    unsigned int branch;
    if (OrFr_alternative(or_frame)) {
      branch = YAMOP_OR_ARG(OrFr_alternative(or_frame)) + 1;
    } else {
      branch = 1;
    }
    branch |= YAMOP_CUT_FLAG;  /* in doubt, assume cut */
    BRANCH(worker_id, depth) = BRANCH(worker_q, depth) = branch;
    OrFr_depth(or_frame) = depth--;
    or_frame = OrFr_next_on_stack(or_frame);
  }
  /* update old shared nodes */
  while (or_frame != REMOTE_top_or_fr(worker_q)) {
    LOCK_OR_FRAME(or_frame);
    BRANCH(worker_q, OrFr_depth(or_frame)) = BRANCH(worker_id, OrFr_depth(or_frame));
    BITMAP_insert(OrFr_members(or_frame), worker_q);
    UNLOCK_OR_FRAME(or_frame);
    or_frame = OrFr_next_on_stack(or_frame);
  }
  /* move conditional bindings to BA */
  {
    tr_fr_ptr top, tr_ptr;
    top = LOCAL_top_cp->cp_tr;
    tr_ptr = TR;
    while (tr_ptr != top) {
      CELL aux_cell = TrailTerm(--tr_ptr);
      if (IsVarTerm(aux_cell) && 
          ((CELL *)aux_cell < B->cp_h || (choiceptr)aux_cell > B) &&
          !((CELL *)aux_cell < H_FZ || (choiceptr)aux_cell > B_FZ)) {
	CELL *ptr = STACK_TO_SBA(aux_cell);
	*ptr = TrailVal(tr_ptr);
        *(CELL *)aux_cell = (CELL)ptr;
      } else if (IsPairTerm(aux_cell) && (ADDR) RepPair(aux_cell) > HeapTop) {
        /* avoid frozen segments */
        aux_cell = (CELL) RepPair(aux_cell);
        tr_ptr = (tr_fr_ptr) aux_cell;
#ifdef MULTI_ASSIGNMENT_VARIABLES
     } else {
       CELL *cell_ptr = RepAppl(aux_cell);
       /* first do as a for a standard cell */
       if ((cell_ptr < B->cp_h || cell_ptr > (CELL *)B) && !(cell_ptr < H_FZ || (choiceptr)cell_ptr > B_FZ)) {
	 CELL *ptr = STACK_TO_SBA(cell_ptr);
	 /* we may have several bindings in the trail */
	 if ((CELL)ptr != *cell_ptr) {
	   *ptr = TrailVal(tr_ptr);
	   *cell_ptr = (CELL)ptr;
	 }
       }
       /* but we also need to skip the old value */
       tr_ptr--;
#endif /* MULTI_ASSIGNMENT_VARIABLES */
     }
    }
  }
  /* update frozen registers */
  B_FZ  = B;
  H_FZ  = B->cp_h;
  TR_FZ = B->cp_tr;
  /* update top shared nodes */
  REMOTE_top_cp(worker_q) = LOCAL_top_cp = B;
  REMOTE_top_or_fr(worker_q) = LOCAL_top_or_fr = LOCAL_top_cp->cp_or_fr;
  /* update prune request */
  if (LOCAL_prune_request) {
    CUT_send_prune_request(worker_q, LOCAL_prune_request);
  }
  /* update load and return */
  REMOTE_load(worker_q) = LOCAL_load = 0;
}