Tomographer  v2.0
Tomographer C++ Framework Documentation
signal_status_report.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 
28 #ifndef TOMOGRAPHER_TOOLS_SIGNAL_STATUS_REPORT
29 #define TOMOGRAPHER_TOOLS_SIGNAL_STATUS_REPORT
30 
31 
40 #include <tomographer2/multiproc.h>
41 
42 
43 namespace Tomographer
44 {
45 namespace Tools
46 {
47 
53 template<typename TaskDispatcher, typename Logger,
54  typename TimerClock = std::chrono::system_clock>
56  : public SignalHandler
57 {
60 
61  SigHandlerTaskDispatcherStatusReporter(TaskDispatcher * tasks_, Logger & logger_)
62  : tasks(tasks_), logger(logger_), time_start()
63  {
64  tasks->setStatusReportHandler(
65  [this](const FullStatusReportType& report) {
66  logger.debug("SigHandlerStatusReporter/lambda", "intermediate progress report lambda called");
67  this->intermediateProgressReport(report);
68  });
69  }
70 
71  TaskDispatcher * tasks;
72  Logger & logger;
73 
74  typename TimerClock::time_point time_start;
75 
76  virtual void handleSignal(int /*sig*/)
77  {
78  tasks->requestStatusReport();
79  }
80 
84  void intermediateProgressReport(const FullStatusReportType& report)
85  {
86  std::string elapsed = fmtDuration(TimerClock::now() - time_start);
87  fprintf(stderr,
88  "\n"
89  "=========================== Intermediate Progress Report ============================\n"
90  " (hit Ctrl+C within %2d sec. to interrupt)\n"
91  " Total Completed Runs: %d/%d: %5.2f%%\n"
92  " %s total elapsed\n",
93  TOMOGRAPHER_SIG_HANDLER_REPEAT_EXIT_DELAY,
94  report.num_completed, report.num_total_runs,
95  (double)report.num_completed/report.num_total_runs*100.0,
96  elapsed.c_str());
97  if (report.workers_running.size() == 1) {
98  if (report.workers_running[0]) {
99  fprintf(stderr, "%s\n", report.workers_reports[0].msg.c_str());
100  }
101  } else if (report.workers_running.size() > 1) {
102  fprintf(stderr,
103  "Current Run(s) information (workers working/spawned %d/%d):\n",
104  (int)std::count(report.workers_running.begin(), report.workers_running.end(), true),
105  (int)report.workers_running.size()
106  );
107  for (unsigned int k = 0; k < report.workers_running.size(); ++k) {
108  std::string msg = report.workers_running[k] ? report.workers_reports[k].msg : std::string("<idle>");
109  fprintf(stderr, "=== #%2u: %s\n", k, msg.c_str());
110  }
111  } else {
112  // no info. (workers_running.size() == 0)
113  }
114  fprintf(stderr,
115  "=====================================================================================\n\n");
116  };
117 
118 };
119 
120 template<typename TaskDispatcher, typename LoggerT>
122 makeSigHandlerTaskDispatcherStatusReporter(TaskDispatcher * tasks, LoggerT & logger)
123 {
125 }
126 
127 
128 } // namespace Tools
129 } // namespace Tomographer
130 
131 
132 #endif
Base namespace for the Tomographer project.
Definition: densellh.h:44
std::vector< TaskStatusReportType, typename Tools::NeedOwnOperatorNew< TaskStatusReportType >::AllocatorType > workers_reports
List with the raw report submitted from each individual thread.
Definition: multiproc.h:117
T end(T...args)
std::string fmtDuration(double seconds)
Format a number of seconds into a human-readable string.
Definition: fmt.h:366
STL class.
An abstract signal handler (C++ interface)
int num_total_runs
Total number of tasks to perform.
Definition: multiproc.h:95
std::vector< bool > workers_running
List specifying for each worker (e.g. a spawned thread) whether it is active or not.
Definition: multiproc.h:105
T count(T...args)
void intermediateProgressReport(const FullStatusReportType &report)
Format a nice intermediate progress report.
T size(T...args)
Some common definitions for multiprocessing interfaces.
A generic handler which requests a status report from an OMPTaskDispatcher.
T begin(T...args)
T c_str(T...args)
Basic common code for intercepting a signal and basic interfacing to C++.
int num_completed
Number of completed tasks.
Definition: multiproc.h:92
A complete status report, abstract version.
Definition: multiproc.h:81