BAPS/src/BAPSolver.cpp

00001 /******************************************************************
00002  *
00003  * Filename    : BAPSolver.cpp
00004  * Author      : David Ong Tat-Wee
00005  *
00006  * Version     : 1.01b
00007  * Date        : 29 May 98
00008  *
00009  * Description : Implements the BAP Solver class
00010  *
00011  * Reference   : nil
00012  *
00013  * Notes       : This class calls the BAP partitioning and packing
00014  *               algorithms.  It serves as an interface between the
00015  *               BAP Package class and the BAP algorithms.
00016  *
00017  * Changes     : nil
00018  *
00019  * Copyright   : Copyright (c) 1998
00020  *               All rights reserved by
00021  *               Resource Allocation and Scheduling Group
00022  *               Department of Information Systems and Computer Science
00023  *               National University of Singapore
00024  *
00025  ******************************************************************/
00026 
00027 
00028 #include "BAPSolver.h"
00029 
00030 using std::cout;
00031 using std::cerr;
00032 using std::endl;
00033 using std::ifstream;
00034 
00035 BAPSolver::BAPSolver(BAPPackage& aPackage, string aJob)
00036 : BAPBase(), mPackage(aPackage), mJob(aJob),
00037   mPartitioner("GP"), mPacker("BP")
00038 {
00039    ReadParameterFile();
00040 }
00041 
00042 BAPSolver::~BAPSolver()
00043 {
00044 }
00045 
00046 void  BAPSolver::Print(int aW, int aDetail) const
00047 {
00048    cout  << tab(aW) << "--== BAP Solver ==--" << endl
00049          << tab(aW) << "  ID   =         " << ID() << endl
00050          << tab(aW) << "  Name =         " << Name() << endl
00051          << endl;
00052 
00053    mPackage.Print(aW + INDENT, aDetail);
00054 }
00055 
00056 BAPPackage& BAPSolver::Package() const
00057 {
00058    return mPackage;
00059 }
00060 
00061 void BAPSolver::Solve()
00062 {
00063    if (mJob == "all")
00064    {
00065 cout << "running all" << endl;
00066 
00067       DoPartitioning();
00068       DoPacking();
00069    }
00070    else if (mJob == "part")
00071    {
00072 cout << "running partitioning" << endl;
00073 
00074       DoPartitioning();
00075    }
00076    else if (mJob == "pack")
00077    {
00078 cout << "running packing" << endl;
00079 
00080 //      mPackage.ReadSolution();
00081       DoPacking();
00082    }
00083    else
00084    {
00085       cerr << "BAPSolver: unknown job '" << mJob << "'" << endl;
00086    }
00087 }
00088 
00089 
00090 void BAPSolver::DoPartitioning()
00091 {
00092    BAPPartitioner* Partitioner;
00093 
00094    if (mPartitioner == "TV")
00095       Partitioner = new BAPTVPartitioner(mPackage);
00096    //else if(mPartitioner=="new partitioner")
00097    else
00098    {
00099       cerr << "Unknown partitioner: " << mPartitioner << endl;
00100       return;
00101    }
00102 
00103    Partitioner->Solve();
00104 
00105    delete Partitioner;
00106 }
00107 
00108 
00109 void BAPSolver::DoPacking()
00110 {
00111    BAPPacker*  Packer;
00112 
00113    if (mPacker == "BP")
00114       Packer = new BAPBPPacker(mPackage);
00115    else
00116    {
00117       cerr << "Unknown packer: " << mPacker << endl;
00118       return;
00119    }
00120 
00121    Packer->Solve();
00122 
00123    delete Packer;
00124 }
00125 
00126 
00127 void BAPSolver::ReadParameterFile()
00128 {
00129    string   paramFile = mPackage.ParamFilename();
00130    char     buf[255];
00131    string   token, mode = "nil";
00132    ifstream ParamFile(paramFile.c_str());
00133 
00134    if (!ParamFile)
00135    {
00136       cerr << "Cannot open parameter file: " << paramFile << endl;
00137       return;
00138    }
00139 
00140    while (!ParamFile.eof())
00141    {
00142       ParamFile.getline(buf,80);
00143 
00144 #ifdef _DEBUG
00145          cout << "tokenizing: " << buf << endl;
00146 #endif
00147 
00148       for (int i = 0; i < 80; i++)     // Convert carriage-return to space
00149          if (13 == (int) buf[i])
00150             buf[i] = ' ';
00151 
00152       if (buf[0] == ' ' || buf[0] == 0)
00153          continue;
00154 
00155       token = strtok(buf, " ");
00156 
00157 #ifdef _DEBUG
00158       cout << "token = " << token << endl;
00159 #endif
00160 
00161       if (token.length() == 0)         // empty line, ignore
00162          continue;
00163       if (token == "#")                // comment line, ignore
00164          continue; 
00165       if (token[0] == '_')             // keyword, change mode
00166          mode = token;
00167       else                             // process data based on mode
00168       {
00169          if (mode == "_PARTITIONING_ALGO")
00170          {
00171             mPartitioner = token;
00172          }
00173          else if (mode == "_PACKING_ALGO")
00174          {
00175             mPacker = token;
00176          }
00177       }
00178    }
00179 }
00180 

Generated on Tue Sep 9 15:40:10 2008 for BAP by  doxygen 1.5.3