27 #ifndef TOMOGRAPHER_MHWALKER_TASKS_H 28 #define TOMOGRAPHER_MHWALKER_TASKS_H 62 template<
typename CountIntType_ =
unsigned int,
typename StepRealType_ =
double>
71 template<
typename MHRWParamsType>
125 return base_seed + k;
134 <<
"\t# iter. / sweep = " << mhrw_params.
n_sweep <<
"\n" 135 <<
"\t# therm. sweeps = " << mhrw_params.
n_therm <<
"\n" 136 <<
"\t# run sweeps = " << mhrw_params.
n_run <<
"\n";
160 template<
typename MHRWStatsCollectorResultType_,
typename CountIntType,
typename StepRealType>
177 acceptance_ratio(
std::numeric_limits<double>::quiet_NaN())
188 template<
typename MHRWStatsCollectorResultTypeInit,
189 typename MHRWParamsType2>
191 MHRWParamsType2 && mhrw_params_,
192 double acceptance_ratio_)
193 : stats_collector_result(
std::forward<MHRWStatsCollectorResultTypeInit>(stats_collector_result_)),
195 acceptance_ratio(acceptance_ratio_)
204 template<
typename MHRWStatsCollectorResultTypeInit,
typename MHRandomWalkType>
206 const MHRandomWalkType & mhrandomwalk)
207 : stats_collector_result(
std::forward<MHRWStatsCollectorResultTypeInit>(stats_collector_result_)),
209 acceptance_ratio(mhrandomwalk.hasAcceptanceRatio() ?
210 mhrandomwalk.acceptanceRatio() :
211 std::numeric_limits<double>::quiet_NaN())
235 template<
typename MHRandomWalkTaskCData,
239 typedef typename MHRandomWalkTaskCData::CountIntType
CountIntType;
240 typedef typename MHRandomWalkTaskCData::StepRealType
StepRealType;
263 CountIntType kstep_ = 0, MHRWParamsType mhrw_params_ =
MHRWParamsType(),
264 double acceptance_ratio_ = 0.0)
265 : MultiProc::TaskStatusReport(fdone, msg),
268 acceptance_ratio(acceptance_ratio_),
306 typename Rng::result_type _seed;
324 template<
typename LoggerType>
326 : _seed(inputseed), result(NULL)
328 logger.longdebug(
"MHRandomWalkTask",
"() inputseed=%d", inputseed);
333 if (result != NULL) {
348 template<
typename LoggerType,
typename TaskManagerIface>
349 inline void run(
const MHRandomWalkTaskCData * pcdata, LoggerType & logger,
350 TaskManagerIface * tmgriface)
354 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"about to construct stats collector.");
357 auto stats = pcdata->createStatsCollector(logger);
358 typedef decltype(stats) MHRWStatsCollectorType;
360 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"stats collector constructed.");
363 typedef StatusReportCheck<TaskManagerIface, MHRWStatsCollectorType> OurStatusReportCheck;
364 OurStatusReportCheck statreportcheck(
this, &stats, tmgriface);
368 OurStatsCollectors ourstatscollectors(stats, statreportcheck);
370 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"about to create MH walker object.");
372 auto mhwalker = pcdata->createMHWalker(rng, logger);
373 typedef decltype(pcdata->createMHWalker(rng, logger)) MHWalkerType;
375 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"MHWalker object created.");
390 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"MHRandomWalk object created, running...");
394 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"MHRandomWalk run finished.");
396 result =
new ResultType(stats.getResult(), rwalk);
399 inline const ResultType & getResult()
const 410 template<
typename TaskManagerIface,
typename MHRWStatsCollectorType>
411 struct StatusReportCheck
413 StatusReportCheck(
MHRandomWalkTask * mhrwtask_, MHRWStatsCollectorType * stats_, TaskManagerIface *tmgriface_)
414 : mhrwtask(mhrwtask_), stats(stats_), tmgriface(tmgriface_)
418 MHRWStatsCollectorType *stats;
419 TaskManagerIface *tmgriface;
421 inline void init() { }
422 inline void thermalizingDone() { }
423 inline void done() { }
425 template<
typename Po
intType,
typename FnValueType,
typename MHRandomWalk>
427 CountIntType k,
bool is_thermalizing,
bool,
bool,
double,
const PointType &, FnValueType,
433 if (tmgriface->statusReportRequested()) {
436 double fdone = (double)k/totiters;
438 bool warn_accept_ratio =
false;
441 warn_accept_ratio = (accept_ratio > 0.35 || accept_ratio < 0.2);
444 "%s %lu/(%lu=%lu*(%lu+%lu)) : %5.2f%% done [%saccept ratio=%.2f%s]",
448 (
unsigned long)k, (
unsigned long)totiters, (
unsigned long)rw.
nSweep(),
449 (
unsigned long)rw.
nTherm(), (
unsigned long)rw.
nRun(),
451 warn_accept_ratio ?
"!!** " :
"",
453 warn_accept_ratio ?
" **!!" :
"" 456 if (MHRWStatsCollectorStatusType::CanProvideStatus) {
459 std::string s = MHRWStatsCollectorStatusType::getStatus(stats);
473 template<
typename Po
intType,
typename FnValueType,
typename MHRandomWalk>
474 inline void processSample(CountIntType, CountIntType,
const PointType &, FnValueType,
MHRandomWalk &)
A Metropolis-Hastings Random Walk.
Utilities for formatting strings.
std::string getBasicCDataMHRWInfo() const
Get some human-readable info about the random walk as a string, see printBasicCDataMHRWInfo() ...
CountIntType_ CountIntType
Type used to count the number of iterations.
MHRWParamsType mhrwParams() const
The parameters of the random walk.
MHRWParamsType mhrw_params
the parameters of the random walk
double acceptance_ratio
the current acceptance ratio of the random walk (see Tomographer::MHRandomWalk::acceptanceRatio() ) ...
Base namespace for the Tomographer project.
MHRWStatsCollectorResultType_ MHRWStatsCollectorResultType
The specified result type of the MHRWStatsCollector the task will be looking at.
MHRandomWalkTaskResult()
Construct an empty task result.
const int base_seed
A base random seed from which each run seed will be derived.
CountIntType n_therm
Number of thermalization sweeps.
CountIntType n_run
Number of live sweeps.
bool hasAcceptanceRatio() const
Query whether we have any statistics about acceptance ratio. This is false, for example, during the thermalizing runs.
MHRandomWalkTask(int inputseed, const MHRandomWalkTaskCData *, LoggerType &logger)
Constructs the MHRandomWalkTask.
RatioType acceptanceRatio() const
Return the acceptance ratio so far.
StepRealType_ StepRealType
Type used to specify the step size.
void run(const MHRandomWalkTaskCData *pcdata, LoggerType &logger, TaskManagerIface *tmgriface)
Run this task.
void printBasicCDataMHRWInfo(std::ostream &str) const
Get some human-readable info about the random walk as a string.
const double acceptance_ratio
The acceptance ratio of the Metropolis-Hastings random walk.
StepRealType step_size
The step size of the random walk.
Random Walk task, collecting statistics.
MHRandomWalkTaskResult(MHRWStatsCollectorResultTypeInit &&stats_collector_result_, MHRWParamsType2 &&mhrw_params_, double acceptance_ratio_)
Constructor, initializes fields to the given values.
CountIntType nRun() const
Number of live run sweeps.
MHRandomWalkTaskResult(MHRWStatsCollectorResultTypeInit &&stats_collector_result_, const MHRandomWalkType &mhrandomwalk)
Constructor with mhrw_params initialized from a random walk instance.
Data needed to be accessible to the working code.
CountIntType kstep
the current iteration number
CDataBase(MHRWParamsType &&p, int base_seed_=0)
Constructor.
const MHRWStatsCollectorResultType stats_collector_result
The result furnished by the stats collector itself.
Basic status report class.
void run()
Run the random walk. (pun intended)
const MHRWParamsType mhrw_params
Parameters of the random walk.
Status Report for a MHRandomWalkTask.
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
CountIntType n_sweep
The number of individual updates to collect together in a "sweep".
CountIntType nTherm() const
Number of thermalizing sweeps.
CountIntType n_total_iters
the total number of iterations required for this random walk
StatusReport(double fdone=0.0, const std::string &msg=std::string(), CountIntType kstep_=0, MHRWParamsType mhrw_params_=MHRWParamsType(), double acceptance_ratio_=0.0)
Constructor which initializes all fields.
Some common definitions for multiprocessing interfaces.
const MHRWParamsType mhrw_params
The parameters of the random walk (see MHRWParams<CountIntType,StepRealType>)
Template, specializable class to get status reports from stats collectors.
StatusReport StatusReportType
Typedef for StatusReport. This is needed by, e.g. MultiProc::OMP::TaskDispatcher. ...
int getTaskInput(int k) const
Returns a random seed to seed the random number generator with for run number k.
MHRWParams< CountIntType, StepRealType > MHRWParamsType
The type to use to store the parameters of the random walk.
Definitions for MHRWStatsCollector Interface's.
Routines for performing a Metropolis-Hastings random walk.
CountIntType nSweep() const
Number of iterations in a sweep.
MHRWParams< CountIntType, StepRealType > MHRWParamsType
Type to store the parameters of the Metropolis-Hastings random walk (number of runs, sweep size, etc.)
MHRandomWalkTaskResult< typename MHRandomWalkTaskCData::MHRWStatsCollectorResultType, CountIntType, StepRealType > ResultType
Result type of a single task run.
A simple MHRWStatsCollector interface which combines several stats collectors.