--- trunk/gadget/ecosystem.cc 2014/02/10 17:09:07 1 +++ trunk/gadget/ecosystem.cc 2017/04/26 09:06:03 24 @@ -1,11 +1,23 @@ #include "ecosystem.h" #include "runid.h" #include "global.h" +#include +#ifdef _OPENMP +#include +#endif + +#ifdef _OPENMP +extern Ecosystem** EcoSystems; +//extern StochasticData* data; +#endif +extern volatile int interrupted_print; + +time_t starttime, stoptime; Ecosystem::Ecosystem(const MainInfo& main) : printinfo(main.getPI()) { funceval = 0; - interrupted = 0; + //interrupted = 0; likelihood = 0.0; keeper = new Keeper; @@ -32,7 +44,9 @@ infile.open(filename, ios::in); handle.checkIfFailure(infile, filename); handle.Open(filename); - this->readOptimisation(commin); + unsigned seed; + main.getForcePrint(); + this->readOptimisation(commin, main.getSeed()); handle.Close(); infile.close(); infile.clear(); @@ -41,12 +55,33 @@ optvec.resize(new OptInfoHooke()); } } - +#ifdef _OPENMP + runParallel=main.runParallel(); + if (!omp_in_parallel()){ + if (main.runOptimise()) + { + switch(runParallel){ + case SPECULATIVE: + handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run parallel speculative optimisation with ", omp_get_max_threads(), " threads"); + break; + case REPRODUCIBLE: + handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run parallel reproducible optimisation with ", omp_get_max_threads(), " threads"); + break; + default: + handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to serial optimisation with ", omp_get_num_threads() , "threads"); + } + } + else + handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run simulation ", omp_get_num_threads(), " threads"); + handle.logMessage(LOGMESSAGE, ""); //write blank line to log file + } +#else if (main.runOptimise()) handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run optimisation"); else handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run simulation"); handle.logMessage(LOGMESSAGE, ""); //write blank line to log file +#endif } Ecosystem::~Ecosystem() { @@ -101,7 +136,36 @@ void Ecosystem::Optimise() { int i; for (i = 0; i < optvec.Size(); i++) { - optvec[i]->OptimiseLikelihood(); + time(&starttime); +#ifdef _OPENMP + if (runParallel){ + int j, numThr = omp_get_max_threads ( ); //AGT REMOVED + DoubleVector v = this->getValues(); + for (j = 0; j < numThr; j++) + EcoSystems[j]->Update(v); + if(runParallel==SPECULATIVE) + { + handle.logMessage(LOGINFO, "\nCalling Speculative Optimization"); + optvec[i]->OptimiseLikelihoodOMP(); + } + else + { + handle.logMessage(LOGINFO, "\nCalling Reproducible parallel Optimization"); + optvec[i]->OptimiseLikelihoodREP(); + } + } + else + { + handle.logMessage(LOGINFO, "\nCalling Reproducible serial Optimization"); + optvec[i]->OptimiseLikelihood(); + } +#else +//#ifdef SPECULATIVE +// optvec[i]->OptimiseLikelihoodOMP(); +//#else + optvec[i]->OptimiseLikelihood(); +#endif + time(&stoptime); this->writeOptValues(); } } @@ -115,6 +179,7 @@ currentval.resize(keeper->numVariables(), 0.0); optflag.resize(keeper->numVariables(), 0); keeper->getOptFlags(optflag); + } j = 0; @@ -148,7 +213,15 @@ for (i = 0; i < likevec.Size(); i++) tmpvec[i] = likevec[i]->getUnweightedLikelihood(); - handle.logMessage(LOGINFO, "\nAfter a total of", funceval, "function evaluations the best point found is"); + int iters = 0; +#ifdef _OPENMP + if (runParallel){ + int numThr = omp_get_max_threads ( ); + for (i = 0; i < numThr; i++) + iters += EcoSystems[i]->getFuncEval(); + } +#endif + handle.logMessage(LOGINFO, "\nAfter a total of", funceval+iters, "function evaluations the best point found is"); keeper->writeBestValues(); handle.logMessage(LOGINFO, "\nThe scores from each likelihood component are"); handle.logMessage(LOGINFO, tmpvec); @@ -156,6 +229,8 @@ handle.logMessage(LOGINFO, "\nThe overall likelihood score is", keeper->getBestLikelihoodScore()); else handle.logMessage(LOGINFO, "\nThe overall likelihood score is", this->getLikelihood()); + + handle.logMessage(LOGINFO, "\n Runtime for the algorithm was ", difftime(stoptime, starttime)," seconds\n"); } void Ecosystem::writeInitialInformation(const char* const filename) { @@ -168,11 +243,11 @@ } void Ecosystem::writeParams(const char* const filename, int prec) const { - if ((funceval > 0) && (interrupted == 0)) { + if ((funceval > 0) && (interrupted_print == 0)) { //JMB - print the final values to any output files specified //in case they have been missed by the -print value if (printinfo.getPrint()) keeper->writeValues(likevec, printinfo.getPrecision()); } - keeper->writeParams(optvec, filename, prec, interrupted); + keeper->writeParams(optvec, filename, prec, interrupted_print); }