Add some string utils; rework `Benchmark`

Этот коммит содержится в:
Jean-Sébastien 2021-12-07 19:02:35 +01:00
родитель d922a0dae1
Коммит 7207370426
4 изменённых файлов: 42 добавлений и 28 удалений

Просмотреть файл

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

Просмотреть файл

@ -37,8 +37,38 @@ const DP MACHINE_EPS_SQ = pow(MACHINE_EPS, 2.0);
namespace ABACUS {
// Inexplicably missing string functions in standard library:
std::string replace(const std::string& str, const std::string& from, const std::string& to) {
std::string repl = str;
size_t start_pos = repl.find(from);
if(start_pos < std::string::npos)
repl.replace(start_pos, from.length(), to);
return repl;
}
std::string replace_all(const std::string& str, const std::string& from, const std::string& to) {
std::string repl = str;
if(from.empty())
return repl;
size_t start_pos = 0;
while((start_pos = repl.find(from, start_pos)) != std::string::npos) {
repl.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return repl;
}
// File checks:
inline unsigned int count_lines(std::string filename)
{
std::ifstream infile(filename);
return(std::count(std::istreambuf_iterator<char>(infile),
std::istreambuf_iterator<char>(), '\n'));
}
inline bool file_exists (const char* filename)
{
std::fstream file;

Просмотреть файл

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

Просмотреть файл

@ -23,19 +23,11 @@ using namespace ABACUS;
namespace ABACUS {
void Benchmark_RAW_File (const char ff_file[], char whichDSF)
void Benchmark_RAW_File (const string ff_file, char whichDSF)
{
// Check size of raw file:
struct stat statbuf;
string filename = ff_file;
stat (ff_file, &statbuf);
int filesize = statbuf.st_size;
// Determine the number of entries approximately
int entry_size = 2* sizeof(float) + 2*sizeof(int);
//const int MAXDATA = 50000000;
const int MAXDATA = filesize/entry_size + 10;
const int MAXDATA = count_lines(filename);
DP* omega = new DP[MAXDATA];
int* iK = new int[MAXDATA];
@ -45,7 +37,7 @@ namespace ABACUS {
string* label = new string[MAXDATA];
ifstream infile;
infile.open(ff_file);
infile.open(filename);
if (infile.fail()) ABACUSerror("The input file was not opened successfully in Benchmark_RAW_File. ");
@ -73,14 +65,10 @@ namespace ABACUS {
for (int i = 0; i < Ndata; ++i) sr_cont[i] = fabs(omega[i]) * ff[i] * ff[i];
}
stringstream outfilename;
string outfilename_string;
outfilename << ff_file << "_bmk";
outfilename_string = outfilename.str();
const char* outfilename_c_str = outfilename_string.c_str();
string outfilename = replace_all(filename, ".", "_") + ".bmk";
ofstream outfile1;
outfile1.open(outfilename_c_str);
outfile1.open(outfilename);
outfile1.precision(16);
for (int i = 0; i < Ndata; i++) {
@ -93,12 +81,8 @@ namespace ABACUS {
// Now the ordered one
QuickSort(sr_cont, index, 0, Ndata - 1);
outfilename << "_srt";
outfilename_string = outfilename.str();
const char* outfilename2_c_str = outfilename_string.c_str();
ofstream outfile2;
outfile2.open(outfilename2_c_str);
outfile2.open(outfilename + "_srt");
outfile2.precision(16);
for (int i = 0; i < Ndata; i++) {
@ -133,7 +117,8 @@ int main(int argc, char* argv[])
ABACUSerror("");
}
const char* rawfilename = argv[1];
// const char* rawfilename = argv[1];
string rawfilename = argv[1];
char whichDSF = *argv[2];
Benchmark_RAW_File (rawfilename, whichDSF);