27 #ifndef TOMOGRAPHER_MHWALKER_TASKS_H
28 #define TOMOGRAPHER_MHWALKER_TASKS_H
67 template<
typename CountIntType_ =
unsigned int,
typename RealType_ =
double>
123 template<
typename MHRWStatsCollectorResultType_,
typename CountIntType>
126 typedef MHRWStatsCollectorResultType_ MHRWStatsCollectorResultType;
128 template<
typename MHRWStatsCollectorResultTypeInit,
typename MHRandomWalkType>
130 const MHRandomWalkType & mhrandomwalk)
131 : stats_collector_result(std::forward<MHRWStatsCollectorResultTypeInit>(stats_collector_result_)),
132 n_sweep(mhrandomwalk.n_sweep()),
133 n_therm(mhrandomwalk.n_therm()),
134 n_run(mhrandomwalk.n_run()),
135 step_size(mhrandomwalk.step_size()),
136 acceptance_ratio(mhrandomwalk.acceptance_ratio())
140 const MHRWStatsCollectorResultType stats_collector_result;
142 const CountIntType n_sweep;
143 const CountIntType n_therm;
144 const CountIntType n_run;
145 const double step_size;
146 const double acceptance_ratio;
159 template<
typename MHRandomWalkTaskCData,
163 typedef typename MHRandomWalkTaskCData::CountIntType CountIntType;
183 CountIntType kstep_ = 0, CountIntType n_sweep_ = 0,
184 CountIntType n_therm_ = 0, CountIntType n_run_ = 0,
185 double acceptance_ratio_ = 0.0)
222 typename Rng::result_type _seed;
243 static inline int get_input(
int k,
const MHRandomWalkTaskCData * pcdata)
245 return pcdata->base_seed + k;
253 template<
typename LoggerType>
255 : _seed(inputseed), result(NULL)
257 logger.longdebug(
"MHRandomWalkTask",
"() inputseed=%d", inputseed);
262 if (result != NULL) {
277 template<
typename LoggerType,
typename TaskManagerIface>
278 inline void run(
const MHRandomWalkTaskCData * pcdata, LoggerType & logger,
279 TaskManagerIface * tmgriface)
284 auto stats = pcdata->createStatsCollector(logger);
285 typedef decltype(stats) MHRWStatsCollectorType;
288 typedef StatusReportCheck<TaskManagerIface, MHRWStatsCollectorType> OurStatusReportCheck;
289 OurStatusReportCheck statreportcheck(
this, &stats, tmgriface);
293 OurStatsCollectors ourstatscollectors(stats, statreportcheck);
295 auto mhwalker = pcdata->createMHWalker(rng, logger);
296 typedef decltype(mhwalker) MHWalkerType;
316 result =
new Result(stats.getResult(), rwalk);
319 inline const Result & getResult()
const
330 template<
typename TaskManagerIface,
typename MHRWStatsCollectorType>
331 struct StatusReportCheck
333 StatusReportCheck(
MHRandomWalkTask * mhrwtask_, MHRWStatsCollectorType * stats_, TaskManagerIface *tmgriface_)
334 : mhrwtask(mhrwtask_), stats(stats_), tmgriface(tmgriface_)
338 MHRWStatsCollectorType *stats;
339 TaskManagerIface *tmgriface;
341 inline void init() { }
342 inline void thermalizing_done() { }
343 inline void done() { }
345 template<
typename Po
intType,
typename FnValueType,
typename MHRandomWalk>
346 inline void raw_move(
347 CountIntType k,
bool is_thermalizing,
bool,
bool,
double,
const PointType &, FnValueType,
348 const PointType &, FnValueType, MHRandomWalk & rw
353 if (tmgriface->status_report_requested()) {
355 CountIntType totiters = rw.n_sweep()*(rw.n_therm()+rw.n_run());
356 double fdone = (double)k/totiters;
358 bool warn_accept_ratio =
false;
359 if (rw.has_acceptance_ratio()) {
360 accept_ratio = rw.acceptance_ratio();
361 warn_accept_ratio = (accept_ratio > 0.35 || accept_ratio < 0.2);
364 "%s %lu/(%lu=%lu*(%lu+%lu)) : %5.2f%% done [%saccept ratio=%.2f%s]",
368 (
unsigned long)k, (
unsigned long)totiters, (
unsigned long)rw.n_sweep(),
369 (
unsigned long)rw.n_therm(), (
unsigned long)rw.n_run(),
371 warn_accept_ratio ?
"!!** " :
"",
373 warn_accept_ratio ?
" **!!" :
""
375 typedef MHRWStatsCollectorStatus<MHRWStatsCollectorType> MHRWStatsCollectorStatusType;
376 if (MHRWStatsCollectorStatusType::CanProvideStatus) {
379 std::string s = MHRWStatsCollectorStatusType::getStatus(stats);
388 tmgriface->submit_status_report(StatusReport(fdone, msg, k, rw.n_sweep(), rw.n_therm(),
389 rw.n_run(), accept_ratio));
394 template<
typename Po
intType,
typename FnValueType,
typename MHRandomWalk>
395 inline void process_sample(CountIntType, CountIntType,
const PointType &, FnValueType, MHRandomWalk &)
A Metropolis-Hastings Random Walk.
Utilities for formatting strings.
static int get_input(int k, const MHRandomWalkTaskCData *pcdata)
Returns a random seed to seed the random number generator with for run number k.
double acceptance_ratio
the current acceptance ratio of the random walk (see Tomographer::MHRandomWalk::acceptance_ratio() ) ...
Base namespace for the Tomographer project.
StatusReport(double fdone=0.0, const std::string &msg=std::string(), CountIntType kstep_=0, CountIntType n_sweep_=0, CountIntType n_therm_=0, CountIntType n_run_=0, double acceptance_ratio_=0.0)
Constructor which initializes all fields.
CountIntType n_therm
the number of thermalization sweeps (see MHRandomWalk)
MHRandomWalkTask(int inputseed, const MHRandomWalkTaskCData *, LoggerType &logger)
Constructs the MHRandomWalkTask.
void run(const MHRandomWalkTaskCData *pcdata, LoggerType &logger, TaskManagerIface *tmgriface)
Run this task.
Random Walk task, collecting statistics.
Data needed to be accessible to the working code.
CountIntType kstep
the current iteration number
CDataBase(int base_seed_=0)
Constructor.
RealType step_size
Parameter of the random walk – step size of the random walk.
CountIntType n_sweep
the number of iterations that form a sweep (see MHRandomWalk)
CountIntType n_run
the number of live run sweeps (see MHRandomWalk)
MHRandomWalkTaskResult< typename MHRandomWalkTaskCData::MHRWStatsCollectorResultType, CountIntType > Result
Result type of a single task run.
Status Report for a MHRandomWalkTask.
CountIntType n_therm
Parameter of the random walk – number of thermalizing sweeps.
CountIntType n_total_iters
the total number of iterations required for this random walk
Some common definitions for multiprocessing interfaces.
CountIntType n_sweep
Parameter of the random walk – number of iterations per sweep.
StatusReport StatusReportType
Typedef for StatusReport. This is needed by, e.g. MultiProc::OMP::TaskDispatcher. ...
CountIntType n_run
Parameter of the random walk – number of "live" sweeps.
Routines for performing a Metropolis-Hastings random walk.
CountIntType_ CountIntType
Type used to count the number of iterations.
Basic status report class.
int base_seed
A base random seed from which each run seed will be derived.
A simple MHRWStatsCollector interface which combines several stats collectors.
RealType_ RealType
Type used to specify the step size.