28 #ifndef TOMOGRAPHER_MHWALKER_TASKS_H 29 #define TOMOGRAPHER_MHWALKER_TASKS_H 46 #include <boost/serialization/serialization.hpp> 47 #include <boost/serialization/vector.hpp> 79 template<
typename MHWalkerParams_,
typename IterCountIntType_ = int,
80 typename RngSeedType_ = std::mt19937::result_type>
108 template<
typename MHRWParamsType>
110 : mhrw_params(
std::forward<MHRWParamsType>(p)),
111 base_seed(base_seed_),
123 template<
typename MHRWParamsType>
125 : mhrw_params(
std::forward<MHRWParamsType>(p)),
127 task_seeds(
std::move(task_seeds_))
132 CDataBase() : mhrw_params(), base_seed(), task_seeds() { }
193 template<
typename TaskNoCountIntType>
196 if (task_seeds.
size() > 0) {
200 << task_seeds.
size()));
208 return base_seed + (RngSeedType)k;
219 <<
"\t# iter. / sweep = " << mhrw_params.
n_sweep <<
"\n" 220 <<
"\t# therm. sweeps = " << mhrw_params.
n_therm <<
"\n" 221 <<
"\t# run sweeps = " << mhrw_params.
n_run <<
"\n";
230 printBasicCDataMHRWInfo(ss);
235 friend boost::serialization::access;
236 template<
typename Archive>
237 void serialize(Archive & a,
unsigned int )
271 template<
typename MHRWStatsResultsType_,
typename IterCountIntType,
typename MHWalkerParams>
292 template<
typename MHRWStatsResultsTypeInit,
293 typename MHRWParamsTypeInit>
295 MHRWParamsTypeInit && mhrw_params_,
296 double acceptance_ratio_)
297 : stats_results(
std::forward<MHRWStatsResultsTypeInit>(stats_results_)),
298 mhrw_params(
std::forward<MHRWParamsTypeInit>(mhrw_params_)),
299 acceptance_ratio(acceptance_ratio_)
310 template<
typename MHRWStatsResultsTypeInit,
typename MHRandomWalkType>
312 const MHRandomWalkType & mhrandomwalk)
313 : stats_results(
std::forward<MHRWStatsResultsTypeInit>(stats_results_)),
314 mhrw_params(mhrandomwalk.mhrwParams()),
315 acceptance_ratio(mhrandomwalk.hasAcceptanceRatio() ?
316 mhrandomwalk.acceptanceRatio() :
317 std::numeric_limits<double>::quiet_NaN())
343 friend boost::serialization::access;
344 template<
typename Archive,
346 void serialize(Archive & a,
unsigned int )
348 MHRWStatsResultsType2 & stats_results_ref = stats_results;
349 a & stats_results_ref;
351 a & acceptance_ratio;
367 template<
typename MHRandomWalkTaskCData,
393 typename Rng::result_type _seed;
412 template<
typename LoggerType>
415 : _seed(inputseed), result(NULL)
417 logger.longdebug(
"MHRandomWalkTask", [&](
std::ostream & stream) {
418 stream <<
"inputseed=" << inputseed;
424 if (result != NULL) {
431 template<
typename LoggerType,
typename TaskMgrIface>
432 struct RunInnerCallable
434 const MHRandomWalkTaskCData * pcdata;
436 LoggerType & baselogger;
437 TaskMgrIface * tmgriface;
439 ResultType ** ppresult;
441 RunInnerCallable(
const MHRandomWalkTaskCData * pcdata_, Rng & rng_, LoggerType & logger_,
442 TaskMgrIface * tmgriface_, ResultType ** ppresult_)
443 : pcdata(pcdata_), rng(rng_), baselogger(logger_), tmgriface(tmgriface_), ppresult(ppresult_)
446 ~RunInnerCallable() {
449 template<
typename MHWalkerType,
typename MHRWStatsCollectorType,
typename MHRWControllerType>
450 inline void operator()(MHWalkerType & mhwalker, MHRWStatsCollectorType & stats,
451 MHRWControllerType & controller)
459 OurStatusReportCheck statreportcheck(
461 [&]() {
return this->tmgriface->statusReportRequested(); },
463 [&](StatusReportType report) { this->tmgriface->submitStatusReport(
std::move(report)); }
468 OurStatsCollectors ourstatscollectors(stats, statreportcheck);
470 logger.longdebug(
"About to creat MHRandomWalk instance");
475 MHRandomWalkType rwalk(
490 logger.longdebug(
"MHRandomWalk object created, running...");
494 logger.longdebug(
"MHRandomWalk run finished.");
496 *ppresult =
new ResultType(stats.stealResult(), rwalk);
499 template<
typename MHWalkerType,
typename MHRWStatsCollectorType>
500 inline void operator()(MHWalkerType & mhwalker, MHRWStatsCollectorType & stats)
503 operator()<MHWalkerType, MHRWStatsCollectorType,
MHRWNoController>(mhwalker, stats, ctrl) ;
518 template<
typename LoggerType,
typename TaskManagerIface>
519 inline void run(
const MHRandomWalkTaskCData * pcdata, LoggerType & baselogger, TaskManagerIface * tmgriface)
524 stream <<
"Starting random walk, using rng seed = " << _seed;
529 RunInnerCallable<LoggerType, TaskManagerIface> run_object(pcdata, rng, baselogger, tmgriface, &result);
531 pcdata->setupRandomWalkAndRun(rng, baselogger, run_object);
534 inline ResultType getResult()
const 539 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.
MHRWParamsType mhrw_params
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.
std::vector< RngSeedType > task_seeds
A list of random seeds to use for each task.
void run(const MHRandomWalkTaskCData *pcdata, LoggerType &baselogger, TaskManagerIface *tmgriface)
Run this task.
CDataBase()
Construct an invalid object – ONLY for use with Boost.serialization.
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.
RngSeedType base_seed
A base random seed from which each run seed will be derived.
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.
CDataBase(MHRWParamsType &&p, std::vector< RngSeedType > task_seeds_)
Constructor.
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.
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_)
Constructor, initializes fields to the given values.
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.
#define streamstr(tokens)
Utility macro to format stream tokens to a std::string.
Routines for performing a Metropolis-Hastings 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.