Ejemplo n.º 1
0
	degree_t getOutDegree( const vertex_descriptor& v ) const { return out_degree( v, _graph ); }
Ejemplo n.º 2
0
 void pre_visit(vertex_descriptor v)
 {
   #pragma mta trace "te"
   int log2d = (int) ceil(log(out_degree(v, g) + 1.0) / log(2.0)) + 1;
   mt_incr(result[log2d], 1);
 }
Ejemplo n.º 3
0
 value_type operator[](const key_type& v) const {
   return out_degree(v, m_g);
 }
Ejemplo n.º 4
0
void SyntacticAnalysisXmlLogger::outputVertex(const LinguisticGraphVertex v,
        const LinguisticGraph& graph,
        const uint64_t offsetBegin,
        const SyntacticData* syntacticData,
        std::ostream& xmlStream,
        std::map< LinguisticAnalysisStructure::Token*, uint64_t >& tokens,
        std::vector< bool >& alreadyDumpedTokens) const
{
    if (v == syntacticData->iterator()->firstVertex() ||
            v == syntacticData->iterator()->lastVertex())
    {
        xmlStream << "<vertex id=\"" << v << "\" />" << std::endl;
        return;
    }
    Token* token = get(vertex_token, graph, v);

    uint64_t tokenId = (*(tokens.find(token))).second;
//    bool alreadyDumped = alreadyDumpedTokens[tokenId];

    xmlStream << "<vertex id=\"" << v << "\" form=\"" << limastring2utf8stdstring(token->stringForm()) << "\" pos=\"" << getPosition(token->position(),offsetBegin) << "\" ";
    const VertexChainIdProp& chains = get(vertex_chain_id, graph,v);
    xmlStream << " >" << std::endl;
    if (chains.size() > 0)
    {
        xmlStream << "<chains>" << std::endl;
        VertexChainIdProp::const_iterator itChains, itChains_end;
        itChains = chains.begin();
        itChains_end = chains.end();
        for (; itChains != itChains_end; itChains++)
        {
            const ChainIdStruct& ids = (*itChains);
            xmlStream << "<chain type=\"";
            if (ids.chainType() == Common::MediaticData::NO_CHAIN_TYPE)
                xmlStream << "0";
            else if (ids.chainType() == Common::MediaticData::NOMINAL)
                xmlStream << "N";
            else
                xmlStream << "V";
            xmlStream << "\" id=\"" << (ids.chainId()) << "\" />" << std::endl;
        }
        xmlStream << "</chains>" << std::endl;
    }

    const DependencyGraph* depGraph = syntacticData->dependencyGraph();
    DependencyGraphVertex depV = syntacticData->depVertexForTokenVertex(v);
    if (out_degree(depV, *depGraph) > 0)
    {

        xmlStream << "<dependents>" << std::endl;
        DependencyGraphOutEdgeIt depIt, depIt_end;
        boost::tie(depIt, depIt_end) = out_edges(depV, *depGraph);
        for (; depIt != depIt_end; depIt++)
        {
            DependencyGraphVertex depTargV = target(*depIt, *depGraph);
            LinguisticGraphVertex targV = syntacticData-> tokenVertexForDepVertex(depTargV);
//             CEdgeDepChainIdPropertyMap chainsMap = get(edge_depchain_id, *depGraph);
            CEdgeDepRelTypePropertyMap relTypeMap = get(edge_deprel_type, *depGraph);
            xmlStream << "<dep v=\"" << targV;
//             xmlStream << "\" c=\"" << chainsMap[*depIt];
            std::string relName=static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(m_language)).getSyntacticRelationName(relTypeMap[*depIt]);
            if (relName.empty())
            {
                relName="UNKNOWN";
            }
            xmlStream << "\" t=\"" << relName << "\" />" << std::endl;
        }
        xmlStream << "</dependents>" << std::endl;
    }

    const FsaStringsPool& sp=Common::MediaticData::MediaticData::single().stringsPool(m_language);

    MorphoSyntacticData* word = get(vertex_data, graph, v);
    word->outputXml(xmlStream,*m_propertyCodeManager,sp);
    xmlStream << "<ref>" << tokenId << "</ref>" << std::endl;
    alreadyDumpedTokens[tokenId] = true;
    xmlStream << "</vertex>" << std::endl;

}
Ejemplo n.º 5
0
	friend degree_size_type out_degree(vertex_descriptor v, const Self &g) {
		return out_degree(v, g.g);
	}
  bool operator()(const edge& g_e, const edge_trg& trg_e,
                  Graph& g, TargetGraph& trg, size_type walk_level)
  {
#ifdef DOUBLE_DEBUG
    printf("si_comp: level: %lu\n", walk_level);
#endif

    // Get vertices.
    vertex g_src = source(g_e, g);
    vertex g_dest = target(g_e, g);
    vertex_trg trg_src = source(trg_e, trg);
    vertex_trg trg_dest = target(trg_e, trg);

    // We know that the triangle walk goes 0->1->2->0, with all edges
    // pointed in the same direction.  In this case, the second vertex
    // (Vertex 1) will be the target of the walk edge at Level 0 and
    // the source of the walk edge at Level 1.  In those, cases, omit
    // the vertex type check (invoke the wild card).  In the other case,
    // match all types strictly as the default comparator would.
    if (walk_level == 0)
    {
      return (g_vtype[g_src] == trg_vtype[trg_src] &&
              g_etype[g_e] == trg_etype[trg_e] &&
              out_degree(g_src, g) >= out_degree(trg_src, trg) &&
              out_degree(g_dest, g) >= out_degree(trg_dest, trg));
    }
    else if (walk_level == 1)
    {
      return (g_etype[g_e] == trg_etype[trg_e] &&
              g_vtype[g_dest] == trg_vtype[trg_dest] &&
              out_degree(g_src, g) >= out_degree(trg_src, trg) &&
              out_degree(g_dest, g) >= out_degree(trg_dest, trg));
    }
    else
    {
      return (g_vtype[g_src] == trg_vtype[trg_src] &&
              g_etype[g_e] == trg_etype[trg_e] &&
              g_vtype[g_dest] == trg_vtype[trg_dest] &&
              out_degree(g_src, g) >= out_degree(trg_src, trg) &&
              out_degree(g_dest, g) >= out_degree(trg_dest, trg));
    }
  }
Ejemplo n.º 7
0
static
bool expandCyclic(NGHolder &h, NFAVertex v) {
    DEBUG_PRINTF("inspecting %zu\n", h[v].index);
    bool changes = false;

    auto v_preds = preds(v, h);
    auto v_succs = succs(v, h);

    set<NFAVertex> start_siblings;
    set<NFAVertex> end_siblings;

    CharReach &v_cr = h[v].char_reach;

    /* We need to find start vertices which have all of our preds.
     * As we have a self loop, it must be one of our succs. */
    for (auto a : adjacent_vertices_range(v, h)) {
        auto a_preds = preds(a, h);

        if (a_preds == v_preds && isutf8start(h[a].char_reach)) {
            DEBUG_PRINTF("%zu is a start v\n", h[a].index);
            start_siblings.insert(a);
        }
    }

    /* We also need to find full cont vertices which have all our own succs;
     * As we have a self loop, it must be one of our preds. */
    for (auto a : inv_adjacent_vertices_range(v, h)) {
        auto a_succs = succs(a, h);

        if (a_succs == v_succs && h[a].char_reach == UTF_CONT_CR) {
            DEBUG_PRINTF("%zu is a full tail cont\n", h[a].index);
            end_siblings.insert(a);
        }
    }

    for (auto s : start_siblings) {
        if (out_degree(s, h) != 1) {
            continue;
        }

        const CharReach &cr = h[s].char_reach;
        if (cr.isSubsetOf(UTF_TWO_START_CR)) {
            if (end_siblings.find(*adjacent_vertices(s, h).first)
                == end_siblings.end()) {
                DEBUG_PRINTF("%zu is odd\n", h[s].index);
                continue;
            }
        } else if (cr.isSubsetOf(UTF_THREE_START_CR)) {
            NFAVertex m = *adjacent_vertices(s, h).first;

            if (h[m].char_reach != UTF_CONT_CR
                || out_degree(m, h) != 1) {
                continue;
            }
            if (end_siblings.find(*adjacent_vertices(m, h).first)
                == end_siblings.end()) {
                DEBUG_PRINTF("%zu is odd\n", h[s].index);
                continue;
            }
        } else if (cr.isSubsetOf(UTF_FOUR_START_CR)) {
            NFAVertex m1 = *adjacent_vertices(s, h).first;

            if (h[m1].char_reach != UTF_CONT_CR
                || out_degree(m1, h) != 1) {
                continue;
            }

            NFAVertex m2 = *adjacent_vertices(m1, h).first;

            if (h[m2].char_reach != UTF_CONT_CR
                || out_degree(m2, h) != 1) {
                continue;
            }

            if (end_siblings.find(*adjacent_vertices(m2, h).first)
                == end_siblings.end()) {
                DEBUG_PRINTF("%zu is odd\n", h[s].index);
                continue;
            }
        } else {
            DEBUG_PRINTF("%zu is bad\n", h[s].index);
          continue;
        }

        v_cr |= cr;
        clear_vertex(s, h);
        changes = true;
    }

    if (changes) {
        v_cr |= UTF_CONT_CR; /* we need to add in cont reach */
        v_cr.set(0xc0); /* we can also add in the forbidden bytes as we require
                         * valid unicode data */
        v_cr.set(0xc1);
        v_cr |= CharReach(0xf5, 0xff);
    }

    return changes;
}
Ejemplo n.º 8
0
 inline degree_type operator ()(vertex_type v, const Graph& g)
 {
     BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept<Graph> ));
     return out_degree(v, g);
 }
Ejemplo n.º 9
0
void LRACC::LRACC_dual()
{
///*
 	//float stepsize = 0.00025*(1-iter_lagrangian+MAX_LAGRANGIAN_NUM);
  float stepsize = 0.001;
  cout<<"#"<<iter_lagrangian<< "\t LRACC_DUAL parameters"<< endl;
	for(TPLGY_EITER eiter = edges(circuit).first;eiter!=edges(circuit).second;eiter++)
	{
		TPLGY_VDES src = source(*eiter,circuit);
		TPLGY_VDES tgt = target(*eiter,circuit);
		float ai,aj,di;
		if ((circuit[src].info->type == IN1) || (circuit[src].info->type == FFMS&&in_degree(src, circuit) == 0)){
			aj = 0;
		}
		else{
			aj = mu_sigma[circuit[src].info->name].first;
		}
		if ((circuit[tgt].info->type == OUT1) || (circuit[tgt].info->type == FFMS&&out_degree(tgt, circuit) == 0))
		{
			ai = Time_Limit;
			di = 0;
		}
		else
		{
			ai = mu_sigma[circuit[tgt].info->name].first;
			di = gate_sizes[circuit[tgt].info->name][1]*gate_sizes[circuit[tgt].info->name][2];
		}
		circuit[*eiter].lm+=stepsize*(aj+di-ai);
		//cout<<"new lm = "<<circuit[*eiter].lm<<std::endl;
		if(circuit[*eiter].lm<0)
			circuit[*eiter].lm = 0;
	}
	//project to KKT condition space
	for (VDES_CONTAINER::iterator ii = order.begin(); ii != order.end(); ++ii)
  {
		if(circuit[*ii].info->type==OUT1) continue;
		if(circuit[*ii].info->type==FFMS&&out_degree(*ii,circuit)==0) continue;
		float out_lm = 0;
		for(TPLGY_OEITER oeiter = out_edges(*ii,circuit).first;oeiter!=out_edges(*ii,circuit).second;oeiter++)
		{
			out_lm+=circuit[*oeiter].lm;
		}
		circuit[*ii].lm = out_lm;
		float in_lm = 0;
		for(TPLGY_IEITER ieiter = in_edges(*ii,circuit).first;ieiter!=in_edges(*ii,circuit).second;ieiter++)
		{
			in_lm+=circuit[*ieiter].lm;
		}
		if(in_lm==0) continue;
		for(TPLGY_IEITER ieiter = in_edges(*ii,circuit).first;ieiter!=in_edges(*ii,circuit).second;ieiter++)
		{
			circuit[*ieiter].lm*=out_lm/in_lm;
		}
	}
	float tmp = (total_a-Area_Limit)/Area_Limit; 
	//if(tmp>0.01) tmp = (1*tmp);
	//else if(tmp<0.01&&tmp>0) tmp = 0.01*1;
	//lm_a += stepsize*tmp;
  //if (lm_a < 0)
  {
  	lm_a = 0;
  }
	cout<<"Area_lm ="<<lm_a<<endl<<endl;
	//*/
	//float stepsize = 0.002*(1-iter_lagrangian+MAX_LAGRANGIAN_NUM);
 	/*float stepsize = 0.02;
  cout<<"#"<<iter_lagrangian<< "\t LRACC_DUAL parameters"<< endl;
	//update lm based on this hyperplane
	for (VDES_CONTAINER::iterator ii = order.begin(); ii != order.end(); ++ii)
  {
		TPLGY_VDES fgate = *ii;
    if (BIT_TEST(circuit[fgate].info->flag, OUTPUT_LOAD))
    {
    	continue;//skip the OUTPUT gate
    }
   	if (BIT_TEST(circuit[fgate].info->flag, INPUT_DRIVER))
    {
    	circuit[fgate].lm = 0;///???????????? just for simplicity
      continue;
    } 
 		string nm = (circuit[fgate].info)->name;
		//cout<<nm<<": "<<circuit[fgate].lm;
		float tmp = -arrival_time_slack[nm].second/Time_Limit;
		if(tmp>=0.01) tmp = (15*tmp);
		else if(tmp<0.01&&tmp>0) tmp = 15*0.01;
		tmp*=gate_sizes[circuit[fgate].info->name][1]*gate_sizes[circuit[fgate].info->name][2];
		circuit[fgate].lm += stepsize*tmp;
    if (circuit[fgate].lm <= 0)
    {
    	circuit[fgate].lm = 0;
    }
		//cout<<" -> lm="<<circuit[fgate].lm<<endl;//the Lagrangian value after calculate
  }
	float tmp = (total_a-Area_Limit)/Area_Limit; 
	//if(tmp>0.01) tmp = (1*tmp);
	//else if(tmp<0.01&&tmp>0) tmp = 0.01*1;
	//lm_a += stepsize*tmp;
  //if (lm_a < 0)
  {
  	lm_a = 0;
  }
	cout<<"Area_lm ="<<lm_a<<endl<<endl;//*/


	/*
	//update history based on last iteration
	dual_hist_obj.push_back(total_p);
	dual_hist_subg.push_back(make_pair(total_a-Area_Limit,arrival_time_slack));	
	//find the supporting hyperplane(this hyperplane will correspond to a history)
	vector<float> min(dual_hist_obj.size(),0);
	for(int i=0;i<dual_hist_obj.size();i++)
	{
		for (VDES_CONTAINER::iterator ii = order.begin(); ii != order.end(); ++ii)
    {
        fgate = *ii;
        if (BIT_TEST(circuit[fgate].info->flag, OUTPUT_LOAD||INPUT_DRIVER))
        {
            continue;//skip the OUTPUT gate
        }
    	  string nm = (circuit[fgate].info)->name;
        min[i] -= ((dual_hist_subg[i].second)[nm].second)*circuit[fgate].lm;
		}
		min[i] += (dual_hist_obj[i]);
		min[i] += (dual_hist_subg[i].first)*lm_a;
	}
	int idx = distance(min.begin(),min_element(min.begin(),min.end()));
	//update lm based on this hyperplane
	for (VDES_CONTAINER::iterator ii = order.begin(); ii != order.end(); ++ii)
  {
		fgate = *ii;
    if (BIT_TEST(circuit[fgate].info->flag, OUTPUT_LOAD))
    {
    	continue;//skip the OUTPUT gate
    }
   	if (BIT_TEST(circuit[fgate].info->flag, INPUT_DRIVER))
    {
    	circuit[fgate].lm = 0;///???????????? just for simplicity
      continue;
    } 
	
 		string nm = (circuit[fgate].info)->name;
		float tmp = -(dual_hist_subg.back().second)[nm].second*(dual_hist_subg.back().second)[nm].first/Time_Limit;
		if(tmp>=0.01) tmp = (30*tmp);
		else if(tmp<0.01&&tmp>0) tmp = 30*0.01;
		circuit[fgate].lm += stepsize*tmp;
    if (circuit[fgate].lm <= 0)
    {
    	circuit[fgate].lm = 0;
    }
		else cout<<nm<<" -> lm="<<circuit[fgate].lm<<endl;//the Lagrangian value after calculate
  }
	float tmp = (dual_hist_subg.back().first)/Area_Limit; 
	if(tmp>0.01) tmp = (20*tmp);
	else if(tmp<0.01&&tmp>0) tmp = 20*0.05;
	lm_a += stepsize*tmp;
  if (lm_a < 0)
  {
  	lm_a = 0;
  }
	cout<<"Area_lm ="<<lm_a<<endl<<endl;//*/
}
Ejemplo n.º 10
0
int main() {
    GraphTraits::out_edge_iterator out_i, out_end;
    GraphTraits::edge_descriptor e;
    boost::graph_traits<Graph>::adjacency_iterator ai;
    boost::graph_traits<Graph>::adjacency_iterator ai_end;
    boost::minstd_rand gen;

    std::map<uint64_t, uint32_t> m;
    std::map<int_type, int_type> id_map;

    size_t count = 0;

    uint64_t *v_array = (uint64_t *)malloc(sizeof(uint64_t)*(n_count + n_count*3));
    // Create graph with 100 nodes and edges with probability 0.05
    double prob = n_count*n_count;
    prob = n_count*3/prob;
    Graph g(ERGen(gen, n_count, prob), ERGen(), n_count);

    std::cout << "done generating\n\n";

    IndexMap index = get(boost::vertex_index, g);
    cow_trie_p my_map = cow_trie_alloc(0, 0);
    my_map->ref_count = 1;
    my_map->child_bitmap = 0;
    my_map->value_bitmap = 0;
    uint32_t id = 0;

    std::cout << "beginning vertex iteration\n\n";
    std::pair<vertex_iter, vertex_iter> vp;
    for (vp = vertices(g); vp.first != vp.second; ++vp.first) {
        Vertex v = *vp.first;
        boost::tie(out_i, out_end) = out_edges(v, g);
        uint32_t deg = out_degree(v, g);
        uint32_t *succs = new uint32_t[deg];
        val_t my_val = cow_trie_create_val(0, deg, succs, 0, 0);
        int_type my_id = cow_trie_insert(my_map, my_val, &my_map);
        id_map[id] = my_id;
        ++id;
    }

    std::cout << "vertex iteration done, beginning edge iteration\n\n";
    boost::graph_traits<Graph>::edge_iterator ei, ei_end;
    boost::tie(ei, ei_end) = edges(g);
    std::cout << "id before is" << id << "\n\n"; 
    for (ei; ei != ei_end; ++ei) {
        val_t my_val = cow_trie_create_val(1, 0, 0, id_map[index[source(*ei, g)]], id_map[index[target(*ei, g)]]);
        int_type my_id = cow_trie_insert(my_map, my_val, &my_map);
        m[index[source(*ei, g)] << 32 | index[target(*ei, g)]] = id;
        ++id;
    }
    std::cout << "id is\n\n" << id << "\n\n";
    std::cout << "done edge iterating\n\n";
    std::cout << "putting in successors\n\n";
    int p = 0;
    for (vp = vertices(g); vp.first != vp.second; ++vp.first) {
        Vertex v = *vp.first;
        val_t my_val;
        cow_trie_lookup(my_map, p, &my_val);
        uint32_t *succs = edge_list_values(my_val._.node.succs);
        boost::tie(out_i, out_end) = out_edges(v, g);
        int eindex = 0;
        for (out_i; out_i != out_end; ++out_i) {
            e = *out_i;
            Vertex src = source(e, g), targ = target(e, g);
            uint32_t id = m[index[src] << 32 | index[targ]];
            succs[eindex] = id_map[id];
            ++eindex;
        }
    ++p;
    }
    val_t v;
    cow_trie_lookup(my_map, 0, &v);
    Vertex vtest = *vertices(g).first;
    std::cout << "beginning random traversal\n\n";
    float t1 = random_walk(vtest, index, g);
    printf("time for theirs %f", t1);
    float t2 = random_walk_c(v, my_map);
    printf("time for ours %f", t2);
    std::cout << "end random traversal\n\n";
}
Ejemplo n.º 11
0
uint32_t BBSISPOptimizerWCP::generateOptimalILPFormulationForSequentialCode(CFGVertex start, CFGVertex end, uint32_t running_id, vector<CFGEdge>& leavingEdges)
{
	vector<CFGVertex> processing;
	vector<CFGVertex> processed;

	processing.push_back(start);

	cfgOutEdgeIter ep;

	LOG_DEBUG(logger, "Processing from: " << start  << " [" << get(startAddrStringNProp, start) << "]" << " to " << end << " [" << get(startAddrStringNProp, end) << "]" << ".");

	while(processing.size() != 0)
	{
		if(logger->isDebugEnabled())
		{
			ostringstream s;
			for(uint32_t i = 0; i < processing.size(); i++)
			{
				s << "(" << processing[i] << ")";
			}
			LOG_DEBUG(logger, "Size of processing list: " << processing.size() << " contains:" << s.str());
		}

		CFGVertex actual_cfg = processing.back();
		processing.pop_back();

		LOG_DEBUG(logger, "Processing " << actual_cfg << " " << get(startAddrStringNProp, actual_cfg));

		// ensure that each node is only handled once.
		bool node_already_processed = false;
		for(uint32_t j = 0; j<processed.size(); j++)
		{
			if(actual_cfg == processed[j])
			{
				node_already_processed = true;
			}
		}
		if(!node_already_processed)
		{
			if(actual_cfg != end)
			{
				if(get(nodeTypeNProp, actual_cfg) == CallPoint)
				{
					// create virtual node for function
					CFGVertex v;
					uint32_t function_id = running_id;
					CFGVertex cfg_return_point = actual_cfg; // to temporally initialize the vertex
					cfgVertexIter vp;
					bool found_return = false;
					// find the right return point for the call point
					uint32_t context_addr = get(endAddrNProp, actual_cfg);
					for (vp = vertices(cfg); (vp.first != vp.second)&&(!found_return); ++vp.first)
					{
						v = *vp.first;
						if((get(nodeTypeNProp, v) == ReturnPoint) && (context_addr == get(endAddrNProp, v)))
						{
							found_return = true;
							cfg_return_point = v;
						}
					}
					assert(found_return);


					stringstream cfg_ilp;

					assert(out_degree(actual_cfg, cfg) == 1);
					cfgOutEdgeIter eop = out_edges(actual_cfg, cfg);
					CFGVertex function_entry_cfg = target(*eop.first, cfg);
					assert(in_degree(cfg_return_point, cfg) == 1);
					cfgInEdgeIter eip = in_edges(cfg_return_point, cfg);
					CFGVertex function_exit_cfg = source(*eip.first, cfg);

					FunctionMap::iterator pos = functionMap.find(function_entry_cfg);

					if(pos == functionMap.end())
					{
						vector<CFGEdge> function_leaving_edges;
						// create ilp for the function body
						running_id = generateOptimalILPFormulationForSequentialCode(function_entry_cfg, function_exit_cfg, ++running_id, function_leaving_edges);
						LOG_DEBUG(logger, "Returned from function to processing from: " << start << " to " << end << ".");
						assert(function_leaving_edges.empty());
						
						FunctionMap::iterator ins_pos;
						bool ins_bool;
						tie(ins_pos, ins_bool) = functionMap.insert(make_pair(function_entry_cfg, function_id));
						assert(ins_bool);
						// create cost for the function with function_id
						cfg_ilp << "cf" << function_id << " = " << " w" << function_entry_cfg  << ";" << endl;

					}
					else
					{
						function_id = pos->second;
					}
					// connect call point with virtual function node wfXctxY
					cfg_ilp << "w" << actual_cfg << " >= wf" << function_id << "c" << hex << context_addr << dec << getPenaltyForFunctionEntering(actual_cfg, function_entry_cfg) << ";" << endl;
					// connect virtual function node with return point, and taking cost of function into account
					// NOTICE the cost of the function exit node (which is an Exit node) to the node to which it is returned (which is an ReturnPoint node) does not need to be considered, because it is free of cost.
					cfg_ilp << "wf" << function_id << "c" << hex << context_addr << dec <<  " >= w" << cfg_return_point << " + cf" << function_id << getPenaltyForFunctionExit(cfg_return_point, function_exit_cfg) << ";" << endl;

					cfg_ilps.push_back(cfg_ilp.str());
					processing.push_back(cfg_return_point);

				}
				else
				{
					for(ep = out_edges(actual_cfg, cfg); ep.first != ep.second; ++ep.first) 
					{
						bool found_loop_head = false;
						uint32_t loop_id = running_id;
						vector<CFGVertex> loop_exits;
						vector<CFGEdge> irregular_loop_exit_edges;
						CFGEdge eo = *ep.first;
						CFGEdge back_edge;
						CFGVertex target_cfg = target(eo, cfg);

						edge_type_t etype = get(edgeTypeEProp, eo);
						if((etype == ForwardStep) || (etype == ForwardJump) || (etype == Meta))
						{

							LOG_DEBUG(logger, "Checking out-edges From: " << actual_cfg << " " << get(startAddrStringNProp, actual_cfg) << " To: " << target_cfg << " " << get(startAddrStringNProp, target_cfg) << " Type: " << etype << " Edge " << eo);

							LoopDataMap::iterator pos = loopMap.find(target_cfg);
							if(pos != loopMap.end())
							{
								if((target_cfg != start) &&  (pos->second.exitNode != end))
								{
									LOG_DEBUG(logger, "Target of out edge " <<  eo << ". " << target_cfg << " is a loop head. Backedge is: " <<  pos->second.backEdge << " exit node is: " <<  pos->second.exitNode);

									loop_exits.push_back(pos->second.exitNode);
									found_loop_head = true;

									stringstream cfg_ilp;
									int32_t loop_bound = getLoopBoundForLoopHead(target_cfg, pos->second.backEdge);
									cfg_ilp << "cl" << loop_id << " = " << loop_bound+1 /* the value from the flow facts determines the number of invocations of the back_edge, i.e. the number of times the loop is entered  */ << " w" << pos->second.startNode;
									if(get(nodeTypeNProp, pos->second.exitNode) == BasicBlock)
									{
										// charge the cost of the loop conserving edge, i.e. of the bottom node of the loop body
										// The cost of this basic block for the loop exiting edge is charged, on connection of the virtual loop node with the code after the loop (see calculation of wlXX and the usage of the variable loop_exits).
										if(!conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION))
										{
											cfg_ilp << " + " << loop_bound << " ce" << source(pos->second.backEdge,cfg) << "t" << target(pos->second.backEdge, cfg);
										}
										else // conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION)
										{
											cfg_ilp << " + " << getEdgeCostConstraint(pos->second.backEdge, loop_bound);
										}
									}
									else
									{
										assert(false); // could there be a loop with a non basic block node at the end??
									}

									cfg_ilp << ";" << endl;
									cfg_ilps.push_back(cfg_ilp.str());

									// create ilp for the loop body
									running_id = generateOptimalILPFormulationForSequentialCode(pos->second.startNode, pos->second.exitNode, ++running_id, irregular_loop_exit_edges);
									LOG_DEBUG(logger, "Returned loop to processing from: " << start << " to " << end << ".");
								}
							}

							stringstream cfg_ilp;

							if(!found_loop_head)
							{
								bool is_on_path = isNodeOnPath(target_cfg, start, end, false);
								LOG_DEBUG(logger, "Successor of actual node " << actual_cfg << " " << get(startAddrStringNProp, actual_cfg) << " (edge: " << eo << ") is no loop head. The node is " << ((!is_on_path)?("not "):("")) << "within the sequential code part");
								cfg_ilp << "w" << actual_cfg << " >= ";
								if(is_on_path)
								{
									cfg_ilp << "w" << target_cfg;
								}
								if(get(nodeTypeNProp, actual_cfg) == BasicBlock)
								{
									if(is_on_path)
									{
										cfg_ilp << " + ";
									}

									if(!conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION))
									{
										cfg_ilp << "ce" << source(eo,cfg) << "t" << target(eo, cfg);
									}
									else // conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION)
									{
										cfg_ilp << getEdgeCostConstraint(eo);
									}
								}


								cfg_ilp << ";" << endl;

								cfg_ilps.push_back(cfg_ilp.str());
								
								// checking if target node is within loop body
								if(is_on_path)
								{
									LOG_DEBUG(logger, "Pushing " << target_cfg << " " << get(startAddrStringNProp, target_cfg) << " to process list");
									processing.push_back(target_cfg);
								}
								else
								{
									LOG_DEBUG(logger, "Node " << target_cfg << " " << get(startAddrStringNProp, target_cfg) << " is not within currently processing code part (function or loop body), cannot add to process list.");
									leavingEdges.push_back(eo);
								}

							}
							else
							{
								LOG_DEBUG(logger, "Successor of actual node " << actual_cfg << " " << get(startAddrStringNProp, actual_cfg) << " (edge: " << eo << ") is loop head");
								cfg_ilp << "w" << actual_cfg << " >= wl" << loop_id;
								if(get(nodeTypeNProp, actual_cfg) == BasicBlock)
								{
									if(!conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION))
									{
										cfg_ilp << " + ce" << source(eo,cfg) << "t" << target(eo, cfg);
									}
									else // conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION)
									{
										cfg_ilp << " + " << getEdgeCostConstraint(eo);
									}
								}
								cfg_ilp << ";" <<  endl;
								cfg_ilps.push_back(cfg_ilp.str());

								for(uint32_t i = 0; i < loop_exits.size(); i++)
								{
									LOG_DEBUG(logger, "Loop exit nodes are: " << loop_exits[i]);
									for(cfgOutEdgeIter ep2 = out_edges(loop_exits[i], cfg); ep2.first != ep2.second; ++ep2.first) 
									{
										CFGEdge el = *ep2.first;
										CFGVertex post_loop_node = target(el, cfg);

										edge_type_t etype = get(edgeTypeEProp, el);
										if((etype == ForwardStep) || (etype == ForwardJump) || (etype == Meta))
										{
											stringstream tmp;
											// the wcet of the loop is the wcet of the following node + the cost of the loop + the cost of the loop out edge

											if(!conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION))
											{
												tmp << "wl" << loop_id << " >= w" << post_loop_node <<  " + cl" << loop_id << " + ce" << source(el,cfg) << "t" << target(el, cfg) << ";" << endl;
											}
											else // conf->getBool(CONF_BBSISP_WCP_SHRINK_ILP_FORMULATION)
											{
												tmp << "wl" << loop_id << " >= w" << post_loop_node <<  " + cl" << loop_id << " + " << getEdgeCostConstraint(el) << ";" << endl;
											}

											cfg_ilps.push_back(tmp.str());

											// checking if target node is within loop body
											if(isNodeOnPath(target_cfg, start, end, false))
											{
												LOG_DEBUG(logger, "Pushing post loop node " << post_loop_node << " " << get(startAddrStringNProp, post_loop_node) << " to process list");
												processing.push_back(post_loop_node);
											}
											else
											{
												LOG_DEBUG(logger, "Post loop node " << target_cfg << " " << get(startAddrStringNProp, target_cfg) << " is not on path, cannot add to process list.");
												leavingEdges.push_back(eo);
												assert(false);
											}
										}
									}

								}
								for(uint32_t i = 0; i < irregular_loop_exit_edges.size(); i++)
								{
									CFGEdge e = irregular_loop_exit_edges[i];
									LOG_DEBUG(logger, "Irregular loop exit edge: " << e);
									// checking if the target of the loop leaving edge can be found whithin the currently activw loop body
									if(isNodeOnPath(target(e, cfg), start, end, false))
									{

										stringstream tmp;
										// If a loop is unexpectively left by an edge e, the target of e may be reached by the loop body (in the worst case at the end of the loop, since the structure of the loop is hidden by the cost of the loop (clXX).
										// The cost of the irregular loop exit edge does not need to be charged here, because it is implicitely already in the cost of the loop (which is maximizes over all possible paths, including the dead end of an irregular leaving edge.
										tmp << "wl" << loop_id << " >= w" << target(e, cfg) <<  " + cl" << loop_id << ";" << endl; 
										cfg_ilps.push_back(tmp.str());
									}
									else
									{
										// The irregular loop leaving edge leaves multiple levels of loop nests. Delegate handling to next loop level.
										leavingEdges.push_back(e);
										assert(false);
									}
								}


							}

						}
					}
				}
			}
			else
			{
				LOG_DEBUG(logger, "Found end node");

				stringstream cfg_ilp;

				// The cost of the loop conserving edge (the bottom basic block) is charged in the loop cost variable: clXX = loop_bound+1 * wLoopHead + ceLoopConservingEdgeXX
				// This is because the loop body is ececuted loop_bound+1, whereas the cost of the loop conserving edge needs to be taken only loop_bound times into account, because the last iteration of the loop uses another edge, which is charged in wlXX >= wYY + clXX + ceExitEdgeOfLoopXX
				cfg_ilp << "w" << actual_cfg << " = 0;" << endl;

				cfg_ilps.push_back(cfg_ilp.str());
			}
			processed.push_back(actual_cfg);
		}
	}
	return running_id;

}
Ejemplo n.º 12
0
LimaStatusCode GreedyPosTagger::process(
  AnalysisContent& analysis) const
{

  // start postagging here !
  TimeUtils::updateCurrentTime();
  PTLOGINIT;
  LINFO << "start greedy posTagging";

  AnalysisGraph* anagraph=static_cast<AnalysisGraph*>(analysis.getData("AnalysisGraph"));
  
  AnalysisGraph* posgraph=new AnalysisGraph("PosGraph",m_language,false,true,*anagraph);

  // walk on the vertex but don't process a vertex if one
  // of its predecessor hasn't been processed.
  LinguisticGraph* graph=posgraph->getGraph();
  LinguisticGraphVertex endVx=posgraph->lastVertex();

  map<LinguisticGraphVertex,uint64_t> processed;
  set<LinguisticGraphVertex> toProcess;
  toProcess.insert(anagraph->firstVertex());
  set<LinguisticGraphVertex> nextToProcess;
  set<LinguisticGraphVertex>::iterator toProcessItr;
  map<LinguisticGraphVertex,uint64_t>::iterator processedItr;
  LinguisticGraphInEdgeIt inItr,inItrEnd;
  LinguisticGraphOutEdgeIt outItr,outItrEnd;

  while (toProcess.size()!=0)
  {

    //cout << "toProcess is ";
    //        copy(toProcess.begin(),toProcess.end(),ostream_iterator<LinguisticGraphVertex>(cout,","));
    //        cout << endl;

    for (toProcessItr=toProcess.begin();
         toProcessItr!=toProcess.end();
         toProcessItr++)
    {

      // process vertex
      processVertex(*toProcessItr,anagraph);
      processed.insert(make_pair(*toProcessItr,out_degree(*toProcessItr,*graph)));
      //cerr << "processed : insert " << *toProcessItr << " with " << out_degree(*toProcessItr,*graph) << endl;

      // remove processed if necessary
      boost::tie(inItr,inItrEnd)=in_edges(*toProcessItr,*graph);
      for (;inItr!=inItrEnd;inItr++)
      {
        processedItr=processed.find(source(*inItr,*graph));
        processedItr->second--;
        //cerr << "processed : vertex " << processedItr->first << " decremented to " << processedItr->second << endl;
        if (processedItr->second==0)
        {
          //cerr << "processed : remove " << processedItr->first << endl;
          processed.erase(processedItr);
        }
      }

      // check and add nex vertex to process
      boost::tie(outItr,outItrEnd)=out_edges(*toProcessItr,*graph);
      for (;outItr!=outItrEnd;outItr++)
      {
        LinguisticGraphVertex next=target(*outItr,*graph);
        if (next==endVx)
        {
          continue;
        }
        // check if all in vertices have been processed
        boost::tie(inItr,inItrEnd)=in_edges(next,*graph);
        bool ok=true;
        for (;inItr!=inItrEnd;inItr++)
        {
          if (processed.find(source(*inItr,*graph))==processed.end())
          {
            ok=false;
            break;
          }
        }
        if (ok)
        {
          nextToProcess.insert(next);
        }
      }

    }

    toProcess.clear();
    toProcess.swap(nextToProcess);

  }

  TimeUtils::logElapsedTime("GreedyPosTagger");
  return SUCCESS_ID;
}
Ejemplo n.º 13
0
BOOST_AUTO_TEST_CASE_TEMPLATE( intint_bgl_mutable_graph_test, Graph, intint_graphtest_types )
{
  typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  typedef typename boost::graph_traits<Graph>::vertex_iterator VertexIter;
  typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
  typedef typename boost::graph_traits<Graph>::edge_iterator EdgeIter;
  typedef typename boost::graph_traits<Graph>::out_edge_iterator OutEdgeIter;
  typedef typename boost::graph_traits<Graph>::in_edge_iterator InEdgeIter;
  
  Graph g;
  
  Vertex v_root = Vertex();
  BOOST_CHECK_NO_THROW( v_root = add_vertex(g) );
  BOOST_CHECK_EQUAL( num_vertices(g), 1);
  BOOST_CHECK_NO_THROW( remove_vertex(v_root,g) );
  BOOST_CHECK_EQUAL( num_vertices(g), 0);
  
  BOOST_CHECK_NO_THROW( v_root = add_vertex(1, g) );
  g[v_root] = 1;
  BOOST_CHECK_EQUAL( g[v_root], 1 );
  
  int vp_rc[] = {2,3,4,5};
  int ep_rc[] = {1002,1003,1004,1005};
  Vertex v_rc[4];
  Edge e_rc[4];
  for(int i = 0; i < 4; ++i) {
    BOOST_CHECK_NO_THROW( v_rc[i] = add_vertex(g) );
    g[ v_rc[i] ] = vp_rc[i];
    BOOST_CHECK_EQUAL( g[ v_rc[i] ], vp_rc[i] );
    bool edge_added_success = false;
    BOOST_CHECK_NO_THROW( boost::tie(e_rc[i],edge_added_success) = add_edge(v_root, v_rc[i], g) );
    BOOST_CHECK( edge_added_success );
    g[ e_rc[i] ] = ep_rc[i];
    BOOST_CHECK_EQUAL( g[ e_rc[i] ], ep_rc[i] );
  };
  BOOST_CHECK_EQUAL( num_vertices(g), 5 );
  
  int vp_rc1c[] = {6,7,8,9};
  int ep_rc1c[] = {2006,2007,2008,2009};
  Vertex v_rc1c[4];
  Edge e_rc1c[4];
  for(std::size_t i = 0; i < 4; ++i) {
    BOOST_CHECK_NO_THROW( v_rc1c[i] = add_vertex(vp_rc1c[i], g) );
    BOOST_CHECK_EQUAL( g[ v_rc1c[i] ], vp_rc1c[i] );
    bool edge_added_success = false;
    BOOST_CHECK_NO_THROW( boost::tie(e_rc1c[i],edge_added_success) = add_edge(v_rc[0], v_rc1c[i], ep_rc1c[i], g) );
    BOOST_CHECK( edge_added_success );
    BOOST_CHECK_EQUAL( g[ e_rc1c[i] ], ep_rc1c[i] );
  };
  BOOST_CHECK_EQUAL( num_vertices(g), 9 );
  
  
  BOOST_CHECK_EQUAL( g[v_root], 1 );
  {
    OutEdgeIter ei, ei_end;
    BOOST_CHECK_NO_THROW( boost::tie(ei,ei_end) = out_edges(v_root,g) );
    std::vector<int> e_list;
    for(; ei != ei_end; ++ei) {
      if(is_edge_valid(*ei,g)) {
        BOOST_CHECK_EQUAL( g[*ei], (g[source(*ei,g)] * 1000 + g[target(*ei,g)]) );
        e_list.push_back(g[*ei]);
      };
    };
    std::sort(e_list.begin(), e_list.end());
    BOOST_CHECK_EQUAL( e_list[0], 1002);
    BOOST_CHECK_EQUAL( e_list[1], 1003);
    BOOST_CHECK_EQUAL( e_list[2], 1004);
    BOOST_CHECK_EQUAL( e_list[3], 1005);
    
    
    InEdgeIter iei, iei_end;
    BOOST_CHECK_NO_THROW( boost::tie(iei, iei_end) = in_edges(v_rc[0], g) );
    BOOST_CHECK( iei != iei_end );
    BOOST_CHECK_EQUAL( g[*iei], 1002);
    ++iei;
    BOOST_CHECK( iei == iei_end );
    
    
    BOOST_CHECK_NO_THROW( boost::tie(ei,ei_end) = out_edges(v_rc[0],g) );
    std::vector<int> e_list2;
    for(; ei != ei_end; ++ei) {
      if(is_edge_valid(*ei,g)) {
        BOOST_CHECK_EQUAL( g[*ei], (g[source(*ei,g)] * 1000 + g[target(*ei,g)]) );
        e_list2.push_back(g[*ei]);
      };
    };
    std::sort(e_list2.begin(), e_list2.end());
    BOOST_CHECK_EQUAL( e_list2[0], 2006);
    BOOST_CHECK_EQUAL( e_list2[1], 2007);
    BOOST_CHECK_EQUAL( e_list2[2], 2008);
    BOOST_CHECK_EQUAL( e_list2[3], 2009);

  };
  
  int vp_rc2c[] = {10,11,12,13};
  int ep_rc2c[] = {3010,3011,3012,3013};
  Vertex v_rc2c[4];
  Edge e_rc2c[4];
  for(std::size_t i = 0; i < 4; ++i) {
#ifdef RK_ENABLE_CXX0X_FEATURES
    BOOST_CHECK_NO_THROW( v_rc2c[i] = add_vertex(std::move(vp_rc2c[i]), g) );
#else
    BOOST_CHECK_NO_THROW( v_rc2c[i] = add_vertex(vp_rc2c[i], g) );
#endif
    bool edge_added_success = false;
#ifdef RK_ENABLE_CXX0X_FEATURES
    BOOST_CHECK_NO_THROW( boost::tie(e_rc2c[i],edge_added_success) = add_edge(v_rc[1], v_rc2c[i], ep_rc2c[i], g) );
#else
    BOOST_CHECK_NO_THROW( boost::tie(e_rc2c[i],edge_added_success) = add_edge(v_rc[1], v_rc2c[i], ep_rc2c[i], g) );
#endif
    BOOST_CHECK( edge_added_success );
  };
  BOOST_CHECK_EQUAL( num_vertices(g), 13 );
  
  {
    OutEdgeIter ei, ei_end;
    BOOST_CHECK_NO_THROW( boost::tie(ei,ei_end) = out_edges(v_rc[1],g) );
    std::vector<int> e_list;
    std::vector<int> vp_list;
    for(; ei != ei_end; ++ei) {
      if(is_edge_valid(*ei,g)) {
        BOOST_CHECK_EQUAL( g[*ei], (g[source(*ei,g)] * 1000 + g[target(*ei,g)]) );
        e_list.push_back(g[*ei]);
        vp_list.push_back(g[target(*ei,g)]);
      };
    };
    std::sort(e_list.begin(), e_list.end());
    BOOST_CHECK_EQUAL( e_list[0], 3010);
    BOOST_CHECK_EQUAL( e_list[1], 3011);
    BOOST_CHECK_EQUAL( e_list[2], 3012);
    BOOST_CHECK_EQUAL( e_list[3], 3013);
    
    std::sort(vp_list.begin(), vp_list.end());
    BOOST_CHECK_EQUAL( vp_list[0], 10);
    BOOST_CHECK_EQUAL( vp_list[1], 11);
    BOOST_CHECK_EQUAL( vp_list[2], 12);
    BOOST_CHECK_EQUAL( vp_list[3], 13);
  };
  
  BOOST_CHECK_NO_THROW( clear_vertex(v_rc[0],g) );
  
  BOOST_CHECK_EQUAL( out_degree(v_rc[0], g), 0 );
  BOOST_CHECK_EQUAL( in_degree(v_rc[0], g), 0 );
  BOOST_CHECK_EQUAL( out_degree(v_root, g), 3 );
  BOOST_CHECK_EQUAL( in_degree(v_rc1c[0], g), 0 );
  BOOST_CHECK_EQUAL( in_degree(v_rc1c[1], g), 0 );
  BOOST_CHECK_EQUAL( in_degree(v_rc1c[2], g), 0 );
  BOOST_CHECK_EQUAL( in_degree(v_rc1c[3], g), 0 );
  
  BOOST_CHECK_EQUAL( num_vertices(g), 13 );
  {
    VertexIter vi, vi_end;
    BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) );
    std::vector<int> vp_list;
    for(; vi != vi_end; ++vi)
      if( is_vertex_valid(*vi, g) )
        vp_list.push_back( g[*vi] );
    std::sort(vp_list.begin(), vp_list.end());
    BOOST_CHECK_EQUAL( vp_list[0], 1 );
    BOOST_CHECK_EQUAL( vp_list[1], 2 );
    BOOST_CHECK_EQUAL( vp_list[2], 3 );
    BOOST_CHECK_EQUAL( vp_list[3], 4 );
    BOOST_CHECK_EQUAL( vp_list[4], 5 );
    BOOST_CHECK_EQUAL( vp_list[5], 6 );
    BOOST_CHECK_EQUAL( vp_list[6], 7 );
    BOOST_CHECK_EQUAL( vp_list[7], 8 );
    BOOST_CHECK_EQUAL( vp_list[8], 9 );
    BOOST_CHECK_EQUAL( vp_list[9], 10 );
    BOOST_CHECK_EQUAL( vp_list[10], 11 );
    BOOST_CHECK_EQUAL( vp_list[11], 12 );
    BOOST_CHECK_EQUAL( vp_list[12], 13 );
  };
  
  BOOST_CHECK_EQUAL( num_edges(g), 7 );
  {
    EdgeIter ei, ei_end;
    BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) );
    std::vector<int> ep_list;
    for(; ei != ei_end; ++ei)
      if( is_edge_valid(*ei, g) )
        ep_list.push_back( g[*ei] );
    std::sort(ep_list.begin(), ep_list.end());
    BOOST_CHECK_EQUAL( ep_list[0], 1003 );
    BOOST_CHECK_EQUAL( ep_list[1], 1004 );
    BOOST_CHECK_EQUAL( ep_list[2], 1005 );
    BOOST_CHECK_EQUAL( ep_list[3], 3010 );
    BOOST_CHECK_EQUAL( ep_list[4], 3011 );
    BOOST_CHECK_EQUAL( ep_list[5], 3012 );
    BOOST_CHECK_EQUAL( ep_list[6], 3013 );
  };
  
  
  
  BOOST_CHECK_NO_THROW( remove_edge(v_rc[1], v_rc2c[2], g) );
  
  BOOST_CHECK_EQUAL( num_vertices(g), 13 );
  {
    VertexIter vi, vi_end;
    BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) );
    std::vector<int> vp_list;
    for(; vi != vi_end; ++vi) {
      if( is_vertex_valid(*vi, g) ) {
        vp_list.push_back( g[*vi] );
      };
    };
    std::sort(vp_list.begin(), vp_list.end());
    BOOST_CHECK_EQUAL( vp_list[0], 1 );
    BOOST_CHECK_EQUAL( vp_list[1], 2 );
    BOOST_CHECK_EQUAL( vp_list[2], 3 );
    BOOST_CHECK_EQUAL( vp_list[3], 4 );
    BOOST_CHECK_EQUAL( vp_list[4], 5 );
    BOOST_CHECK_EQUAL( vp_list[5], 6 );
    BOOST_CHECK_EQUAL( vp_list[6], 7 );
    BOOST_CHECK_EQUAL( vp_list[7], 8 );
    BOOST_CHECK_EQUAL( vp_list[8], 9 );
    BOOST_CHECK_EQUAL( vp_list[9], 10 );
    BOOST_CHECK_EQUAL( vp_list[10], 11 );
    BOOST_CHECK_EQUAL( vp_list[11], 12 );
    BOOST_CHECK_EQUAL( vp_list[12], 13 );
  };
  
  BOOST_CHECK_EQUAL( num_edges(g), 6 );
  {
    EdgeIter ei, ei_end;
    BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) );
    std::vector<int> ep_list;
    for(; ei != ei_end; ++ei) {
      if( is_edge_valid(*ei, g) ) {
        ep_list.push_back( g[*ei] );
      };
    };
    std::sort(ep_list.begin(), ep_list.end());
    BOOST_CHECK_EQUAL( ep_list[0], 1003 );
    BOOST_CHECK_EQUAL( ep_list[1], 1004 );
    BOOST_CHECK_EQUAL( ep_list[2], 1005 );
    BOOST_CHECK_EQUAL( ep_list[3], 3010 );
    BOOST_CHECK_EQUAL( ep_list[4], 3011 );
    BOOST_CHECK_EQUAL( ep_list[5], 3013 );
  };
  
  
  
  BOOST_CHECK_NO_THROW( remove_edge(e_rc2c[3], g) );
  
  BOOST_CHECK_EQUAL( num_vertices(g), 13 );
  {
    VertexIter vi, vi_end;
    BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) );
    std::vector<int> vp_list;
    for(; vi != vi_end; ++vi) {
      if( is_vertex_valid(*vi, g) ) {
        vp_list.push_back( g[*vi] );
      };
    };
    std::sort(vp_list.begin(), vp_list.end());
    BOOST_CHECK_EQUAL( vp_list[0], 1 );
    BOOST_CHECK_EQUAL( vp_list[1], 2 );
    BOOST_CHECK_EQUAL( vp_list[2], 3 );
    BOOST_CHECK_EQUAL( vp_list[3], 4 );
    BOOST_CHECK_EQUAL( vp_list[4], 5 );
    BOOST_CHECK_EQUAL( vp_list[5], 6 );
    BOOST_CHECK_EQUAL( vp_list[6], 7 );
    BOOST_CHECK_EQUAL( vp_list[7], 8 );
    BOOST_CHECK_EQUAL( vp_list[8], 9 );
    BOOST_CHECK_EQUAL( vp_list[9], 10 );
    BOOST_CHECK_EQUAL( vp_list[10], 11 );
    BOOST_CHECK_EQUAL( vp_list[11], 12 );
    BOOST_CHECK_EQUAL( vp_list[12], 13 );
  };
  
  BOOST_CHECK_EQUAL( num_edges(g), 5 );
  {
    EdgeIter ei, ei_end;
    BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) );
    std::vector<int> ep_list;
    for(; ei != ei_end; ++ei) {
      if( is_edge_valid(*ei, g) ) {
        ep_list.push_back( g[*ei] );
      };
    };
    std::sort(ep_list.begin(), ep_list.end());
    BOOST_CHECK_EQUAL( ep_list[0], 1003 );
    BOOST_CHECK_EQUAL( ep_list[1], 1004 );
    BOOST_CHECK_EQUAL( ep_list[2], 1005 );
    BOOST_CHECK_EQUAL( ep_list[3], 3010 );
    BOOST_CHECK_EQUAL( ep_list[4], 3011 );
  };
  
  
  
  BOOST_CHECK_NO_THROW( clear_vertex(v_rc2c[0], g) );
  BOOST_CHECK_NO_THROW( remove_vertex(v_rc2c[0], g) );
  
  BOOST_CHECK_EQUAL( num_vertices(g), 12 );
  {
    VertexIter vi, vi_end;
    BOOST_CHECK_NO_THROW( boost::tie(vi, vi_end) = vertices(g) );
    std::vector<int> vp_list;
    for(; vi != vi_end; ++vi) {
      if( is_vertex_valid(*vi, g) ) {
        vp_list.push_back( g[*vi] );
      };
    };
    std::sort(vp_list.begin(), vp_list.end());
    BOOST_CHECK_EQUAL( vp_list[0], 1 );
    BOOST_CHECK_EQUAL( vp_list[1], 2 );
    BOOST_CHECK_EQUAL( vp_list[2], 3 );
    BOOST_CHECK_EQUAL( vp_list[3], 4 );
    BOOST_CHECK_EQUAL( vp_list[4], 5 );
    BOOST_CHECK_EQUAL( vp_list[5], 6 );
    BOOST_CHECK_EQUAL( vp_list[6], 7 );
    BOOST_CHECK_EQUAL( vp_list[7], 8 );
    BOOST_CHECK_EQUAL( vp_list[8], 9 );
    BOOST_CHECK_EQUAL( vp_list[9], 11 );
    BOOST_CHECK_EQUAL( vp_list[10], 12 );
    BOOST_CHECK_EQUAL( vp_list[11], 13 );
  };
  
  BOOST_CHECK_EQUAL( num_edges(g), 4 );
  {
    EdgeIter ei, ei_end;
    BOOST_CHECK_NO_THROW( boost::tie(ei, ei_end) = edges(g) );
    std::vector<int> ep_list;
    for(; ei != ei_end; ++ei) {
      if( is_edge_valid(*ei, g) ) {
        ep_list.push_back( g[*ei] );
      };
    };
    std::sort(ep_list.begin(), ep_list.end());
    BOOST_CHECK_EQUAL( ep_list[0], 1003 );
    BOOST_CHECK_EQUAL( ep_list[1], 1004 );
    BOOST_CHECK_EQUAL( ep_list[2], 1005 );
    BOOST_CHECK_EQUAL( ep_list[3], 3011 );
  };
  
  
};
Ejemplo n.º 14
0
void handleExtendedParams(ReportManager &rm, NGWrapper &g,
                          UNUSED const CompileContext &cc) {
    if (!hasExtParams(g)) {
        return;
    }

    depth minWidth = findMinWidth(g);
    depth maxWidth = findMaxWidth(g);
    bool is_anchored = !has_proper_successor(g.startDs, g)
                     && out_degree(g.start, g);
    bool has_offset_adj = hasOffsetAdjustments(rm, g);

    DEBUG_PRINTF("minWidth=%s, maxWidth=%s, anchored=%d, offset_adj=%d\n",
                 minWidth.str().c_str(), maxWidth.str().c_str(), is_anchored,
                 has_offset_adj);

    DepthMinMax match_depths = findMatchLengths(rm, g);
    DEBUG_PRINTF("match depths %s\n", match_depths.str().c_str());

    if (is_anchored && maxWidth.is_finite() && g.min_offset > maxWidth) {
        ostringstream oss;
        oss << "Expression is anchored and cannot satisfy min_offset="
            << g.min_offset << " as it can only produce matches of length "
            << maxWidth << " bytes at most.";
        throw CompileError(g.expressionIndex, oss.str());
    }

    if (minWidth > g.max_offset) {
        ostringstream oss;
        oss << "Expression has max_offset=" << g.max_offset << " but requires "
             << minWidth << " bytes to match.";
        throw CompileError(g.expressionIndex, oss.str());
    }

    if (maxWidth.is_finite() && match_depths.max < g.min_length) {
        ostringstream oss;
        oss << "Expression has min_length=" << g.min_length << " but can "
            "only produce matches of length " << match_depths.max <<
            " bytes at most.";
        throw CompileError(g.expressionIndex, oss.str());
    }

    if (g.min_length && g.min_length <= match_depths.min) {
        DEBUG_PRINTF("min_length=%llu constraint is unnecessary\n",
                     g.min_length);
        g.min_length = 0;
    }

    if (!hasExtParams(g)) {
        return;
    }

    pruneVacuousEdges(g);
    pruneUnmatchable(g, rm);

    if (!has_offset_adj) {
        pruneExtUnreachable(g);
    }

    // We may have removed all the edges to accept, in which case this
    // expression cannot match.
    if (in_degree(g.accept, g) == 0 && in_degree(g.acceptEod, g) == 1) {
        throw CompileError(g.expressionIndex, "Extended parameter "
                "constraints can not be satisfied for any match from "
                "this expression.");
    }

    // Remove reports on vertices without an edge to accept (which have been
    // pruned above).
    clearReports(g);

    // Recalc.
    minWidth = findMinWidth(g);
    maxWidth = findMaxWidth(g);
    is_anchored = proper_out_degree(g.startDs, g) == 0 &&
                  out_degree(g.start, g);
    has_offset_adj = hasOffsetAdjustments(rm, g);

    // If the pattern is completely anchored and has a min_length set, this can
    // be converted to a min_offset.
    if (g.min_length && (g.min_offset <= g.min_length) && is_anchored) {
        DEBUG_PRINTF("converting min_length to min_offset=%llu for "
                     "anchored case\n", g.min_length);
        g.min_offset = g.min_length;
        g.min_length = 0;
    }

    if (g.min_offset && g.min_offset <= minWidth && !has_offset_adj) {
        DEBUG_PRINTF("min_offset=%llu constraint is unnecessary\n",
                     g.min_offset);
        g.min_offset = 0;
    }

    if (!hasExtParams(g)) {
        return;
    }

    // If the pattern has a min_length and is of "ratchet" form with one
    // unbounded repeat, that repeat can become a bounded repeat.
    // e.g. /foo.*bar/{min_length=100} --> /foo.{94,}bar/
    if (g.min_length && transformMinLengthToRepeat(rm, g)) {
        DEBUG_PRINTF("converted min_length to bounded repeat\n");
        // recalc
        minWidth = findMinWidth(g);
    }

    // If the pattern is unanchored, has a max_offset and has not asked for
    // SOM, we can use that knowledge to anchor it which will limit its
    // lifespan. Note that we can't use this transformation if there's a
    // min_length, as it's currently handled using "sly SOM".

    // Note that it is possible to handle graphs that have a combination of
    // anchored and unanchored paths, but it's too tricky for the moment.

    if (g.max_offset != MAX_OFFSET && !g.som && !g.min_length &&
                !has_offset_adj && isUnanchored(g)) {
        if (anchorPatternWithBoundedRepeat(g, minWidth, maxWidth)) {
            DEBUG_PRINTF("minWidth=%s, maxWidth=%s\n", minWidth.str().c_str(),
                         maxWidth.str().c_str());
            if (minWidth == maxWidth) {
                // For a fixed width pattern, we can retire the offsets as they
                // are implicit in the graph now.
                g.min_offset = 0;
                g.max_offset = MAX_OFFSET;
            }
        }
    }
    //dumpGraph("final.dot", g.g);

    if (!hasExtParams(g)) {
        return;
    }

    set<NFAVertex> done;
    updateReportBounds(rm, g, g.accept, done);
    updateReportBounds(rm, g, g.acceptEod, done);
}
Ejemplo n.º 15
0
inline typename DIRECTED_GRAPH::degree_size_type
out_degree(typename DIRECTED_GRAPH::vertex_descriptor v, DIRECTED_GRAPH const& g)
{ return out_degree(v, g.impl()); }
Ejemplo n.º 16
0
int read_dimacs_max_flow_internal(Graph& g,
                                  CapacityMap capacity,
                                  ReverseEdgeMap reverse_edge,
                                  typename graph_traits<Graph>::vertex_descriptor& src,
                                  typename graph_traits<Graph>::vertex_descriptor& sink,
                                  std::istream& in,
                                  bool require_source_and_sink,
                                  const std::string& problem_type)
{
  //  const int MAXLINE = 100;      /* max line length in the input file */
  const int ARC_FIELDS = 3;     /* no of fields in arc line  */
  const int NODE_FIELDS = 2;    /* no of fields in node line  */
  const int P_FIELDS = 3;       /* no of fields in problem line */

  typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;

  std::vector<vertex_descriptor> verts;

  long m, n,                    /*  number of edges and nodes */
    i, head, tail, cap;

  long no_lines=0,              /* no of current input line */
    no_plines=0,                /* no of problem-lines */
    no_nslines=0,               /* no of node-source-lines */
    no_nklines=0,               /* no of node-source-lines */
    no_alines=0;                /* no of arc-lines */

  std::string in_line;          /* for reading input line */
  char pr_type[4];              /* for reading type of the problem */
  char nd;                      /* source (s) or sink (t) */

  int k,                        /* temporary */
    err_no;                     /* no of detected error */

  /* -------------- error numbers & error messages ---------------- */
  const int EN1   = 0;
  const int EN2   = 1;
  const int EN3   = 2;
  const int EN4   = 3;
  //  const int EN6   = 4;
  //  const int EN10  = 5;
  //  const int EN7   = 6;
  const int EN8   = 7;
  const int EN9   = 8;
  const int EN11  = 9;
  const int EN12 = 10;
  //  const int EN13 = 11;
  const int EN14 = 12;
  const int EN16 = 13;
  const int EN15 = 14;
  const int EN17 = 15;
  const int EN18 = 16;
  const int EN21 = 17;
  const int EN19 = 18;
  const int EN20 = 19;
  const int EN22 = 20;

  static const char *err_message[] =
  {
    /* 0*/    "more than one problem line.",
    /* 1*/    "wrong number of parameters in the problem line.",
    /* 2*/    "it is not a Max Flow problem line.",
    /* 3*/    "bad value of a parameter in the problem line.",
    /* 4*/    "can't obtain enough memory to solve this problem.",
    /* 5*/    "more than one line with the problem name.",
    /* 6*/    "can't read problem name.",
    /* 7*/    "problem description must be before node description.",
    /* 8*/    "this parser doesn't support multiply sources and sinks.",
    /* 9*/    "wrong number of parameters in the node line.",
    /*10*/    "wrong value of parameters in the node line.",
    /*11*/    " ",
    /*12*/    "source and sink descriptions must be before arc descriptions.",
    /*13*/    "too many arcs in the input.",
    /*14*/    "wrong number of parameters in the arc line.",
    /*15*/    "wrong value of parameters in the arc line.",
    /*16*/    "unknown line type in the input.",
    /*17*/    "reading error.",
    /*18*/    "not enough arcs in the input.",
    /*19*/    "source or sink doesn't have incident arcs.",
    /*20*/    "can't read anything from the input file."
  };
  /* --------------------------------------------------------------- */

  /* The main loop:
     -  reads the line of the input,
     -  analyses its type,
     -  checks correctness of parameters,
     -  puts data to the arrays,
     -  does service functions
  */

  while (std::getline(in, in_line)) {
    ++no_lines;

    switch (in_line[0]) {
    case 'c':                  /* skip lines with comments */
    case '\n':                 /* skip empty lines   */
    case '\0':                 /* skip empty lines at the end of file */
      break;

    case 'p':                  /* problem description      */
      if ( no_plines > 0 )
        /* more than one problem line */
        { err_no = EN1 ; goto error; }

      no_plines = 1;

      if (
          /* reading problem line: type of problem, no of nodes, no of arcs */
          std::sscanf ( in_line.c_str(), "%*c %3s %ld %ld", pr_type, &n, &m )
          != P_FIELDS
          )
        /*wrong number of parameters in the problem line*/
        { err_no = EN2; goto error; }

      if ( pr_type != problem_type )
        /*wrong problem type*/
        { err_no = EN3; goto error; }

      if ( n <= 0  || m <= 0 )
        /*wrong value of no of arcs or nodes*/
        { err_no = EN4; goto error; }

      {
        for (long vi = 0; vi < n; ++vi)
          verts.push_back(add_vertex(g));
      }
      break;

    case 'n':                    /* source(s) description */
      if ( no_plines == 0 )
        /* there was not problem line above */
        { err_no = EN8; goto error; }

      /* reading source  or sink */
      k = std::sscanf ( in_line.c_str(),"%*c %ld %c", &i, &nd );
      --i; // index from 0
      if ( k < NODE_FIELDS )
        /* node line is incorrect */
        { err_no = EN11; goto error; }

      if ( i < 0 || i > n )
        /* wrong value of node */
        { err_no = EN12; goto error; }

      switch (nd) {
      case 's':  /* source line */

        if ( no_nslines != 0)
          /* more than one source line */
          { err_no = EN9; goto error; }

        no_nslines = 1;
        src = verts[i];
        break;

      case 't':  /* sink line */

        if ( no_nklines != 0)
          /* more than one sink line */
          { err_no = EN9; goto error; }

        no_nklines = 1;
        sink = verts[i];
        break;

      default:
        /* wrong type of node-line */
        err_no = EN12; goto error;
      }
      break;

    case 'a':                    /* arc description */
      if ( require_source_and_sink && (no_nslines == 0 || no_nklines == 0) )
        /* there was not source and sink description above */
        { err_no = EN14; goto error; }

      if ( no_alines >= m )
        /*too many arcs on input*/
        { err_no = EN16; goto error; }

      if (
          /* reading an arc description */
          std::sscanf ( in_line.c_str(),"%*c %ld %ld %ld",
                        &tail, &head, &cap )
          != ARC_FIELDS
          )
        /* arc description is not correct */
        { err_no = EN15; goto error; }

      --tail; // index from 0, not 1
      --head;
      if ( tail < 0  ||  tail > n  ||
           head < 0  ||  head > n
           )
        /* wrong value of nodes */
        { err_no = EN17; goto error; }

      {
        edge_descriptor e1, e2;
        bool in1, in2;
        boost::tie(e1, in1) = add_edge(verts[tail], verts[head], g);
        boost::tie(e2, in2) = add_edge(verts[head], verts[tail], g);
        if (!in1 || !in2) {
          std::cerr << "unable to add edge (" << head << "," << tail << ")"
                    << std::endl;
          return -1;
        }
        capacity[e1] = cap;
        capacity[e2] = 0;
        reverse_edge[e1] = e2;
        reverse_edge[e2] = e1;
      }
      ++no_alines;
      break;

    default:
      /* unknown type of line */
      err_no = EN18; goto error;

    } /* end of switch */
  }     /* end of input loop */

  /* ----- all is red  or  error while reading ----- */

  if ( in.eof() == 0 ) /* reading error */
    { err_no=EN21; goto error; }

  if ( no_lines == 0 ) /* empty input */
    { err_no = EN22; goto error; }

  if ( no_alines < m ) /* not enough arcs */
    { err_no = EN19; goto error; }

  if ( require_source_and_sink &&
       (out_degree(src, g) == 0 || out_degree(sink, g) == 0) )
    /* no arc goes out of the source */
    { err_no = EN20; goto error; }

  /* Thanks God! all is done */
  return (0);

  /* ---------------------------------- */
 error:  /* error found reading input */

  std::printf ( "\nline %ld of input - %s\n",
                no_lines, err_message[err_no] );

  return -1;
}
Ejemplo n.º 17
0
void compare(const string filename, const string samp_file, const string test_file,
			const int num_iter, const int probes,
			const double theta) {

	Graph g(filename, theta);

	int number_of_nodes, len_samp;
	// ifstream fin(samp_file + ".stat");
	ifstream fin(test_file + ".stat");	
	fin >> number_of_nodes >> len_samp;	
	fin.close();

	string pp_file = samp_file + "_probes-" + to_string(probes)+ ".p";
	
	// getting p
	vdoub_t p = load_p(pp_file, number_of_nodes, num_iter);

	fin.open(test_file);
	if (! fin.is_open()) {
		cerr << "cost: file not found: " << test_file << endl;		
	}

	// getting uniform:
	vdoub_t unif(number_of_nodes, 1.0/number_of_nodes);

	// getting out_degree:
	vdoub_t out_degree(number_of_nodes);
	for (int u = 0; u<number_of_nodes; ++u) {
		out_degree[u] = (double) g.outdeg[u]/(g.number_of_edges);
	}
	
	// getting in_degree:
	vdoub_t in_degree(number_of_nodes);
	for (int u = 0; u<number_of_nodes; ++u) {
		in_degree[u] = (double) g.indeg[u]/(g.number_of_edges);
	}


	// getting total_degree:
	vdoub_t tot_degree(number_of_nodes);
	for (int u = 0; u<number_of_nodes; ++u) {
		tot_degree[u] = (double) g.totaldeg[u]/(2*g.number_of_edges);
	}


	vdoub_t cost_val(5,0);	
	int k, u;
	double pS_p, pS_unif, pS_out, pS_in, pS_tot;	
	double cost_p = 0, cost_unif = 0, cost_out = 0, cost_in = 0, cost_tot = 0;
	while (fin >> k) {
		// vector<double> pS(pp.size(), 0);
		pS_p = 0;
		pS_unif = 0;
		pS_out = 0;
		pS_in = 0;
		pS_tot = 0;

		for (int i=0; i<k; ++i) {			
			fin >> u;
			pS_p += p[u];
			pS_unif += unif[u];
			pS_out += out_degree[u];
			pS_in  += in_degree[u];
			pS_tot += tot_degree[u];
			
		}

		cost_p += 1.0/(1 - theta*pow(1-pS_p,probes));
		cost_unif += 1.0/(1 - theta*pow(1-pS_unif,probes));
		cost_out += 1.0/(1 - theta*pow(1-pS_out,probes));
		cost_in += 1.0/(1 - theta*pow(1-pS_in,probes));
		cost_tot += 1.0/(1 - theta*pow(1-pS_tot,probes));

	}
	fin.close();

	cost_p /= len_samp;
	cost_unif /= len_samp;
	cost_out /= len_samp;
	cost_in /= len_samp;
	cost_tot /= len_samp;
	
	ofstream fout_comp(test_file + "-compare");
	// fout_comp << "p\t" << cost_p << endl;
	// fout_comp << "unif\t" << cost_unif << endl;
	// fout_comp << "out_degree\t" << cost_out << endl;
	// fout_comp << "in_degree\t" << cost_in << endl;
	// fout_comp << "tot_degree\t" << cost_tot << endl;

	fout_comp <<  cost_p << endl;
	fout_comp <<  cost_unif << endl;
	fout_comp <<  cost_out << endl;
	fout_comp <<  cost_in << endl;
	fout_comp <<  cost_tot << endl;

	fout_comp.close();
}