1//=====================================================
2// File   :  smooth.cxx
3// Author :  L. Plagne <laurent.plagne@edf.fr)>
4// Copyright (C) EDF R&D,  lun sep 30 14:23:15 CEST 2002
5//=====================================================
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19//
20#include "utilities.h"
21#include <vector>
22#include <deque>
23#include <string>
24#include <iostream>
25#include <fstream>
26#include "bench_parameter.hh"
27#include <set>
28
29using namespace std;
30
31void read_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops);
32void write_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops);
33void smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width);
34void centered_smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width);
35
36/////////////////////////////////////////////////////////////////////////////////////////////////
37
38int main( int argc , char *argv[] )
39{
40
41  // input data
42
43  if (argc<3){
44    INFOS("!!! Error ... usage : main filename window_half_width smooth_filename");
45    exit(0);
46  }
47  INFOS(argc);
48
49  int window_half_width=atoi(argv[2]);
50
51  string filename=argv[1];
52  string smooth_filename=argv[3];
53
54  INFOS(filename);
55  INFOS("window_half_width="<<window_half_width);
56
57  vector<int> tab_sizes;
58  vector<double> tab_mflops;
59
60  read_xy_file(filename,tab_sizes,tab_mflops);
61
62  // smoothing
63
64  vector<double> smooth_tab_mflops;
65
66  //smooth_curve(tab_mflops,smooth_tab_mflops,window_half_width);
67  centered_smooth_curve(tab_mflops,smooth_tab_mflops,window_half_width);
68
69  // output result
70
71  write_xy_file(smooth_filename,tab_sizes,smooth_tab_mflops);
72
73
74}
75
76///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77
78template<class VECTOR>
79double weighted_mean(const VECTOR & data)
80{
81
82  double mean=0.0;
83
84  for (int i=0 ; i<data.size() ; i++){
85
86    mean+=data[i];
87
88  }
89
90  return mean/double(data.size()) ;
91
92}
93
94
95
96
97///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98
99
100void smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width){
101
102  int window_width=2*window_half_width+1;
103
104  int size=tab_mflops.size();
105
106  vector<double> sample(window_width);
107
108  for (int i=0 ; i < size ; i++){
109
110    for ( int j=0 ; j < window_width ; j++ ){
111
112      int shifted_index=i+j-window_half_width;
113      if (shifted_index<0) shifted_index=0;
114      if (shifted_index>size-1) shifted_index=size-1;
115      sample[j]=tab_mflops[shifted_index];
116
117    }
118
119    smooth_tab_mflops.push_back(weighted_mean(sample));
120
121  }
122
123}
124
125void centered_smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width){
126
127  int max_window_width=2*window_half_width+1;
128
129  int size=tab_mflops.size();
130
131
132  for (int i=0 ; i < size ; i++){
133
134    deque<double> sample;
135
136
137    sample.push_back(tab_mflops[i]);
138
139    for ( int j=1 ; j <= window_half_width ; j++ ){
140
141      int before=i-j;
142      int after=i+j;
143
144      if ((before>=0)&&(after<size)) // inside of the vector
145	{
146	  sample.push_front(tab_mflops[before]);
147	  sample.push_back(tab_mflops[after]);
148	}
149    }
150
151    smooth_tab_mflops.push_back(weighted_mean(sample));
152
153  }
154
155}
156
157
158///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159
160void write_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops){
161
162  ofstream output_file (filename.c_str(),ios::out) ;
163
164  for (int i=0 ; i < tab_sizes.size() ; i++)
165    {
166      output_file << tab_sizes[i] << " " <<  tab_mflops[i] << endl ;
167    }
168
169  output_file.close();
170
171}
172
173
174///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
175
176void read_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops){
177
178  ifstream input_file (filename.c_str(),ios::in) ;
179
180  if (!input_file){
181    INFOS("!!! Error opening "<<filename);
182    exit(0);
183  }
184
185  int nb_point=0;
186  int size=0;
187  double mflops=0;
188
189  while (input_file >> size >> mflops ){
190    nb_point++;
191    tab_sizes.push_back(size);
192    tab_mflops.push_back(mflops);
193  }
194  SCRUTE(nb_point);
195
196  input_file.close();
197}
198
199