Salome HOME
fbb2cae7003d927f6b06f9edbbe0c5bec65f66d4
[modules/yacs.git] / src / engine / Executor.hxx
1 // Copyright (C) 2006-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef __EXECUTOR_HXX__
21 #define __EXECUTOR_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "Mutex.hxx"
25 #include "Thread.hxx"
26 #include "Semaphore.hxx"
27 #include "Exception.hxx"
28 #include "define.hxx"
29
30 #ifdef WIN32
31 #include <windows.h>
32 #pragma comment(lib,"winmm.lib")
33 #endif
34 #include <list>
35 #include <vector>
36 #include <set>
37 #include <string>
38 #include <fstream>
39 #include <ctime>
40 #include <chrono>
41
42 namespace WorkloadManager
43 {
44   struct RunInfo;
45 }
46
47 namespace YACS
48 {
49   namespace ENGINE
50   {
51     class Scheduler;
52     class ComposedNode;
53     class Task;
54     
55 /*! \brief Threaded Executor 
56  *
57  * \ingroup Executors
58  *
59  *
60  */
61     class YACSLIBENGINE_EXPORT Executor
62     {
63     protected:
64       Scheduler *_mainSched;
65       ComposedNode *_root;
66       int _nbOfConcurrentThreads;
67       YACS::BASES::Mutex _mutexForNbOfConcurrentThreads;
68       YACS::BASES::Condition _condForNewTasksToPerform;
69       YACS::BASES::Semaphore _semForMaxThreads;
70       YACS::BASES::Condition _condForStepByStep;
71       YACS::BASES::Condition _condForPilot;
72       YACS::BASES::Mutex _mutexForSchedulerUpdate;
73       YACS::BASES::Mutex _mutexForTrace;
74       bool _toContinue;
75       bool _isOKToEnd;
76       bool _stopOnErrorRequested;
77       bool _dumpOnErrorRequested;
78       bool _errorDetected;
79       bool _isRunningunderExternalControl;
80       bool _isWaitingEventsFromRunningTasks;
81       int _numberOfRunningTasks;
82       std::set<Task *> _runningTasks; 
83       int _numberOfEndedTasks;
84       int _semThreadCnt;
85       YACS::ExecutorState _executorState;
86       YACS::ExecutionMode _execMode;
87       std::list<std::string> _listOfBreakPoints;
88       std::list<std::string> _listOfTasksToLoad;
89       std::vector<Task *> _tasks; 
90       std::vector<Task *> _tasksSave; 
91       std::list< YACS::BASES::Thread * > _groupOfAllThreadsCreated;
92       std::ofstream _trace;
93       std::string _dumpErrorFile;
94       bool _keepGoingOnFail;
95       //! specifies if scope DynParaLoop is active or not. False by default.
96       bool _DPLScopeSensitive;
97       std::chrono::steady_clock::time_point _start;
98     public:
99       Executor();
100       virtual ~Executor();
101       void RunA(Scheduler *graph,int debug=0, bool fromScratch=true);
102       void RunW(Scheduler *graph,int debug=0, bool fromScratch=true);
103       void RunB(Scheduler *graph,int debug=0, bool fromScratch=true);
104       void runWlm(Scheduler *graph,int debug=0, bool fromScratch=true);
105       void setKeepGoingProperty(bool newVal) { _keepGoingOnFail=newVal; }
106       bool getKeepGoingProperty() const { return _keepGoingOnFail; }
107       void setDPLScopeSensitive(bool newVal) { _DPLScopeSensitive=newVal; }
108       bool getDPLScopeSensitive() const { return _DPLScopeSensitive; }
109       YACS::ExecutionMode getCurrentExecMode();
110       YACS::ExecutorState getExecutorState();
111       void setExecMode(YACS::ExecutionMode mode);
112       void setListOfBreakPoints(std::list<std::string> listOfBreakPoints);
113       std::list<std::string> getTasksToLoad();
114       bool setStepsToExecute(std::list<std::string> listToExecute);
115       bool resumeCurrentBreakPoint();
116       bool isNotFinished();
117       void stopExecution();
118       bool saveState(const std::string& xmlFile);
119       bool loadState();
120       int getNbOfThreads();
121       int getNumberOfRunningTasks() const { return _numberOfRunningTasks; }
122       void displayDot(Scheduler *graph);
123       void setStopOnError(bool dumpRequested=false, std::string xmlFile="");
124       void unsetStopOnError();
125       void waitPause();
126       bool suspendASAP();
127       void resume(bool suspended);
128       static int _maxThreads;
129       static size_t _threadStackSize;
130       YACS::BASES::Mutex& getTheMutexForSchedulerUpdate() { return _mutexForSchedulerUpdate; }
131       ///// new executor !!!!!
132       void loadTask(Task *task, const WorkloadManager::RunInfo& runInfo);
133       YACS::Event runTask(Task *task);
134       void makeDatastreamConnections(Task *task);
135       void beginTask(Task *task);
136       void endTask(Task *task, YACS::Event ev);
137       void failTask(Task *task, const std::string& message);
138       ////////////
139     protected:
140       bool checkBreakPoints();
141       void waitResume();
142       void loadTask(Task *task, const Executor *execInst);
143       void loadParallelTasks(const std::vector<Task *>& tasks, const Executor *execInst);
144       void launchTasks(const std::vector<Task*>& tasks);
145       void launchTask(Task *task);
146       void wakeUp();
147       void sleepWhileNoEventsFromAnyRunningTask();
148       void notifyEndOfThread(YACS::BASES::Thread *thread);
149       void traceExec(Task *task, const std::string& message, const std::string& placement);
150       void _displayDot(Scheduler *graph);
151       virtual void sendEvent(const std::string& event);
152       static void FilterTasksConsideringContainers(std::vector<Task *>& tsks);
153       static std::string ComputePlacement(Task *zeTask);
154     protected:
155       static void *functionForTaskLoad(void *);
156       static void *functionForTaskExecution(void *);
157     };
158   }
159 }
160
161 #endif