<commit_before>#include <string>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>

#include "taskScheduling.hpp"
#include "../nnet/nnetmultitask.hpp"

using namespace std;
using namespace std::tr1;

static const double CRAZY_ENERGY(-1000.0);

dynProg::dynProg(int numOfTask, vector<double> voltageTable, double deadline, vector<double> taskDuration, vector<double> taskEnergy) {

	m_numOfTask = numOfTask;
	m_numOfVolt = voltageTable.size();
	m_deadline = (int)(deadline*10);
	m_inputDuration = taskDuration;
	m_inputEnergy = taskEnergy;

	volSel = voltageTable;

	m_taskDuration = vector<vector<int> >(numOfTask, vector<int>(m_numOfVolt, 0));
	m_taskEnergy = vector<vector<double> >(numOfTask, vector<double>(m_numOfVolt, 0.0));
	m_taskCurrent = vector<vector<double> >(numOfTask, vector<double>(m_numOfVolt, 0.0));

	m_scheduleEnergy = vector<vector<double> >(numOfTask, vector<double>(m_deadline + 1 , 0.0));
	m_scheduleVolt =  vector<vector<double> >(numOfTask, vector<double>(m_deadline + 1, 0.0));
	m_lastStepDuration = vector<vector<double> >(numOfTask, vector<double>(m_deadline + 1, 0.0));

	// for(int i = 0; i < numOfTask; i ++){
	// 	cout<<"Task "<<i<<" :"<<m_inputDuration[i]<<", "<<m_inputEnergy[i]<<endl;
	// }
	// memory the task energy and duration at each voltage;
	for (int i = 0; i < m_numOfTask; i++) {
		// cout<<"Task "<<i<<":";
		for (int k = 0; k < m_numOfVolt; k++) {
			m_taskDuration[i][k] = (int)ceil(m_inputDuration[i] * 10.0 / volSel[k]);
			m_taskEnergy[i][k] = m_inputEnergy[i] * volSel[k] * volSel[k];
			m_taskCurrent[i][k] = m_inputEnergy[i] / m_inputDuration[i] * volSel[k] * volSel[k] ;
			// cout<<m_taskDuration[i][k]<<","<<m_taskEnergy[i][k]<<endl;
		}
	}

	// System model related parameters
	m_initialEnergy = 20;
	m_solarPower = 3.0;

	// Initialize DP table with idle task
	m_scheduleWithIdleTask = vector<vector<dpTableEntry> >(m_numOfTask+m_numOfTask+1, vector<dpTableEntry>(m_deadline+1, dpTableEntry()));
	for (size_t i = 0; i < m_scheduleWithIdleTask.size(); i += 2) {
		for (size_t j = 0; j != m_scheduleWithIdleTask[i].size(); ++j) {
			// Idle tasks
			m_scheduleWithIdleTask[i][j].taskID = i/2;
			// Real tasks
			if (i+1 < m_scheduleWithIdleTask.size()) {
				m_scheduleWithIdleTask[i+1][j].taskID = i/2;
			}
		}
	}
}

void dynProg::taskTimelineWithIdle() {
	// initialize the first task -- idle task
	populateFirstIdleTask(m_scheduleWithIdleTask[0]);
	for (size_t i = 0; i < m_scheduleWithIdleTask.size()-1; i += 2) {
		// populating for the even number tasks -- real tasks
		populateRealTask(m_scheduleWithIdleTask[i], m_scheduleWithIdleTask[i+1]);
		// populating for the odd number tasks -- idle tasks
		populateIdleTask(m_scheduleWithIdleTask[i+1], m_scheduleWithIdleTask[i+2]);
	}
#ifdef DEBUG_VERBOSE
	dumpDPTable();
#endif
}

void dynProg::populateFirstIdleTask(vector<dpTableEntry> &firstIdleRow) {
	for (tableEntryIter iter = firstIdleRow.begin(); iter != firstIdleRow.end(); ++iter) {
		size_t len = (iter-firstIdleRow.begin());
		iter->totalEnergy = energyCalculator(m_solarPower, m_initialEnergy, len);
		iter->len = len;
	}
	return;
}

void dynProg::populateIdleTask(const vector<dpTableEntry> &lastRealRow, vector<dpTableEntry> &thisIdleRow) {

	double inputPower = m_solarPower;
	if (inputPower < 0.0)
		return;

	for (vector<dpTableEntry>::const_iterator iter = lastRealRow.begin(); iter != lastRealRow.end(); ++iter) {
		if (iter->totalEnergy < 0)
			continue;
		tableEntryIter iterIdleHead = thisIdleRow.begin()+(iter-lastRealRow.begin());
		for (tableEntryIter iterIdle = iterIdleHead; iterIdle != thisIdleRow.end(); ++iterIdle) {
			int taskDur = iterIdle - iterIdleHead;
#ifdef DEBUG_VERBOSE
			cout << "I am predicting idle task " << iterIdle->taskID << " from time " << iterIdleHead - thisIdleRow.begin() << " to " << iterIdle - thisIdleRow.begin() << "." << endl;
#endif
			double energy = energyCalculator(inputPower, iter->totalEnergy, taskDur);
			if (energy > iterIdle->totalEnergy) {
#ifdef DEBUG_VERBOSE
				cout << "Taskid: idle " << iterIdle->taskID << " entry " << iterIdle-thisIdleRow.begin() << " has been updated from "
					<< iterIdle->totalEnergy << " to " << energy << endl;;
#endif
				// Update the table entry
				iterIdle->setAllFields(energy, -1.0, -1.0, -1, iterIdle->taskID, taskDur, iterIdleHead-thisIdleRow.begin());
			}<commit_msg>Set ideal task to be voltage 1.0 current 0.0<commit_after>#include <string>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>

#include "taskScheduling.hpp"
#include "../nnet/nnetmultitask.hpp"

using namespace std;
using namespace std::tr1;

static const double CRAZY_ENERGY(-1000.0);

dynProg::dynProg(int numOfTask, vector<double> voltageTable, double deadline, vector<double> taskDuration, vector<double> taskEnergy) {

	m_numOfTask = numOfTask;
	m_numOfVolt = voltageTable.size();
	m_deadline = (int)(deadline*10);
	m_inputDuration = taskDuration;
	m_inputEnergy = taskEnergy;

	volSel = voltageTable;

	m_taskDuration = vector<vector<int> >(numOfTask, vector<int>(m_numOfVolt, 0));
	m_taskEnergy = vector<vector<double> >(numOfTask, vector<double>(m_numOfVolt, 0.0));
	m_taskCurrent = vector<vector<double> >(numOfTask, vector<double>(m_numOfVolt, 0.0));

	m_scheduleEnergy = vector<vector<double> >(numOfTask, vector<double>(m_deadline + 1 , 0.0));
	m_scheduleVolt =  vector<vector<double> >(numOfTask, vector<double>(m_deadline + 1, 0.0));
	m_lastStepDuration = vector<vector<double> >(numOfTask, vector<double>(m_deadline + 1, 0.0));

	// for(int i = 0; i < numOfTask; i ++){
	// 	cout<<"Task "<<i<<" :"<<m_inputDuration[i]<<", "<<m_inputEnergy[i]<<endl;
	// }
	// memory the task energy and duration at each voltage;
	for (int i = 0; i < m_numOfTask; i++) {
		// cout<<"Task "<<i<<":";
		for (int k = 0; k < m_numOfVolt; k++) {
			m_taskDuration[i][k] = (int)ceil(m_inputDuration[i] * 10.0 / volSel[k]);
			m_taskEnergy[i][k] = m_inputEnergy[i] * volSel[k] * volSel[k];
			m_taskCurrent[i][k] = m_inputEnergy[i] / m_inputDuration[i] * volSel[k] * volSel[k] ;
			// cout<<m_taskDuration[i][k]<<","<<m_taskEnergy[i][k]<<endl;
		}
	}

	// System model related parameters
	m_initialEnergy = 20;
	m_solarPower = 3.0;

	// Initialize DP table with idle task
	m_scheduleWithIdleTask = vector<vector<dpTableEntry> >(m_numOfTask+m_numOfTask+1, vector<dpTableEntry>(m_deadline+1, dpTableEntry()));
	for (size_t i = 0; i < m_scheduleWithIdleTask.size(); i += 2) {
		for (size_t j = 0; j != m_scheduleWithIdleTask[i].size(); ++j) {
			// Idle tasks
			m_scheduleWithIdleTask[i][j].taskID = i/2;
			// Real tasks
			if (i+1 < m_scheduleWithIdleTask.size()) {
				m_scheduleWithIdleTask[i+1][j].taskID = i/2;
			}
		}
	}
}

void dynProg::taskTimelineWithIdle() {
	// initialize the first task -- idle task
	populateFirstIdleTask(m_scheduleWithIdleTask[0]);
	for (size_t i = 0; i < m_scheduleWithIdleTask.size()-1; i += 2) {
		// populating for the even number tasks -- real tasks
		populateRealTask(m_scheduleWithIdleTask[i], m_scheduleWithIdleTask[i+1]);
		// populating for the odd number tasks -- idle tasks
		populateIdleTask(m_scheduleWithIdleTask[i+1], m_scheduleWithIdleTask[i+2]);
	}
#ifdef DEBUG_VERBOSE
	dumpDPTable();
#endif
}

void dynProg::populateFirstIdleTask(vector<dpTableEntry> &firstIdleRow) {
	for (tableEntryIter iter = firstIdleRow.begin(); iter != firstIdleRow.end(); ++iter) {
		size_t len = (iter-firstIdleRow.begin());
		iter->totalEnergy = energyCalculator(m_solarPower, m_initialEnergy, len);
		iter->len = len;
	}
	return;
}

void dynProg::populateIdleTask(const vector<dpTableEntry> &lastRealRow, vector<dpTableEntry> &thisIdleRow) {

	double inputPower = m_solarPower;
	if (inputPower < 0.0)
		return;

	for (vector<dpTableEntry>::const_iterator iter = lastRealRow.begin(); iter != lastRealRow.end(); ++iter) {
		if (iter->totalEnergy < 0)
			continue;
		tableEntryIter iterIdleHead = thisIdleRow.begin()+(iter-lastRealRow.begin());
		for (tableEntryIter iterIdle = iterIdleHead; iterIdle != thisIdleRow.end(); ++iterIdle) {
			int taskDur = iterIdle - iterIdleHead;
#ifdef DEBUG_VERBOSE
			cout << "I am predicting idle task " << iterIdle->taskID << " from time " << iterIdleHead - thisIdleRow.begin() << " to " << iterIdle - thisIdleRow.begin() << "." << endl;
#endif
			double energy = energyCalculator(inputPower, iter->totalEnergy, taskDur);
			if (energy > iterIdle->totalEnergy) {
#ifdef DEBUG_VERBOSE
				cout << "Taskid: idle " << iterIdle->taskID << " entry " << iterIdle-thisIdleRow.begin() << " has been updated from "
					<< iterIdle->totalEnergy << " to " << energy << endl;;
#endif
				// Update the table entry
				iterIdle->setAllFields(energy, 1.0, 0.0, -1, iterIdle->taskID, taskDur, iterIdleHead-thisIdleRow.begin());
			}
{'fraction_non_alphanumeric': 0.1600721451921993, 'fraction_numerical': 0.013076316086123323, 'mean_word_length': 5.1228433402346445, 'pattern_counts': {'":': 2, '<': 215, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1265551', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3291, 'n_words': 672}