Hybrid Query Processing Engine for Coprocessing in Database Systems
HyPE
offline_algorithm.cpp
Go to the documentation of this file.
00001 /***********************************************************************************************************
00002 Copyright (c) 2012, Sebastian Breß, Otto-von-Guericke University of Magdeburg, Germany. All rights reserved.
00003 
00004 This program and accompanying materials are made available under the terms of the 
00005 GNU LESSER GENERAL PUBLIC LICENSE - Version 3, http://www.gnu.org/licenses/lgpl-3.0.txt
00006 ***********************************************************************************************************/
00007 #include <core/offline_algorithm.hpp>
00008 #include <core/plotscriptgenerator.hpp>
00009 #include <core/operation.hpp>
00010 
00011 #include <boost/circular_buffer.hpp>
00012 #include <boost/tokenizer.hpp>
00013 #include <boost/filesystem.hpp>
00014 #include <boost/lexical_cast.hpp>
00015 
00016 #include <core/scheduler.hpp>
00017 
00018 namespace hype{
00019    namespace core{
00020 
00021       const std::string& Offline_Algorithm::getAlgorithmName() const throw(){
00022          return name_;
00023       }
00024 
00025       const std::string& Offline_Algorithm::getOperationName() const throw(){
00026          return operation_name_;
00027       }
00028 
00029       DeviceSpecification Offline_Algorithm::getDeviceSpecification() const throw(){
00030          return device_;
00031       }
00032 
00033       unsigned int Offline_Algorithm::getNumberOfMeasurementPairs() const throw(){
00034          return offline_mesurement_pairs_.size();
00035       }
00036 
00037       Offline_Algorithm::Offline_Algorithm(DeviceSpecification device,std::string algorithm_name, std::string opname, std::string filepath) 
00038          : offline_mesurement_pairs_(), device_(device), name_(algorithm_name), operation_name_(opname), current_mesurementpair_index_(0),filepath_(filepath) {
00039 
00040          if(!quiet) std::cout << "Loading data from file: '" << filepath << "'..." << std::endl;
00041          loadMeasurementpairsfromFile(filepath);
00042 
00043          if(!quiet && verbose && debug) printstoredMeasurementpairs();
00044             
00045       }
00046 
00047       core::MeasurementPair Offline_Algorithm::getNext(){
00048          if(offline_mesurement_pairs_.size()>current_mesurementpair_index_)
00049             return offline_mesurement_pairs_[current_mesurementpair_index_++];
00050          else
00051             return core::MeasurementPair(); //null sample, indicator that all samples were processed
00052       }
00053 
00054       bool Offline_Algorithm::hasNext(){
00055          return (offline_mesurement_pairs_.size()>current_mesurementpair_index_);
00056       }
00057 
00058       void Offline_Algorithm::reset(){
00059          current_mesurementpair_index_=0;
00060       }
00061 
00062       //the first line of a file determines, how many feature values are expected: for n values, the first n-1 values are considered feature Values and the remaining 1 value is considered the measured execution time
00063       void Offline_Algorithm::loadMeasurementpairsfromFile(std::string filepath){
00064 
00065          std::ifstream fin(filepath.c_str());  //"Makefile");
00066          std::string buffer;
00067 
00068          if(!quiet && verbose && debug) std::cout << "Hier der Inhalt der Datei:\n";
00069 
00070          unsigned int samplecounter=0;
00071          unsigned int number_of_feature_values=0;
00072 
00073          while (fin.good()) {
00074             getline(fin,buffer,'\n');
00075             if(buffer=="") break;
00076 
00077             if(!quiet && verbose && debug) std::cout << samplecounter << " " <<buffer<< std::endl;
00078 
00079             //std::string s = buffer;
00080             std::string str = ";;Hello|world||-foo--bar;yow;baz|";
00081             str = buffer; //s;
00082 
00083             //std::string xvalue=""; 
00084             //std::string yvalue=""; 
00085 
00086             typedef boost::tokenizer<boost::char_separator<char> > 
00087                tokenizer;
00088             boost::char_separator<char> sep("\t");
00089             tokenizer tokens(str, sep);
00090 
00091             if(samplecounter==0){
00092 
00093                for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
00094                   number_of_feature_values++;
00095 
00096                number_of_feature_values--; //last value is for measured value
00097             }
00098 
00099             unsigned int tokencounter=0;
00100             Tuple t;
00101             MeasuredTime time;
00102             for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter){
00103                if(tokencounter<number_of_feature_values){
00104                   double d = boost::lexical_cast<double>(*tok_iter);
00105                   t.push_back(d);
00106                }else{
00107                   time=MeasuredTime(boost::lexical_cast<double>(*tok_iter));
00108                }
00109                tokencounter++;
00110             }
00111             assert(number_of_feature_values==t.size());
00112             offline_mesurement_pairs_.push_back(core::MeasurementPair(t,time,EstimatedTime(0)));
00113             samplecounter++;
00114          }
00115          //std::cout << "\nEnde der Ausgabe\n";
00116          fin.close();
00117 
00118 
00119       }
00120 
00121 
00122       void Offline_Algorithm::storeInFile(const std::string& file_name){
00123          std::fstream file(file_name.c_str(),std::ios_base::out | std::ios_base::trunc);
00124          for(unsigned int i=0;i<offline_mesurement_pairs_.size();i++){
00125             file << offline_mesurement_pairs_[i].toPlainString() << std::endl; //.datasize << "\t" << offline_mesurement_pairs_[i].measured_execution_time << std::endl;
00126             //std::cout << offline_mesurement_pairs_[i].datasize << " ";   
00127          }
00128          //std::cout << std::endl;  
00129       }
00130 
00131       void Offline_Algorithm::printstoredMeasurementpairs(){
00132 
00133          for(unsigned int i=0;i<offline_mesurement_pairs_.size();i++){
00134             std::cout << offline_mesurement_pairs_[i] << std::endl; //.datasize << "\t" << offline_mesurement_pairs_[i].measured_execution_time << std::endl;
00135             //std::cout << offline_mesurement_pairs_[i].datasize << " ";   
00136          }
00137          //std::cout << std::endl;  
00138       }
00139 
00140       std::vector<Offline_Algorithm> Offline_Algorithm::randomize_dataset_of_offline_algorithms(std::vector<Offline_Algorithm> offline_algorithms){
00141 
00142          std::vector<Offline_Algorithm> tmp_algorithms(offline_algorithms); //copy vector
00143          std::vector<Offline_Algorithm> result_algorithms(offline_algorithms);
00144          //std::vector<Offline_Algorithm> result_algorithms(offline_algorithms.size()); //create vector as big as offline_algorithms (objects are default constructed)
00145 
00146          for(unsigned int j=0;j<result_algorithms.size();j++){
00147 
00148             result_algorithms[j].offline_mesurement_pairs_.clear(); //delete samples, we want a clean vector to put the randomized sampels in
00149          }
00150 
00151          unsigned long long number_of_input_measurement_values = offline_algorithms[0].offline_mesurement_pairs_.size();
00152 
00153          for(unsigned int i=0;i<number_of_input_measurement_values;i++){
00154             unsigned int random_index = rand()%tmp_algorithms[0].offline_mesurement_pairs_.size();
00155             if(!quiet && verbose && debug) std::cout << "random index: " << random_index << std::endl;
00156             for(unsigned int j=0;j<tmp_algorithms.size();j++){
00157 
00158                core::MeasurementPair pair = tmp_algorithms[j].offline_mesurement_pairs_[random_index];
00159                result_algorithms[j].offline_mesurement_pairs_.push_back(pair);
00160                tmp_algorithms[j].offline_mesurement_pairs_.erase(tmp_algorithms[j].offline_mesurement_pairs_.begin()+random_index); //random_index
00161             }
00162          }
00163 
00164          return result_algorithms;
00165       }
00166 
00167    }; //end namespace core
00168 }; //end namespace hype
00169 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines