Add some string utils; rework `Benchmark`
This commit is contained in:
parent
d922a0dae1
commit
7207370426
|
@ -22,6 +22,7 @@ const char ABACUS_VERSION[20] = "ABACUS_0a";
|
||||||
// Standard includes
|
// Standard includes
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <complex> // for complex number algebra
|
#include <complex> // for complex number algebra
|
||||||
|
#include <algorithm> // for count
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
|
@ -37,8 +37,38 @@ const DP MACHINE_EPS_SQ = pow(MACHINE_EPS, 2.0);
|
||||||
|
|
||||||
namespace ABACUS {
|
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:
|
// 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)
|
inline bool file_exists (const char* filename)
|
||||||
{
|
{
|
||||||
std::fstream file;
|
std::fstream file;
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Plots benchmark files produced from Benchmark_RAW_File.
|
Plots benchmark files produced from Benchmark_RAW_File.
|
||||||
|
|
||||||
Usage: python plot_benchmarks.py [benchmark file name].
|
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
|
import matplotlib.pyplot as plt
|
||||||
|
|
|
@ -23,19 +23,11 @@ using namespace ABACUS;
|
||||||
|
|
||||||
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:
|
string filename = ff_file;
|
||||||
struct stat statbuf;
|
|
||||||
|
|
||||||
stat (ff_file, &statbuf);
|
const int MAXDATA = count_lines(filename);
|
||||||
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;
|
|
||||||
|
|
||||||
DP* omega = new DP[MAXDATA];
|
DP* omega = new DP[MAXDATA];
|
||||||
int* iK = new int[MAXDATA];
|
int* iK = new int[MAXDATA];
|
||||||
|
@ -45,7 +37,7 @@ namespace ABACUS {
|
||||||
string* label = new string[MAXDATA];
|
string* label = new string[MAXDATA];
|
||||||
|
|
||||||
ifstream infile;
|
ifstream infile;
|
||||||
infile.open(ff_file);
|
infile.open(filename);
|
||||||
|
|
||||||
if (infile.fail()) ABACUSerror("The input file was not opened successfully in Benchmark_RAW_File. ");
|
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];
|
for (int i = 0; i < Ndata; ++i) sr_cont[i] = fabs(omega[i]) * ff[i] * ff[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
stringstream outfilename;
|
string outfilename = replace_all(filename, ".", "_") + ".bmk";
|
||||||
string outfilename_string;
|
|
||||||
outfilename << ff_file << "_bmk";
|
|
||||||
outfilename_string = outfilename.str();
|
|
||||||
const char* outfilename_c_str = outfilename_string.c_str();
|
|
||||||
|
|
||||||
ofstream outfile1;
|
ofstream outfile1;
|
||||||
outfile1.open(outfilename_c_str);
|
outfile1.open(outfilename);
|
||||||
outfile1.precision(16);
|
outfile1.precision(16);
|
||||||
|
|
||||||
for (int i = 0; i < Ndata; i++) {
|
for (int i = 0; i < Ndata; i++) {
|
||||||
|
@ -93,12 +81,8 @@ namespace ABACUS {
|
||||||
// Now the ordered one
|
// Now the ordered one
|
||||||
QuickSort(sr_cont, index, 0, Ndata - 1);
|
QuickSort(sr_cont, index, 0, Ndata - 1);
|
||||||
|
|
||||||
outfilename << "_srt";
|
|
||||||
outfilename_string = outfilename.str();
|
|
||||||
const char* outfilename2_c_str = outfilename_string.c_str();
|
|
||||||
|
|
||||||
ofstream outfile2;
|
ofstream outfile2;
|
||||||
outfile2.open(outfilename2_c_str);
|
outfile2.open(outfilename + "_srt");
|
||||||
outfile2.precision(16);
|
outfile2.precision(16);
|
||||||
|
|
||||||
for (int i = 0; i < Ndata; i++) {
|
for (int i = 0; i < Ndata; i++) {
|
||||||
|
@ -133,7 +117,8 @@ int main(int argc, char* argv[])
|
||||||
ABACUSerror("");
|
ABACUSerror("");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* rawfilename = argv[1];
|
// const char* rawfilename = argv[1];
|
||||||
|
string rawfilename = argv[1];
|
||||||
char whichDSF = *argv[2];
|
char whichDSF = *argv[2];
|
||||||
|
|
||||||
Benchmark_RAW_File (rawfilename, whichDSF);
|
Benchmark_RAW_File (rawfilename, whichDSF);
|
||||||
|
|
Loading…
Reference in New Issue