SkePU(integratedwithStarPU)  0.8.1
 All Classes Namespaces Files Functions Enumerations Friends Macros Groups Pages
data_collector.h
Go to the documentation of this file.
1 
5 #ifndef DATA_COLLECTOR_H
6 #define DATA_COLLECTOR_H
7 
8 #include <string>
9 #include <set>
10 #include <utility>
11 #include <fstream>
12 #include <iomanip>
13 
14 #include <iostream>
15 
16 namespace skepu
17 {
18 
19 enum DataExportFormat
20 {
21  GNUPLOT,
22  PREDICTION_FILE
23 };
24 
38 template <typename Tx, typename Ty>
40 {
41 
42 public:
43  DataCollector2D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY);
44 
45  void addData(Tx x, Ty y);
46  void clear();
47  void writeDataToFile(const std::string& filename = "", DataExportFormat format = GNUPLOT);
48 
49 private:
50  std::string dataSetName;
51  std::pair<std::string, std::string> axisNames;
52  std::multiset< std::pair<Tx, Ty> > dataSet;
53 
54  void writeGnuPlotFile(const std::string& filename);
55 
56 };
57 
65 template <typename Tx, typename Ty>
66 DataCollector2D<Tx, Ty>::DataCollector2D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY)
67 {
68  dataSetName = _dataSetName;
69  axisNames.first = _axisNameX;
70  axisNames.second = _axisNameY;
71 }
72 
79 template <typename Tx, typename Ty>
81 {
82  dataSet.insert(std::make_pair(x, y));
83 }
84 
88 template <typename Tx, typename Ty>
90 {
91  dataSet.clear();
92 }
93 
100 template <typename Tx, typename Ty>
101 void DataCollector2D<Tx, Ty>::writeDataToFile(const std::string& filename, DataExportFormat format)
102 {
103  std::string _filename;
104 
105  if(filename.empty())
106  {
107  _filename = dataSetName + ".dat";
108  }
109  else
110  {
111  _filename = filename;
112  }
113 
114  //Call right exporter
115  if(format == GNUPLOT)
116  {
117  writeGnuPlotFile(_filename);
118  }
119 }
120 
126 template <typename Tx, typename Ty>
127 void DataCollector2D<Tx, Ty>::writeGnuPlotFile(const std::string& filename)
128 {
129  std::ofstream file(filename.c_str());
130  int tabLength = axisNames.first.length()+20;
131  file<<std::left;
132  file<<std::fixed <<std::setprecision(5);
133  if(file.is_open())
134  {
135  //First add name and axis names as comments
136  file<<"# " <<dataSetName <<"\n";
137  file<<"# " <<std::setw(tabLength) <<axisNames.first <<std::setw(tabLength) <<axisNames.second <<"\n";
138 
139  //Add data in two columns
140  for(typename std::multiset< std::pair<Tx, Ty> >::iterator it = dataSet.begin(); it != dataSet.end(); ++it)
141  {
142  file<<" " <<std::setw(tabLength) <<it->first <<std::setw(tabLength) <<it->second <<"\n";
143  }
144 
145  file.close();
146  }
147 }
148 
149 }
150 
151 #endif
152 
A class that can be used to collect 2D data.
Definition: data_collector.h:39
void writeDataToFile(const std::string &filename="", DataExportFormat format=GNUPLOT)
Definition: data_collector.h:101
void addData(Tx x, Ty y)
Definition: data_collector.h:80
DataCollector2D(const std::string &_dataSetName, const std::string &_axisNameX, const std::string &_axisNameY)
Definition: data_collector.h:66
void clear()
Definition: data_collector.h:89