www.pudn.com > ghs1.2.rar > mtal.cpp


/*
 * Copyright (c) 2000-2003 Illinois Institute of Technology.
 *                         All rights reserved.
 *
 * This file is part of the GHS software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * GHS source distribution.
 *
 * Released Date: August 18 2005
 * This is the GHS I release by the
 *   SCS Group,
 *   http://www.cs.iit.edu/~scs
 *   Department of Computer Science,
 *   Illinois Institute of Technology.
 *
 * This release has been tested under SUN OS 5.9.
 *
 * Supervisor
 * ----------
 * Illiniois Institute of Technology:
 * -Dr. Xian-He Sun
 *
 * Author(s)
 * -----------------
 * Illinois Institute of Technology:
 * -Ming Wu
 * -Xian-He Sun
 *
 */

#include
#include
#include
#include
#include
#include
#include
#include "ghs.h"

using namespace std;

class TaskAllocator
{
  private:
    double maxJobWorkload;

    vector utilPara, servPara, servtmp, arriPara, servSTDPara, workPara, speedPara;

    vector machName;
    vector taskName;

	// maptab will be used as a two-dimentional array
    vector maptab;

    vector currworkload;
    int wsNum;
    int jobNum;

    // Grid Jobs workload array
    vector gJobWorkload;
    vector lastItemIndex;

  public:
    TaskAllocator();
	void PartitionWorkload();
    void GetResourcePara(char *configFile);
	void GetTaskPara(char *configFile);
    void OutputAllocation(char *outputFile);
};

TaskAllocator::TaskAllocator():maxJobWorkload(2000)
{
  //maxJobWorkload = 2000;
}

/* task partition for a group of independ tasks
 * each task has its own workload and can not be further partitioned
 * we use the min-min partition method
 */

void 
TaskAllocator::PartitionWorkload()
{
  int bestJob, bestMachine, tmpMachine;
  vector jobtag;
  int firstJobCond;
  int firstMachCond;

  // the sum of (1-utilization)* speed
  double globalBestTime, tmpBestTime, finishTime;

  int i;
  for (i = 0; i < jobNum; i++) 
  {
    jobtag.push_back(0);
  }

  for (i = 0; i < wsNum; i++) 
  {
    lastItemIndex.push_back(0);
    currworkload.push_back(0);
  }

  int j;

  for (i = 0; i < wsNum; i++) 
  {
    for (j = 0; j < jobNum; j++)
    {
	  maptab.push_back(0);
    }
  }

  int k;
  
  for (i = 0; i < jobNum; i++) 
  {
    firstJobCond = 0;

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

      if ( jobtag[k] == 0) 
      {
			  
        firstMachCond = 0;

        for (j = 0; j < wsNum; j++)
        {
          //cout << gJobWorkload[k] << currworkload[j] << arriPara[j] << servPara[j] << speedPara[j] << endl;
          finishTime = (gJobWorkload[k] + currworkload[j]) / ((1 - arriPara[j] * servPara[j]) * speedPara[j]);
					
          if ( firstJobCond == 0) 
          {
            tmpBestTime = finishTime;
            globalBestTime = tmpBestTime;
            bestJob = k;
            bestMachine = j;
            firstJobCond = 1;
          }

          if (firstMachCond == 0) 
          {
            tmpBestTime = finishTime;
            tmpMachine = j;
            firstMachCond = 1;
          }

          if (tmpBestTime > finishTime) 
          {
            tmpBestTime = finishTime;
            tmpMachine = j;
          }
        } // end for
      } // end if

      if (globalBestTime > tmpBestTime) 
      {
        globalBestTime = tmpBestTime;
        bestJob = k;
        bestMachine = tmpMachine;
      }
    } // end for

    jobtag[bestJob] = 1;
    currworkload[bestMachine] += gJobWorkload[bestJob];
    maptab[bestMachine*jobNum + lastItemIndex[bestMachine]] = bestJob;
    lastItemIndex[bestMachine]++;
  } // end for

  for (i = 0; i < wsNum; i++) 
  {
    workPara.push_back(currworkload[i]);
  }

}


/*  read resource parameters
 *	resource name, utilization, arrival rate, servicestd
 */
void 
TaskAllocator::GetResourcePara(char *configFile) 
{

  fstream resourceFile;
  resourceFile.open(configFile, fstream::in);

  if(!resourceFile.is_open()) {
	cout << "Error in open the resource inputfile " <> tmpName >> tmpUtil >> tmpArri >> tmpServ >> tmpSpeed;
    machName.push_back(tmpName);
    utilPara.push_back(tmpUtil);
    arriPara.push_back(tmpArri);
    servPara.push_back(tmpUtil/tmpArri);
    servSTDPara.push_back(tmpServ);
    speedPara.push_back(tmpSpeed);
    i++;
  }

  if (i==MAX_WS_NUM)
  {
    cout << "the machine number is beyond the maximum, we will not take the extra resources into account" << endl;
  }
  wsNum = i;
  resourceFile.close();

}

/*  read task parameters
 *	task name, task workload
 */

void 
TaskAllocator::GetTaskPara(char *configFile) 
{
  fstream inFile;
  inFile.open(configFile, fstream::in);
  if(!inFile.is_open()) {
	cout << "Error in open the task inputfile " <> tmpName >> tmpWork;
    taskName.push_back(tmpName);
    gJobWorkload.push_back(tmpWork);
    i++;
  }

  if (i==MAX_GJOB_NUM)
  {
    cout << "the task number is beyond the maximum, we will not take the extra tasks into account" << endl;
  }

  jobNum = i;
  inFile.close();

}

/* write the task allocation result into a result file
 * which name is provided by argv[3]
 */

void 
TaskAllocator::OutputAllocation(char *outputFile)
{
  fstream oFile;

  oFile.open(outputFile, fstream::out);

  if(!oFile.is_open()) {
	cout << "Error in open output file" << endl;
	exit(1);
  }

  for (int i = 0; i < wsNum; i++) {
	oFile << machName[i] << " : " << workPara[i] << endl;
    for (int j = 0; j < lastItemIndex[i]; j++) {
      oFile << taskName[maptab[i*jobNum + j]] <