From cbb77ac458cb031890953035df5e53ea0fcb2031 Mon Sep 17 00:00:00 2001 From: bothameister Date: Tue, 6 Jul 2010 22:38:21 +0000 Subject: Added simple multi-threading during hyperparameter resampling. Added cmdarg for controlling number of threads. Moved Timer to its own header file. Cleaned up Makefile.am git-svn-id: https://ws10smt.googlecode.com/svn/trunk@170 ec762483-ff6d-05da-a07a-a48fb63a330f --- gi/pyp-topics/src/workers.hh | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 gi/pyp-topics/src/workers.hh (limited to 'gi/pyp-topics/src/workers.hh') diff --git a/gi/pyp-topics/src/workers.hh b/gi/pyp-topics/src/workers.hh new file mode 100644 index 00000000..1f496acf --- /dev/null +++ b/gi/pyp-topics/src/workers.hh @@ -0,0 +1,62 @@ +#ifndef WORKERS_HH +#define WORKERS_HH + +#include +#include +#include +#include +#include +#include + +//#include + +#include "timing.h" + +template +class SimpleWorker +{ +typedef boost::packaged_task PackagedTask; +public: + SimpleWorker(J& job) : job(job), tasktime(0.0) + { + PackagedTask task(boost::bind(&SimpleWorker::run, this)); + future = task.get_future(); + boost::thread t(boost::move(task)); + } + + R run() //this is called upon thread creation + { + R wresult = 0; + + assert(job); + timer.Reset(); + wresult = job(); + tasktime = timer.Elapsed(); + return wresult; + } + + R getResult() + { + if (!future.is_ready()) + future.wait(); + assert(future.is_ready()); + return future.get(); + } + + double getTaskTime() + { + return tasktime; + } + +private: + + J job; + + boost::unique_future future; + + Timer timer; + double tasktime; + +}; + +#endif -- cgit v1.2.3