示例#1
0
//Full Collision Detection Sweep
void sweep_and_prune(){
  update_tokens();
  sweep(xtokens);
  sweep(ytokens);
  prune();
}
示例#2
0
文件: lzw.c 项目: g-mainak/LZW
int encode(int args[])
{
	//freopen("hash.h", "r", stdin);
	Hash *h = malloc(sizeof(Hash));
	int size = 1 << (args[1]+1);
	unsigned int time =1;
	initialize(h, size, args[3], time);
	int c=EMPTY, k, latestCode;
	int bits=(args[3])?3:9;
	printf("%d %d %d|", args[1], args[2], args[3]);
	while((k = getchar()) != EOF)
		if(findInHash(h, c , (char)k) != EMPTY)
			c = findInHash(h, c , (char)k);
		else
		{
			if(args[3] && findInHash(h, EMPTY, (char)k) == EMPTY) //Time to send escape sequence
			{
				if (c!=EMPTY)
				{
					putBits(bits, c);
					h->shadowArray[c]->time=(++time);
				}
				putBits(bits, ESCAPE);
				putBits(CHAR_BIT, k);
				if(h->numElements < ( 1 << args[1]))
				{
					latestCode = insert(h, EMPTY, (char)k, 1);
					if(latestCode>= (1<<(bits)))
					{
						putBits(bits, INCR);
						bits++;
					}
				}
				c = EMPTY;
			}
			else
			{
				putBits(bits, c);
				h->shadowArray[c]->time=(++time);
				if(h->numElements < ( 1 << args[1]))
				{
					latestCode = insert(h, c, (char)k, 1);
					if(latestCode>= (1<<(bits)))
					{
						putBits(bits, INCR);
						bits++;
					}
				}
				c = findInHash(h, EMPTY, (char)k);
			}
			if(args[2] && h->numElements == ( 1 << args[1])-1)
			{
				putBits(bits, RESET);
				putBits(bits, c);
				prune(args, &h, time);
				//bitchange
				c=EMPTY;
			}
		}
	if (c!=EMPTY)
		putBits(bits, c);
	flushBits();
	//printHash(h);
	freeHash (h);
	return 0;
}
示例#3
0
文件: lzw.c 项目: g-mainak/LZW
int decode()
{
	int k;
	Hash *h = malloc(sizeof(Hash));
	Stack *kStack = NULL;
	int oldC = EMPTY;
	int newC, C;
	char finalK;
	//freopen("b.txt", "r", stdin);
	int args[NUMARGS];
	scanf("%d %d %d|", args+1, args+2, args+3);
	int size = 1 << (args[1]+1);
	unsigned int time =1;
	initialize(h, size, args[3], time);
	int bits = (args[3])?3:9;
	while((newC = C = getBits(bits)) != EOF)
	{
		if(C==INCR)
		{
			bits++;
			continue;
		}
		if(C == ESCAPE) //Time to add a new sequence
		{
			newC = C = getBits(CHAR_BIT);
			if(h->numElements< ( 1 << (args[1])))
				insert(h, EMPTY, (char)C, 1);
			oldC = EMPTY;
			putchar(C);
			continue;
		}
		if(C == RESET)
		{
			Element *e  = findArray(h, getBits(bits));
			if (e && e->ch)
			{
				putchar(e->ch);
				finalK=e->ch;
			}
			prune(args, &h, time);
			//bitchange
			oldC= EMPTY;
			continue;
		}

		if(findArray(h, C)==EMPTY)
		{
			push(&kStack, finalK);
			C = oldC;
		}
		else
		{
			Element *elem = findArray(h, C);
			elem->time = time++;
		}

		Element *elem = findArray(h, C);
		while (elem && elem->prefix!=EMPTY)
		{
			push(&kStack, elem->ch);
			C = elem->prefix;
			elem = findArray(h, C);
		}

		finalK = elem->ch;
		putchar(finalK);

		while(kStack)
		{
			k = pop(&kStack);
			putchar(k);
		}

		if (oldC != EMPTY && h->numElements < ( 1 << (args[1])))
			insert(h, oldC, finalK, 1);
		oldC = newC;
	}
	//printArray(arr);
	free(h);
	return 0;
}
示例#4
0
void SimpleSkeletonGrower::backward_grow()
{
	if(_surface.is_loaded())
	{
		std::vector <osg::Vec3> backward_grow = _surface._relative_pts;

		//1. Initialization: setup a grow_map for book-keeping the backward growing
		//Key: rank of the <x,z> coords; Value: <low depth(y), high depth(y), isDone(1 or -1)>
		//this is used in addition to the relative bound for growing a dense foilage
		std::map <int, osg::Vec3> grow_map;

		for(unsigned int i=0; i<_surface._surface_pts.size(); i++)
		{
			osg::Vec3 pt = _surface._surface_pts[i];

			//only consider points that lay inside the boundary
			if(!_pruner.is_inside_ortho(pt))
				continue;

			int rank = (pt.x()+1000)*2500 + (pt.z()+1000);

			//check if exists, update its value
			if(grow_map.find(rank) != grow_map.end())
			{
				osg::Vec3 paras = grow_map[rank];
				osg::Vec3 new_paras = paras;

				if(pt.y() < paras.x())
					new_paras.x() = pt.y();
				if(pt.y() > paras.y())
					new_paras.y() = pt.y();

				grow_map[rank] = new_paras;
			}
			//new cloud point
			else
			{
				osg::Vec3 paras(pt.y(), pt.y(), -1);//new point has the same-depth and is un-done
				grow_map[rank] = paras;
			}
		}

		float inter_cons = 2.0f;
		float neighbor_dist = pow(inter_cons*1.05, 0.5) * _close_threshold;
		float neighbor_diag = neighbor_dist * 0.707f;
		//2. Loop each node pt in grow_map, un-rank it, and pretend to be a point in the relative bound
		for(std::map <int, osg::Vec3>::iterator it=grow_map.begin(); it!=grow_map.end(); it++)
		{
			//a. recover point, the y-coord is randomly picked between low and high
			int rank = it->first;
			//osg::Vec3 paras = it->second;
			//int y = rand()%(int(paras.y()-paras.x())) + paras.x();
			//osg::Vec3 pt(rank/2500-1000, y, rank%2500-1000);

			osg::Vec3 pt(rank/2500-1000, rank%2500-1000, 0.0f);
			backward_grow.push_back(pt);

			//add more neighbor points
			osg::Vec3 probe;
			if(false)
			{
				probe = osg::Vec3(pt.x()+neighbor_dist, pt.y(), 0.0f);//right
				if(!_pruner.is_inside_ortho(probe))
					backward_grow.push_back(probe);
				probe = osg::Vec3(pt.x()-neighbor_dist, pt.y(), 0.0f);//left
				if(!_pruner.is_inside_ortho(probe))
					backward_grow.push_back(probe);
				probe = osg::Vec3(pt.x(), pt.y()+neighbor_dist, 0.0f);//top
				if(!_pruner.is_inside_ortho(probe))
					backward_grow.push_back(probe);
				probe = osg::Vec3(pt.x(), pt.y()-neighbor_dist, 0.0f);//bottom
				if(!_pruner.is_inside_ortho(probe))
					backward_grow.push_back(probe);
			}

			probe = osg::Vec3(pt.x()+neighbor_diag, pt.y()+neighbor_diag, 0.0f);//top-right
			if(!_pruner.is_inside_ortho(probe))
				backward_grow.push_back(probe);
			probe = osg::Vec3(pt.x()-neighbor_diag, pt.y()+neighbor_diag, 0.0f);//top-left
			if(!_pruner.is_inside_ortho(probe))
				backward_grow.push_back(probe);
			probe = osg::Vec3(pt.x()+neighbor_diag, pt.y()-neighbor_diag, 0.0f);//bottom-right
			if(!_pruner.is_inside_ortho(probe))
				backward_grow.push_back(probe);
			probe = osg::Vec3(pt.x()-neighbor_diag, pt.y()-neighbor_diag, 0.0f);//bottom-left
			if(!_pruner.is_inside_ortho(probe))
				backward_grow.push_back(probe);
		}

		//given the non-even retaive contour points, backward grow it to the nearest tree nodes
		//each each node cannot grow more than twice, also the newly added retaive points are 
		//required to be mutually separated from previously added points, in addition to the old tree nodes
		std::vector <LibraryElement> elements = _library._library_element;

		std::vector <osg::Vec3> newly_added;
		std::map <BDLSkeletonNode *, int> grown_cnt;

		//put inside the loop becasue trees is updated, put outside for non-continuous effect
		std::vector <BDLSkeletonNode *> trees = bfs();
		BDLSkeletonNode *copied_root = BDLSkeletonNode::copy_tree(_root);

		for(unsigned int j=0; j<backward_grow.size(); j++)
		{
			osg::Vec3 temp = backward_grow[j];
			osg::Vec3 pt(temp.x(), 0.0f, temp.y());//add depth later

			//b. find min projected distance from tree
			float min_proj_dist = -1.0f;
			for(unsigned int i=0; i<trees.size(); i++)
			{
				osg::Vec3 T = Transformer::toVec3(trees[i]);
				float proj_dist = osg::Vec2(pt.x()-T.x(), pt.z()-T.z()).length2();
				if(min_proj_dist == -1.0f || proj_dist < min_proj_dist)
					min_proj_dist = proj_dist;
			}

			//c. find min projected distance from previously added points
			float min_proj_dist2 = -1.0f;
			for(unsigned int i=0; i<newly_added.size(); i++)
			{
				osg::Vec3 na = newly_added[i];
				float proj_dist = osg::Vec2(pt.x()-na.x(), pt.z()-na.z()).length2();
				if(min_proj_dist2 == -1.0f || proj_dist < min_proj_dist2)
					min_proj_dist2 = proj_dist;
			}

			//c. if minimum projected distance from trees T is larger than _close_threshold, then backward grow it
			float close_threshold2 = _close_threshold * _close_threshold;
			if(min_proj_dist > close_threshold2 && (min_proj_dist2 > inter_cons*close_threshold2 || newly_added.empty()))
			{
				//find the closest node N in trees T to grow
				BDLSkeletonNode *N = NULL;
				float min_tree_dist = -1.0f;
				for(unsigned int i=0; i<trees.size(); i++)
				{
					BDLSkeletonNode *tnode = trees[i];

					//ensure N has _prev
					if(!tnode->_prev)
						continue;

					//ensure N has no child
					//if(tnode->_children.size() > 1)
					//	continue;

					//check if N has been used more than twice first
					if(grown_cnt.find(tnode) != grown_cnt.end() && grown_cnt[tnode] > 0)
						continue;

					float cur_dist = (pt - Transformer::toVec3(tnode)).length2();
					if(min_tree_dist == -1.0f || cur_dist < min_tree_dist)
					{
						min_tree_dist = cur_dist;
						N = tnode;
					}
				}
				//fuel for backward grow is used up
				if(!N)
					return;

				//add depth of pt = 'depth of N' +/- 'half length of N and N->_prev'
				float par_len = BDLSkeletonNode::dist(N, N->_prev);
				if(par_len > 1.0f)
					pt.y() = N->_sy + rand()%int(par_len) - par_len/2.0f;
				else
					pt.y() = N->_sy + (rand()%3-1)*par_len/2.0f;

				//update book-keeping
				newly_added.push_back(pt);
				if(grown_cnt.find(N) != grown_cnt.end())
					grown_cnt[N]++;
				else
					grown_cnt[N] = 1;

				//shorten the N-pt segment by 10% to prevent unwanted pruning
				osg::Vec3 wrap = Transformer::toVec3(N);
				osg::Vec3 dir = pt - wrap;
				float desired_len = dir.length() * 0.95f;
				dir.normalize();
				pt = wrap + dir * desired_len;

				//new tip node
				BDLSkeletonNode *new_node = new BDLSkeletonNode(pt.x(), pt.y(), pt.z());

				//use advanced approach to grow it as in laser data
				if(true)
				{
					//before attaching, check if the divergent angle is too large
					float div_ang = Transformer::divergent_angle(N->_prev, N, new_node);
					if(div_ang > 30.0f)
					{
						//osg::Vec3 translateN = Transformer::toVec3(N->_prev) - wrap;
						//float len = translateN.length();
						//translateN.normalize();
						//translateN = wrap + translateN * len * 0.5f;

						//N->_sx = wrap.x();
						//N->_sy = wrap.y();
						//N->_sz = wrap.z();
						//if(N->_prev->_prev)
						//{
						//	N = N->_prev;
						//	div_ang = Transformer::divergent_angle(N->_prev, N, new_node);
						//}

						//search for the most similar divergent angle in the original tree
						BDLSkeletonNode *a, *b;
						if(Transformer::similar_angle_in_tree(copied_root, div_ang, a, b))
						{
							//printf("angle(%f) div(%f)\n", angle, Transformer::divergent_angle(a->_prev, a, b));
							//construct an elemnt out of a and b
							BDLSkeletonNode *support_tip = NULL;
							BDLSkeletonNode *sub_tree = Transformer::extract_subtree(a->_prev, b, support_tip);
							if(sub_tree && support_tip)
							{
								//should not add
								//new_node->_prev = N;
								//new_node->_prev_support = N;
								//N->_children.push_back(new_node);
								N->_prev_support = N->_prev;

								LibraryElement element(sub_tree, SLink(sub_tree, sub_tree->_children[0]));
								//rotation can't be random!
								int min_tran_ang = -1;
								float min_tran_ang_dist = -1.0f;
								for(int ta=0; ta<360; ta+=12)
								{
									LibraryElement element_test = element;
									Transformer::transform(N, ta, element_test);
									std::vector <BDLSkeletonNode *> compare = Transformer::terminal_nodes(element_test._root);
									for(unsigned int ter=0; ter<compare.size(); ter++)
									{
										float cur_dist = BDLSkeletonNode::dist(compare[ter], new_node);
										if(min_tran_ang_dist == -1.0f || cur_dist < min_tran_ang_dist)
										{
											min_tran_ang_dist = cur_dist;
											min_tran_ang = ta;
										}
									}
								}

								//printf("min_tran_ang_dist(%f)\n", min_tran_ang_dist);
								std::vector <BDLSkeletonNode *> subtree = replace_branch(N, element, min_tran_ang);
								prune(subtree);

								//printf("%f %f %f\n", pt.x(), pt.y(), pt.z());
								//pt = Transformer::toVec3(N);
								//printf("%f %f %f\n", pt.x(), pt.y(), pt.z());
								if(false || j==617)
								{
									Transformer::straight_up(element);
									char buffer[100];
									sprintf(buffer, "/tmp/log_sk/log_skeleton_%d", j);
									BDLSkeletonNode::save_skeleton(element._root, buffer);
								}
							}
							else
								printf("SimplSkeletonGrower::backward_grow():extract_subtree: a(%p) b(%p) error\n", a, b);
							delete new_node;
						}
						else
						{
							//if no suitable sub-tree can be found, just don't grow the node
							//printf("%f %f %f\n", pt.x(), pt.y(), pt.z());
							delete new_node;
						}
					}
					else
					{
						//directly attach it to produce a straight branch
						new_node->_prev = N;
						N->_children.push_back(new_node);

						//too far away, replace it
						//if(desired_len > _close_threshold * 2.0f)
						//{
						//	new_node->_prev_support = N;
						//	std::vector <BDLSkeletonNode *> subtree = replace_branch(new_node, elements[rand()%elements.size()], rand()%360);
						//	//prune(subtree); //prune leniently
						//}
					}
				}

				//visualize pt
				//printf("%f %f %f\n", pt.x(), pt.y(), pt.z());
			}
			//d. continue for the next point
		} //end for loop
		BDLSkeletonNode::delete_this(copied_root);
	}
}
示例#5
0
文件: rmaps_ppr.c 项目: bgoglin/ompi
static int ppr_mapper(orte_job_t *jdata)
{
    int rc = ORTE_SUCCESS, j, n;
    mca_base_component_t *c=&mca_rmaps_ppr_component.base_version;
    orte_node_t *node;
    orte_proc_t *proc;
    orte_app_context_t *app;
    orte_vpid_t total_procs, nprocs_mapped;
    opal_hwloc_level_t start=OPAL_HWLOC_NODE_LEVEL;
    hwloc_obj_t obj;
    hwloc_obj_type_t lowest;
    unsigned cache_level=0;
    unsigned int nobjs, i;
    bool pruning_reqd = false;
    opal_hwloc_level_t level;
    opal_list_t node_list;
    opal_list_item_t *item;
    orte_std_cntr_t num_slots;
    orte_app_idx_t idx;
    char **ppr_req, **ck;
    size_t len;
    bool initial_map=true;

    /* only handle initial launch of loadbalanced
     * or NPERxxx jobs - allow restarting of failed apps
     */
    if (ORTE_FLAG_TEST(jdata, ORTE_JOB_FLAG_RESTART)) {
        opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                            "mca:rmaps:ppr: job %s being restarted - ppr cannot map",
                            ORTE_JOBID_PRINT(jdata->jobid));
        return ORTE_ERR_TAKE_NEXT_OPTION;
    }
    if (NULL != jdata->map->req_mapper &&
        0 != strcasecmp(jdata->map->req_mapper, c->mca_component_name)) {
        /* a mapper has been specified, and it isn't me */
        opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                            "mca:rmaps:ppr: job %s not using ppr mapper",
                            ORTE_JOBID_PRINT(jdata->jobid));
        return ORTE_ERR_TAKE_NEXT_OPTION;
    }
    if (NULL == jdata->map->ppr ||
        ORTE_MAPPING_PPR != ORTE_GET_MAPPING_POLICY(jdata->map->mapping)) {
        /* not for us */
        opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                            "mca:rmaps:ppr: job %s not using ppr mapper PPR %s policy %s",
                            ORTE_JOBID_PRINT(jdata->jobid),
                            (NULL == jdata->map->ppr) ? "NULL" : jdata->map->ppr,
                            (ORTE_MAPPING_PPR == ORTE_GET_MAPPING_POLICY(jdata->map->mapping)) ? "PPRSET" : "PPR NOTSET");
        return ORTE_ERR_TAKE_NEXT_OPTION;
    }

    opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                        "mca:rmaps:ppr: mapping job %s with ppr %s",
                        ORTE_JOBID_PRINT(jdata->jobid), jdata->map->ppr);

    /* flag that I did the mapping */
    if (NULL != jdata->map->last_mapper) {
        free(jdata->map->last_mapper);
    }
    jdata->map->last_mapper = strdup(c->mca_component_name);

    /* initialize */
    memset(ppr, 0, OPAL_HWLOC_HWTHREAD_LEVEL * sizeof(opal_hwloc_level_t));

    /* parse option */
    n=0;
    ppr_req = opal_argv_split(jdata->map->ppr, ',');
    for (j=0; NULL != ppr_req[j]; j++) {
        /* split on the colon */
        ck = opal_argv_split(ppr_req[j], ':');
        if (2 != opal_argv_count(ck)) {
            /* must provide a specification */
            orte_show_help("help-orte-rmaps-ppr.txt", "invalid-ppr", true, jdata->map->ppr);
            opal_argv_free(ppr_req);
            opal_argv_free(ck);
            return ORTE_ERR_SILENT;
        }
        len = strlen(ck[1]);
        if (0 == strncasecmp(ck[1], "node", len)) {
            ppr[OPAL_HWLOC_NODE_LEVEL] = strtol(ck[0], NULL, 10);
            ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYNODE);
            start = OPAL_HWLOC_NODE_LEVEL;
            n++;
        } else if (0 == strncasecmp(ck[1], "hwthread", len) ||
                   0 == strncasecmp(ck[1], "thread", len)) {
            ppr[OPAL_HWLOC_HWTHREAD_LEVEL] = strtol(ck[0], NULL, 10);
            start = OPAL_HWLOC_HWTHREAD_LEVEL;
            ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYHWTHREAD);
            n++;
        } else if (0 == strncasecmp(ck[1], "core", len)) {
            ppr[OPAL_HWLOC_CORE_LEVEL] = strtol(ck[0], NULL, 10);
            if (start < OPAL_HWLOC_CORE_LEVEL) {
                start = OPAL_HWLOC_CORE_LEVEL;
                ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYCORE);
            }
            n++;
        } else if (0 == strncasecmp(ck[1], "socket", len) ||
                   0 == strncasecmp(ck[1], "skt", len)) {
            ppr[OPAL_HWLOC_SOCKET_LEVEL] = strtol(ck[0], NULL, 10);
            if (start < OPAL_HWLOC_SOCKET_LEVEL) {
                start = OPAL_HWLOC_SOCKET_LEVEL;
                ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYSOCKET);
            }
            n++;
        } else if (0 == strncasecmp(ck[1], "l1cache", len)) {
            ppr[OPAL_HWLOC_L1CACHE_LEVEL] = strtol(ck[0], NULL, 10);
            if (start < OPAL_HWLOC_L1CACHE_LEVEL) {
                start = OPAL_HWLOC_L1CACHE_LEVEL;
                cache_level = 1;
                ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYL1CACHE);
            }
            n++;
        } else if (0 == strncasecmp(ck[1], "l2cache", len)) {
            ppr[OPAL_HWLOC_L2CACHE_LEVEL] = strtol(ck[0], NULL, 10);
            if (start < OPAL_HWLOC_L2CACHE_LEVEL) {
                start = OPAL_HWLOC_L2CACHE_LEVEL;
                cache_level = 2;
                ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYL2CACHE);
            }
            n++;
        } else if (0 == strncasecmp(ck[1], "l3cache", len)) {
            ppr[OPAL_HWLOC_L3CACHE_LEVEL] = strtol(ck[0], NULL, 10);
            if (start < OPAL_HWLOC_L3CACHE_LEVEL) {
                start = OPAL_HWLOC_L3CACHE_LEVEL;
                cache_level = 3;
                ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYL3CACHE);
            }
            n++;
        } else if (0 == strncasecmp(ck[1], "numa", len)) {
            ppr[OPAL_HWLOC_NUMA_LEVEL] = strtol(ck[0], NULL, 10);
            if (start < OPAL_HWLOC_NUMA_LEVEL) {
                start = OPAL_HWLOC_NUMA_LEVEL;
                ORTE_SET_MAPPING_POLICY(jdata->map->mapping, ORTE_MAPPING_BYNUMA);
            }
            n++;
        } else {
            /* unknown spec */
            orte_show_help("help-orte-rmaps-ppr.txt", "unrecognized-ppr-option", true, ck[1], jdata->map->ppr);
            opal_argv_free(ppr_req);
            opal_argv_free(ck);
            return ORTE_ERR_SILENT;
        }
        opal_argv_free(ck);
    }
    opal_argv_free(ppr_req);
    /* if nothing was given, that's an error */
    if (0 == n) {
        opal_output(0, "NOTHING GIVEN");
        return ORTE_ERR_SILENT;
    }
    /* if more than one level was specified, then pruning will be reqd */
    if (1 < n) {
        pruning_reqd = true;
    }

    opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                        "mca:rmaps:ppr: job %s assigned policy %s",
                        ORTE_JOBID_PRINT(jdata->jobid),
                        orte_rmaps_base_print_mapping(jdata->map->mapping));

    /* convenience */
    level = start;
    lowest = opal_hwloc_levels[start];

    for (idx=0; idx < (orte_app_idx_t)jdata->apps->size; idx++) {
        if (NULL == (app = (orte_app_context_t*)opal_pointer_array_get_item(jdata->apps, idx))) {
            continue;
        }

        /* if the number of total procs was given, set that
         * limit - otherwise, set to max so we simply fill
         * all the nodes with the pattern
         */
        if (0 < app->num_procs) {
            total_procs = app->num_procs;
        } else {
            total_procs = ORTE_VPID_MAX;
        }

        /* get the available nodes */
        OBJ_CONSTRUCT(&node_list, opal_list_t);
        if(ORTE_SUCCESS != (rc = orte_rmaps_base_get_target_nodes(&node_list, &num_slots, app,
                                                                  jdata->map->mapping, initial_map, false))) {
            ORTE_ERROR_LOG(rc);
            goto error;
        }
        /* flag that all subsequent requests should not reset the node->mapped flag */
        initial_map = false;

        /* if a bookmark exists from some prior mapping, set us to start there */
        jdata->bookmark = orte_rmaps_base_get_starting_point(&node_list, jdata);

        /* cycle across the nodes */
        nprocs_mapped = 0;
        for (item = opal_list_get_first(&node_list);
             item != opal_list_get_end(&node_list);
             item = opal_list_get_next(item)) {
            node = (orte_node_t*)item;
            /* bozo check */
            if (NULL == node->topology || NULL == node->topology->topo) {
                orte_show_help("help-orte-rmaps-ppr.txt", "ppr-topo-missing",
                               true, node->name);
                rc = ORTE_ERR_SILENT;
                goto error;
            }
            /* add the node to the map, if needed */
            if (!ORTE_FLAG_TEST(node, ORTE_NODE_FLAG_MAPPED)) {
                ORTE_FLAG_SET(node, ORTE_NODE_FLAG_MAPPED);
                OBJ_RETAIN(node);
                opal_pointer_array_add(jdata->map->nodes, node);
                jdata->map->num_nodes++;
            }
            /* if we are mapping solely at the node level, just put
             * that many procs on this node
             */
            if (OPAL_HWLOC_NODE_LEVEL == start) {
                obj = hwloc_get_root_obj(node->topology->topo);
                for (j=0; j < ppr[start] && nprocs_mapped < total_procs; j++) {
                    if (NULL == (proc = orte_rmaps_base_setup_proc(jdata, node, idx))) {
                        rc = ORTE_ERR_OUT_OF_RESOURCE;
                        goto error;
                    }
                    nprocs_mapped++;
                    orte_set_attribute(&proc->attributes, ORTE_PROC_HWLOC_LOCALE, ORTE_ATTR_LOCAL, obj, OPAL_PTR);
                }
            } else {
                /* get the number of lowest resources on this node */
                nobjs = opal_hwloc_base_get_nbobjs_by_type(node->topology->topo,
                                                           lowest, cache_level,
                                                           OPAL_HWLOC_AVAILABLE);

                /* map the specified number of procs to each such resource on this node,
                 * recording the locale of each proc so we know its cpuset
                 */
                for (i=0; i < nobjs; i++) {
                    obj = opal_hwloc_base_get_obj_by_type(node->topology->topo,
                                                          lowest, cache_level,
                                                          i, OPAL_HWLOC_AVAILABLE);
                    for (j=0; j < ppr[start] && nprocs_mapped < total_procs; j++) {
                        if (NULL == (proc = orte_rmaps_base_setup_proc(jdata, node, idx))) {
                            rc = ORTE_ERR_OUT_OF_RESOURCE;
                            goto error;
                        }
                        nprocs_mapped++;
                        orte_set_attribute(&proc->attributes, ORTE_PROC_HWLOC_LOCALE, ORTE_ATTR_LOCAL, obj, OPAL_PTR);
                    }
                }

                if (pruning_reqd) {
                    /* go up the ladder and prune the procs according to
                     * the specification, adjusting the count of procs on the
                     * node as we go
                     */
                    level--;
                    prune(jdata->jobid, idx, node, &level, &nprocs_mapped);
                }
            }

            if (!(ORTE_MAPPING_DEBUGGER & ORTE_GET_MAPPING_DIRECTIVE(jdata->map->mapping))) {
                /* set the total slots used */
                if ((int)node->num_procs <= node->slots) {
                    node->slots_inuse = (int)node->num_procs;
                } else {
                    node->slots_inuse = node->slots;
                }

                /* if no-oversubscribe was specified, check to see if
                 * we have violated the total slot specification - regardless,
                 * if slots_max was given, we are not allowed to violate it!
                 */
                if ((node->slots < (int)node->num_procs) ||
                    (0 < node->slots_max && node->slots_max < (int)node->num_procs)) {
                    if (ORTE_MAPPING_NO_OVERSUBSCRIBE & ORTE_GET_MAPPING_DIRECTIVE(jdata->map->mapping)) {
                        orte_show_help("help-orte-rmaps-base.txt", "orte-rmaps-base:alloc-error",
                                       true, node->num_procs, app->app);
                        ORTE_UPDATE_EXIT_STATUS(ORTE_ERROR_DEFAULT_EXIT_CODE);
                        rc = ORTE_ERR_SILENT;
                        goto error;
                    }
                    /* flag the node as oversubscribed so that sched-yield gets
                     * properly set
                     */
                    ORTE_FLAG_SET(node, ORTE_NODE_FLAG_OVERSUBSCRIBED);
                    ORTE_FLAG_SET(jdata, ORTE_JOB_FLAG_OVERSUBSCRIBED);
                    /* check for permission */
                    if (ORTE_FLAG_TEST(node, ORTE_NODE_FLAG_SLOTS_GIVEN)) {
                        /* if we weren't given a directive either way, then we will error out
                         * as the #slots were specifically given, either by the host RM or
                         * via hostfile/dash-host */
                        if (!(ORTE_MAPPING_SUBSCRIBE_GIVEN & ORTE_GET_MAPPING_DIRECTIVE(jdata->map->mapping))) {
                            orte_show_help("help-orte-rmaps-base.txt", "orte-rmaps-base:alloc-error",
                                           true, app->num_procs, app->app);
                            ORTE_UPDATE_EXIT_STATUS(ORTE_ERROR_DEFAULT_EXIT_CODE);
                            return ORTE_ERR_SILENT;
                        } else if (ORTE_MAPPING_NO_OVERSUBSCRIBE & ORTE_GET_MAPPING_DIRECTIVE(jdata->map->mapping)) {
                            /* if we were explicitly told not to oversubscribe, then don't */
                            orte_show_help("help-orte-rmaps-base.txt", "orte-rmaps-base:alloc-error",
                                           true, app->num_procs, app->app);
                            ORTE_UPDATE_EXIT_STATUS(ORTE_ERROR_DEFAULT_EXIT_CODE);
                            return ORTE_ERR_SILENT;
                        }
                    }
                }
            }

            /* if we haven't mapped all the procs, continue on to the
             * next node
             */
            if (total_procs == nprocs_mapped) {
                break;
            }
        }
        if (0 == app->num_procs) {
            app->num_procs = nprocs_mapped;
        }
        if (ORTE_VPID_MAX != total_procs && nprocs_mapped < total_procs) {
            /* couldn't map them all */
            orte_show_help("help-orte-rmaps-ppr.txt", "ppr-too-many-procs",
                           true, app->app, app->num_procs, jdata->map->ppr);
            rc = ORTE_ERR_SILENT;
            goto error;
        }

        /* track the total number of processes we mapped - must update
         * this AFTER we compute vpids so that computation is done
         * correctly
         */
        jdata->num_procs += app->num_procs;

        while (NULL != (item = opal_list_remove_first(&node_list))) {
            OBJ_RELEASE(item);
        }
        OBJ_DESTRUCT(&node_list);
    }
    return ORTE_SUCCESS;

  error:
    while (NULL != (item = opal_list_remove_first(&node_list))) {
        OBJ_RELEASE(item);
    }
    OBJ_DESTRUCT(&node_list);
    return rc;
}
// clear all rows of type "id" from the buffer.
void LogBuffer::clear(log_id_t id, uid_t uid) {
    pthread_mutex_lock(&mLogElementsLock);
    prune(id, ULONG_MAX, uid);
    pthread_mutex_unlock(&mLogElementsLock);
}
示例#7
0
float DspSegmentAlgorithm::frameSegmentation() {
	time++;

	//0时刻放入初值
	if (time == 0) {
		
		if((unsigned int)(m_pMemPool + *m_pMPidx)%sizeof(DSPStateTrace))
			*m_pMPidx += sizeof(DSPStateTrace) - (unsigned int)(m_pMemPool + *m_pMPidx)%sizeof(DSPStateTrace);

		DSPStateTrace* s0 = (DSPStateTrace* )(m_pMemPool + *m_pMPidx);
		*m_pMPidx += sizeof(DSPStateTrace);

		int cb[STATE_NUM_IN_WORD];
		m_pDict->getWordCbId(ansList[0], NO_PREVIOUS_WORD, NO_NEXT_WORD, cb);

		//种子放入第一字的I0状态
		int cbidx0 = cb[INITIAL0];
		float stateLh0 = m_pBC->getStateLh(cbidx0, time);

		stateLh0;	
		s0->set(time, cbidx0, stateLh0, 0, NULL, -1);
		(m_binSet + 1)->addStateTrace(s0);


		//种子放入句首的sil
		DSPStateTrace* s1 = (DSPStateTrace* )(m_pMemPool + *m_pMPidx);
		*m_pMPidx += sizeof(DSPStateTrace);

		int cbidx1 = NOISE_ID;
		float stateLh1 = m_pBC->getStateLh(cbidx1, time);
		s1->set(time, cbidx1, stateLh1, 0, NULL, -1);
		m_binSet->addStateTrace(s1);

		/*return 99;*/
		//test
	return 99;
	}

	//time != 0

	for (int i = m_nBinsetLen - 1; i >= 0; i--) {
		DSPStateTraceBin* b = m_binSet + i;
		
		int cbidx = b->cbidx;
		
		
		//在一句话的最后一字可能发生这样的情况,因为不存在下一字,无法协同发音,协同发音的状态ID的值为INVALID_STATE_ID
		if (cbidx == -1) 
			continue;

		float stateLh = m_pBC->getStateLh(cbidx, time);

		/*if(stateLh < -50000000 || stateLh > 0)
			return 88000 + cbidx;*/
		//处理状态内跳转
		for (int j = 0; j < b->contentSize; j++) {
			DSPStateTrace* st = b->content[j];
		/*if(st->lh < -5000000 || st->lh > 0)
			return 101;*/
			float newLh = st->lh + stateLh;
			st->lh = newLh;
		}

		//状态剪枝
		//b->prune(factory);
		/*if(stateLh < -50000000 || stateLh > 0)
			return 77000 + cbidx;*/
		//处理状态间跳转		
		DSPStateTrace* previousBest = bestPreviousTrace(b);
		if (previousBest == NULL)
			continue;

		/*if(stateLh < -50000000 || stateLh > 0)
			return 66000 + cbidx;*/

		if((unsigned int)(m_pMemPool + *m_pMPidx)%sizeof(DSPStateTrace))
			*m_pMPidx += sizeof(DSPStateTrace) - (unsigned int)(m_pMemPool + *m_pMPidx)%sizeof(DSPStateTrace);

		DSPStateTrace* copy = (DSPStateTrace* )(m_pMemPool + *m_pMPidx);
		*m_pMPidx += sizeof(DSPStateTrace);

		copy->set(previousBest->enterTime, previousBest->cbidx, previousBest->lh, 0, previousBest->prev, -1);
		if (previousBest->prev != NULL)
			previousBest->prev->refcnt++;
		
		/*if(copy->lh < -50000000 || copy->lh > 0)
			return 3303;
		if(stateLh < -50000000 || stateLh > 0)
			return 44000 + cbidx;*/
		float newLh = stateLh + previousBest->lh;
		DSPStateTrace* newTrace = (DSPStateTrace* )(m_pMemPool + *m_pMPidx);
		*m_pMPidx += sizeof(DSPStateTrace);
		newTrace->set(time, cbidx, newLh, 0, copy, -1);
		copy->refcnt++;
		b->addStateTrace(newTrace);

		/*if(newTrace->lh < -50000000 || newTrace->lh > 0)
			return cbidx;*/


		//状态剪枝
		prune(b);
		/*if(test == 5)
			return test;*/
	}
	return 99;
}
void ImageResource::clear()
{
    prune();
    clearImage();
    setEncodedSize(0);
}
示例#9
0
void PageCache::pruneToSizeNow(unsigned size, PruningReason pruningReason)
{
    TemporaryChange<unsigned> change(m_maxSize, size);
    prune(pruningReason);
}
示例#10
0
void analyzeVideo(const std::string& folder, const Camera& calibrated_camera, float label_width)
{
    // Start with a single hypotheses of the cube.
    std::vector<ProbabalisticCube> cube_hypotheses;
    cube_hypotheses.push_back(ProbabalisticCube());
    for (int frame_i = 0;;)
    {
        printf("Frame %i\n", frame_i);

        char buf[1024];
        sprintf(buf, "%s/frame%05d.png", folder.c_str(), frame_i);
        cv::Mat3b img = cv::imread(buf, cv::IMREAD_COLOR);
        if (img.empty())
        {
            break;
        }

        const size_t num_hypotheses_before = cube_hypotheses.size();
        printf("Num hypotheses before: %lu\n", num_hypotheses_before);

        cube_hypotheses = predict(cube_hypotheses);
        const size_t num_hypotheses_after = cube_hypotheses.size();
        printf("Num hypotheses after:  %lu\n", num_hypotheses_after);
        printf("Brancing factor:  %f\n", double(num_hypotheses_after) / num_hypotheses_before);

        const size_t max_hypotheses = 216;
        if (num_hypotheses_after > max_hypotheses)
        {
            const size_t pruned_num = num_hypotheses_after - max_hypotheses;
            const double removed_percentage = pruned_num * 100.0 / num_hypotheses_after;
            printf("Pruning to %lu hypotheses, removing %lu (%.1f%%) hypotheses.\n",
                max_hypotheses, pruned_num, removed_percentage);
        }
        prune(cube_hypotheses, max_hypotheses);

        printf("Most likely cubes:\n");
        for (int i = 0; i < cube_hypotheses.size() && i < 5; ++i)
        {
            const ProbabalisticCube& cube = cube_hypotheses[i];
            const std::string permutation = cube.cube_permutation.to_String();
            const double likelihood_percent = exp(cube.log_likelihood) * 100.0;
            printf("%d: %s %3.5f%%\n", i, permutation.c_str(), likelihood_percent);
        }

        std::vector<LabelContour> labels = findLabelContours(img, 12, true);
        std::vector<std::vector<cv::Point2f>> detected_corners = findLabelCorners(labels);

        std::vector<Camera> all_camera_candidates;
        for (const auto& corners : detected_corners)
        {
            std::vector<Camera> cameras = predictCameraPosesForLabel(calibrated_camera, corners, label_width);
            all_camera_candidates.insert(all_camera_candidates.end(), cameras.begin(), cameras.end());
        }

        std::vector<double> camera_scores;
        camera_scores.reserve(all_camera_candidates.size());
        {
            cv::Mat1f accumulation(img.size(), 0.f);
            for (const auto& cam : all_camera_candidates)
            {
                cv::Mat1f contribution(img.size(), 0.f);
                std::vector<cv::Point2f> predicted_corners = projectCubeCorners(cam, label_width);
                double score = scorePredictedCorners(predicted_corners, detected_corners);
                camera_scores.push_back(score);

                for (int i = 0; i < predicted_corners.size(); i += 4)
                {
                    std::vector<cv::Point> corners = {
                        predicted_corners[i + 0],
                        predicted_corners[i + 1],
                        predicted_corners[i + 2],
                        predicted_corners[i + 3],
                    };
                    cv::polylines(contribution, corners, true, cv::Scalar(score * score));
                }
                accumulation += contribution;
            }
            double minval;
            double maxval;
            cv::minMaxLoc(accumulation, &minval, &maxval);
            cv::imshow("accumulation", accumulation / maxval);
        }
        if (!camera_scores.empty())
        {
            std::vector<double> sorted_camera_scores = camera_scores;
            std::sort(sorted_camera_scores.begin(), sorted_camera_scores.end());
            std::reverse(sorted_camera_scores.begin(), sorted_camera_scores.end());
            printf("Min score: %f max score: %f\n", sorted_camera_scores.back(), sorted_camera_scores.front());

            for (int i = 0; i < sorted_camera_scores.size() && i < 5; ++i)
            {
                double score = sorted_camera_scores[i];
                printf("#%d score: %f\n", i + 1, score);
            }
        }

        if (!all_camera_candidates.empty())
        {
            const size_t index = std::distance(camera_scores.begin(),
                std::max_element(camera_scores.begin(), camera_scores.end()));
            printf("detected_corners size: %lu index: %lu\n", detected_corners.size(), index);

            const Camera& cam = all_camera_candidates[index];
            std::vector<cv::Point2f> predicted_corners = projectCubeCorners(cam, label_width);

            cv::Mat3b canvas = img * 0.25f;
            for (int i = 0; i < predicted_corners.size(); i += 4)
            {
                std::vector<cv::Point> corners = {
                    predicted_corners[i + 0],
                    predicted_corners[i + 1],
                    predicted_corners[i + 2],
                    predicted_corners[i + 3],
                };
                cv::polylines(canvas, corners, true, cv::Scalar(0, 0, 255));
            }

            {
                std::vector<cv::Point> corners = {
                    detected_corners[index / 9][0],
                    detected_corners[index / 9][1],
                    detected_corners[index / 9][2],
                    detected_corners[index / 9][3],
                };
                cv::polylines(canvas, corners, true, cv::Scalar(255, 0, 255));
            }

            cv::imshow("predicted labels", canvas);
        }

        {
            cv::Mat3b canvas = img * 0.25f;
            drawLabels(canvas, labels, cv::Scalar(255, 255, 255));

            for (const auto& corners : detected_corners)
            {
                cv::polylines(canvas, cast<cv::Point>(corners), true, cv::Scalar(0, 0, 255));
                for (size_t i = 0; i < corners.size(); ++i)
                {
                    char text[12];
                    sprintf(text, "%lu", i);
                    cv::putText(canvas, text, corners[i],
                        cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 255));
                }
            }

            cv::imshow("detected labels", canvas);
        }

        if (int key = cv::waitKey(0) & 255)
        {
            if (key == 27)
            {
                break; // stop by pressing ESC
            }
            if (key == 32) // space
            {
                ++frame_i;
            }
            if (key == 83) // right arrow
            {
                ++frame_i;
            }
            if (key == 82) // up arrow
            {

            }
            if (key == 81) // left arrow
            {
                --frame_i;
            }
            if (key == 84) // down arrow
            {

            }
            if (key != 255)
            {
                printf("Key: %d\n", key);
                fflush(stdout);
            }
        }
    }
}
示例#11
0
    void quadtree::prune(uint32_t tolerance)
	{
		prune(root_, tolerance);
	}
示例#12
0
void SimpleSkeletonGrower::grow_potential(int no_branch)
{
	//overview: maintain a volume to represent the potential energy at each step,
	//then find the min_element_index and the min_rotate that require the minimum potential energy

	if(!_surface.is_loaded() || !_root)
	{
		printf("SimpleSkeletonGrower::surface error\n");
		return;
	}

	//find the dimensions of the volume
	float v_height = -1.0f;
	osg::Vec3 origin(0.0f, 0.0f, 0.0f);
	for(unsigned int i=0; i<_surface._surface_pts.size(); i++)
	{
		float cur = (_surface._surface_pts[i] - origin).length2();
		if(v_height == -1.0f || cur > v_height)
			v_height = cur;
	}
	if(v_height == -1.0f)
	{
		printf("SimpleSkeletonGrower::grow_potential():v_height(-1.0f) error\n");
		return;
	}

	//v_height = sqrt(v_height) * 1.5f; //old
	v_height = sqrt(v_height) * 2.0f; //new
	float v_step = v_height / 100.0f;
	osg::Vec3 corner(-v_height/2.0f, -v_height/2.0f, 0.0f);
	SimpleVolumeFloat space(v_height, v_height, v_height, v_step, v_step, v_step);
	float energy_effect = v_step * 18;

	//negative charges on initial skeleton
	//bfs
	std::queue <BDLSkeletonNode *> Queue;
	Queue.push(_root);

	while(!Queue.empty())
	{
		BDLSkeletonNode *front = Queue.front();
		Queue.pop();

		osg::Vec3 cur = Transformer::toVec3(front) - corner;
		space.add_potential(cur.x(), cur.y(), cur.z(), -3, energy_effect);

		for(unsigned int i=0; i<front->_children.size(); i++)
			Queue.push(front->_children[i]);
	}

	//positive charges on volume surface
	for(unsigned int i=0; i<_surface._surface_pts.size(); i++)
	{
		osg::Vec3 cur = _surface._surface_pts[i] - corner;
		space.add_potential(cur.x(), cur.y(), cur.z(), 1, energy_effect*1.5f);
	}

    std::vector <LibraryElement> elements = _library._library_element;

    for(int i=0; i<no_branch; i++)
    {
		//pick a tail first
		//not by minimum generation, but by farest position from surface point
        BDLSkeletonNode *tail = pick_branch(true);
        if(!tail)
        {
            printf("SimpleSkeletonGrower::grow():converged at %d-th step.\n", i+1);
            break;
        }

		//compute the node-->id dict
		std::map <BDLSkeletonNode *, int> dict;
		int id = 0;
		
		//bfs on source
		std::queue <BDLSkeletonNode *> Queue;
		Queue.push(_root);

		while(!Queue.empty())
		{
			BDLSkeletonNode *front = Queue.front();
			Queue.pop();

			dict[front] = id;

			for(unsigned int j=0; j<front->_children.size(); j++)
				Queue.push(front->_children[j]);

			id++;
		}

		int tail_id = dict[tail];

		//####trial starts####
		int min_element_index = -1;
		int min_rotate = -1;
		float min_energy = -1.0f;

		for(unsigned int e=0; e<elements.size(); e++)
			for(int r=0; r<360; r+=30)
			{
				LibraryElement element_trial = elements[e];

				if(false)
				{
					//copy the original(_root) tree and find the corresponding tail
					BDLSkeletonNode *copy_root = BDLSkeletonNode::copy_tree(_root);
					BDLSkeletonNode *copy_tail = NULL;

					//bfs on target, Queue is now empty at this point
					id = 0;
					Queue.push(copy_root);

					while(!Queue.empty())
					{
						BDLSkeletonNode *front = Queue.front();
						Queue.pop();

						if(id == tail_id)
							copy_tail = front;

						for(unsigned int j=0; j<front->_children.size(); j++)
							Queue.push(front->_children[j]);

						id++;
					}

					//branch replacement
					std::vector <BDLSkeletonNode *> subtree_trial = replace_branch(copy_tail, element_trial, r);
					std::vector <BDLSkeletonNode *> non_pruned_nodes = after_pruned(subtree_trial); //but logically
					//prune(subtree_trial); //no need to do it actually
				}

				std::vector <BDLSkeletonNode *> subtree_trial = replace_branch_unchanged(tail, element_trial, r);
				std::vector <BDLSkeletonNode *> non_pruned_nodes = after_pruned(subtree_trial); //but logically

				//compute the potential energy
				//float energy = non_pruned_nodes.empty() ? 23418715 : compute_potential_energy(subtree_trial, space);
				float energy = compute_potential_energy(non_pruned_nodes, space);

				if(energy == 23418715.0f) //to let it grow a little bit randomly even it's completely outside the segmentation
				{
					min_element_index = rand()%elements.size();
					min_rotate = rand()%360;
					min_energy = 0.0f;
				}
				else if(min_energy == -1.0f || energy > min_energy) //the max in value is the min potential energy for negative charge
				{
					min_element_index = e;
					min_rotate = r;
					min_energy = energy;
				}

				//delete the copied tree
				//BDLSkeletonNode::delete_this(copy_root);
				for(unsigned int j=0; j<subtree_trial.size(); j++)
					delete subtree_trial[j];
			}
		//####trial ends####

		//actual replacement
		if(min_energy != -1.0f)
		{
			//printf("tail(%p) min_energy(%f) min_element_index(%d) min_rotate(%d)\n", tail, min_energy, min_element_index, min_rotate);
			std::vector <BDLSkeletonNode *> subtree = replace_branch(tail, elements[min_element_index], min_rotate);
			std::vector <BDLSkeletonNode *> update_nodes = after_pruned(subtree);
			prune(subtree); //prune leniently

			//update space
			for(unsigned int j=0; j<update_nodes.size(); j++)
			{
				osg::Vec3 cur = Transformer::toVec3(update_nodes[j]) - corner;
				space.add_potential(cur.x(), cur.y(), cur.z(), -3, energy_effect); //negative charge
			}
		}
		//if(i > no_branch * 0.5f && i < no_branch * 0.75f)
		if(i > no_branch * 0.75f)
		{
			if(!_approaching_done)
				_close_threshold /= 2.0f;
			_approaching_done = true;
		}
		else if(i >= no_branch * 0.90f)
			_approaching_done = false;
    }

	//aftermath: should be shared for different grow methods
	//prune strictly on entire tree
	prune_strictly(_root);
	if(true)
	{
		//printf("backward_grow()\n");
		backward_grow();
		prune_strictly(_root);
	}
}
示例#13
0
void SimpleSkeletonGrower::grow_exhaustive(int no_branch)
{
    std::vector <LibraryElement> elements = _library._library_element;

    for(int i=0; i<no_branch; i++)
    {
		//pick a tail first
        BDLSkeletonNode *tail = pick_branch();
        if(!tail)
        {
            printf("SimpleSkeletonGrower::grow():converged at %d-th step.\n", i+1);
            break;
        }

		//compute the node-->id dict
		std::map <BDLSkeletonNode *, int> dict;
		int id = 0;
		
		//bfs on source
		std::queue <BDLSkeletonNode *> Queue;
		Queue.push(_root);

		while(!Queue.empty())
		{
			BDLSkeletonNode *front = Queue.front();
			Queue.pop();

			dict[front] = id;

			for(unsigned int j=0; j<front->_children.size(); j++)
				Queue.push(front->_children[j]);

			id++;
		}

		int tail_id = dict[tail];

		//####trial starts####
		int min_element_index = -1;
		int min_rotate = -1;
		float min_energy = -1.0f;

		for(unsigned int e=0; e<elements.size(); e++)
			for(int r=0; r<360; r+=30)
			{
				LibraryElement element_trial = elements[e];
				//copy the original(_root) tree and find the corresponding tail
				BDLSkeletonNode *copy_root = BDLSkeletonNode::copy_tree(_root);
				BDLSkeletonNode *copy_tail = NULL;

				//bfs on target, Queue is now empty at this point
				id = 0;
				Queue.push(copy_root);

				while(!Queue.empty())
				{
					BDLSkeletonNode *front = Queue.front();
					Queue.pop();

					if(id == tail_id)
						copy_tail = front;

					for(unsigned int j=0; j<front->_children.size(); j++)
						Queue.push(front->_children[j]);

					id++;
				}

				//branch replacement
				std::vector <BDLSkeletonNode *> subtree_trial = replace_branch(copy_tail, element_trial, r);
				prune(subtree_trial);

				//find energy
				float energy = compute_energy(copy_root);
				if(energy >= 0.0f && (min_energy == -1.0f || energy < min_energy))
				{
					min_element_index = e;
					min_rotate = r;
					min_energy = energy;
				}

				//delete the copied tree
				BDLSkeletonNode::delete_this(copy_root);
			}
		//####trial ends####

		//actual replacement
		if(min_energy != -1.0f)
		{
			std::vector <BDLSkeletonNode *> subtree = replace_branch(tail, elements[min_element_index], min_rotate);
			prune(subtree);
		}
    }
}
示例#14
0
void SimpleSkeletonGrower::prune(std::vector<BDLSkeletonNode *> roots)
{
    for(unsigned int i=0; i<roots.size(); i++)
        prune(roots[i]);
}
示例#15
0
void hcct_enter(UINT32 routine_id, UINT16 call_site) {

	++lss_enter_events;
	
    lss_hcct_node_t *parent = stack[stack_idx];
    lss_hcct_node_t *node;    

    // check if calling context is already in the tree
    for (node = parent->first_child; 
         node != NULL; 
         node = node->next_sibling)
        if (node->routine_id == routine_id &&
            node->call_site == call_site) break;
    
    // node was already in the tree
    if (node != NULL) {

        stack[++stack_idx] = node;

        if (IsMonitored(node)) {
            node->counter++;
            return;
        }
    } 

    // add new child to parent
    else {
        pool_alloc(node_pool, free_list, 
                   node, lss_hcct_node_t);
        node->routine_id    = routine_id;
        node->call_site     = call_site;
        node->first_child   = NULL;
        node->next_sibling  = parent->first_child;
        node->parent        = parent;
        parent->first_child = node;
        stack[++stack_idx]  = node;
    }

    // Mark node as monitored
    SetMonitored(node);

    // if table is not full
    if (!queue_full) {
        #if KEEP_EPS
        node->eps = 0;
        #endif
        node->counter = 1;
        queue[num_queue_items++] = node;
        queue_full = num_queue_items == epsilon;
        return;
    }

    #if INLINE_UPD_MIN == 1
 
    // version with swaps
    #if 0
    while (++min_idx < epsilon)
        if (queue[min_idx]->counter == min) goto end;

    UINT32 i;
    min = queue[second_min_idx]->counter;
    min_idx = epsilon-1;
    swap_and_decr(second_min_idx, min_idx);
    for (i = 0; i <= min_idx; ++i) {
        if (queue[i]->counter > min) continue;
        if (queue[i]->counter < min)  {
            second_min_idx = i;
            min_idx = epsilon-1;
            min = queue[i]->counter;
        }
        swap_and_decr(i, min_idx);
    }
    end: 
    #endif

    #if UPDATE_MIN_SENTINEL == 1

    // version with second_min + sentinel
    while (queue[++min_idx]->counter > min);

    if (min_idx < epsilon) goto end;

    UINT32 i;
    min = queue[second_min_idx]->counter;
    for (min_idx = 0, i = 0; i < epsilon; ++i) {
        if (queue[i]->counter < min) {
            second_min_idx = min_idx;
            min_idx = i;
            min = queue[i]->counter;
        }
    }
    queue[epsilon]->counter = min;
    end: 

    // version with second_min
    #else
    while (++min_idx < epsilon)
        if (queue[min_idx]->counter == min) goto end;

    UINT32 i;
    min = queue[second_min_idx]->counter;
    for (min_idx = 0, i = 0; i < epsilon; ++i) {
        if (queue[i]->counter < min) {
            second_min_idx = min_idx;
            min_idx = i;
            min = queue[i]->counter;
        }
    }
    end: 
    #endif
    
    #else
    // no explicit inlining: -O3 should do it anyway, though
    update_min();
    #endif

    #if KEEP_EPS
    node->eps=min;
    #endif

    node->counter = min + 1;

    UnsetMonitored(queue[min_idx]);

    if (queue[min_idx]->first_child==NULL)
        prune(queue[min_idx]);

    queue[min_idx]=node;
}
示例#16
0
void PageCache::setMaxSize(unsigned maxSize)
{
    m_maxSize = maxSize;
    prune(PruningReason::None);
}
示例#17
0
void c_elegans::update(){
    if(has_gone){
        has_gone=false;//prevent more than one update happening without call to rhs
        if(!first_round){
            if(next_comb(abl_neur, num_neur)){
                holder->write_dat();
                char ind_str[20];//won't ever have a 20 digit index
                //handy buffer to overflow for those hacking this.
                sprintf(ind_str, "%d", cur_ind);
                err(std::string("Combinations exhausted in index ") + ind_str,
                        "c_elegans::update","rhs/c_elegans.cpp", FATAL_ERROR);
            }
            auto dat_inds =
                std::shared_ptr<writer>(new writer(true));
            dat_inds->add_data(data::create("Ablations", abl_neur.data(), abl_neur.size()), writer::OTHER);
            holder->add_writer(dat_inds);

        }
        else{
            first_round=false;
        }

        Matrix<double, Dynamic, Dynamic> ag_dense(num_neur, num_neur);
        //insert code to zero it out later

        auto ag_m = ag_full;
        AEchem_trans = AEchem_trans_full;
        auto fncval = [this](int i, int j, double val)->bool{
            for(int kk = 0; kk < (int)this->abl_neur.size(); kk++){
                if((int)this->abl_neur[kk] == i || (int)this->abl_neur[kk] == j){
                    return false;
                }
            }
            return true;
        };
        AEchem_trans.prune(fncval);
        ag_m.prune(fncval);
        ag_dense = ag_m *  Matrix<double, num_neur, num_neur, ColMajor>::Identity();
        dmd(ag_dense);
        auto sumvals = ag_dense.colwise().sum();
        sparse_type temp(num_neur, num_neur);
        //generate the sparse diagonal matrix to build the lapacian
        std::vector<Triplet<double, int> > temp_tr;
        for(int i = 0; i < (int)num_neur; i++){
            temp_tr.push_back(Triplet<double, int>(i, i, sumvals[i]));
        }
        temp.setFromTriplets(temp_tr.begin(), temp_tr.end());
        laplacian = temp - ag_m;
        //initialize the Echem array
        for(size_t i = 0; i < num_neur; i++){
            if(GABAergic[i]){
                Echem[i] = EchemInh;
            }
            else{
                Echem[i] = EchemEx;
            }
        }
        //Initialize the sig array
        for(size_t i = 0; i < num_neur; i++){
            sig[i] = 0.5;
        }

        eqS = sig * (ar/(ar*sig + ad));
        //more initialization of temporary dense matrices
        Matrix<double, Dynamic, Dynamic> ldense(num_neur,num_neur);
        ldense = laplacian*Matrix<double, num_neur, num_neur>::Identity();
        Matrix<double, Dynamic, Dynamic> aedense(num_neur, num_neur);
        aedense= AEchem_trans*Matrix<double, num_neur, num_neur>::Identity();
        Matrix<double, Dynamic, Dynamic> C(num_neur, num_neur);
        //create the C matrix
        C= memG*Matrix<double, num_neur, num_neur>::Identity() + gelec*ldense;
        //initialize matrix to modify diagonal part of C
        Matrix<double, num_neur, 1> tmp =(gchem * aedense * eqS.matrix()); 
        for(size_t i = 0; i < num_neur; i++){
            C(i, i) += tmp(i);
        }
        Matrix<double, num_neur, 1> Ivals;
        Ivals.setZero();
        double amp=2e4;
        Ivals[276]=amp;
        Ivals[278]=amp;
        Matrix<double, num_neur, 1> b;
        //create B vector
        b= gchem*aedense*(eqS * Echem).matrix();
        for(size_t i = 0; i < num_neur; i++){
            b[i] += (memG*memV + Ivals[i]);
        }
        //calculate eqV
        eqV.matrix() = C.inverse()*b;
        vmean = eqV+(1.0/beta) * (1.0/sig - 1).log();
        for(auto val : abl_neur){
            eqV[val] = vmean [val] = eqS[val] = 0;
        };
    }
}
示例#18
0
int main(int argc, char **argv)
{
	clock_t begin, end;
	double time_spent;
	begin = clock();
	int NUM_FINDCODE_CALLS = 0;
	int NUM_HASHCELLS_VISITED = 0;
	int c = 0; //char in (pref, char)
	int prefix = EMPTY_CODE;
	int index = EMPTY_CODE;
	int MAXBITS = 15; //default
	char DUMP_TO[PATH_MAX];
	char INIT_FROM[PATH_MAX];
	int PRUNE = -1;
	Table *stringTable = initStringTable(MAXBITS);
//setting params from cmd line argv's STARTING FROM 2 BC ENCODE/DECODE
	for (int i = 2; i < argc; ++i)
	{
		if (strcmp(argv[i], "-m") == 0) {
			//ERROR CHECK FOR IF NEXT ARGV IS NOT INTEGER STRING??? 
			MAXBITS = atoi(argv[i + 1]);
			i++;
		} else if (strcmp(argv[i], "-o") == 0) {
			if(i + 1 == argc) {
				fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
				exit(EXIT_FAILURE);
				//EDGE CASE: IF -O -O HI, THEN DO WE DUMP TO '-O' AND SIGNAL ERROR ON HI? OR DO WE DUMP TO -O, AND OVERRIDE WITH DUMPING TO 'HI'? 
			}
			strncpy(DUMP_TO, argv[i + 1], strlen(argv[i + 1]));
			i++;
		} else if (strcmp(argv[i], "-i") == 0) {
			if(i + 1 == argc) {
				fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
				exit(EXIT_FAILURE);
			}
			strncpy(INIT_FROM, argv[i + 1], strlen(argv[i + 1]));
			i++;
		} else if (strcmp(argv[i], "-p") == 0) {
			if(i + 1 == argc) {
				fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
				exit(EXIT_FAILURE);
			}
			PRUNE = atoi(argv[i + 1]); //test if not an integer; what if prune is 0? w
			i++;
			if(PRUNE){}
		} else {
			fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n");
			exit(EXIT_FAILURE);
		}
	}
	fprintf(stderr, "argv[0]: %s\n", argv[0]);
	if(strcmp(argv[1], "encode") == 0) {
//ENCODE	
		while((c = getchar()) != EOF) {
			index = findCode(prefix, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED);
			if (index != -1) //(pref, char) found in stringTable
			{
				prefix = index;
				(stringTable->table[prefix]->useCount)++;
				continue;
			} else { //not found in stringTable
				putBits(stringTable->nBits, prefix);
				insertToTable(prefix, c, &stringTable, PRUNE);
				prefix = findCode(0, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED); //start search again at finalChar of newly found sequence
				(stringTable->table[prefix]->useCount)++;
			}
		}
		putBits(stringTable->nBits, prefix);
		flushBits();
		// printTable(stringTable, "encode");
		// fprintf(stderr, "\nNUM_FINDCODE_CALLS: %d\nNUM_HASHCELLS_VISITED: %d\nAVG_SEARCH_RATIO: %d\n", NUM_FINDCODE_CALLS, NUM_HASHCELLS_VISITED, NUM_HASHCELLS_VISITED / NUM_FINDCODE_CALLS);
	} else if(strcmp(argv[1], "decode") == 0) {
//DECODE
		char ENCODE_OR_DECODE[25] = "decode";
		int oldIndex = 0, firstCharOfNewIndex = 0; //used to build stringTable, 1 step behind encode
		int newIndex = 0;
		int cascadingIndex = 0; //used to print a code string recursively
		int kwkwk = 0; //used in kwkwk case
		int prunedThisPass = 0;
		int HIT_THE_LIMIT = 0;

		while( (newIndex = cascadingIndex = getBits(stringTable->nBits)) != EOF) {
				//nBits was incremented just before to accomodate increased nBits in ENCODE that hasn't been automatically detected in DECODE
				//and we decrement it now bc it will be automatically incremented in INSERTTABLE
				//but if == MAXBITS, then we did not artificially increment nBits last round 

			if(stringTable->last + 1 == stringTable->size && !HIT_THE_LIMIT ) {//2nd condition happens when table is already maxSize, in which case no more doubling or pruning happens
				if(!prunedThisPass) //when pruning results in an at-limit table
					stringTable->nBits--;
				if(stringTable->size != 1<<stringTable->nBits){
					fprintf(stderr, "size: %d, nBits: %d\n", stringTable->size, stringTable->nBits);
					exit(EXIT_FAILURE);
				} else {
				}
			}
			if(newIndex > stringTable->last) //newIndex is an unknown code (kwkwk abnormal case)
			{
				kwkwk = 1;
				if(newIndex < stringTable->size)
					stringTable->table[newIndex]->useCount++; //useCount usually incremented in printRecursive function, which will not receive newIndex in this case
				cascadingIndex = oldIndex;
			}
			printRecursive(&cascadingIndex, stringTable);
			if (kwkwk == 1) {
				printf("%c", firstCharOfNewIndex); //output should be oldCode, oldCode, firstCharOfOldCode
				kwkwk = 0;
			}
			if(cascadingIndex >= stringTable->size) {
				fprintf(stderr, "cascadingIndex: %d, size: %d\n", cascadingIndex, stringTable->size);
				exit(EXIT_FAILURE);
			}
			firstCharOfNewIndex = stringTable->table[cascadingIndex]->c; //finalK = char(c)
			if(oldIndex != 0 && !prunedThisPass) { //every time except first time, and after pruning, add new code to table
				insertToTable(oldIndex, firstCharOfNewIndex, &stringTable, PRUNE);
			}
			prunedThisPass = 0;
			oldIndex = newIndex;
			if(stringTable->last + 1 >= stringTable->size) { 
			//decode is one step behind encode; deocde inserts new code every time it reads a code, starting from 2nd code
			//encode adds a code at every code starting from 1st code. CURRENT CODE + firstChar of NEXT CODE was the last code
			//encode tried to add to table and PRUNED or DOUBLE'd
			//=>PRUNING or DOUBLING::
			//if DOUBLING: next code will increment nBits
			//if PRUNING: next code is encoded from a PRUNED table, not CURRENT table (and we don't insert next round)
				if(stringTable->nBits != stringTable->MAXBITS) {
					if(stringTable->nBits == 8){
						exit(EXIT_FAILURE);
					}
					stringTable->nBits++;
				} else { //next insert exceeds table size, and table size is MAXBITS size, so PRUNE
					HIT_THE_LIMIT = 1;
					if(PRUNE != -1) {
						prune(&stringTable, PRUNE, ENCODE_OR_DECODE);
						prunedThisPass = 1;
						if (stringTable->last + 1 == stringTable->size)
						{
							fprintf(stderr, "HERE!!! last: %d, size: %d\n", stringTable->last, stringTable->size);
							char temp[10] = "encode";
							printTable(stringTable, temp);
						}
						if(stringTable->last + 1 != 1<<stringTable->MAXBITS)
							HIT_THE_LIMIT = 0;
					}
				}
			}
		}
		// printTable(stringTable, "decode");
	}
	moses(stringTable);
	end = clock();
	time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
	fprintf(stderr, "time_spent: %f\n", time_spent);
}
示例#19
0
size_t TRILINOS_KLU_kernel   /* final size of LU on output */
(
    /* input, not modified */
    Int n,	    /* A is n-by-n */
    Int Ap [ ],	    /* size n+1, column pointers for A */
    Int Ai [ ],	    /* size nz = Ap [n], row indices for A */
    Entry Ax [ ],   /* size nz, values of A */
    Int Q [ ],	    /* size n, optional input permutation */
    size_t lusize,  /* initial size of LU on input */

    /* output, not defined on input */
    Int Pinv [ ],   /* size n, inverse row permutation, where Pinv [i] = k if
		     * row i is the kth pivot row */
    Int P [ ],	    /* size n, row permutation, where P [k] = i if row i is the
		     * kth pivot row. */
    Unit **p_LU,	/* LU array, size lusize on input */
    Entry Udiag [ ],	/* size n, diagonal of U */
    Int Llen [ ],       /* size n, column length of L */
    Int Ulen [ ],	/* size n, column length of U */
    Int Lip [ ],	/* size n, column pointers for L */
    Int Uip [ ],	/* size n, column pointers for U */
    Int *lnz,		/* size of L*/
    Int *unz,		/* size of U*/
    /* workspace, not defined on input */
    Entry X [ ],    /* size n, undefined on input, zero on output */

    /* workspace, not defined on input or output */
    Int Stack [ ],  /* size n */
    Int Flag [ ],   /* size n */
    Int Ap_pos [ ],	/* size n */

    /* other workspace: */
    Int Lpend [ ],		    /* size n workspace, for pruning only */

    /* inputs, not modified on output */
    Int k1,	    	/* the block of A is from k1 to k2-1 */
    Int PSinv [ ],  	/* inverse of P from symbolic factorization */
    double Rs [ ],  	/* scale factors for A */

    /* inputs, modified on output */
    Int Offp [ ],   /* off-diagonal matrix (modified by this routine) */
    Int Offi [ ],
    Entry Offx [ ],
    /* --------------- */
    TRILINOS_KLU_common *Common
)
{
    Entry pivot ;
    double abs_pivot, xsize, nunits, tol, memgrow ;
    Entry *Ux ;
    Int *Li, *Ui ;
    Unit *LU ;		/* LU factors (pattern and values) */
    Int k, p, i, j, pivrow, kbar, diagrow, firstrow, lup, top, scale, len ;
    size_t newlusize ;

#ifndef NDEBUG
    Entry *Lx ;
#endif

    ASSERT (Common != NULL) ;
    scale = Common->scale ;
    tol = Common->tol ;
    memgrow = Common->memgrow ;
    *lnz = 0 ;
    *unz = 0 ;

    /* ---------------------------------------------------------------------- */
    /* get initial Li, Lx, Ui, and Ux */
    /* ---------------------------------------------------------------------- */

    PRINTF (("input: lusize %d \n", lusize)) ;
    ASSERT (lusize > 0) ;
    LU = *p_LU ;

    /* ---------------------------------------------------------------------- */
    /* initializations */
    /* ---------------------------------------------------------------------- */

    firstrow = 0 ;
    lup = 0 ;

    for (k = 0 ; k < n ; k++)
    {
	/* X [k] = 0 ; */
	CLEAR (X [k]) ;
	Flag [k] = EMPTY ;
	Lpend [k] = EMPTY ;	/* flag k as not pruned */
    }

    /* ---------------------------------------------------------------------- */
    /* mark all rows as non-pivotal and determine initial diagonal mapping */
    /* ---------------------------------------------------------------------- */

    /* PSinv does the symmetric permutation, so don't do it here */
    for (k = 0 ; k < n ; k++)
    {
	P [k] = k ;
	Pinv [k] = FLIP (k) ;	/* mark all rows as non-pivotal */
    }
    /* initialize the construction of the off-diagonal matrix */
    Offp [0] = 0 ;

    /* P [k] = row means that UNFLIP (Pinv [row]) = k, and visa versa.
     * If row is pivotal, then Pinv [row] >= 0.  A row is initially "flipped"
     * (Pinv [k] < EMPTY), and then marked "unflipped" when it becomes
     * pivotal. */

#ifndef NDEBUG
    for (k = 0 ; k < n ; k++)
    {
	PRINTF (("Initial P [%d] = %d\n", k, P [k])) ;
    }
#endif

    /* ---------------------------------------------------------------------- */
    /* factorize */
    /* ---------------------------------------------------------------------- */

    for (k = 0 ; k < n ; k++)
    {

	PRINTF (("\n\n==================================== k: %d\n", k)) ;

	/* ------------------------------------------------------------------ */
	/* determine if LU factors have grown too big */
	/* ------------------------------------------------------------------ */

	/* (n - k) entries for L and k entries for U */
	nunits = DUNITS (Int, n - k) + DUNITS (Int, k) +
		 DUNITS (Entry, n - k) + DUNITS (Entry, k) ;

        /* LU can grow by at most 'nunits' entries if the column is dense */
        PRINTF (("lup %d lusize %g lup+nunits: %g\n", lup, (double) lusize,
	    lup+nunits));
	xsize = ((double) lup) + nunits ;
	if (xsize > (double) lusize)
        {
            /* check here how much to grow */
	    xsize = (memgrow * ((double) lusize) + 4*n + 1) ;
            if (INT_OVERFLOW (xsize))
            {
                PRINTF (("Matrix is too large (Int overflow)\n")) ;
		Common->status = TRILINOS_KLU_TOO_LARGE ;
                return (lusize) ;
            }
            newlusize = memgrow * lusize + 2*n + 1 ;
	    /* Future work: retry mechanism in case of malloc failure */
	    LU = (Unit*) TRILINOS_KLU_realloc (newlusize, lusize, sizeof (Unit), LU, Common) ;
	    Common->nrealloc++ ;
            *p_LU = LU ;
            if (Common->status == TRILINOS_KLU_OUT_OF_MEMORY)
            {
                PRINTF (("Matrix is too large (LU)\n")) ;
                return (lusize) ;
            }
	    lusize = newlusize ;
            PRINTF (("inc LU to %d done\n", lusize)) ;
        }

	/* ------------------------------------------------------------------ */
	/* start the kth column of L and U */
	/* ------------------------------------------------------------------ */

	Lip [k] = lup ;

	/* ------------------------------------------------------------------ */
	/* compute the nonzero pattern of the kth column of L and U */
	/* ------------------------------------------------------------------ */

#ifndef NDEBUG
	for (i = 0 ; i < n ; i++)
	{
	    ASSERT (Flag [i] < k) ;
	    /* ASSERT (X [i] == 0) ; */
	    ASSERT (IS_ZERO (X [i])) ;
	}
#endif

	top = lsolve_symbolic (n, k, Ap, Ai, Q, Pinv, Stack, Flag,
		    Lpend, Ap_pos, LU, lup, Llen, Lip, k1, PSinv) ;

#ifndef NDEBUG
	PRINTF (("--- in U:\n")) ;
	for (p = top ; p < n ; p++)
	{
	    PRINTF (("pattern of X for U: %d : %d pivot row: %d\n",
		p, Stack [p], Pinv [Stack [p]])) ;
	    ASSERT (Flag [Stack [p]] == k) ;
	}
	PRINTF (("--- in L:\n")) ;
	Li = (Int *) (LU + Lip [k]);
	for (p = 0 ; p < Llen [k] ; p++)
	{
	    PRINTF (("pattern of X in L: %d : %d pivot row: %d\n",
		p, Li [p], Pinv [Li [p]])) ;
	    ASSERT (Flag [Li [p]] == k) ;
	}
	p = 0 ;
	for (i = 0 ; i < n ; i++)
	{
	    ASSERT (Flag [i] <= k) ;
	    if (Flag [i] == k) p++ ;
	}
#endif

	/* ------------------------------------------------------------------ */
	/* get the column of the matrix to factorize and scatter into X */
	/* ------------------------------------------------------------------ */

	construct_column (k, Ap, Ai, Ax, Q, X,
	    k1, PSinv, Rs, scale, Offp, Offi, Offx) ;

	/* ------------------------------------------------------------------ */
	/* compute the numerical values of the kth column (s = L \ A (:,k)) */
	/* ------------------------------------------------------------------ */

	lsolve_numeric (Pinv, LU, Stack, Lip, top, n, Llen, X) ;

#ifndef NDEBUG
	for (p = top ; p < n ; p++)
	{
	    PRINTF (("X for U %d : ",  Stack [p])) ;
	    PRINT_ENTRY (X [Stack [p]]) ;
	}
	Li = (Int *) (LU + Lip [k]) ;
	for (p = 0 ; p < Llen [k] ; p++)
	{
	    PRINTF (("X for L %d : ", Li [p])) ;
	    PRINT_ENTRY (X [Li [p]]) ;
	}
#endif

	/* ------------------------------------------------------------------ */
	/* partial pivoting with diagonal preference */
	/* ------------------------------------------------------------------ */

	/* determine what the "diagonal" is */
	diagrow = P [k] ;   /* might already be pivotal */
	PRINTF (("k %d, diagrow = %d, UNFLIP (diagrow) = %d\n",
	    k, diagrow, UNFLIP (diagrow))) ;

	/* find a pivot and scale the pivot column */
	if (!lpivot (diagrow, &pivrow, &pivot, &abs_pivot, tol, X, LU, Lip,
		    Llen, k, n, Pinv, &firstrow, Common))
	{
	    /* matrix is structurally or numerically singular */
	    Common->status = TRILINOS_KLU_SINGULAR ;
	    if (Common->numerical_rank == EMPTY)
	    {
		Common->numerical_rank = k+k1 ;
		Common->singular_col = Q [k+k1] ;
	    }
	    if (Common->halt_if_singular)
	    {
		/* do not continue the factorization */
		return (lusize) ;
	    }
	}

	/* we now have a valid pivot row, even if the column has NaN's or
	 * has no entries on or below the diagonal at all. */
	PRINTF (("\nk %d : Pivot row %d : ", k, pivrow)) ;
	PRINT_ENTRY (pivot) ;
	ASSERT (pivrow >= 0 && pivrow < n) ;
	ASSERT (Pinv [pivrow] < 0) ;

	/* set the Uip pointer */
	Uip [k] = Lip [k] + UNITS (Int, Llen [k]) + UNITS (Entry, Llen [k]) ;

        /* move the lup pointer to the position where indices of U
         * should be stored */
        lup += UNITS (Int, Llen [k]) + UNITS (Entry, Llen [k]) ;

        Ulen [k] = n - top ;

        /* extract Stack [top..n-1] to Ui and the values to Ux and clear X */
	GET_POINTER (LU, Uip, Ulen, Ui, Ux, k, len) ;
        for (p = top, i = 0 ; p < n ; p++, i++)
        {
	    j = Stack [p] ;
	    Ui [i] = Pinv [j] ;
	    Ux [i] = X [j] ;
	    CLEAR (X [j]) ;
        }

        /* position the lu index at the starting point for next column */
        lup += UNITS (Int, Ulen [k]) + UNITS (Entry, Ulen [k]) ;

	/* U(k,k) = pivot */
	Udiag [k] = pivot ;

	/* ------------------------------------------------------------------ */
	/* log the pivot permutation */
	/* ------------------------------------------------------------------ */

	ASSERT (UNFLIP (Pinv [diagrow]) < n) ;
	ASSERT (P [UNFLIP (Pinv [diagrow])] == diagrow) ;

	if (pivrow != diagrow)
	{
	    /* an off-diagonal pivot has been chosen */
	    Common->noffdiag++ ;
	    PRINTF ((">>>>>>>>>>>>>>>>> pivrow %d k %d off-diagonal\n",
			pivrow, k)) ;
	    if (Pinv [diagrow] < 0)
	    {
		/* the former diagonal row index, diagrow, has not yet been
		 * chosen as a pivot row.  Log this diagrow as the "diagonal"
		 * entry in the column kbar for which the chosen pivot row,
		 * pivrow, was originally logged as the "diagonal" */
		kbar = FLIP (Pinv [pivrow]) ;
		P [kbar] = diagrow ;
		Pinv [diagrow] = FLIP (kbar) ;
	    }
	}
	P [k] = pivrow ;
	Pinv [pivrow] = k ;

#ifndef NDEBUG
	for (i = 0 ; i < n ; i++) { ASSERT (IS_ZERO (X [i])) ;}
	GET_POINTER (LU, Uip, Ulen, Ui, Ux, k, len) ;
	for (p = 0 ; p < len ; p++)
	{
	    PRINTF (("Column %d of U: %d : ", k, Ui [p])) ;
	    PRINT_ENTRY (Ux [p]) ;
	}
	GET_POINTER (LU, Lip, Llen, Li, Lx, k, len) ;
	for (p = 0 ; p < len ; p++)
	{
	    PRINTF (("Column %d of L: %d : ", k, Li [p])) ;
	    PRINT_ENTRY (Lx [p]) ;
	}
#endif

	/* ------------------------------------------------------------------ */
	/* symmetric pruning */
	/* ------------------------------------------------------------------ */

	prune (Lpend, Pinv, k, pivrow, LU, Uip, Lip, Ulen, Llen) ;

	*lnz += Llen [k] + 1 ; /* 1 added to lnz for diagonal */
	*unz += Ulen [k] + 1 ; /* 1 added to unz for diagonal */
    }

    /* ---------------------------------------------------------------------- */
    /* finalize column pointers for L and U, and put L in the pivotal order */
    /* ---------------------------------------------------------------------- */

    for (p = 0 ; p < n ; p++)
    {
	Li = (Int *) (LU + Lip [p]) ;
	for (i = 0 ; i < Llen [p] ; i++)
	{
	    Li [i] = Pinv [Li [i]] ;
	}
    }

#ifndef NDEBUG
    for (i = 0 ; i < n ; i++)
    {
	PRINTF (("P [%d] = %d   Pinv [%d] = %d\n", i, P [i], i, Pinv [i])) ;
    }
    for (i = 0 ; i < n ; i++)
    {
	ASSERT (Pinv [i] >= 0 && Pinv [i] < n) ;
	ASSERT (P [i] >= 0 && P [i] < n) ;
	ASSERT (P [Pinv [i]] == i) ;
	ASSERT (IS_ZERO (X [i])) ;
    }
#endif

    /* ---------------------------------------------------------------------- */
    /* shrink the LU factors to just the required size */
    /* ---------------------------------------------------------------------- */

    newlusize = lup ;
    ASSERT ((size_t) newlusize <= lusize) ;

    /* this cannot fail, since the block is descreasing in size */
    LU = (Unit*) TRILINOS_KLU_realloc (newlusize, lusize, sizeof (Unit), LU, Common) ;
    *p_LU = LU ;
    return (newlusize) ;
}
示例#20
0
文件: rmaps_ppr.c 项目: bgoglin/ompi
/* recursively climb the topology, pruning procs beyond that allowed
 * by the given ppr
 */
static void prune(orte_jobid_t jobid,
                  orte_app_idx_t app_idx,
                  orte_node_t *node,
                  opal_hwloc_level_t *level,
                  orte_vpid_t *nmapped)
{
    hwloc_obj_t obj, top;
    unsigned int i, nobjs;
    hwloc_obj_type_t lvl;
    unsigned cache_level = 0, k;
    int nprocs;
    hwloc_cpuset_t avail;
    int n, limit, nmax, nunder, idx, idxmax = 0;
    orte_proc_t *proc, *pptr, *procmax;
    opal_hwloc_level_t ll;
    char dang[64];
    hwloc_obj_t locale;

    opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                        "mca:rmaps:ppr: pruning level %d",
                        *level);

    /* convenience */
    ll = *level;

    /* convenience */
    lvl = opal_hwloc_levels[ll];
    limit = ppr[ll];

    if (0 == limit) {
        /* no limit at this level, so move up if necessary */
        if (0 == ll) {
            /* done */
            return;
        }
        --(*level);
        prune(jobid, app_idx, node, level, nmapped);
        return;
    }

    /* handle the darn cache thing again */
    if (OPAL_HWLOC_L3CACHE_LEVEL == ll) {
        cache_level = 3;
    } else if (OPAL_HWLOC_L2CACHE_LEVEL == ll) {
        cache_level = 2;
    } else if (OPAL_HWLOC_L1CACHE_LEVEL == ll) {
        cache_level = 1;
    }

    /* get the number of resources at this level on this node */
    nobjs = opal_hwloc_base_get_nbobjs_by_type(node->topology->topo,
                                               lvl, cache_level,
                                               OPAL_HWLOC_AVAILABLE);

    /* for each resource, compute the number of procs sitting
     * underneath it and check against the limit
     */
    for (i=0; i < nobjs; i++) {
        obj = opal_hwloc_base_get_obj_by_type(node->topology->topo,
                                              lvl, cache_level,
                                              i, OPAL_HWLOC_AVAILABLE);
        /* get the available cpuset */
        avail = obj->cpuset;

        /* look at the intersection of this object's cpuset and that
         * of each proc in the job/app - if they intersect, then count this proc
         * against the limit
         */
        nprocs = 0;
        for (n=0; n < node->procs->size; n++) {
            if (NULL == (proc = (orte_proc_t*)opal_pointer_array_get_item(node->procs, n))) {
                continue;
            }
            if (proc->name.jobid != jobid ||
                proc->app_idx != app_idx) {
                continue;
            }
            locale = NULL;
            if (orte_get_attribute(&proc->attributes, ORTE_PROC_HWLOC_LOCALE, (void**)&locale, OPAL_PTR)) {
                ORTE_ERROR_LOG(ORTE_ERR_NOT_FOUND);
                return;
            }
            if (hwloc_bitmap_intersects(avail, locale->cpuset)) {
                nprocs++;
            }
        }
        opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                            "mca:rmaps:ppr: found %d procs limit %d",
                            nprocs, limit);

        /* check against the limit */
        while (limit < nprocs) {
            /* need to remove procs - do this in a semi-intelligent
             * manner to provide a little load balancing by cycling
             * across the objects beneath this one, removing procs
             * in a round-robin fashion until the limit is satisfied
             *
             * NOTE: I'm sure someone more knowledgeable with hwloc
             * will come up with a more efficient way to do this, so
             * consider this is a starting point
             */

            /* find the first level that has more than
             * one child beneath it - if all levels
             * have only one child, then return this
             * object
             */
            top = find_split(node->topology->topo, obj);
            hwloc_obj_type_snprintf(dang, 64, top, 1);
            opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                                "mca:rmaps:ppr: SPLIT AT LEVEL %s", dang);

            /* cycle across the children of this object */
            nmax = 0;
            procmax = NULL;
            idx = 0;
            /* find the child with the most procs underneath it */
            for (k=0; k < top->arity && limit < nprocs; k++) {
                /* get this object's available cpuset */
                nunder = 0;
                pptr = NULL;
                for (n=0; n < node->procs->size; n++) {
                    if (NULL == (proc = (orte_proc_t*)opal_pointer_array_get_item(node->procs, n))) {
                        continue;
                    }
                    if (proc->name.jobid != jobid ||
                        proc->app_idx != app_idx) {
                        continue;
                    }
                    locale = NULL;
                    if (orte_get_attribute(&proc->attributes, ORTE_PROC_HWLOC_LOCALE, (void**)&locale, OPAL_PTR)) {
                        ORTE_ERROR_LOG(ORTE_ERR_NOT_FOUND);
                        return;
                    }
                    if (hwloc_bitmap_intersects(top->children[k]->cpuset, locale->cpuset)) {
                        nunder++;
                        if (NULL == pptr) {
                            /* save the location of the first proc under this object */
                            pptr = proc;
                            idx = n;
                        }
                    }
                }
                if (nmax < nunder) {
                    opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                                        "mca:rmaps:ppr: PROCS UNDER CHILD %d %d MAX %d",
                                        k, nunder, nmax);
                    nmax = nunder;
                    procmax = pptr;
                    idxmax = idx;
                }
            }
            if (NULL == procmax) {
                /* can't find anything to remove - error out */
                goto error;
            }
            /* remove it */
            opal_output_verbose(5, orte_rmaps_base_framework.framework_output,
                                "mca:rmaps:ppr: removing proc at posn %d",
                                idxmax);
            opal_pointer_array_set_item(node->procs, idxmax, NULL);
            node->num_procs--;
            node->slots_inuse--;
            if (node->slots_inuse < 0) {
                node->slots_inuse = 0;
            }
            nprocs--;
            *nmapped -= 1;
            OBJ_RELEASE(procmax);
        }
    }
    /* finished with this level - move up if necessary */
    if (0 == ll) {
        return;
    }
    --(*level);
    prune(jobid, app_idx, node, level, nmapped);
    return;

 error:
    opal_output(0, "INFINITE LOOP");
}
示例#21
0
/*
 * routine:
 *	main
 *
 * purpose:
 *	argument processing and primary dispatch
 *
 * returns:
 *	error codes per filesync.1 (ERR_* in filesync.h)
 *
 * notes:
 *	read filesync.1 in order to understand the argument processing
 *
 *	most of the command line options just set some opt_ global
 *	variable that is later looked at by the code that actually
 *	implements the features.  Only file names are really processed
 *	in this routine.
 */
int
main(int argc, char **argv)
{	int i;
	int c;
	errmask_t errs = ERR_OK;
	int do_prune = 0;
	char *srcname = 0;
	char *dstname = 0;
	struct base *bp;

	/* keep the error messages simple	*/
	argv[0] = "filesync";

	/* gather together all of the options	*/
	while ((c = getopt(argc, argv, "AaehmnqvyD:E:r:s:d:f:o:")) != EOF)
		switch (c) {
			case 'a':	/* always scan for acls	*/
				opt_acls = TRUE;
				break;
			case 'e':	/* everything agrees	*/
				opt_everything = TRUE;
				break;
			case 'h':	/* halt on error	*/
				opt_halt = TRUE;
				break;
			case 'm':	/* preserve modtimes	*/
				opt_mtime = TRUE;
				break;
			case 'n':	/* notouch		*/
				opt_notouch = TRUE;
				break;
			case 'q':	/* quiet		*/
				opt_quiet = TRUE;
				break;
			case 'v':	/* verbose		*/
				opt_verbose = TRUE;
				break;
			case 'y':	/* yes			*/
				opt_yes = TRUE;
				break;
			case 'D':	/* debug options	*/
				if (!isdigit(optarg[0])) {
					dbg_usage();
					exit(ERR_INVAL);
				}
				opt_debug |= strtol(optarg, (char **)NULL, 0);
				break;

			case 'E':	/* error simulation	*/
				if (dbg_set_error(optarg)) {
					err_usage();
					exit(ERR_INVAL);
				}
				opt_errors = TRUE;
				break;

			case 'f':	/* force conflict resolution	*/
				switch (optarg[0]) {
					case 's':
						opt_force = OPT_SRC;
						break;
					case 'd':
						opt_force = OPT_DST;
						break;
					case 'o':
						opt_force = OPT_OLD;
						break;
					case 'n':
						opt_force = OPT_NEW;
						break;
					default:
						fprintf(stderr,
							gettext(ERR_badopt),
							c, optarg);
						errs |= ERR_INVAL;
						break;
				}
				break;

			case 'o':	/* one way propagation		*/
				switch (optarg[0]) {
					case 's':
						opt_oneway = OPT_SRC;
						break;
					case 'd':
						opt_oneway = OPT_DST;
						break;
					default:
						fprintf(stderr,
							gettext(ERR_badopt),
							c, optarg);
						errs |= ERR_INVAL;
						break;
				}
				break;

			case 'r':	/* restricted reconciliation	*/
				if (num_restrs < MAX_RLIST)
					rlist[ num_restrs++ ] = optarg;
				else {
					fprintf(stderr, gettext(ERR_tomany),
						MAX_RLIST);
					errs |= ERR_INVAL;
				}
				break;

			case 's':
				if ((srcname = qualify(optarg)) == 0)
					errs |= ERR_MISSING;
				break;

			case 'd':
				if ((dstname = qualify(optarg)) == 0)
					errs |= ERR_MISSING;
				break;

			default:
			case '?':
				errs |= ERR_INVAL;
				break;
		}

	if (opt_debug & DBG_MISC)
		fprintf(stderr, "MISC: DBG=%s\n", showflags(dbgmap, opt_debug));

	/* if we have file names, we need a source and destination */
	if (optind < argc) {
		if (srcname == 0) {
			fprintf(stderr, gettext(ERR_nosrc));
			errs |= ERR_INVAL;
		}
		if (dstname == 0) {
			fprintf(stderr, gettext(ERR_nodst));
			errs |= ERR_INVAL;
		}
	}

	/* check for simple usage errors	*/
	if (errs & ERR_INVAL) {
		usage();
		exit(errs);
	}

	/* locate our baseline and rules files	*/
	if (c = findfiles())
		exit(c);

	/* figure out file creation defaults	*/
	whoami();

	/* read in our initial baseline		*/
	if (!new_baseline && (c = read_baseline(file_base)))
		errs |= c;

	/* read in the rules file if we need or have rules	*/
	if (optind >= argc && new_rules) {
		fprintf(stderr, ERR_nonames);
		errs |= ERR_INVAL;
	} else if (!new_rules)
		errs |= read_rules(file_rules);

	/* if anything has failed with our setup, go no further	*/
	if (errs) {
		cleanup(errs);
		exit(errs);
	}

	/*
	 * figure out whether or not we are willing to do a one-sided
	 * analysis (where we don't even look at the other side.  This
	 * is an "I'm just curious what has changed" query, and we are
	 * only willing to do it if:
	 *	we aren't actually going to do anything
	 *	we have a baseline we can compare against
	 * otherwise, we are going to insist on being able to access
	 * both the source and destination.
	 */
	if (opt_notouch && !new_baseline)
		opt_onesided = opt_oneway;

	/*
	 * there are two interested usage scenarios:
	 *	file names specified
	 *		create new rules for the specified files
	 *		evaulate and reconcile only the specified files
	 *	no file names specified
	 *		use already existing rules
	 *		consider restricting them to specified subdirs/files
	 */
	if (optind < argc) {
		/* figure out what base pair we're working on	*/
		bp = add_base(srcname, dstname);

		/* perverse default rules to avoid trouble	*/
		if (new_rules) {
			errs |= add_ignore(0, SUFX_RULES);
			errs |= add_ignore(0, SUFX_BASE);
		}

		/* create include rules for each file/dir arg	*/
		while (optind < argc)
			errs |= add_include(bp, argv[ optind++ ]);

		/*
		 * evaluate the specified base on each side,
		 * being careful to limit evaulation to new rules
		 */
		errs |= evaluate(bp, OPT_SRC, TRUE);
		errs |= evaluate(bp, OPT_DST, TRUE);
	} else {
		/* note any possible evaluation restrictions	*/
		for (i = 0; i < num_restrs; i++)
			errs |= add_restr(rlist[i]);

		/*
		 * we can only prune the baseline file if we have done
		 * a complete (unrestricted) analysis.
		 */
		if (i == 0)
			do_prune = 1;

		/* evaulate each base on each side		*/
		for (bp = bases; bp; bp = bp->b_next) {
			errs |= evaluate(bp, OPT_SRC, FALSE);
			errs |= evaluate(bp, OPT_DST, FALSE);
		}
	}

	/* if anything serious happened, skip reconciliation	*/
	if (errs & ERR_FATAL) {
		cleanup(errs);
		exit(errs);
	}

	/* analyze and deal with the differenecs		*/
	errs |= analyze();

	/* see if there is any dead-wood in the baseline	*/
	if (do_prune) {
		c = prune();

		if (c > 0 && opt_verbose)
			fprintf(stdout, V_prunes, c);
	}

	/* print out a final summary				*/
	summary();

	/* update the rules and baseline files (if needed)	*/
	(void) umask(my_umask);
	errs |= write_baseline(file_base);
	errs |= write_rules(file_rules);

	if (opt_debug & DBG_MISC)
		fprintf(stderr, "MISC: EXIT=%s\n", showflags(errmap, errs));

	/* just returning ERR_RESOLVABLE upsets some people	*/
	if (errs == ERR_RESOLVABLE && !opt_notouch)
		errs = 0;

	/* all done	*/
	cleanup(0);
	return (errs);
}
示例#22
0
void SimpleSpeechRec::frameSegmentation() {
	time++;
	if (time == 0) {
		timeZeroInit();
		return;
	}

	SRecToken** newTokenBuffer = new SRecToken*[cbNum];
	for (int i = 0; i < cbNum; i++) {
		newTokenBuffer[i] = NULL;
	}

	int* cbTypeLookup = new int[cbNum];
	for (int i = 0; i < cbNum; i++) {
		cbTypeLookup[i] = dict->getCbType(i);
	}

	omp_set_dynamic(true);
#pragma omp parallel for 
	for (int i = 0; i < cbNum; i++) {
		STokenBin* bin = binSet[i];
		SRecToken* candToken = bin->getPreviousBest();
		if (!candToken)
			continue;
		int cbType = (i == cbNum-1) ? DI_TAIL_NOISE: cbTypeLookup[i];
		bool isCrossWord = isCrossWordCb(cbType);



		SRecToken* candWord = NULL;
		SRecToken* newToken = factory.getInstance();
		//newToken->copyFrom(candToken);
		if (isCrossWord) {
			candWord = factory.getInstance();
			candWord->copyFrom(candToken);
			if (candToken->prev)
				InterlockedIncrement(&candToken->prev->refcnt);
			candWord->endTime = time;
		} else {
			candWord = candToken->prev;
			newToken->CId = candToken->CId;
			newToken->VId = candToken->VId;
			newToken->wordId = candToken->wordId;
			if (dict->triPhone) {	
				if (cbType == INITIAL1) {   
					newToken->CId = dict->getCVIdFromCbId(i);
				} else if (cbType == FINAL0) {
					newToken->VId = dict->getCVIdFromCbId(i);
					newToken->wordId = dict->getWordIdFromCVLink(candToken->currentCbId, i);
				}
			}
			else{
				if (cbType == DI_INITIAL1) {
					newToken->CId = dict->getCVIdFromCbId(i);
				} else if (cbType == DI_FINAL0) {
					newToken->VId = dict->getCVIdFromCbId(i);
					newToken->wordId = dict->getWordIdFromCVLink(candToken->currentCbId, i);
				}
			}


		}
		//
		newToken->currentCbId = i;
		newToken->dur = 1;



		double durLh = useSegmentModel ? bc->getDurLh(i, 1) : 0;
		double stateLh = bc->getStateLh(i, time);
		newToken->lh = candToken->lh + durLh + stateLh;
		newToken->prev = candWord;
		InterlockedIncrement(&candWord->refcnt);

		newTokenBuffer[i] = newToken;
	}

	//状态驻留
	for (int i = 0; i < cbNum; i++) {
		STokenBin* bin = binSet.at(i);

		int k =i;
		if(m_bHeadNOise&&i == cbNum-1)
			k=  dict->noiseId;

		double stateLh = bc->getStateLh(k, time);

		for (auto j = bin->content.begin(); j != bin->content.end(); j++) {
			SRecToken* t = *j;

			t->dur += 1;
			int dur = t->dur;
			double deltaDurLh = useSegmentModel ? bc->getDurLhDelta(k, dur) : 0;

			t->lh += deltaDurLh + stateLh;
		}
	}

	//完成状态跳转
	for (int i = 0; i < cbNum; i++) {
		STokenBin* bin = binSet.at(i);
		if (newTokenBuffer[i] != NULL)
			bin->addToken(newTokenBuffer[i]);
		prune(bin);
	}

	delete [] cbTypeLookup;
	delete [] newTokenBuffer;
}