|
SkePU 0.7
|
00001 00005 #ifndef DATA_COLLECTOR_H 00006 #define DATA_COLLECTOR_H 00007 00008 #include <string> 00009 #include <set> 00010 #include <utility> 00011 #include <fstream> 00012 #include <iomanip> 00013 00014 #include <iostream> 00015 00016 namespace skepu 00017 { 00018 00019 enum DataExportFormat 00020 { 00021 GNUPLOT, 00022 PREDICTION_FILE 00023 }; 00024 00040 template <typename Tx, typename Ty> 00041 class DataCollector2D 00042 { 00043 00044 public: 00045 DataCollector2D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY); 00046 00047 void addData(Tx x, Ty y); 00048 void clear(); 00049 void writeDataToFile(const std::string& filename = "", DataExportFormat format = GNUPLOT); 00050 00051 private: 00052 std::string dataSetName; 00053 std::pair<std::string, std::string> axisNames; 00054 std::multiset< std::pair<Tx, Ty> > dataSet; 00055 00056 void writeGnuPlotFile(const std::string& filename); 00057 00058 }; 00059 00067 template <typename Tx, typename Ty> 00068 DataCollector2D<Tx, Ty>::DataCollector2D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY) 00069 { 00070 dataSetName = _dataSetName; 00071 axisNames.first = _axisNameX; 00072 axisNames.second = _axisNameY; 00073 } 00074 00081 template <typename Tx, typename Ty> 00082 void DataCollector2D<Tx, Ty>::addData(Tx x, Ty y) 00083 { 00084 dataSet.insert(std::make_pair(x, y)); 00085 } 00086 00090 template <typename Tx, typename Ty> 00091 void DataCollector2D<Tx, Ty>::clear() 00092 { 00093 dataSet.clear(); 00094 } 00095 00102 template <typename Tx, typename Ty> 00103 void DataCollector2D<Tx, Ty>::writeDataToFile(const std::string& filename, DataExportFormat format) 00104 { 00105 std::string _filename; 00106 00107 if(filename.empty()) 00108 { 00109 _filename = dataSetName + ".dat"; 00110 } 00111 else 00112 { 00113 _filename = filename; 00114 } 00115 00116 //Call right exporter 00117 if(format == GNUPLOT) 00118 { 00119 writeGnuPlotFile(_filename); 00120 } 00121 } 00122 00128 template <typename Tx, typename Ty> 00129 void DataCollector2D<Tx, Ty>::writeGnuPlotFile(const std::string& filename) 00130 { 00131 std::ofstream file(filename.c_str()); 00132 int tabLength = axisNames.first.length()+20; 00133 file<<std::left; 00134 file<<std::fixed <<std::setprecision(5); 00135 if(file.is_open()) 00136 { 00137 //First add name and axis names as comments 00138 file<<"# " <<dataSetName <<"\n"; 00139 file<<"# " <<std::setw(tabLength) <<axisNames.first <<std::setw(tabLength) <<axisNames.second <<"\n"; 00140 00141 //Add data in two columns 00142 for(typename std::multiset< std::pair<Tx, Ty> >::iterator it = dataSet.begin(); it != dataSet.end(); ++it) 00143 { 00144 file<<" " <<std::setw(tabLength) <<it->first <<std::setw(tabLength) <<it->second <<"\n"; 00145 } 00146 00147 file.close(); 00148 } 00149 } 00150 00151 } 00152 00153 #endif 00154
1.7.4