this repo has no description
1/************************************************************ WorkManager.hh
2 Copyright (c) 2010 Universite d'Orleans - Jeremie Vautard
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 *************************************************************************/
22
23#ifndef __QECODE_WORKMANAGER__
24#define __QECODE_WORKMANAGER__
25
26#include <time.h>
27#include <list>
28#include <queue>
29#include <vector>
30#include "Strategy.hh"
31#include "qecode.hh"
32#include "AbstractWorker.hh"
33#include "Work.hh"
34#include "QCOPPlus.hh"
35#include "gecode/minimodel.hh"
36#include "gecode/support.hh"
37#include "gecode/search.hh"
38#include "gecode/search/support.hh"
39
40
41using namespace Gecode;
42using namespace std;
43using namespace Gecode::Support;
44using namespace Gecode::Search;
45
46class QECODE_VTABLE_EXPORT WorkComparator {
47 public :
48 virtual bool cmp(QWork a,QWork b)=0;
49};
50
51class QECODE_VTABLE_EXPORT WorkPool {
52private:
53 list<QWork> l;
54 WorkComparator* cmp;
55public:
56 QECODE_EXPORT WorkPool(WorkComparator* a);
57 QECODE_EXPORT void push(QWork q);
58 QECODE_EXPORT QWork pop();
59 QECODE_EXPORT bool empty() {return l.empty();}
60 QECODE_EXPORT QWork top() {return l.front();}
61 QECODE_EXPORT int size() {return l.size();}
62
63 QECODE_EXPORT void trash(vector<int> prefix);
64};
65
66
67class QECODE_VTABLE_EXPORT WorkManager {
68private:
69 bool finished;
70 Strategy S;
71 WorkPool Todos;
72 list<AQWorker*> actives;
73 list<AQWorker*> idles;
74 void getNewWorks();
75 void updateTrue(Strategy s);
76 void updateFalse(Strategy s);
77 void trashWorks(vector<int> prefix);
78 Strategy result;
79 Gecode::Support::Mutex mex; // = PTHREAD_MUTEX_INITIALIZER;
80public:
81 Qcop* problem;
82
83 QECODE_EXPORT WorkManager(Qcop* p,WorkComparator* c);
84
85 QECODE_EXPORT QWork getWork(AQWorker* worker);
86
87 QECODE_EXPORT void returnWork(AQWorker* worker,Strategy ret,list<QWork> todo,vector<int> position);
88
89 QECODE_EXPORT forceinline Strategy getResult() {return S;}
90
91 QECODE_EXPORT forceinline bool isFinished() {mex.acquire();bool ret=finished;mex.release();return ret;}
92
93 QECODE_EXPORT void printStatus();
94};
95
96#endif