You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.0 KiB

  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. using namespace std;
  29. void read_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops);
  30. void write_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops);
  31. void smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width);
  32. void centered_smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width);
  33. /////////////////////////////////////////////////////////////////////////////////////////////////
  34. int main( int argc , char *argv[] )
  35. {
  36. // input data
  37. if (argc<3){
  38. INFOS("!!! Error ... usage : main filename window_half_width smooth_filename");
  39. exit(0);
  40. }
  41. INFOS(argc);
  42. int window_half_width=atoi(argv[2]);
  43. string filename=argv[1];
  44. string smooth_filename=argv[3];
  45. INFOS(filename);
  46. INFOS("window_half_width="<<window_half_width);
  47. vector<int> tab_sizes;
  48. vector<double> tab_mflops;
  49. read_xy_file(filename,tab_sizes,tab_mflops);
  50. // smoothing
  51. vector<double> smooth_tab_mflops;
  52. //smooth_curve(tab_mflops,smooth_tab_mflops,window_half_width);
  53. centered_smooth_curve(tab_mflops,smooth_tab_mflops,window_half_width);
  54. // output result
  55. write_xy_file(smooth_filename,tab_sizes,smooth_tab_mflops);
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. template<class VECTOR>
  59. double weighted_mean(const VECTOR & data)
  60. {
  61. double mean=0.0;
  62. for (int i=0 ; i<data.size() ; i++){
  63. mean+=data[i];
  64. }
  65. return mean/double(data.size()) ;
  66. }
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. void smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width){
  69. int window_width=2*window_half_width+1;
  70. int size=tab_mflops.size();
  71. vector<double> sample(window_width);
  72. for (int i=0 ; i < size ; i++){
  73. for ( int j=0 ; j < window_width ; j++ ){
  74. int shifted_index=i+j-window_half_width;
  75. if (shifted_index<0) shifted_index=0;
  76. if (shifted_index>size-1) shifted_index=size-1;
  77. sample[j]=tab_mflops[shifted_index];
  78. }
  79. smooth_tab_mflops.push_back(weighted_mean(sample));
  80. }
  81. }
  82. void centered_smooth_curve(const vector<double> & tab_mflops, vector<double> & smooth_tab_mflops,int window_half_width){
  83. int max_window_width=2*window_half_width+1;
  84. int size=tab_mflops.size();
  85. for (int i=0 ; i < size ; i++){
  86. deque<double> sample;
  87. sample.push_back(tab_mflops[i]);
  88. for ( int j=1 ; j <= window_half_width ; j++ ){
  89. int before=i-j;
  90. int after=i+j;
  91. if ((before>=0)&&(after<size)) // inside of the vector
  92. {
  93. sample.push_front(tab_mflops[before]);
  94. sample.push_back(tab_mflops[after]);
  95. }
  96. }
  97. smooth_tab_mflops.push_back(weighted_mean(sample));
  98. }
  99. }
  100. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  101. void write_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops){
  102. ofstream output_file (filename.c_str(),ios::out) ;
  103. for (int i=0 ; i < tab_sizes.size() ; i++)
  104. {
  105. output_file << tab_sizes[i] << " " << tab_mflops[i] << endl ;
  106. }
  107. output_file.close();
  108. }
  109. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  110. void read_xy_file(const string & filename, vector<int> & tab_sizes, vector<double> & tab_mflops){
  111. ifstream input_file (filename.c_str(),ios::in) ;
  112. if (!input_file){
  113. INFOS("!!! Error opening "<<filename);
  114. exit(0);
  115. }
  116. int nb_point=0;
  117. int size=0;
  118. double mflops=0;
  119. while (input_file >> size >> mflops ){
  120. nb_point++;
  121. tab_sizes.push_back(size);
  122. tab_mflops.push_back(mflops);
  123. }
  124. SCRUTE(nb_point);
  125. input_file.close();
  126. }