GrainQueue< TGrain > Class Template Reference
Simple difficulty-based work queue for easy parallelization.
More...
List of all members.
Public Types |
typedef TGrain | Grain |
typedef TGrain | Grain |
Public Member Functions |
void | Init () |
| Initializes.
|
void | Init () |
| Initializes.
|
Grain * | Pop () |
| Pops the most desirable grain to work on.
|
Grain * | Pop () |
| Pops the most desirable grain to work on.
|
void | Put (double difficulty, Grain *grain) |
| Puts a grain into the queue to be dispatched.
|
void | Put (double difficulty, Grain *grain) |
| Puts a grain into the queue to be dispatched.
|
index_t | size () const |
| Gets the size of this queue.
|
index_t | size () const |
| Gets the size of this queue.
|
Detailed Description
template<typename TGrain>
class GrainQueue< TGrain >
Simple difficulty-based work queue for easy parallelization.
To use this, simply divide up your work into grains (as shown below) that are probably a bit smaller than what one thread should be able to handle. Enqueue each grain into the GrainQueue, associating it with a "difficulty" measure which should estimate the relative amount of time for each grain. You can then use this to automatically run a number of threads.
To allow grains to be sent over multiple machines, put only basic data into your grains, and associate the grain queue with a context, which is a pointer to something. This context will be passed to every grain when it is run.
TODO: This has a bug that, if any of the threads find an empty queue, that thread will die. This would prohibit you from recursively building a kd-tree or such, as after the root node is de-queued all the other threads would die because they think there is no work to do.
This class is thread safe, so it is perfectly fine for grains to put more work on the grain queue.
TODO: Update documentation to reflect the fact that ThreadedGrainRunner is a separate class now.
struct SolverGrain {
Solver *solver;
int a;
int b;
~SolverGrain() {}
SolverGrain(Solver *solver_in, int a_in, int b_in) {
solver = solver_in;
a = a_in;
b = b_in;
}
void Run() {
solver->Solve(a, b);
}
};
class Solver {
void SolveRange(int a_min, int a_max, int b_min, int b_max) {
GrainQueue<SolverGrain> queue;
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
queue->Put(a * b, new SolverGrain(this, a, b));
}
}
}
void Solve(int a, int b) {....}
}
Definition at line 107 of file grain.h.
Member Function Documentation
template<typename TGrain>
Initializes.
Definition at line 122 of file grain.h.
template<typename TGrain>
Initializes.
Definition at line 122 of file grain.h.
template<typename TGrain>
Pops the most desirable grain to work on.
You might not have to call this yourself.
Definition at line 142 of file grain.h.
template<typename TGrain>
Pops the most desirable grain to work on.
You might not have to call this yourself.
Definition at line 142 of file grain.h.
template<typename TGrain>
void GrainQueue< TGrain >::Put |
( |
double |
difficulty, |
|
|
Grain * |
grain | |
|
) |
| | [inline] |
Puts a grain into the queue to be dispatched.
- Parameters:
-
| difficulty | relative problem difficulty |
Definition at line 131 of file grain.h.
template<typename TGrain>
void GrainQueue< TGrain >::Put |
( |
double |
difficulty, |
|
|
Grain * |
grain | |
|
) |
| | [inline] |
Puts a grain into the queue to be dispatched.
- Parameters:
-
| difficulty | relative problem difficulty |
Definition at line 131 of file grain.h.
template<typename TGrain>
index_t GrainQueue< TGrain >::size |
( |
|
) |
const [inline] |
Gets the size of this queue.
Definition at line 152 of file grain.h.
template<typename TGrain>
index_t GrainQueue< TGrain >::size |
( |
|
) |
const [inline] |
Gets the size of this queue.
Definition at line 152 of file grain.h.
The documentation for this class was generated from the following files: