Exemple #1
0
int balance_quad(int cycle)
{
	int i;

	if( cycle%50000 == 0)	grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 1, thread_stats);
	else			grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats);


	if( !borders ) 	{
		borders = (grid_border_t**) malloc (n_grid_units*sizeof(grid_border_t*)); assert(borders);
		for(i=0;i<n_grid_units;i++) {
			borders[i] = (grid_border_t*) malloc (m_grid_units*sizeof(grid_border_t)); assert(borders[i]);
		}
	}

	total_conflicts = grid_border_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, borders);

	//balance
	if( !qtree )
	{
		qtree = (quad_node_t*)malloc(sizeof(quad_node_t)); if (qtree == NULL) return 0;
		quad_node_init(qtree, NULL, tm_wm.map_r, (tm_wm.depth+1)/2);
	}


	quad_node_balance(qtree);

	grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y );

	//quad_node_deinit(qtree);

	return 1;
}
Exemple #2
0
int balance_spread(int cycle)
{
	int i, j;

	grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats);
	for(i = 0; i < sv.num_threads; i++)	thread_stats[i] = 0;

	//balance
	for(i=0; i<n_grid_units; i++)
	{
		for(j=0; j<m_grid_units; j++)
		{
			if(grid[i][j].n_players == 0)  {
				grid[i][j].tid = (i+j) % (sv.num_threads);
				continue;
			}

			grid[i][j].tid = grid_least_loaded_thread(thread_stats);
			thread_stats[grid[i][j].tid] += grid[i][j].n_players;
		}
	}

	grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y );


	return 1;
}
Exemple #3
0
int layout_add(struct TrayIcon *ti)
{
	if (grid_add(ti)) {
		grid_update(ti, True);
		return SUCCESS;
	} else
		return FAILURE;
}
Exemple #4
0
int grid_remove(struct TrayIcon *ti)
{
	/* implementation is similar to that of layout_handle_icon_resize(),
	 * see detailed description there */
	icon_list_forall_from(ti, &layout_unset_flag);
	if (settings.shrink_back_mode || settings.scrollbars_mode != SB_MODE_NONE) grid_update(ti, False);
	/* Since NULL is a special case for icon_list_froall_from,
	 * avoid calling it for the last icon */
	if (ti->next != NULL)
		RETURN_STATUS(icon_list_forall_from(ti->next, &grid_add_wrapper) == NULL);
	else
		RETURN_STATUS(SUCCESS);
}
Exemple #5
0
int balance_areatree(int cycle)
{
	grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats);

	//balance
	if( !atree )
	{
		atree = (tree_t*)malloc(sizeof(tree_t)); if (atree == NULL) return 0;
		tree_init(atree, NULL, tm_wm.map_r, DIR_UP, tm_wm.depth);
	}

	grid_set_owner_no_tid( grid, n_grid_units, m_grid_units, thread_stats );
	tree_balance(atree);

	grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y );

	return 1;
}
Exemple #6
0
int balance_static2(int cycle)
{
	int i, j;

	//balance
	int stripesz = (int)rint((double)m_grid_units / (double)sv.num_threads); // round to closest int
	for( i = 0; i < n_grid_units; i++ )
	{
		for( j = 0; j < sv.num_threads * stripesz; j++ )
		{
			if( j >= m_grid_units )	break;
			grid[i][j].tid = j / stripesz;
		}
		for( j = sv.num_threads * stripesz; j < m_grid_units; j++ )
			grid[i][j].tid = sv.num_threads - 1;
	}

	grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats);
	grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y );

	return 1;
}
Exemple #7
0
int balance_static3(int cycle)
{
	//Call recursive helper with required parameters - First split vertically
	static3_helper(0, sv.num_threads, 0, 0, 0, n_grid_units, m_grid_units);

        //burceam: this code was NOT here; it is NOT required for static3; I am putting it here
        //to see if my heuristic h3 works as expected with it here; this should be temporary.
        
        //if( !borders ) 	{
        //   int i;
	//   borders = (grid_border_t**) malloc (n_grid_units*sizeof(grid_border_t*)); assert(borders);
	//   for(i=0;i<n_grid_units;i++) {
	//       borders[i] = (grid_border_t*) malloc (m_grid_units*sizeof(grid_border_t)); assert(borders[i]);
	//   }
	//}

	//total_conflicts = grid_border_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, borders);

        //burceam end of code_that_shouldn't_be_here

	grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats);
	grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y );
	return 1;
}
Exemple #8
0
int layout_handle_icon_resize(struct TrayIcon *ti)
{
	struct Rect old_grd_rect;
	struct Point old_grid_sz;
	int rc;
#ifdef DEBUG
	if (settings.log_level >= LOG_LEVEL_TRACE) {
		LOG_TRACE(("currently managed icons:\n"));
		icon_list_forall(&print_icon_data);
	}
#endif
	if (!icon_list_backup()) return FAILURE;
	old_grid_sz = grid_sz;
	if (ti->is_layed_out) {
		/* if the icon is already layed up and
		 * its grid rect did not change we do nothing,
		 * since its position inside grid cell will be
		 * updated by embedder_update_positions */
		old_grd_rect = ti->l.grd_rect;
		grid_translate_from_window(ti);
		if (ti->l.grd_rect.w == old_grd_rect.w &&
		    ti->l.grd_rect.h == old_grd_rect.h)
		{
			icon_list_backup_purge();
			return SUCCESS;
		}
	}
	/* Here's the place where icon sorting start playing its role.
	 * It is easy to see that resizing ti affects only those icons
	 * that are after ti in the icon list. */
	/* 1. Unset layout flags of all icons after ti */
	icon_list_forall_from(ti, &layout_unset_flag);
	/* 2. If shrink mode is on, recalculate the size of the grid 
	 *    for the remaining icons. This ensures that final grid
	 *    will be "minimal" (I did not prove that :) */
	if (settings.shrink_back_mode) grid_update(ti, False);
#ifdef DEBUG
	if (settings.log_level >= LOG_LEVEL_TRACE) {
		LOG_TRACE(("list of icons which are to be added back to the grid\n"));
		icon_list_forall_from(ti, &print_icon_data);
	}
#endif
	/* 3. Start adding icons after ti back to the grid one
	 *    by one. If this fails for some icon, forall_icons_from()
	 *    will return non-NULL value and this will be considered 
	 *    as a error condition */ 
	if (icon_list_forall_from(ti, &grid_add_wrapper) == NULL) {
		/* Everything is OK */
		icon_list_backup_purge();
		grid_update(ti, True);
		rc = SUCCESS;
	} else {
		/* Error has occured */
		icon_list_restore();
		grid_sz = old_grid_sz;
		rc = FAILURE;
	}
#ifdef DEBUG
	if (settings.log_level >= LOG_LEVEL_TRACE) {
		LOG_TRACE(("currently managed icons:\n"));
		icon_list_forall(&print_icon_data);
	}
#endif
	return rc;
}
int main(void) {
	int i, position_x, position_y;
	
	/* Declaration of the needed data structures. */
	grid_t * certainty_grid;
	range_measure_t measure[MEASUREMENTS];
	hist_t * polar_histogram;
	control_signal_t control_signal;
	
	/*
	** Initialization of the grid and the histogram.
	*/

	certainty_grid = grid_init(50, 10);
	polar_histogram = hist_init(2, 20, 10, 5);
	
	/* Are the initializations ok? */
	if (certainty_grid == NULL) return -1;
	if (certainty_grid->cells == NULL) return -1;
	if (polar_histogram == NULL) return -1;
	if (polar_histogram->densities == NULL) return -1;
	
	/*
	** Fake measures.
	*/

	printf("Measures\n");
		
	for (i = 0; i < MEASUREMENTS; ++i) {
		measure[i].direction = (int) ((360.0 * rand()) / RAND_MAX); /* [degrees] */
		measure[i].distance = (int) (((130.0 * rand()) / RAND_MAX) + 20); /* [cm] */
		
		if (i < 50)
			printf("\t%2d: %3d [cm], %3d [degrees]\n", i, (int) measure[i].distance,
				measure[i].direction);
	}
	
	if (i > 50) printf("\t...\n");

	/* Let's assume the 'robot' is in the middle of the grid. */
	position_x = position_y = (certainty_grid->dimension + 1) / 2;

	printf("\nPosition of the robot: (%d, %d)\n", position_x, position_y);

	/* Add the information from the measures to the grid. */
	printf("\nUpdating the certainty grid...\n");
	for (i = 0; i < MEASUREMENTS; ++i) {
		grid_update(certainty_grid, position_x, position_y, measure[i]);
	}
		
	/*
	** Calculating the control signals.
	*/
	
	/* Generate the histogram for the current grid. */
	printf("\nUpdating the polar histogram...\n");
	hist_update(polar_histogram, certainty_grid);
	
	/* What's the next direction? */
	control_signal.direction = calculate_direction(polar_histogram, OBJECTIVE_DIRECTION);
	
	printf("\nNext direction: %d [degrees]\n", control_signal.direction);

	free(certainty_grid);
	free(polar_histogram);
	
	certainty_grid = NULL;
	polar_histogram = NULL;

	return 0;
}
Exemple #10
0
int balance_lightest(int cycle)
{
	int i, j;

	grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats);

	// distribute each grid unit to least loaded thread
	int most_loaded, least_loaded, best_i, best_j;
	int count = 0;


	//balance
	while(1)
	{
		most_loaded = grid_most_loaded_thread(thread_stats);
		//printf("---\n\tMost loaded=%d  npl[T%d]=%d\n", most_loaded, most_loaded, thread_stats[most_loaded]);

		if( !grid_is_overloaded_thread( thread_stats, most_loaded ) )
			break; // no one overloaded anymore

		least_loaded = grid_least_loaded_thread(thread_stats);
		//printf("\tLeast loaded=%d  npl[T%d]=%d\n", least_loaded, least_loaded, thread_stats[least_loaded]);

		if( grid_is_overloaded_thread( thread_stats, least_loaded ) ) break;
		if( most_loaded == least_loaded ) break;

		// find best region to move
		best_i = -1; best_j = -1;
		for(i=0; i<n_grid_units; i++)
		{
			for(j=0; j<m_grid_units; j++)
			{
				if(grid[i][j].tid != most_loaded)  continue;
				if(grid[i][j].n_players == 0)  continue;

				if(grid[i][j].tid == most_loaded &&
				     (best_i == -1 ||
				       (grid[i][j].n_players + thread_stats[least_loaded] < OVERLOAD_THRESHOLD && //doesn't overload least loaded thread
				        grid[i][j].n_players > grid[best_i][best_j].n_players) // is best region
				     )
				   )
				{
					best_i = i;
					best_j = j;
				}
			}
		}

		// check if any region found
		if(best_i == -1)   break;
		//printf("\tmoving region(%d;%d) players:%d\n", best_i, best_j, grid[best_i][best_j].n_players);

		// move region
		grid[best_i][best_j].tid = least_loaded;
		thread_stats[most_loaded]  -= grid[best_i][best_j].n_players;
		thread_stats[least_loaded] += grid[best_i][best_j].n_players;

		if(count++ > 1000){	printf("WARNING: Lightest count overflow\n");break;	}
	}

	grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y );


	return 1;
}
Exemple #11
0
int balance_none()
{
	grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats);
	return 1;
}