SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WorkUnitProducer.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9  License for the specific language governing rights and limitations under
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 #ifndef ENGINE_SCHEDULER_DYNAMICEXECUTOR_WORKUNITPRODUCER_H
30 #define ENGINE_SCHEDULER_DYNAMICEXECUTOR_WORKUNITPRODUCER_H
31 
36 #include <Core/Thread/Mutex.h>
37 #include <boost/foreach.hpp>
38 #include <boost/thread.hpp>
39 
41 
42 namespace SCIRun {
43  namespace Dataflow {
44  namespace Engine {
45  namespace DynamicExecutor {
46 
47  class SCISHARE ModuleProducer : public ProducerInterface, boost::noncopyable
48  {
49  public:
52  lookup_(lookup), bounds_(bounds), network_(network), lock_(lock),
53  work_(work), doneCount_(0), shouldLog_(SCIRun::Core::Logging::Log::get().verbose())
54  {
55  log_.setVerbose(shouldLog_);
56  }
57 
58  virtual void enqueueReadyModules() const
59  {
60  Core::Thread::Guard g(lock_->get());
61  if (!isDone())
62  {
63  auto order = scheduler_.schedule(*network_);
64  if (shouldLog_)
65  {
66  std::ostringstream ostr;
67  ostr << "Producer received this ordering: \n" << order << std::endl;
68  log_ << Core::Logging::DEBUG_LOG << ostr.str() << std::endl;
69  log_ << Core::Logging::DEBUG_LOG << "Producer processing min group " << order.minGroup();
70  }
71  auto groupIter = order.getGroup(order.minGroup());
72  BOOST_FOREACH(const ParallelModuleExecutionOrder::ModulesByGroup::value_type& mod, groupIter)
73  {
74  auto module = network_->lookupModule(mod.second);
75 
76  if (module->executionState() == Networks::ModuleInterface::Waiting)
77  {
78  if (shouldLog_)
79  log_ << Core::Logging::DEBUG_LOG << "Producer pushing module " << mod.second << std::endl;
80 
81  if (doneIds_.find(mod.second) != doneIds_.end())
82  {
83  if (shouldLog_)
84  SCIRun::Core::Logging::Log::get() << SCIRun::Core::Logging::INFO << "Module producer: wants to enqueue module " << mod.second << " a second time." << std::endl;
85  }
86  else
87  {
88  work_->push(module);
89  doneIds_.insert(mod.second);
90  doneCount_.fetch_add(1);
91 
92  if (shouldLog_)
93  log_ << Core::Logging::DEBUG_LOG << "Producer status: " << doneCount_ << " out of " << network_->nmodules() << std::endl;
94  }
95  }
96  }
97  }
98  }
99 
100  void operator()() const
101  {
102  ScopedExecutionBoundsSignaller signaller(bounds_, [=]() { return lookup_->errorCode(); });
103 
104  if (shouldLog_)
105  log_ << Core::Logging::DEBUG_LOG << "Producer started" << std::endl;
106 
107  enqueueReadyModules();
108 
109  while (!isDone())
110  boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
111 
112  if (shouldLog_)
113  log_ << Core::Logging::DEBUG_LOG << "Producer is done." << std::endl;
114  }
115 
116  bool isDone() const
117  {
118  return doneCount_ >= network_->nmodules();
119  }
120  private:
121 
122  struct ModuleWaiting
123  {
124  bool operator()(Networks::ModuleHandle mh) const
125  {
126  return mh->executionState() != Networks::ModuleInterface::Completed;
127  }
128  };
129 
130  static ModuleWaiting filter() { return ModuleWaiting(); }
131  static BoostGraphParallelScheduler scheduler_;
132  const Networks::ExecutableLookup* lookup_;
133  const ExecutionBounds& bounds_;
134  const Networks::NetworkInterface* network_;
135  Core::Thread::Mutex* lock_;
136  ModuleWorkQueuePtr work_;
137  mutable boost::atomic<int> doneCount_;
138  mutable std::set<Networks::ModuleId> doneIds_;
139  static Core::Logging::Log& log_;
140  bool shouldLog_;
141  };
142 
143  typedef boost::shared_ptr<ModuleProducer> ModuleProducerPtr;
144 
145  }}
146 
147  }}
148 
149 #endif
virtual void enqueueReadyModules() const
Definition: WorkUnitProducer.h:58
#define SCISHARE
Definition: share.h:39
bool isDone() const
Definition: WorkUnitProducer.h:116
Definition: Mutex.h:43
boost::shared_ptr< ModuleProducer > ModuleProducerPtr
Definition: WorkUnitProducer.h:143
Definition: Log.h:59
boost::shared_ptr< ModuleWorkQueue > ModuleWorkQueuePtr
Definition: WorkQueue.h:49
Definition: SchedulerInterfaces.h:56
static Log & get()
Definition: Log.cc:183
void operator()() const
Definition: WorkUnitProducer.h:100
boost::shared_ptr< ModuleInterface > ModuleHandle
Definition: NetworkFwd.h:74
ModuleProducer(const Networks::ExecutableLookup *lookup, const ExecutionBounds &bounds, const Networks::NetworkInterface *network, Core::Thread::Mutex *lock, ModuleWorkQueuePtr work)
Definition: WorkUnitProducer.h:50
boost::lock_guard< boost::mutex > Guard
Definition: Mutex.h:55
Definition: WorkUnitProducerInterface.h:39
Definition: NetworkInterface.h:74
Definition: NetworkInterface.h:48
Definition: Log.h:64