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