Salome HOME
[EDF30062] and [EDF30014] : addition of --activate-custom-overrides parameter in...
[modules/yacs.git] / src / engine / Executor.hxx
1 // Copyright (C) 2006-2024  CEA, EDF
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 #include <cstdint>
42
43 namespace WorkloadManager
44 {
45   struct RunInfo;
46 }
47
48 namespace YACS
49 {
50   namespace ENGINE
51   {
52     class Scheduler;
53     class ComposedNode;
54     class Task;
55     
56 /*! \brief Threaded Executor 
57  *
58  * \ingroup Executors
59  *
60  *
61  */
62     class YACSLIBENGINE_EXPORT Executor
63     {
64     protected:
65       Scheduler *_mainSched;
66       ComposedNode *_root;
67       int _nbOfConcurrentThreads;
68       YACS::BASES::Mutex _mutexForNbOfConcurrentThreads;
69       YACS::BASES::Condition _condForNewTasksToPerform;
70       YACS::BASES::Semaphore _semForMaxThreads;
71       YACS::BASES::Condition _condForStepByStep;
72       YACS::BASES::Condition _condForPilot;
73       YACS::BASES::Mutex _mutexForSchedulerUpdate;
74       YACS::BASES::Mutex _mutexForTrace;
75       std::uint32_t _maxNbThreads = 10000;
76       bool _toContinue;
77       bool _isOKToEnd;
78       bool _stopOnErrorRequested;
79       bool _dumpOnErrorRequested;
80       bool _errorDetected;
81       bool _isRunningunderExternalControl;
82       bool _isWaitingEventsFromRunningTasks;
83       int _numberOfRunningTasks;
84       std::set<Task *> _runningTasks; 
85       int _numberOfEndedTasks;
86       int _semThreadCnt;
87       YACS::ExecutorState _executorState;
88       YACS::ExecutionMode _execMode;
89       std::list<std::string> _listOfBreakPoints;
90       std::list<std::string> _listOfTasksToLoad;
91       std::vector<Task *> _tasks; 
92       std::vector<Task *> _tasksSave; 
93       std::ofstream _trace;
94       std::string _dumpErrorFile;
95       bool _keepGoingOnFail;
96       //! specifies if scope DynParaLoop is active or not. False by default.
97       bool _DPLScopeSensitive;
98       std::chrono::steady_clock::time_point _start;
99     public:
100       Executor();
101       virtual ~Executor();
102       void RunA(Scheduler *graph,int debug=0, bool fromScratch=true);
103       void RunW(Scheduler *graph,int debug=0, bool fromScratch=true);
104       void RunB(Scheduler *graph,int debug=0, bool fromScratch=true);
105       void runWlm(Scheduler *graph,int debug=0, bool fromScratch=true);
106       void setKeepGoingProperty(bool newVal) { _keepGoingOnFail=newVal; }
107       bool getKeepGoingProperty() const { return _keepGoingOnFail; }
108       void setDPLScopeSensitive(bool newVal) { _DPLScopeSensitive=newVal; }
109       bool getDPLScopeSensitive() const { return _DPLScopeSensitive; }
110       YACS::ExecutionMode getCurrentExecMode();
111       YACS::ExecutorState getExecutorState();
112       void setExecMode(YACS::ExecutionMode mode);
113       void setListOfBreakPoints(std::list<std::string> listOfBreakPoints);
114       std::list<std::string> getTasksToLoad();
115       bool setStepsToExecute(std::list<std::string> listToExecute);
116       bool resumeCurrentBreakPoint();
117       bool isNotFinished();
118       void stopExecution();
119       bool saveState(const std::string& xmlFile);
120       bool loadState();
121       int getMaxNbOfThreads() const;
122       void setMaxNbOfThreads(int maxNbThreads);
123       int getNbOfThreads();
124       int getNumberOfRunningTasks();
125       void displayDot(Scheduler *graph);
126       void setStopOnError(bool dumpRequested=false, std::string xmlFile="");
127       void unsetStopOnError();
128       void waitPause();
129       bool suspendASAP();
130       void resume(bool suspended);
131       static int _maxThreads;
132       static size_t _threadStackSize;
133       YACS::BASES::Mutex& getTheMutexForSchedulerUpdate() { return _mutexForSchedulerUpdate; }
134       ///// new executor !!!!!
135       void loadTask(Task *task, const WorkloadManager::RunInfo& runInfo);
136       YACS::Event runTask(Task *task);
137       void makeDatastreamConnections(Task *task);
138       void beginTask(Task *task);
139       void endTask(Task *task, YACS::Event ev);
140       void failTask(Task *task, const std::string& message);
141       ////////////
142     protected:
143       bool checkBreakPoints();
144       void waitResume();
145       void loadTask(Task *task, const Executor *execInst);
146       void loadParallelTasks(const std::vector<Task *>& tasks, const Executor *execInst);
147       void launchTasks(const std::vector<Task*>& tasks);
148       void launchTask(Task *task);
149       void wakeUp();
150       void sleepWhileNoEventsFromAnyRunningTask();
151       void traceExec(Task *task, const std::string& message, const std::string& placement);
152       void _displayDot(Scheduler *graph);
153       virtual void sendEvent(const std::string& event);
154       void filterTasksConsideringContainers(std::vector<Task *>& tsks);
155       static std::string ComputePlacement(Task *zeTask);
156     protected:
157       static void *functionForTaskLoad(void *);
158       static void *functionForTaskExecution(void *);
159     };
160   }
161 }
162
163 #endif