?? noxim_explorer.cpp.svn-base
字號:
eparams.repetitions = DEF_REPETITIONS; for (uint i=0; i<explorer_params.size(); i++) { istringstream iss(explorer_params[i]); string label; iss >> label; if (label == SIMULATOR_LABEL) iss >> eparams.simulator; else if (label == REPETITIONS_LABEL) iss >> eparams.repetitions; else if (label == TMP_DIR_LABEL) iss >> eparams.tmp_dir; else { error_msg = "Invalid explorer option '" + label + "'"; return false; } } return true;}//---------------------------------------------------------------------------bool PrintHeader(const string& fname, const TExplorerParams& eparams, const string& def_cmd_line, const string& conf_cmd_line, ofstream& fout, string& error_msg){ fout.open(fname.c_str(), ios::out); if (!fout) { error_msg = "Cannot create " + fname; return false; } fout << "% fname: " << fname << endl << "% " << eparams.simulator << " " << conf_cmd_line << " " << def_cmd_line << endl << endl; return true;}//---------------------------------------------------------------------------bool PrintMatlabFunction(const string& mfname, ofstream& fout, string& error_msg){ fout << "function [max_pir, max_throughput, min_delay] = " << mfname << "(symbol)" << endl << endl; return true;}//---------------------------------------------------------------------------bool ReadResults(const string& fname, TSimulationResults& sres, string& error_msg){ ifstream fin(fname.c_str(), ios::in); if (!fin) { error_msg = "Cannot read " + fname; return false; } int nread = 0; while (!fin.eof()) { string line; getline(fin, line); uint pos; pos = line.find(RPACKETS_LABEL); if (pos != string::npos) { nread++; istringstream iss(line.substr(pos + string(RPACKETS_LABEL).size())); iss >> sres.rpackets; continue; } pos = line.find(RFLITS_LABEL); if (pos != string::npos) { nread++; istringstream iss(line.substr(pos + string(RFLITS_LABEL).size())); iss >> sres.rflits; continue; } pos = line.find(AVG_DELAY_LABEL); if (pos != string::npos) { nread++; istringstream iss(line.substr(pos + string(AVG_DELAY_LABEL).size())); iss >> sres.avg_delay; continue; } pos = line.find(AVG_THROUGHPUT_LABEL); if (pos != string::npos) { nread++; istringstream iss(line.substr(pos + string(AVG_THROUGHPUT_LABEL).size())); iss >> sres.avg_throughput; continue; } pos = line.find(THROUGHPUT_LABEL); if (pos != string::npos) { nread++; istringstream iss(line.substr(pos + string(THROUGHPUT_LABEL).size())); iss >> sres.throughput; continue; } pos = line.find(MAX_DELAY_LABEL); if (pos != string::npos) { nread++; istringstream iss(line.substr(pos + string(MAX_DELAY_LABEL).size())); iss >> sres.max_delay; continue; } pos = line.find(TOTAL_ENERGY_LABEL); if (pos != string::npos) { nread++; istringstream iss(line.substr(pos + string(TOTAL_ENERGY_LABEL).size())); iss >> sres.total_energy; continue; } } if (nread != 7) { error_msg = "Output file " + fname + " corrupted"; return false; } return true;}//---------------------------------------------------------------------------bool RunSimulation(const string& cmd_base, const string& tmp_dir, TSimulationResults& sres, string& error_msg){ string tmp_fname = tmp_dir + TMP_FILE_NAME; string cmd = cmd_base + " >& " + tmp_fname; cout << cmd << endl; system(cmd.c_str()); if (!ReadResults(tmp_fname, sres, error_msg)) return false; string rm_cmd = string("rm -f ") + tmp_fname; system(rm_cmd.c_str()); return true;}//---------------------------------------------------------------------------bool RunSimulations(double start_time, pair<uint,uint>& sim_counter, const string& cmd, const string& tmp_dir, const int repetitions, const TConfiguration& aggr_conf, ofstream& fout, string& error_msg){ int h, m, s; for (int i=0; i<repetitions; i++) { cout << "# simulation " << (++sim_counter.first) << " of " << sim_counter.second; if (i != 0) cout << ", estimated time to finish " << h << "h " << m << "m " << s << "s"; cout << endl; TSimulationResults sres; if (!RunSimulation(cmd, tmp_dir, sres, error_msg)) return false; double current_time = GetCurrentTime(); TimeToFinish(current_time-start_time, sim_counter.first, sim_counter.second, h, m, s); // Print aggragated parameters fout << " "; for (uint i=0; i<aggr_conf.size(); i++) fout << setw(MATRIX_COLUMN_WIDTH) << aggr_conf[i].second; // Print results; fout << setw(MATRIX_COLUMN_WIDTH) << sres.avg_delay << setw(MATRIX_COLUMN_WIDTH) << sres.throughput << setw(MATRIX_COLUMN_WIDTH) << sres.max_delay << setw(MATRIX_COLUMN_WIDTH) << sres.total_energy << setw(MATRIX_COLUMN_WIDTH) << sres.rpackets << setw(MATRIX_COLUMN_WIDTH) << sres.rflits << endl; } return true;}//---------------------------------------------------------------------------bool PrintMatlabVariableBegin(const TParametersSpace& aggragated_params_space, ofstream& fout, string& error_msg){ fout << MATLAB_VAR_NAME << " = [" << endl; fout << "% "; for (TParametersSpace::const_iterator i=aggragated_params_space.begin(); i!=aggragated_params_space.end(); i++) fout << setw(MATRIX_COLUMN_WIDTH) << i->first; fout << setw(MATRIX_COLUMN_WIDTH) << "avg_delay" << setw(MATRIX_COLUMN_WIDTH) << "throughput" << setw(MATRIX_COLUMN_WIDTH) << "max_delay" << setw(MATRIX_COLUMN_WIDTH) << "total_energy" << setw(MATRIX_COLUMN_WIDTH) << "rpackets" << setw(MATRIX_COLUMN_WIDTH) << "rflits"; fout << endl; return true;}//---------------------------------------------------------------------------bool GenMatlabCode(const string& var_name, const int fig_no, const int repetitions, const int column, ofstream& fout, string& error_msg){ fout << var_name << " = [];" << endl << "for i = 1:rows/" << repetitions << "," << endl << " ifirst = (i - 1) * " << repetitions << " + 1;" << endl << " ilast = ifirst + " << repetitions << " - 1;" << endl << " tmp = " << MATLAB_VAR_NAME << "(ifirst:ilast, cols-6+" << column << ");" << endl << " avg = mean(tmp);" << endl << " [h sig ci] = ttest(tmp, 0.1);" << endl << " ci = (ci(2)-ci(1))/2;" << endl << " " << var_name << " = [" << var_name << "; " << MATLAB_VAR_NAME << "(ifirst, 1:cols-6), avg ci];" << endl << "end" << endl << endl; fout << "figure(" << fig_no << ");" << endl << "hold on;" << endl << "plot(" << var_name << "(:,1), " << var_name << "(:,2), symbol);" << endl << endl; return true;}//---------------------------------------------------------------------------bool GenMatlabCodeSaturationAnalysis(const string& var_name, ofstream& fout, string& error_msg){ fout << endl << "%-------- Saturation Analysis -----------" << endl << "slope=[];" << endl << "for i=2:size(" << var_name << "_throughput,1)," << endl << " slope(i-1) = (" << var_name << "_throughput(i,2)-" << var_name << "_throughput(i-1,2))/(" << var_name << "_throughput(i,1)-" << var_name << "_throughput(i-1,1));" << endl << "end" << endl << endl << "for i=2:size(slope,2)," << endl << " if slope(i) < (0.95*mean(slope(1:i)))" << endl << " max_pir = " << var_name << "_throughput(i, 1);" << endl << " max_throughput = " << var_name << "_throughput(i, 2);" << endl << " min_delay = " << var_name << "_delay(i, 2);" << endl << " break;" << endl << " end" << endl << "end" << endl; return true;}//---------------------------------------------------------------------------bool PrintMatlabVariableEnd(const int repetitions, ofstream& fout, string& error_msg){ fout << "];" << endl << endl; fout << "rows = size(" << MATLAB_VAR_NAME << ", 1);" << endl << "cols = size(" << MATLAB_VAR_NAME << ", 2);" << endl << endl; if (!GenMatlabCode(string(MATLAB_VAR_NAME) + "_delay", 1, repetitions, 1, fout, error_msg)) return false; if (!GenMatlabCode(string(MATLAB_VAR_NAME) + "_throughput", 2, repetitions, 2, fout, error_msg)) return false; if (!GenMatlabCode(string(MATLAB_VAR_NAME) + "_maxdelay", 3, repetitions, 3, fout, error_msg)) return false; if (!GenMatlabCode(string(MATLAB_VAR_NAME) + "_totalenergy", 4, repetitions, 4, fout, error_msg)) return false; if (!GenMatlabCodeSaturationAnalysis(string(MATLAB_VAR_NAME), fout, error_msg)) return false; return true;}//---------------------------------------------------------------------------bool RunSimulations(const TConfigurationSpace& conf_space, const TParameterSpace& default_params, const TParametersSpace& aggragated_params_space, const TParameterSpace& explorer_params, string& error_msg){ TExplorerParams eparams; if (!ExtractExplorerParams(explorer_params, eparams, error_msg)) return false; // Make dafault parameters string string def_cmd_line; for (uint i=0; i<default_params.size(); i++) def_cmd_line = def_cmd_line + default_params[i] + " "; // Explore configuration space TConfigurationSpace aggr_conf_space = Explore(aggragated_params_space); pair<uint,uint> sim_counter(0, conf_space.size() * aggr_conf_space.size() * eparams.repetitions); double start_time = GetCurrentTime(); for (uint i=0; i<conf_space.size(); i++) { string conf_cmd_line = Configuration2CmdLine(conf_space[i]); string mfname = Configuration2FunctionName(conf_space[i]); string fname = mfname + ".m"; ofstream fout; if (!PrintHeader(fname, eparams, def_cmd_line, conf_cmd_line, fout, error_msg)) return false; if (!PrintMatlabFunction(mfname, fout, error_msg)) return false; if (!PrintMatlabVariableBegin(aggragated_params_space, fout, error_msg)) return false; for (uint j=0; j<aggr_conf_space.size(); j++) { string aggr_cmd_line = Configuration2CmdLine(aggr_conf_space[j]); string cmd = eparams.simulator + " " + def_cmd_line + " " + conf_cmd_line + " " + aggr_cmd_line; if (!RunSimulations(start_time, sim_counter, cmd, eparams.tmp_dir, eparams.repetitions, aggr_conf_space[j], fout, error_msg)) return false; } if (!PrintMatlabVariableEnd(eparams.repetitions, fout, error_msg)) return false; } return true;}//---------------------------------------------------------------------------bool RunSimulations(const string& script_fname, string& error_msg){ TParametersSpace ps; if (!ParseConfigurationFile(script_fname, ps, error_msg)) return false; TParameterSpace default_params; if (!RemoveParameter(ps, DEFAULT_KEY, default_params, error_msg)) cout << "Warning: " << error_msg << endl; TParameterSpace aggregated_params; TParametersSpace aggragated_params_space; if (!RemoveParameter(ps, AGGREGATION_KEY, aggregated_params, error_msg)) cout << "Warning: " << error_msg << endl; else if (!RemoveAggregateParameters(ps, aggregated_params, aggragated_params_space, error_msg)) return false; TParameterSpace explorer_params; if (!RemoveParameter(ps, EXPLORER_KEY, explorer_params, error_msg)) cout << "Warning: " << error_msg << endl; TConfigurationSpace conf_space = Explore(ps); if (!RunSimulations(conf_space, default_params, aggragated_params_space, explorer_params, error_msg)) return false; return true;}//---------------------------------------------------------------------------int main(int argc, char **argv){ if (argc < 2) { cout << "Usage: " << argv[0] << " <cfg file> [<cfg file>]" << endl; return -1; } for (int i=1; i<argc; i++) { string fname(argv[i]); cout << "# Exploring configuration space " << fname << endl; string error_msg; if (!RunSimulations(fname, error_msg)) cout << "Error: " << error_msg << endl; cout << endl; } return 0;}//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -