28 #ifndef TOMOGRAPHER_MHWALKER_TASKS_H 29 #define TOMOGRAPHER_MHWALKER_TASKS_H 56 namespace tomo_internal {
131 template<
typename MHWalkerParams_,
typename IterCountIntType_ = int,
132 typename RngSeedType_ = std::mt19937::result_type>
160 template<
typename MHRWParamsType>
162 : mhrw_params(
std::forward<MHRWParamsType>(p)),
163 base_seed(base_seed_)
201 template<
typename TaskNoCountIntType>
206 return (RngSeedType)k;
216 <<
"\t# iter. / sweep = " << mhrw_params.
n_sweep <<
"\n" 217 <<
"\t# therm. sweeps = " << mhrw_params.
n_therm <<
"\n" 218 <<
"\t# run sweeps = " << mhrw_params.
n_run <<
"\n";
227 printBasicCDataMHRWInfo(ss);
256 template<
typename MHRWStatsResultsType_,
typename IterCountIntType,
typename MHWalkerParams>
286 template<
typename MHRWStatsResultsTypeInit,
287 typename MHRWParamsTypeInit>
289 MHRWParamsTypeInit && mhrw_params_,
290 double acceptance_ratio_)
291 : stats_results(
std::forward<MHRWStatsResultsTypeInit>(stats_results_)),
292 mhrw_params(
std::forward<MHRWParamsTypeInit>(mhrw_params_)),
293 acceptance_ratio(acceptance_ratio_)
304 template<
typename MHRWStatsResultsTypeInit,
typename MHRandomWalkType>
306 const MHRandomWalkType & mhrandomwalk)
307 : stats_results(
std::forward<MHRWStatsResultsTypeInit>(stats_results_)),
308 mhrw_params(mhrandomwalk.mhrwParams()),
309 acceptance_ratio(mhrandomwalk.hasAcceptanceRatio() ?
310 mhrandomwalk.acceptanceRatio() :
311 std::numeric_limits<double>::quiet_NaN())
343 template<
typename MHRandomWalkTaskCData,
369 typename Rng::result_type _seed;
388 template<
typename LoggerType>
391 : _seed(inputseed), result(NULL)
393 logger.longdebug(
"MHRandomWalkTask", [&](
std::ostream & stream) { stream <<
"() inputseed=" << inputseed; });
398 if (result != NULL) {
405 template<
typename LoggerType,
typename TaskMgrIface>
406 struct RunInnerCallable
408 const MHRandomWalkTaskCData * pcdata;
410 LoggerType & baselogger;
411 TaskMgrIface * tmgriface;
413 ResultType ** ppresult;
415 RunInnerCallable(
const MHRandomWalkTaskCData * pcdata_, Rng & rng_, LoggerType & logger_,
416 TaskMgrIface * tmgriface_, ResultType ** ppresult_)
417 : pcdata(pcdata_), rng(rng_), baselogger(logger_), tmgriface(tmgriface_), ppresult(ppresult_)
420 ~RunInnerCallable() {
423 template<
typename MHWalkerType,
typename MHRWStatsCollectorType,
typename MHRWControllerType>
424 inline void operator()(MHWalkerType & mhwalker, MHRWStatsCollectorType & stats,
425 MHRWControllerType & controller)
433 OurStatusReportCheck statreportcheck(
435 [&]() {
return this->tmgriface->statusReportRequested(); },
437 [&](StatusReportType report) { this->tmgriface->submitStatusReport(
std::move(report)); }
442 OurStatsCollectors ourstatscollectors(stats, statreportcheck);
444 logger.longdebug(
"About to creat MHRandomWalk instance");
449 MHRandomWalkType rwalk(
464 logger.longdebug(
"MHRandomWalk object created, running...");
468 logger.longdebug(
"MHRandomWalk run finished.");
470 *ppresult =
new ResultType(stats.stealResult(), rwalk);
473 template<
typename MHWalkerType,
typename MHRWStatsCollectorType>
474 inline void operator()(MHWalkerType & mhwalker, MHRWStatsCollectorType & stats)
477 operator()<MHWalkerType, MHRWStatsCollectorType,
MHRWNoController>(mhwalker, stats, ctrl) ;
492 template<
typename LoggerType,
typename TaskManagerIface>
493 inline void run(
const MHRandomWalkTaskCData * pcdata, LoggerType & baselogger, TaskManagerIface * tmgriface)
499 RunInnerCallable<LoggerType, TaskManagerIface> run_object(pcdata, rng, baselogger, tmgriface, &result);
501 pcdata->setupRandomWalkAndRun(rng, baselogger, run_object);
504 inline ResultType getResult()
const 509 inline ResultType stealResult()
A Metropolis-Hastings Random Walk.
Utilities for formatting strings.
MHRWParams< MHWalkerParams, IterCountIntType > MHRWParamsType
Type to stores the parameters of the random walk.
MHRWStatsResultsType_ MHRWStatsResultsType
The type which stores the results from the statistics carried out during the random walk...
MHRandomWalkTaskCData::MHWalkerParams MHWalkerParams
The type used to describe a step size (see MHRWParams)
Base namespace for the Tomographer project.
CDataBase(MHRWParamsType &&p, RngSeedType base_seed_)
Constructor.
CountIntType n_run
Number of live sweeps.
RngSeedType getTaskInput(TaskNoCountIntType k) const
Returns a random seed to seed the random number generator with for run number k.
void run(const MHRandomWalkTaskCData *pcdata, LoggerType &baselogger, TaskManagerIface *tmgriface)
Run this task.
MHRWStatsResultsType stats_results
The result(s) coming from stats collecting (may be processed, see MHRandomWalkTaskCData Interface) ...
CountIntType n_therm
Number of thermalization sweeps.
double acceptance_ratio
The acceptance ratio of the Metropolis-Hastings random walk.
A "stats collector" which produces status reports whenever a predicate evaluates to true...
Random Walk task, collecting statistics.
void printBasicCDataMHRWInfo(std::ostream &str) const
Get some human-readable info about the random walk as a string.
Data needed to be accessible to the working code.
MHRWParamsType mhrw_params
The parameters of the random walk (see MHRWParams<MHWalkerParams,IterCountIntType>) ...
CountIntType n_sweep
The number of individual updates to collect together in a "sweep".
MHRandomWalkTask(typename Rng::result_type inputseed, const MHRandomWalkTaskCData *, LoggerType &logger)
Constructs the MHRandomWalkTask.
RngSeedType_ RngSeedType
Type used to specify the seed of the random number generator.
LocalLogger< BaseLoggerType > makeLocalLogger(std::string origin_fn_name, BaseLoggerType &baselogger)
Create a local logger.
A MHRWController Interface which combines several independent random walk controllers.
const RngSeedType base_seed
A base random seed from which each run seed will be derived.
MHRWParams< MHWalkerParams, IterCountIntType > MHRWParamsType
The type to use to store the parameters of the random walk.
MHRWParams< MHWalkerParams, IterCountIntType > MHRWParamsType
Type to store the parameters of the Metropolis-Hastings random walk (number of runs, sweep size, etc.)
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
Status Report structure representing the status of a MHRandomWalk.
std::string getBasicCDataMHRWInfo() const
Get some human-readable info about the random walk as a string, see printBasicCDataMHRWInfo() ...
Some common definitions for multiprocessing interfaces.
MHRandomWalkTaskResult(MHRWStatsResultsTypeInit &&stats_results_, MHRWParamsTypeInit &&mhrw_params_, double acceptance_ratio_)
Construct an empty task result.
MHRandomWalkTaskResult(MHRWStatsResultsTypeInit &&stats_results_, const MHRandomWalkType &mhrandomwalk)
Constructor with mhrw_params initialized from a random walk instance.
Definitions for MHRWStatsCollector Interface's.
IterCountIntType_ IterCountIntType
Type used to count the number of iterations.
Routines for performing a Metropolis-Hastings random walk.
const MHRWParamsType mhrw_params
Parameters of the random walk.
T setprecision(T... args)
MHRandomWalkTaskCData::IterCountIntType IterCountIntType
The type used to count iterations (see MHRWParams)
MHWalkerParams_ MHWalkerParams
Type used to specify the step size.
MHWalkerParams mhwalker_params
The parameters of the mh-walker, typically just the step size of the random walk. ...
MHRWStatusReport< MHRWParamsType > StatusReportType
Typedef for MHRWStatusReport. This is needed by, e.g. MultiProc::OMP::TaskDispatcher.
MHRandomWalkTaskResult< typename MHRandomWalkTaskCData::MHRWStatsResultsType, IterCountIntType, MHWalkerParams > ResultType
Result type of a single task run.
A simple MHRWStatsCollector interface which combines several stats collectors.