SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WorkQueue.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) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
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 
30 
31 ///
32 ///@file WorkQueue.h
33 ///@brief Manage assignments of work
34 ///
35 ///@author Steve Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date June 1997
39 ///
40 
41 #ifndef Core_Thread_WorkQueue_h
42 #define Core_Thread_WorkQueue_h
43 
45 
46 #include <vector>
47 
48 
50 namespace SCIRun {
51  struct WorkQueue_private;
52 
53 /**************************************
54 
55 @class
56  WorkQueue
57 
58 KEYWORDS
59  Thread, Work
60 
61 @details
62  Doles out work assignment to various worker threads. Simple
63  attempts are made at evenly distributing the workload.
64  Initially, assignments are relatively large, and will get smaller
65  towards the end in an effort to equalize the total effort.
66 
67 ****************************************/
69  public:
70  //////////
71  /// Make an empty work queue with no assignments.
72  WorkQueue(const char* name);
73 
74  //////////
75  /// Fill the work queue with the specified total number of work
76  /// assignments. <i>num_threads</i> specifies the approximate
77  /// number of threads which will be working from this queue.
78  /// The optional <i>granularity</i> specifies the degree to
79  /// which the tasks are divided. A large granularity will
80  /// create more assignments with smaller assignments. A
81  /// granularity of zero will recieve a single assignment of
82  /// approximately uniform size. <i>name</i> should be a static
83  /// string which describes the primitive for debugging purposes.
84  void refill(int totalAssignments, int nthreads,
85  int granularity=5);
86 
87  //////////
88  /// Destroy the work queue. Any unassigned work will be lost.
89  ~WorkQueue();
90 
91  //////////
92  /// Called by each thread to get the next assignment. If
93  /// <i>nextAssignment</i> returns true, the thread has a valid
94  /// assignment, and then would be responsible for the work
95  /// from the returned <i>start</i> through <i>end-l</i>.
96  /// Assignments can range from 0 to <i>totalAssignments</i>-1.
97  /// When <i>nextAssignment</i> returns false, all work has
98  /// been assigned.
99  bool nextAssignment(int& start, int& end);
100 
101  private:
102  const char* name_;
103  int num_threads_;
104  int total_assignments_;
105  int granularity_;
106  std::vector<int> assignments_;
107  AtomicCounter current_assignment_;
108  bool dynamic_;
109 
110  void fill();
111 
112  // Cannot copy them
113  WorkQueue(const WorkQueue& copy);
114  WorkQueue& operator=(const WorkQueue&);
115  };
116 } // End namespace SCIRun
117 
118 #endif
119 
Definition: WorkQueue.h:68
#define SCISHARE
Definition: share.h:39
Definition: AtomicCounter.h:70
const char * name[]
Definition: BoostGraphExampleTests.cc:87
std::queue< UnitPtr > WorkQueue
Definition: SchedulingWithBoostGraph.cc:411