Browse Source

Add some string utils; rework `Benchmark`

master
Jean-Sébastien 2 years ago
parent
commit
7207370426
4 changed files with 42 additions and 28 deletions
  1. 1
    0
      include/ABACUS.h
  2. 30
    0
      include/ABACUS_Utils.h
  3. 2
    4
      scripts/plot_benchmarks.py
  4. 9
    24
      src/EXECS/Benchmark_RAW_File.cc

+ 1
- 0
include/ABACUS.h View File

@@ -22,6 +22,7 @@ const char ABACUS_VERSION[20] = "ABACUS_0a";
22 22
 // Standard includes
23 23
 #include <cmath>
24 24
 #include <complex>  // for complex number algebra
25
+#include <algorithm>  // for count
25 26
 #include <string>
26 27
 
27 28
 #include <iostream>

+ 30
- 0
include/ABACUS_Utils.h View File

@@ -37,8 +37,38 @@ const DP MACHINE_EPS_SQ = pow(MACHINE_EPS, 2.0);
37 37
 
38 38
 namespace ABACUS {
39 39
 
40
+  // Inexplicably missing string functions in standard library:
41
+
42
+  std::string replace(const std::string& str, const std::string& from, const std::string& to) {
43
+    std::string repl = str;
44
+    size_t start_pos = repl.find(from);
45
+    if(start_pos < std::string::npos)
46
+      repl.replace(start_pos, from.length(), to);
47
+    return repl;
48
+  }
49
+
50
+  std::string replace_all(const std::string& str, const std::string& from, const std::string& to) {
51
+    std::string repl = str;
52
+    if(from.empty())
53
+      return repl;
54
+    size_t start_pos = 0;
55
+    while((start_pos = repl.find(from, start_pos)) != std::string::npos) {
56
+      repl.replace(start_pos, from.length(), to);
57
+      start_pos += to.length();
58
+    }
59
+    return repl;
60
+  }
61
+
62
+
40 63
   // File checks:
41 64
 
65
+  inline unsigned int count_lines(std::string filename)
66
+  {
67
+    std::ifstream infile(filename);
68
+    return(std::count(std::istreambuf_iterator<char>(infile),
69
+		      std::istreambuf_iterator<char>(), '\n'));
70
+  }
71
+
42 72
   inline bool file_exists (const char* filename)
43 73
   {
44 74
     std::fstream file;

+ 2
- 4
scripts/plot_benchmarks.py View File

@@ -1,11 +1,9 @@
1
-#!/usr/bin/python
2
-
3 1
 """
4 2
 Plots benchmark files produced from Benchmark_RAW_File.
5 3
 
6 4
 Usage: python plot_benchmarks.py [benchmark file name].
7 5
 
8
-Example: python plot_benchmarks.py LiebLin_Rho_Rho_c_4_L_64_N_64_64_0__iK_32.raw_bmk
6
+Example: python plot_benchmarks.py LiebLin_Rho_Rho_c_4_L_64_N_64_64_0__iK_32_raw.bmk
9 7
 """
10 8
 
11 9
 import matplotlib.pyplot as plt
@@ -18,7 +16,7 @@ index, srcont = np.loadtxt(filename, usecols=(0,1), unpack=True)
18 16
 index_srt, srcont_srt = np.loadtxt(filename + '_srt', usecols=(0, 1), unpack=True)
19 17
 
20 18
 plt.semilogy(index, srcont, '.', markersize=1, label='Realized order')
21
-plt.semilogy(index, srcont_srt, '.', markersize=1, label='Ideal order') # careful: index, not index_srt 
19
+plt.semilogy(index, srcont_srt, '.', markersize=1, label='Ideal order') # careful: index, not index_srt
22 20
 
23 21
 plt.xlabel('Order')
24 22
 plt.ylabel('ln(ME)')

+ 9
- 24
src/EXECS/Benchmark_RAW_File.cc View File

@@ -23,19 +23,11 @@ using namespace ABACUS;
23 23
 
24 24
 namespace ABACUS {
25 25
 
26
-  void Benchmark_RAW_File (const char ff_file[], char whichDSF)
26
+  void Benchmark_RAW_File (const string ff_file, char whichDSF)
27 27
   {
28
-    // Check size of raw file:
29
-    struct stat statbuf;
28
+    string filename = ff_file;
30 29
 
31
-    stat (ff_file, &statbuf);
32
-    int filesize = statbuf.st_size;
33
-
34
-    // Determine the number of entries approximately
35
-    int entry_size = 2* sizeof(float) + 2*sizeof(int);
36
-
37
-    //const int MAXDATA = 50000000;
38
-    const int MAXDATA = filesize/entry_size + 10;
30
+    const int MAXDATA = count_lines(filename);
39 31
 
40 32
     DP* omega = new DP[MAXDATA];
41 33
     int* iK = new int[MAXDATA];
@@ -45,7 +37,7 @@ namespace ABACUS {
45 37
     string* label = new string[MAXDATA];
46 38
 
47 39
     ifstream infile;
48
-    infile.open(ff_file);
40
+    infile.open(filename);
49 41
 
50 42
     if (infile.fail()) ABACUSerror("The input file was not opened successfully in Benchmark_RAW_File. ");
51 43
 
@@ -73,14 +65,10 @@ namespace ABACUS {
73 65
       for (int i = 0; i < Ndata; ++i) sr_cont[i] = fabs(omega[i]) * ff[i] * ff[i];
74 66
     }
75 67
 
76
-    stringstream outfilename;
77
-    string outfilename_string;
78
-    outfilename << ff_file << "_bmk";
79
-    outfilename_string = outfilename.str();
80
-    const char* outfilename_c_str = outfilename_string.c_str();
68
+    string outfilename = replace_all(filename, ".", "_") + ".bmk";
81 69
 
82 70
     ofstream outfile1;
83
-    outfile1.open(outfilename_c_str);
71
+    outfile1.open(outfilename);
84 72
     outfile1.precision(16);
85 73
 
86 74
     for (int i = 0; i < Ndata; i++) {
@@ -93,12 +81,8 @@ namespace ABACUS {
93 81
     // Now the ordered one
94 82
     QuickSort(sr_cont, index, 0, Ndata - 1);
95 83
 
96
-    outfilename << "_srt";
97
-    outfilename_string = outfilename.str();
98
-    const char* outfilename2_c_str = outfilename_string.c_str();
99
-
100 84
     ofstream outfile2;
101
-    outfile2.open(outfilename2_c_str);
85
+    outfile2.open(outfilename + "_srt");
102 86
     outfile2.precision(16);
103 87
 
104 88
     for (int i = 0; i < Ndata; i++) {
@@ -133,7 +117,8 @@ int main(int argc, char* argv[])
133 117
     ABACUSerror("");
134 118
   }
135 119
 
136
-  const char* rawfilename = argv[1];
120
+  // const char* rawfilename = argv[1];
121
+  string rawfilename = argv[1];
137 122
   char whichDSF = *argv[2];
138 123
 
139 124
   Benchmark_RAW_File (rawfilename, whichDSF);

Loading…
Cancel
Save