Changeset 3402


Ignore:
Timestamp:
Mar 31, 2015, 7:02:58 AM (8 years ago)
Author:
Peter
Message:

add interface to set priority of Scheduler::Job and make the order the tasks are executed closer to FIFO. refs #800

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Makefile.am

    r3399 r3402  
    55# Copyright (C) 2005 Jari Häkkinen, Peter Johansson
    66# Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
    7 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Peter Johansson
     7# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Peter Johansson
    88#
    99# This file is part of the yat library, http://dev.thep.lu.se/yat
     
    8282  test/score.test \
    8383  test/scheduler.test \
     84  test/scheduler2.test \
    8485  test/segment.test test/smart_ptr.test \
    8586  test/smith_waterman.test \
  • trunk/yat/utility/Scheduler.cc

    r3401 r3402  
    2626#include <cassert>
    2727
     28#include <iostream>
     29
    2830namespace theplu {
    2931namespace yat {
     
    3133
    3234  Scheduler::Scheduler(unsigned int threads)
    33     : running_jobs_(0)
     35    : running_jobs_(0), job_counter_(0)
    3436  {
    3537    assert(threads);
     
    6971      return;
    7072    job->status_ = Job::prepared;
     73    job->id_ = job_counter_;
     74    ++job_counter_;
    7175
    7276    // If we have prerequisite that need to be run first, process them
     
    128132  // Scheduler::Job
    129133
    130   Scheduler::Job::Job(void)
    131     : status_(pristine) {}
     134  Scheduler::Job::Job(unsigned int prio)
     135    : status_(pristine), priority_(prio), id_(0) {}
    132136
    133137
     
    136140
    137141
     142  unsigned int Scheduler::Job::priority(void) const
     143  {
     144    return priority_;
     145  }
     146
     147
     148  bool Scheduler::LowerPriority::operator()(const JobPtr& lhs,
     149                                            const JobPtr& rhs) const
     150  {
     151    if (rhs.get() == NULL)
     152      return false;
     153    if (lhs.get() == NULL)
     154      return true;
     155
     156    if (lhs->priority() != rhs->priority())
     157      return lhs->priority() < rhs->priority();
     158
     159    return lhs->id_ > rhs->id_;
     160  }
     161
     162
    138163  // Scheduler::Worker
    139164
    140   Scheduler::Worker::Worker(Queue<boost::shared_ptr<Job> >& q,
    141                             Queue<boost::shared_ptr<Job> >& c)
     165  Scheduler::Worker::Worker(JobQueue& q, Queue<JobPtr>& c)
    142166    : queue_(q), completed_(c)
    143167  {
  • trunk/yat/utility/Scheduler.h

    r3401 r3402  
    2323*/
    2424
     25#include "PriorityQueue.h"
    2526#include "Queue.h"
    2627
     
    4142     series of Jobs. The Jobs can have dependencies, such that Job X
    4243     must finish before Job Y can run, and the Scheduler takes care of
    43      these dependencies.Here is a small code example in which two
     44     these dependencies. Here is a small code example in which two
    4445     threads are used to process the four jobs, \c MyJob. The jobs
    4546     have a dependency that job4 can not run until the three first
     
    5758     scheduler.add_dependency(job4, job3);
    5859     scheduler.submit(job4);
    59      scheduler.launch();
     60     scheduler.wait();
    6061
    6162     \endcode
     63
     64     The Scheduler sends jobs to the workers taking into account
     65     dependencies, priorities and the order in which the Scheduler has
     66     seen the Jobs. Of the Jobs that are ready to run, i.e., all jobs
     67     it depends on have completed, the Scheduler chose the Job with
     68     highest priority. If two Jobs have the same priority, the
     69     Scheduler sends them in the order of appearance, i.e., if the
     70     Scheduler saw Job X before Job Y, Job X is run before Job X.
     71
     72     \since New in yat 0.13
    6273   */
    6374  class Scheduler
     
    7384         \brief constructor
    7485       */
    75       Job(void);
     86      explicit Job(unsigned int prio=0);
    7687
    7788      /**
     
    7990       */
    8091      virtual ~Job(void);
     92
     93      /**
     94         Jobs with greater priority are run before Jobs with less priority.
     95       */
     96      unsigned int priority(void) const;
    8197
    8298      /**
     
    92108      enum status { pristine, prepared, running, completed};
    93109      status status_;
     110      unsigned priority_;
     111      unsigned id_;
    94112    }; // end class Job
    95113
     
    124142
    125143  private:
     144    typedef boost::shared_ptr<Scheduler::Job> JobPtr;
     145
     146    struct LowerPriority
     147    {
     148      bool operator()(const JobPtr& lhs, const JobPtr& rhs) const;
     149    };
     150
     151    // some typedefs for convenience
     152    typedef PriorityQueue<JobPtr, LowerPriority> JobQueue;
     153
    126154    // \internal class that does the job
    127155    //
     
    133161    {
    134162    public:
    135       Worker(Queue<boost::shared_ptr<Job> >& queue,
    136              Queue<boost::shared_ptr<Job> >& completed);
     163      Worker(JobQueue& queue, Queue<JobPtr>& completed);
    137164      void operator()(void);
    138165    private:
    139       Queue<boost::shared_ptr<Job> >& queue_;
    140       Queue<boost::shared_ptr<Job> >& completed_;
     166      JobQueue& queue_;
     167      Queue<JobPtr>& completed_;
    141168    }; // end class Worker
    142169
     
    155182    void queue(boost::shared_ptr<Job> job);
    156183
    157     typedef boost::shared_ptr<Scheduler::Job> JobPtr;
    158184    int running_jobs_;
    159185
    160     Queue<boost::shared_ptr<Job> > queue_;
    161     Queue<boost::shared_ptr<Job> > completed_;
     186    JobQueue queue_;
     187    Queue<JobPtr> completed_;
    162188    boost::thread_group workers_;
    163 
     189    unsigned long int job_counter_;
    164190  }; // end class Scheduler
    165191
Note: See TracChangeset for help on using the changeset viewer.