Commit bd45787b authored by frey_m's avatar frey_m
Browse files

Merge branch 'master' of gitlab.psi.ch:OPAL/src into sampler-memory-reduction

parents 39a99b95 9c617167
......@@ -83,20 +83,20 @@ protected:
/// Write the variator config file
void writeVariatorCfg();
// implementing poller hooks
bool onMessage(MPI_Status status, size_t length);
void postPoll();
///@{ implementing poller hooks
virtual bool onMessage(MPI_Status status, size_t length);
virtual void postPoll();
void setupPoll() {}
void prePoll() {
virtual void setupPoll() {}
virtual void prePoll() {
// std::ostringstream debug;
// debug << "IN PRE POLL: ";
// debug << getStateString(curState_m) << std::endl;
// progress_->log(debug);
}
void onStop() {}
// helper sending evaluation requests to the pilot
virtual void onStop() {}
///@}
/// helper sending evaluation requests to the pilot
void dispatch_forward_solves();
......@@ -140,16 +140,16 @@ private:
Variator_t;
boost::scoped_ptr<Variator_t> variator_m;
std::vector<unsigned int> pp_all;
std::vector<unsigned int> parent_queue_;
std::set<unsigned int> archive_;
std::set<unsigned int> to_selector_;
std::vector<unsigned int> pp_all; ///< IDs of population
std::vector<unsigned int> parent_queue_; ///< IDs that will make new offspring
// std::set<unsigned int> archive_;
std::set<unsigned int> to_selector_; ///< Successfully run IDs to go into population
// to compute the front
std::vector<int> copies;
std::vector<int> copies; ///< number of individuals in the n-th front
std::vector<double> dist;
std::vector< std::vector<int> > front;
std::map<size_t, double> fitness_;
std::vector< std::vector<int> > front; ///< individuals in each front
std::map<size_t, double> fitness_; ///< map between id and fitness (sum of front number and dist)
/// communicator bundle for the optimizer
Comm::Bundle_t comms_;
......
......@@ -253,7 +253,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
job_trace_->log(dump);
std::cout << "NON-EXISTING JOB with ID = " << jid << std::endl;
throw OptPilotException("FixedPisaNsga2::onMessage",
"non-existing job");
"non-existing job");
}
boost::shared_ptr<individual> ind = it->second;
......@@ -624,7 +624,7 @@ void FixedPisaNsga2<CO, MO>::runStateMachine() {
typedef typename FixedPisaNsga2::Individual_t individual;
typename std::map<unsigned int, boost::shared_ptr<individual> >
::iterator it;
for(it = variator_m->population()->begin();
for(it = variator_m->population()->begin();
it != variator_m->population()->end(); it++) {
//archive_.insert(it->first);
pp_all.push_back(it->first);
......@@ -687,12 +687,13 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
typename std::map<unsigned int, boost::shared_ptr<individual> >::iterator it;
it = variator_m->population()->begin();
// find maximum length of ID
auto maxID = it->first;
for(it ++; it != variator_m->population()->end(); it++) {
if (it->first > maxID)
maxID = it->first;
}
size_t numDigits = std::to_string(maxID).length();
const size_t numDigits = std::to_string(maxID).length();
size_t next = 0, last = 0;
std::string delimiter = ",";
next = file_param_descr_.find(delimiter, last);
......@@ -711,7 +712,7 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
file.precision(6);
file << std::scientific;
for(it = variator_m->population()->begin();
for(it = variator_m->population()->begin();
it != variator_m->population()->end(); it++) {
file << std::setw(numDigits + 1) << it->first << " ";
......@@ -862,38 +863,35 @@ void FixedPisaNsga2<CO, MO>::calcFitnesses()
int i, j, l;
int size = pp_all.size();
std::vector<int> d(size);
std::vector<int> f(size);
std::vector<int> d(size,0); // 1 if dominated by another ind, 0 if not
std::vector<int> f(size,1); // 1 if not yet in a front, -1 if in a front
/* initialize fitness and strength values */
for (i = 0; i < size; i++) {
fitness_.insert(std::pair<size_t, double>(pp_all[i], 0.0));
d[i] = 1;
f[i] = 1;
copies[i] = 0;
}
/* calculate strength values */
int num = size;
for (l = 0; l < size; l++) {
int num = size; // number of individuals not yet in a front
for (l = 0; l < size; l++) { // loop over fronts, there can be maximally `size` fronts
/* find next front */
for (i = 0; i < size; i++) {
d[i] = 0;
if (f[i] != -1) {
for (j = 0; j < i && d[i] == 0; j++)
if (f[j] != -1)
if (dominates(pp_all[j], pp_all[i]))
d[i] = 1;
for(j = i+1; j < size && d[i] == 0; j++)
if (f[j] != -1)
if (dominates(pp_all[j], pp_all[i]))
d[i] = 1;
d[i] = 0; // reset dominate flag
if (f[i] == -1) continue; // don't consider if already in a front
for (j = 0; j < size && j != i; j++) {
if (f[j] == -1) continue;
if (dominates(pp_all[j], pp_all[i])) {
d[i] = 1;
break;
}
}
}
/* extract front */
for (i = 0; i < size; i++) {
if (f[i] != -1 && d[i] == 0) {
// add to front
fitness_[pp_all[i]] = l;
f[i] = -1;
num--;
......@@ -982,18 +980,19 @@ void FixedPisaNsga2<CO, MO>::environmentalSelection() {
for (int i = 0; i < size; i++)
fitness_[pp_all[i]] += 1.0 / dist[i];
// get alpha_m fittest individuals
for (size_t i = 0; i < alpha_m; i++) {
int min = i;
for (int j = i + 1; j < size; j++) {
if (fitness_[pp_all[j]] < fitness_[pp_all[min]])
min = j;
}
// swap
unsigned int p_min = pp_all[min];
pp_all[min] = pp_all[i];
pp_all[i] = p_min;
}
// erase others
pp_all.erase(pp_all.begin() + alpha_m, pp_all.end());
}
......@@ -1003,12 +1002,12 @@ template< template <class> class CO, template <class> class MO >
void FixedPisaNsga2<CO, MO>::matingSelection() {
//FIXME:
int tournament = 2;
int tournament = 1; ///< number of opponents
for (size_t i = 0; i < selector_mu_; i++) {
int winner = irand(pp_all.size());
for (int j = 1; j < tournament; j++) {
for (int j = 0; j < tournament; j++) {
int opponent = irand(pp_all.size());
if (fitness_[pp_all[opponent]] < fitness_[pp_all[winner]]
|| winner == opponent) {
......
......@@ -153,7 +153,7 @@ public:
if (viableA == true && viableB == true) {
break;
}
std::cout << "Individual not viable, I try again: iter= " << iter << std::endl;
std::cout << "Individual not viable, I try again: iter= " << iter << std::endl;
iter++;
// if maximum number of tries then create new individual(s)
if (iter > 100) {
......
......@@ -21,3 +21,25 @@ void CmdArguments::split(std::string &name,
boost::trim(name);
boost::trim(value);
}
char** CmdArguments::getArguments() const {
const unsigned int size = arguments_.size();
char** args = new char*[2 * size];
unsigned int i = 0;
auto it = arguments_.cbegin();
const auto end = arguments_.cend();
for (; it != end; ++ it) {
const std::string &key = it->first;
char* argname = new char[key.length() + 1];
strcpy(argname, key.c_str());
args[i ++] = argname;
const std::string &value = it->second;
char* argvalue = new char[value.length() + 1];
strcpy(argvalue, value.c_str());
args[i ++] = argvalue;
}
return args;
}
\ No newline at end of file
......@@ -31,6 +31,9 @@ public:
addArguments(argc, argv);
}
CmdArguments()
{}
~CmdArguments()
{}
......@@ -84,6 +87,36 @@ public:
return value;
}
template <class T>
void addArgument(const std::string &name, const T &value) {
std::ostringstream oss;
oss << value;
if (arguments_.find(name) != arguments_.end()) {
throw OptPilotException("CmdArguments::addArgument",
"Argument '" + name + "' exists");
}
arguments_.insert(std::make_pair(name, oss.str()));
}
template <class T>
void replaceArgument(const std::string &name, const T &value) {
std::ostringstream oss;
oss << value;
if (arguments_.find(name) == arguments_.end()) {
arguments_.insert(std::make_pair(name, oss.str()));
return;
}
arguments_.at(name) = oss.str();
}
char** getArguments() const;
unsigned int getNumArguments() const {
return arguments_.size();
}
//template<>
//size_t getArg<size_t>(const std::string name, bool isFatal = false) {
//return getArg<size_t>(name, 0, isFatal);
......@@ -135,4 +168,4 @@ private:
typedef boost::shared_ptr<CmdArguments> CmdArguments_t;
#endif
#endif
\ No newline at end of file
......@@ -81,13 +81,13 @@ namespace Hypervolume {
POINT p = *(POINT*)v1;
POINT q = *(POINT*)v2;
#if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--)
for (int i = n - fr - 1; i >= 0; i--) {
#else
for (int i = n - 1; i >= 0; i--)
for (int i = n - 1; i >= 0; i--) {
#endif
if BEATS(p.objectives[i],q.objectives[i]) return 1;
else
if BEATS(q.objectives[i],p.objectives[i]) return -1;
if BEATS(p.objectives[i],q.objectives[i]) return 1;
else if BEATS(q.objectives[i],p.objectives[i]) return -1;
}
return 0;
}
......
......@@ -2,7 +2,7 @@
using namespace std;
void IlyaPogorelovFilter::apply(vector<double> &LineDensity) {
void StencilFilter::apply(vector<double> &LineDensity) {
int size = LineDensity.size();
vector<double> temp1(size + 8, 0.0);
vector<double> temp2(5, 0.0);
......@@ -29,7 +29,7 @@ void IlyaPogorelovFilter::apply(vector<double> &LineDensity) {
LineDensity[i] = (7. * (temp1[i+2] + temp1[i+6]) + 24. * (temp1[i+3] + temp1[i+5]) + 34.*temp1[i+4]) / 96.;
}
void IlyaPogorelovFilter::calc_derivative(vector<double> &LineDensity, const double &h) {
void StencilFilter::calc_derivative(vector<double> &LineDensity, const double &h) {
vector<double> temp(LineDensity.begin(), LineDensity.end());
const double N = LineDensity.size();
LineDensity[0] = (-25.*temp[0] + 48.*temp[1] - 36.*temp[2] + 16.*temp[3] + temp[4]) / (12.*h);
......@@ -40,5 +40,4 @@ void IlyaPogorelovFilter::calc_derivative(vector<double> &LineDensity, const dou
LineDensity[N-2] = (-temp[N-5] + 6.*temp[N-4] - 18.*temp[N-3] + 10.*temp[N-2] + 3.*temp[N-1]) / (12.*h);
LineDensity[N-1] = (3.*temp[N-5] - 16.*temp[N-4] + 36.*temp[N-3] - 48.*temp[N-2] + 25.*temp[N-1]) / (12.*h);
apply(LineDensity);
}
}
\ No newline at end of file
......@@ -3,11 +3,11 @@
#include "Filters/Filter.h"
class IlyaPogorelovFilter: public Filter {
class StencilFilter: public Filter {
public:
IlyaPogorelovFilter() { ;}
StencilFilter() { ;}
void apply(std::vector<double> &histogram);
void calc_derivative(std::vector<double> &histogram, const double &h);
};
#endif // CLASSIC_STENCIL_HH
#endif // CLASSIC_STENCIL_HH
\ No newline at end of file
......@@ -132,7 +132,7 @@ void CSRIGFWakeFunction::calculateLineDensity(PartBunchBase<double, 3> *bunch, s
(*fit)->apply(lineDensity_m);
}
dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end());
filters_m.back()->calc_derivative(dlineDensitydz_m, meshInfo.second);
diffOp_m.calc_derivative(dlineDensitydz_m, meshInfo.second);
}
void CSRIGFWakeFunction::calculateGreenFunction(PartBunchBase<double, 3> *bunch, double meshSpacing)
......
......@@ -2,11 +2,13 @@
#define CSRIGFWAKEFUNCTION_HH
#include "Solvers/WakeFunction.hh"
#include "Filters/Stencil.h"
#include <vector>
#include <string>
class ElementBase;
class Filter;
class CSRIGFWakeFunction: public WakeFunction {
public:
......@@ -27,6 +29,7 @@ private:
double calcPsi(const double &psiInitial, const double &x, const double &Ds) const;
std::vector<Filter *> filters_m;
StencilFilter diffOp_m;
LineDensity lineDensity_m;
LineDensity dlineDensitydz_m;
......@@ -61,4 +64,4 @@ private:
};
#endif //CSRIGFWAKEFUNCTION_HH
#endif //CSRIGFWAKEFUNCTION_HH
\ No newline at end of file
......@@ -17,7 +17,6 @@ CSRWakeFunction::CSRWakeFunction(const std::string &name, ElementBase *element,
filters_m(filters.begin(), filters.end()),
lineDensity_m(),
dlineDensitydz_m(),
d2lineDensitydz2_m(),
bendRadius_m(0.0),
totalBendAngle_m(0.0)
{ }
......@@ -126,7 +125,7 @@ void CSRWakeFunction::calculateLineDensity(PartBunchBase<double, 3> *bunch, std:
}
dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end());
filters_m.back()->calc_derivative(dlineDensitydz_m, meshInfo.second);
diffOp_m.calc_derivative(dlineDensitydz_m, meshInfo.second);
}
void CSRWakeFunction::calculateContributionInside(size_t sliceNumber, double angleOfSlice, double meshSpacing) {
......
......@@ -2,11 +2,13 @@
#define CSRWAKEFUNCTION_HH
#include "Solvers/WakeFunction.hh"
#include "Filters/Stencil.h"
#include <vector>
#include <string>
class ElementBase;
class Filter;
class CSRWakeFunction: public WakeFunction {
public:
......@@ -26,9 +28,10 @@ private:
double calcPsi(const double &psiInitial, const double &x, const double &Ds) const;
std::vector<Filter *> filters_m;
StencilFilter diffOp_m;
LineDensity lineDensity_m;
LineDensity dlineDensitydz_m;
LineDensity d2lineDensitydz2_m;
// Longitudinal CSR field.
std::vector<double> Ez_m;
......@@ -55,4 +58,4 @@ private:
};
#endif //CSRWAKEFUNCTION_HH
#endif //CSRWAKEFUNCTION_HH
\ No newline at end of file
......@@ -176,7 +176,7 @@ void OpalFilter::initOpalFilter() {
} else if (type == "RELATIVEFFTLOWPASS") {
filter_m = new RelativeFFTLowPassFilter(std::abs(Attributes::getReal(itsAttr[THRESHOLD])));
} else if (type == "STENCIL") {
filter_m = new IlyaPogorelovFilter();
filter_m = new StencilFilter();
} else {
filter_m = 0;
INFOMSG("no filter attached" << endl);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment