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

using namespace std;

void read_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops);
void write_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops);
void smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width);
void centered_smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width);

/////////////////////////////////////////////////////////////////////////////////////////////////

int main( int argc , char *argv[] )
{

  // input data

  if (argc<3){
    INFOS("!!! Error ... usage : main filename window_half_width smooth_filename");
    exit(0);
  }
  INFOS(argc);

  int window_half_width=atoi(argv[2]);

  string filename=argv[1];
  string smooth_filename=argv[3];
  
  INFOS(filename);
  INFOS("window_half_width="<<window_half_width);

  vector<int> tab_sizes;
  vector<double> tab_mflops;

  read_xy_file(filename,tab_sizes,tab_mflops);

  // smoothing

  vector<double> smooth_tab_mflops;

  //smooth_curve(tab_mflops,smooth_tab_mflops,window_half_width);
  centered_smooth_curve(tab_mflops,smooth_tab_mflops,window_half_width);

  // output result

  write_xy_file(smooth_filename,tab_sizes,smooth_tab_mflops);
  

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template<class VECTOR>
double weighted_mean(const VECTOR & data)
{

  double mean=0.0;
  
  for (int i=0 ; i<data.size() ; i++){

    mean+=data[i];

  }

  return mean/double(data.size()) ;

}    




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width){
  
  int window_width=2*window_half_width+1;

  int size=tab_mflops.size();

  vector<double> sample(window_width);
  
  for (int i=0 ; i < size ; i++){
    
    for ( int j=0 ; j < window_width ; j++ ){
      
      int shifted_index=i+j-window_half_width;
      if (shifted_index<0) shifted_index=0;
      if (shifted_index>size-1) shifted_index=size-1;
      sample[j]=tab_mflops[shifted_index];
      
    }

    smooth_tab_mflops.push_back(weighted_mean(sample));

  }

}

void centered_smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width){
  
  int max_window_width=2*window_half_width+1;

  int size=tab_mflops.size();

  
  for (int i=0 ; i < size ; i++){

    deque<double> sample;

    
    sample.push_back(tab_mflops[i]);

    for ( int j=1 ; j <= window_half_width ; j++ ){
      
      int before=i-j;
      int after=i+j;
      
      if ((before>=0)&&(after<size)) // inside of the vector
	{ 
	  sample.push_front(tab_mflops[before]);
	  sample.push_back(tab_mflops[after]);
	}
    }
    
    smooth_tab_mflops.push_back(weighted_mean(sample));
    
  }

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void write_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops){

  ofstream output_file (filename.c_str(),ios::out) ;
  
  for (int i=0 ; i < tab_sizes.size() ; i++)
    {
      output_file << tab_sizes[i] << " " <<  tab_mflops[i] << endl ;
    }
  
  output_file.close();

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void read_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops){

  ifstream input_file (filename.c_str(),ios::in) ;

  if (!input_file){
    INFOS("!!! Error opening "<<filename);
    exit(0);
  }
  
  int nb_point=0;
  int size=0;
  double mflops=0;

  while (input_file >> size >> mflops ){
    nb_point++;
    tab_sizes.push_back(size);
    tab_mflops.push_back(mflops);
  }
  SCRUTE(nb_point);

  input_file.close();
}