Example #1
0
int main()
{
	long long int t, T;							// Test cases and test case counter

	init();
	scanf("%lld", &T);
    // Go through all test cases
	for( t = 0 ; t < T ; t ++)
	{
		long long int i;                        // for loop counters
		long long int num;						// The input
		struct factor facts[20];				// Factors structure
		int numFacts;							// number of factors
		long long int sum = 1;
		
        scanf("%lld", &num);			    	// Get the number

		factorise(num, facts, numFacts);		// Factorise it

/* Tested working
        for ( i = 0 ; i < numFacts ; i ++ ) {
            printf("%lld %d\n", facts[i].factr, facts[i].power);
        }
*/
		for ( i = 1; i < numFacts ; i ++ ) {
			long long int powSum = getPowSum(facts[i].factr, facts[i].power);
			// printf("%lld\n", powSum);
			sum *= powSum;
		}
		printf("%lld\n", sum);
	}
	return 0;
}
Example #2
0
void generate_periodic_taskset(vector<float_task> *tasks, long hyperperiod, int num_tasks, float comp_util, float thermal_util)
{
    tasks->clear();
    
    vector<int> factors;
    factorise(&factors, hyperperiod);

    float_task temp;

    float sumU=comp_util;
    float t_sumU=thermal_util;

    float t_next_sumU = t_sumU;
    float next_sumU;
    bool violation=false;
    float total_util = 0, t_total_util = 0;

    for (int i = 0; i < num_tasks; i++) {
        int index = rand() % factors.size();
        temp.period = (float)factors[index];
        temp.taskset = 0;

        float cutil;
        float rnum;
retry:
        if(i<num_tasks-1) {
            rnum = rand() % 100;
            if(rnum < 50 || rnum >= 70) //Percentage should be not too low nor too high
                goto retry;
            next_sumU = sumU * (rnum / (float) (100));
            cutil = sumU - next_sumU; 
        }
        else {
            cutil = sumU;
        }

        temp.computation_time = temp.period * (cutil);
        temp.computation_time = floor(temp.computation_time /(W_INT*temp.period))*W_INT*temp.period;
        
        sumU = sumU - (temp.computation_time/(temp.period)); 

        if (temp.computation_time > 0) {
            //cout << "i = " << i << " C = " << temp.computation_time << " T = " << temp.period << " cutil = " << cutil   \
                << " sumU = " << sumU << endl;
            total_util += cutil;
            tasks->push_back(temp);
        }
        else {
           // cout << "Negative computation time " << endl;
        }
    }
Example #3
0
static void update_time() {
    // Get a tm structure
    time_t temp = time(NULL);
    struct tm *tick_time = localtime(&temp);

    // Create a long-lived buffer
    static char buffer[] = "00:00";
    static char text[20];
    char buffer_int[] = "0000";

    // Write the current hours and minutes into the buffer
    if(clock_is_24h_style() == true) {
        // Use 24 hour format
        strftime(buffer, sizeof("00:00"), "%H:%M", tick_time);
        strftime(buffer_int, sizeof("0000"), "%H%M", tick_time);
    } else {
        // Use 12 hour format
        strftime(buffer, sizeof("00:00"), "%I:%M", tick_time);
        strftime(buffer_int, sizeof("0000"), "%I%M", tick_time);
    }

    // Main time display
    text_layer_set_text(s_time_layer, buffer);

    int number = atoi(buffer_int);
    int factors[12];
    int factor_count = 0;
    factorise(number, factors, &factor_count);
    strcpy( text, "" );
    if( factor_count == 1 ) {
        if( number == 1 || number == 0 ) {
            strcpy( text, "It's complicated" );
        } else {
            strcpy( text, "Prime" );
        }
    } else {
        for( int i = 0; i < factor_count; i++ ) {
            char factor[100];
            snprintf(factor, sizeof("99999") , "%d", factors[i] );
            if( i > 0 ) strcat(text, "x");
            strcat(text, factor);
        }
    }
    text_layer_set_text(s_factors_layer, text);
}
Example #4
0
void factorise(int number, int *factors, int *factor_count) {
    int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
    if( number == 1 || number == 0 ) {
        // Reached a prime within the search space
        return;
    }
    for(int i = 0; i < 15; i++) {
        int prime = primes[i];
        if( number%prime == 0 ) {
            factors[*factor_count] = prime;
            *factor_count = *factor_count + 1;
            number = number / prime;
            factorise(number, factors, factor_count);
            return;
        }
    }

    // Reached a prime not within the search space
    factors[*factor_count] = number;
    *factor_count = *factor_count + 1;
    return;
}
int main(int argc, char ** argv)
{
	/* ----------- VARIABLE DECLARATIONS ----------- */

	/* Size of matrix - the matrix will be n x n */
	int n;

	/* Number of processes and rank of this process*/
	int nprocs, rank;
	
	/* Size and periodicity of cartesian grid */
	int dim_size[2];
	int periods[2];
	
	/* Number of processes in x and y directions */
	int npx, npy;
	
	/* Offset for this process in x and y directions */
	int x_offset, y_offset;
	
	/* The standard (normal) number of rows and cols
	per core - not valid for the last in a row or col */
	int std_rows_in_core, std_cols_in_core;
	
	/* Coordinates in cartesian grid, and num of rows and cols
	this process has */
	int coords[2];
	int rows_in_core;
	int cols_in_core;
	
	/* Array to store the chunk of the matrix that we have */
	double **array;
	double **new_array;
	
	
	/* Loop variables */
	int i, j;
	
	/* Cartesian communicator */
	MPI_Comm cart_comm;
	
	int coords2[2];
	int rank2;

	int global_x1, global_x2;
	int global_y1, global_y2;

	struct global_index gi;
	struct local_index li;
	
	MPI_Request send_request;
	MPI_Request recv_request;
	
	/* Stores the number of requests this core will need to keep track of
	and creates a pointer to eventually hold the array of these requests */
	int n_requests;
	MPI_Request *all_requests;
	
	int request_counter;
	
	int tag;
	
	/* Determines whether the final result should be printed or not */
	int print = 0;
	
	/* Holds start and end times for the transpose */
	double tstart, tend, walltime;
	
	/* ----------- PROCESS CMD LINE ARGUMENTS ----------- */
	
	/* If there isn't one command-line argument (argc also counts the program name)
	then print an error message and exit.
	Only do this if we're on process 0 - otherwise nprocs error messages will be printed!*/
	if (argc < 2 && rank == 0)
	{
		printf("You must specify the size of the matrix (n) as a command-line argument\n");
		printf("Exiting\n");
		MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
		return EXIT_FAILURE;
	}
	
	/* As we've got here, there must be at least one command-line argument, so try and convert it to an
	integer */
	n = atoi(argv[1]);
	
	/* Process other command line arguments */
	/* Process command line arguments checking them and running the appropriate function */
	for (i = 2; i < argc; i++)  /* Skip argv[0] (program name) and argv[1] (size of matrix)*/
    {
    	/* if strcmp returns 0 then the strings are identical */
        if (strcmp(argv[i], "print") == 0)
        {
            print = 1;
        }

    }	
	
	/* If atoi returns zero it is either because it couldn't convert the string to an integer,
	or the integer given was 0. In either case, we must print an error message because it is a
	nonsensical size for the matrix */
	if (n == 0)
	{
		printf("You must an integer > 0 as the size of the matrix\n");
		printf("Exiting\n");
		MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
		return EXIT_FAILURE;
	}
	
	/* ----------- MPI Setup ----------- */

	/* Initialise MPI */
	MPI_Init(&argc, &argv);

	/* Get the rank for this process, and the number of processes */
	MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	
	/* Work out how big the grid should be, given the number of processes */
	factorise( nprocs, &npx, &npy );
	
	/* Create the cartesian communicator */
	dim_size[0] = npy;
	dim_size[1] = npx;
	periods[0] = 1;
	periods[1] = 1;
	
	MPI_Cart_create(MPI_COMM_WORLD, 2, dim_size, periods, 1, &cart_comm);
	
	/* Get our co-ordinates within that communicator */
	MPI_Cart_coords(cart_comm, rank, 2, coords);
		
	rows_in_core = ceil(n / (float) npx);
	cols_in_core = ceil(n / (float) npy);
	
	std_rows_in_core = rows_in_core;
	std_cols_in_core = cols_in_core;
		
	if (coords[0] == (npy - 1))
	{
		/* We're at the far end of a row */
		cols_in_core = n - (cols_in_core * (npy - 1));
	}
	if (coords[1] == (npx - 1))
	{
		/* We're at the bottom of a col */
		rows_in_core = n - (rows_in_core * (npx - 1));
	}
	
	/* Calculate the number of individual communications that are needed to transpose the matrix.
	We know that this will be the number of requests we need to store, so malloc some memory for the
	array of the right size. Each process has its own requests array - so it's simply 2 * ncols * nrows
	*/
	n_requests = 2 * rows_in_core * cols_in_core;
	all_requests = malloc(n_requests * sizeof(MPI_Request));
	
	/* ----------- INITIALISE MATRIX CHUNKS ----------- */
	
	/* Allocate an array to store this chunk of the matrix.
	If the allocation fails, print an error */
	array = alloc_2d_double(rows_in_core, cols_in_core);
	new_array = alloc_2d_double(rows_in_core, cols_in_core);
	if (array == NULL || new_array == NULL)
	{
		printf("Problem with array allocation.\nExiting\n");
		return 1;
	}
	
	for (i = 0; i < rows_in_core; i++)
	{
		for (j = 0; j < cols_in_core; j++)
		{
			new_array[i][j] = NAN;
		}
	}
	
	/* Calculate the offset of the top left-hand corner of our chunk of the matrix from the
	top left-hand corner of the whole matrix */
	x_offset = coords[0] * std_cols_in_core;
	y_offset = coords[1] * std_rows_in_core;
	
	for (i = 0; i < rows_in_core; i++)
	{
		/*printf("Process (%d, %d): ", coords[0], coords[1]);*/
		for (j = 0; j < cols_in_core; j++)
		{
			array[i][j] = (float) ( (i + y_offset) * n) + ( (j + x_offset) + 1);
			
			/*printf("%f ", array[i][j]);*/
		}
		/*printf("\n");*/
	}
	
	MPI_Barrier(MPI_COMM_WORLD);
	
	/* ----------- DO TRANSPOSE ----------- */
	/* Record the start time, making sure all processes have got there at the same time using
	a barrier */
	MPI_Barrier(cart_comm);
	tstart = timer();
		
	request_counter = 0;
	
	for (i = 0; i < rows_in_core; i++)
	{
		for (j = 0; j < cols_in_core; j++)
		{
			/* For each item in this chunk of the array:
			
			Send it to the opposite index
			Receive from the opposite index */
						
			/* Calculate the opposite index */
			gi = local_to_global(coords[0], coords[1], j, i, std_cols_in_core, std_rows_in_core);
						
			/* The two global indices for swapping */
			global_x1 = gi.x;
			global_y1 = gi.y;
			
			global_x2 = global_y1;
			global_y2 = global_x1;
			

			/* We know which rank one of them is (as we're running as it!)
			but we need to calculate the rank of the other which involves:
			1. Getting the local index of the swapped global index
			2. Converting the X, Y process co-ordinates to a rank */
			li = global_to_local(global_x2, global_y2, std_cols_in_core, std_rows_in_core);
						
			coords2[0] = li.procX;
			coords2[1] = li.procY;
						
			MPI_Cart_rank(cart_comm, coords2, &rank2);
			
			tag = (global_x2 + 1) * (global_y2 + 1);
						
			/*printf("Swapping\n");
			printf("Value = %f\n", array[i][j]);
			printf("(%f) From global (%d, %d) - Processor (%d, %d) with local (%d, %d)\n", array[i][j], global_x1, global_y1, coords[0], coords[1], j, i);
			printf("(%f) To global (%d, %d)- Processor (%d, %d) with local (%d, %d)\n", array[i][j], global_x2, global_y2, coords2[0], coords2[1], li.arrayX, li.arrayY);
			printf("(%f) Going to rank %d\n", array[i][j], rank2); */
			
			MPI_Isend(&array[i][j], 1, MPI_DOUBLE, rank2, (global_y2 * n) + global_x2, MPI_COMM_WORLD, &send_request);

			MPI_Irecv(&new_array[i][j], 1, MPI_DOUBLE, rank2, (global_x2 * n) + global_y2, MPI_COMM_WORLD, &recv_request);
			all_requests[request_counter] = send_request;
			request_counter++;
			all_requests[request_counter] = recv_request;
			request_counter++;
		}
	}
	MPI_Waitall(request_counter, all_requests, MPI_STATUSES_IGNORE);

	/* Store end time (using barrier before-hand to make the calculated times for all of the
	processes almost exactly equal, meaning we can just take the time from one process
	for output */
	MPI_Barrier(cart_comm);
	tend = timer();	
	
	/* ----------- PRINT OUTPUT AND FINALISE ENVIRONMENT ----------- */
		
	walltime = tend - tstart;
	 
	if (print == 1)
	{
		for (i = 0; i < rows_in_core; i++)
		{
			printf("Result at Process (%d, %d): ", coords[0], coords[1]);
			for (j = 0; j < cols_in_core; j++)
			{			
				printf("%f ", new_array[i][j]);
			}
			printf("\n");
		}
	}
	
	if (rank == 0)
	{
			printf("%d\t%d\t%f\n", nprocs, n, walltime);
	}
	
	/* Free the memory we grabbed for the requests array */
	free(all_requests);
	
	/* Free the memory we grabbed earlier for the data arrays */
	free_2d_double(array);
	free_2d_double(new_array);
	
	/* Close down the MPI environment */
	MPI_Finalize();
	
	return EXIT_SUCCESS;
}
Example #6
0
void ab_generate_taskset(vector<float_task> *tasks, long hyperperiod, int num_tasks, float comp_util, float thermal_util)
{
    tasks->clear();
	vector<int> factors;
	factorise(&factors, hyperperiod);

	float_task temp;

	float sumU=comp_util;
	float t_sumU=thermal_util;

	float t_next_sumU;
    float next_sumU;
	bool violation=false;

	for (int i = 0; i < num_tasks; i++) {
		int index = rand() % factors.size();
		temp.period = factors[index];
        temp.taskset = 0;

		float cutil;
		if(i<num_tasks-1) {
			next_sumU=sumU*pow((float)(rand()/((float)(RAND_MAX))),1.0/((float)(num_tasks-(i+1))));
			cutil=sumU-next_sumU;

		} else {
			cutil=sumU;
		}

		temp.computation_time=temp.period*(cutil);
		temp.computation_time=floor(temp.computation_time/(W_INT*temp.period))*W_INT*temp.period;
		sumU=sumU-(temp.computation_time/temp.period);


		if (temp.computation_time > 0) {
			tasks->push_back(temp);
		}
	}
    float t_total_util;
retry_thermal:
    t_sumU=thermal_util;
    t_total_util = 0;
	for(unsigned int i=0;i<tasks->size() && !violation;i++) {
		float tutil;
		float max_tutil=t_sumU;
		float min_tutil=t_sumU;
		if(i<num_tasks-1) {
			for(unsigned int j=i+1;j<tasks->size();j++) {
				max_tutil=max_tutil-MIN_POWER*(*tasks)[j].computation_time/(corrected_threshold*beta*(*tasks)[j].period);
				min_tutil=min_tutil-MAX_POWER*(*tasks)[j].computation_time/(corrected_threshold*beta*(*tasks)[j].period);
			}

			float local_max;
			float local_min;

			local_max=MAX_POWER*(*tasks)[i].computation_time/(corrected_threshold*beta*(*tasks)[i].period);
			local_min=MIN_POWER*(*tasks)[i].computation_time/(corrected_threshold*beta*(*tasks)[i].period);

			max_tutil=max_tutil>local_max?local_max:max_tutil;
			min_tutil=min_tutil<local_min?local_min:min_tutil;

			if(min_tutil>max_tutil) {
				cout<<" error detected min tutil "<<min_tutil<<" max util "<<max_tutil<<endl;
			}

			tutil=-1;

			int iteration=0;
			while((tutil<min_tutil || tutil>max_tutil) && !violation)
			{

				t_next_sumU=t_sumU*pow(rand()/((float)(RAND_MAX)),1.0/((float)(num_tasks-(i+1))));
				tutil=t_sumU-t_next_sumU;

				if(iteration>1000)
				{
					violation=true;
				}

				iteration++;
			}
//			cout<<" exited while loop "<<endl;
		}
		else
		{
			tutil=t_sumU;
		}
        t_total_util += tutil;
		(*tasks)[i].power=corrected_threshold*beta * (*tasks)[i].period*tutil/(*tasks)[i].computation_time;
		(*tasks)[i].power=floor((*tasks)[i].power*10.0)/10.0;
		(*tasks)[i].power=(*tasks)[i].power>MAX_POWER?MAX_POWER:(*tasks)[i].power<MIN_POWER?MIN_POWER:(*tasks)[i].power;

		t_sumU=t_sumU-(*tasks)[i].computation_time*(*tasks)[i].power/(corrected_threshold*beta*(*tasks)[i].period);


		//cout<<"numerator "<<corrected_threshold*beta * temp.period*tutil<<endl;

	}


    if(t_total_util > thermal_util+0.05 || t_total_util < thermal_util-0.05) {
        //cout << "retry t_total_util " << t_total_util << endl;
        violation = false;
        goto retry_thermal;
    }
//	cout<<"checkpoint 1"<<endl;

//	for(unsigned int i=0;i<tasks->size();i++)
//	{
//		if((*tasks)[i].power<MIN_POWER || (*tasks)[i].power>MAX_POWER)
//		{
//			violation=true;
//			break;
//		}
//	}

//	if(violation)
//	{
//		tasks->clear();
//		ab_generate_taskset(tasks, hyperperiod, num_tasks, comp_util, thermal_util);
//	}

	for(unsigned int i =0;i<tasks->size();i++)
	{
		(*tasks)[i].index=i;
	}


	taskset temp_set;
    int num_tasksets = 1;
	for (int i = 0; i < num_tasksets; i++) {

		int hyp_length = hyperperiod;//compute_lcm(tasks, 0);
		float thermal_capacity = ((float) hyp_length) * corrected_threshold;
		//int thermal_utilization = rand() % 51 + 50;
        float thermal_utilization = thermal_util;
		float taskset_target_TTI = thermal_capacity
				* ((float) thermal_utilization) / 100;
		float average_power = taskset_target_TTI / hyp_length * 2; //*beta;//beta not added to reduce power

		int start = -1;
		int end;
		for (unsigned int j = 0; j < tasks->size(); j++) {
			if ((*tasks)[j].taskset == i) {
				end = j;
				if (start == -1) {
					start = j;
				}
			}
		}

		float total_util = 0;
		for (int j = start; j <= end; j++) {

			     #if(ENABLE_PRINTS)
			cout << "task:" << j << "taskset:" << (*tasks)[j].taskset
					<< " computation time:" << (*tasks)[j].computation_time
					<< " period:" << (*tasks)[j].period << " Utilization: "
					<< (float) (*tasks)[j].computation_time
							/ (float) (*tasks)[j].period << " power: "<< (*tasks)[j].power<<endl;
			     #endif
			total_util = total_util
					+ (float) (*tasks)[j].computation_time
							/ (float) (*tasks)[j].period;

		}
#if(ENABLE_PRINTS)

		cout<<"total_utilization="<<total_util<<endl;
        cout<<"thermal utilization="<<t_total_util<<endl;
#endif
		temp_set.c_util = total_util;
		average_power = average_power / total_util;
#if(ENABLE_PRINTS)

		cout<<"length of hyperperiod:"<<hyp_length<<endl;
		cout<<"thermal utilization: "<<thermal_utilization<<"average_power: "<<average_power<<endl;
#endif
/*		float deviation = average_power * 1.6;
		for (int j = start; j <= end; j++) {
			float power = average_power * 0.2
					+ ((double) (rand()) / RAND_MAX) * deviation;
			(*tasks)[j].power = power;
		}*/

		float total_impact = 0;
		for (int j = start; j < end; j++) {
			total_impact = total_impact
					+ (*tasks)[j].power * (*tasks)[j].computation_time
							* (hyp_length / (*tasks)[j].period) / beta;
		}
		temp_set.TTI = total_impact / GRANULARITY;
		temp_set.t_util = total_impact / (corrected_threshold * (hyp_length));
		temp_set.average_power = total_impact / hyp_length * beta;
		temp_set.hyperperiod = hyp_length;
		tasksets.push_back(temp_set);
    }

}
Example #7
0
void generate_taskset(vector<float_task> *tasks, long hyperperiod, int num_tasks, float comp_util, float thermal_util)
{
	vector<int> factors;
	factorise(&factors, hyperperiod);

	float_task temp;
//	int num_tasks =

//	cout<<" number of tasks "<<num_tasks<<endl;
	float sumU=comp_util;
	float t_sumU=thermal_util;

//	cout<<" initial sumU "<<sumU<<" t_sumU "<<t_sumU<<endl;
	float t_next_sumU;
	float next_sumU;

	bool violation=false;

	for (int i = 0; i < num_tasks; i++)
	{
		int index = rand() % factors.size();
		temp.period = factors[index];

		float cutil;
		if(i<num_tasks-1)
		{
			next_sumU=sumU*pow((float)(rand()/((float)(RAND_MAX))),1.0/((float)(num_tasks-(i+1))));
//			cout<<" sumu "<<sumU<<" next sumu "<<next_sumU<<endl;
			cutil=sumU-next_sumU;

		}
		else
		{
			cutil=sumU;
		}


		temp.computation_time=temp.period*(cutil);
		temp.computation_time=floor(temp.computation_time/(W_INT*temp.period))*W_INT*temp.period;
		sumU=sumU-(temp.computation_time/temp.period);


		if (temp.computation_time > 0)
		{
			tasks->push_back(temp);
		}
	}

	for(unsigned int i=0;i<tasks->size() && !violation;i++)
	{
		float tutil;
		float max_tutil=t_sumU;
		float min_tutil=t_sumU;
		if(i<num_tasks-1)
		{
			for(unsigned int j=i+1;j<tasks->size();j++)
			{
				max_tutil=max_tutil-MIN_POWER*(*tasks)[j].computation_time/(corrected_threshold*beta*(*tasks)[j].period);
				min_tutil=min_tutil-MAX_POWER*(*tasks)[j].computation_time/(corrected_threshold*beta*(*tasks)[j].period);
			}

			float local_max;
			float local_min;

			local_max=MAX_POWER*(*tasks)[i].computation_time/(corrected_threshold*beta*(*tasks)[i].period);
			local_min=MIN_POWER*(*tasks)[i].computation_time/(corrected_threshold*beta*(*tasks)[i].period);

			max_tutil=max_tutil>local_max?local_max:max_tutil;
			min_tutil=min_tutil<local_min?local_min:min_tutil;

			if(min_tutil>max_tutil || max_tutil<min_tutil)
			{
				cout<<" error detected min tutil "<<min_tutil<<" max util "<<max_tutil<<endl;
			}


	//		cout<<" index "<<i<<" num tasks "<<num_tasks<<" max "<<max_tutil<<" min "<<min_tutil<<endl;

			tutil=-1;

			int iteration=0;
			while((tutil<min_tutil || tutil>max_tutil) && !violation)
			{

				t_next_sumU=t_sumU*pow(rand()/((float)(RAND_MAX)),1.0/((float)(num_tasks-(i+1))));
				tutil=t_sumU-t_next_sumU;

				if(iteration>1000)
				{
					violation=true;
				}

				iteration=iteration+1;
			}
//			cout<<" exited while loop "<<endl;
		}
		else
		{
			tutil=t_sumU;
		}

		(*tasks)[i].power=corrected_threshold*beta * (*tasks)[i].period*tutil/(*tasks)[i].computation_time;
		(*tasks)[i].power=floor((*tasks)[i].power*10.0)/10.0;
		(*tasks)[i].power=(*tasks)[i].power>MAX_POWER?MAX_POWER:(*tasks)[i].power<MIN_POWER?MIN_POWER:(*tasks)[i].power;

		t_sumU=t_sumU-(*tasks)[i].computation_time*(*tasks)[i].power/(corrected_threshold*beta*(*tasks)[i].period);


		//cout<<"numerator "<<corrected_threshold*beta * temp.period*tutil<<endl;

	}


//	cout<<"checkpoint 1"<<endl;

//	for(unsigned int i=0;i<tasks->size();i++)
//	{
//		if((*tasks)[i].power<MIN_POWER || (*tasks)[i].power>MAX_POWER)
//		{
//			violation=true;
//			break;
//		}
//	}

	if(violation)
	{
		tasks->clear();
		generate_taskset(tasks, hyperperiod, num_tasks, comp_util, thermal_util);
	}

	for(unsigned int i =0;i<tasks->size();i++)
	{
		(*tasks)[i].index=i;
	}

}
Example #8
0
void generate_tasksets(vector<task>* tasks, int num_tasksets, int hyperperiod,int min_util, int max_util)
{
//	cout << "entered generate tasksets" << endl;
	vector<int> factors;
	factorise(&factors, hyperperiod);

	task temp;
	for (int i = 0; i < num_tasksets; i++) {
		temp.taskset = i;
		int num_tasks = rand() % (11) + 10;
		
		//num_tasks=5;

		int utilization = 0;
		if ((max_util - min_util) > 0) {
			//utilization = 2*rand() % (max_util - min_util) + min_util;
            utilization = max_util;
		} else {
			utilization = min_util;
		}
        cout << "utilizatio = "<<utilization<< endl;
		for (int j = 0; j < num_tasks; j++) {
			temp.index = i;
			int index = rand() % factors.size();
			int task_utilization;
			temp.period = factors[index]; //* MULT_FACTOR;
			if (j < num_tasks - 1) {
				task_utilization = rand()
						% (utilization / ((num_tasks - j) / 2)
								- utilization / (2 * (num_tasks - j)))
						+ utilization / (2 * (num_tasks - j));

             //   task_utilization = utilization * ((( rand() % (num_tasks - j)) + 1) / (num_tasks - j));
                utilization = utilization - task_utilization;
			} else {
				task_utilization = utilization;
			}
            cout << "j:" << j << "util:" << task_utilization <<endl;
			temp.computation_time = temp.period * task_utilization / 100;
			//temp.stat_stream=new ifstream();
			temp.priority=20;
			temp.state=FINISHED;
			temp.core=CORE;
			if (temp.computation_time > 0) {
				tasks->push_back(temp);
			}
		}
	}

    cout << "tasks->size()="<<tasks->size()<<endl;

	for (unsigned int i = 0; i < tasks->size(); i++) {
		(*tasks)[i].tid = i;
	}

	taskset temp_set;
	for (int i = 0; i < num_tasksets; i++) {

		int hyp_length = compute_lcm(tasks, 0);
		float thermal_capacity = ((float) hyp_length) * corrected_threshold;
		int thermal_utilization = rand() % 51 + 50;
		float taskset_target_TTI = thermal_capacity
				* ((float) thermal_utilization) / 100;
		float average_power = taskset_target_TTI / hyp_length * 2; //*beta;//beta not added to reduce power

		int start = -1;
		int end;
		for (unsigned int j = 0; j < tasks->size(); j++) {
			if ((*tasks)[j].taskset == i) {
				end = j;
				if (start == -1) {
					start = j;
				}
			}
		}

		float total_util = 0;
		for (int j = start; j <= end; j++) {

			     //#if(ENABLE_PRINTS)
			cout << "task:" << j << "taskset:" << (*tasks)[j].taskset
					<< " computation time:" << (*tasks)[j].computation_time
					<< " period:" << (*tasks)[j].period << " Utilization: "
					<< (float) (*tasks)[j].computation_time
							/ (float) (*tasks)[j].period << endl;
			     //#endif
			total_util = total_util
					+ (float) (*tasks)[j].computation_time
							/ (float) (*tasks)[j].period;

		}
#if(ENABLE_PRINTS)

		cout<<"total_utilization="<<total_util<<endl;
#endif
		temp_set.c_util = total_util;
		average_power = average_power / total_util;
#if(ENABLE_PRINTS)

		cout<<"length of hyperperiod:"<<hyp_length<<endl;
		cout<<"thermal utilization: "<<thermal_utilization<<"average_power: "<<average_power<<endl;
#endif
		float deviation = average_power * 1.6;
		for (int j = start; j <= end; j++) {
			float power = average_power * 0.2
					+ ((double) (rand()) / RAND_MAX) * deviation;
			(*tasks)[j].power = power;
		}

		float total_impact = 0;
		for (int j = start; j < end; j++) {
			total_impact = total_impact
					+ (*tasks)[j].power * (*tasks)[j].computation_time
							* (hyp_length / (*tasks)[j].period) / beta;
		}
		temp_set.TTI = total_impact / GRANULARITY;
		temp_set.t_util = total_impact / (corrected_threshold * (hyp_length));
		temp_set.average_power = total_impact / hyp_length * beta;
		temp_set.hyperperiod = hyp_length;
		tasksets.push_back(temp_set);
#if(ENABLE_PRINTS)

		cout<<"required_impact "<<taskset_target_TTI<<" actual thermal impact: "<<total_impact<<endl;
#endif
	}
/*#if(OPT_ENABLE==1)
	generate_instances(tasks,"input");
#endif*/
//    exit(1);

//	cout << "taskset generated" << endl;
}