Commit 28f0506b authored by frey_m's avatar frey_m

Merge branch 'sampler-memory-reduction' into 'master'

Sampler memory reduction

Closes #264

See merge request !44
parents 56e68b89 6f69978a
......@@ -13,23 +13,21 @@ class LatinHyperCube : public SamplingMethod
public:
typedef typename std::uniform_real_distribution<double> dist_t;
LatinHyperCube(double lower, double upper, std::size_t n)
: binsize_m((upper - lower) / double(n))
LatinHyperCube(double lower, double upper)
: binsize_m(0.0)
, upper_m(upper)
, lower_m(lower)
, dist_m(0.0, 1.0)
{
this->fillBins_m(n);
RNGInstance_m = RNGStream::getInstance();
}
LatinHyperCube(double lower, double upper, int seed, std::size_t n)
: binsize_m((upper - lower) / double(n))
LatinHyperCube(double lower, double upper, int seed)
: binsize_m(0.0)
, upper_m(upper)
, lower_m(lower)
, dist_m(0.0, 1.0)
{
this->fillBins_m(n);
RNGInstance_m = RNGStream::getInstance(seed);
}
......@@ -44,6 +42,13 @@ public:
ind->genes[i] = map2domain_m(RNGInstance_m->getNext(dist_m));
}
void allocate(std::size_t n) {
binsize_m = ( upper_m - lower_m ) / double(n);
this->fillBins_m(n);
}
private:
double map2domain_m(double val) {
/* y = mx + q
......@@ -77,6 +82,7 @@ private:
std::deque<std::size_t> bin_m;
double binsize_m;
double upper_m;
double lower_m;
dist_t dist_m;
......
......@@ -139,9 +139,9 @@ void OpalSample::initialize(const std::string &dvarName,
size_m = static_cast<FromFile*>(sampleMethod_m.get())->getSize();
} else if (type == "LATIN_HYPERCUBE") {
if (Attributes::getReal(itsAttr[SEED])) {
sampleMethod_m.reset( new LatinHyperCube(lower, upper, seed, size_m) );
sampleMethod_m.reset( new LatinHyperCube(lower, upper, seed) );
} else {
sampleMethod_m.reset( new LatinHyperCube(lower, upper, size_m) );
sampleMethod_m.reset( new LatinHyperCube(lower, upper) );
}
} else {
throw OpalException("OpalSample::initialize()",
......
......@@ -135,8 +135,8 @@ void SampleCmd::execute() {
throw OpalException("SampleCmd::execute",
"Number of sampling methods != number of design variables.");
std::map< std::string, std::shared_ptr<SamplingMethod> > sampleMethods;
typedef std::map< std::string, std::shared_ptr<SamplingMethod> > sampleMethods_t;
sampleMethods_t sampleMethods;
std::map<std::string, std::pair<double, double> > vars;
......@@ -429,6 +429,14 @@ void SampleCmd::execute() {
boost::shared_ptr<Comm_t> comm(new Comm_t(args, MPI_COMM_WORLD));
if (comm->isWorker())
stashEnvironment();
if ( comm->isOptimizer() ) {
for (sampleMethods_t::iterator it = sampleMethods.begin();
it != sampleMethods.end(); ++it)
{
it->second->allocate(nSample);
}
}
boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm, funcs, dvars, objectives, sampleMethods, storeobjstr));
if (comm->isWorker())
......
......@@ -11,6 +11,22 @@ class SamplingMethod
public:
virtual void create(boost::shared_ptr<SampleIndividual>& ind, size_t i) = 0;
/*!
* Allocate memory for sampling. Not every sampling method
* requires that.
*
* This function is used to reduce memory since only the
* sampler ranks need these sampling methods.
*
* @param n number of samples
*/
virtual void allocate(std::size_t n) {
/* Some sampling methods require a container.
* In order to reduce memory only samplers should allocate
* the memory
*/
}
};
#endif
\ No newline at end of file
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