Debian Codegraph @

Source/Reasoner/Kernel/Calculation/CConfigDependedCalculationEnvironmentFactory.cpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "CConfigDependedCalculationEnvironmentFactory.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Kernel {
29
30 namespace Calculation {
31
32
33 CConfigDependedCalculationEnvironmentFactory::CConfigDependedCalculationEnvironmentFactory(CTaskHandleAlgorithmBuilder* taskHandleAlgBuilder) {
34 mTaskHandleAlgBuilder = taskHandleAlgBuilder;
35 }
36
37
38 CConfigDependedCalculationEnvironmentFactory::~CConfigDependedCalculationEnvironmentFactory() {
39 }
40
41
42 CCalculationEnviroment *CConfigDependedCalculationEnvironmentFactory::createCalculationContext(CConfigurationProvider* configProvider) {
43 CCalculationEnviroment *context = nullptr;
44
45 CConfigurationBase* config = nullptr;
46 if (configProvider) {
47 config = configProvider->getCurrentConfiguration();
48 }
49
50
51 CWatchDog *watchDog = 0;
52
53
54 bool configErrorFlag = false;
55
56 bool taskCalcManager = false;
57
58
59 QString calcManagerString = CConfigDataReader::readConfigString(config,"Konclude.Execution.CalculationManager",QString(),&configErrorFlag);
60 if (!configErrorFlag) {
61 if (calcManagerString.isEmpty() || calcManagerString == "Konclude.Calculation.Calculator.ConcurrentTaskCalculationManager") {
62 taskCalcManager = true;
63 } else {
64 taskCalcManager = true;
65 }
66 } else {
67 taskCalcManager = true;
68 }
69
70 if (taskCalcManager) {
71 CConcurrentTaskCalculationEnvironment *taskContext = new CConcurrentTaskCalculationEnvironment();
72
73 qint64 taskProcessorCount = 1;
74
75 QString taskProcessorCountString = CConfigDataReader::readConfigString(config,"Konclude.Calculation.ProcessorCount",QString(),&configErrorFlag);
76 if (!configErrorFlag) {
77 if (taskProcessorCountString == "AUTO") {
78 taskProcessorCount = CThread::idealThreadCount();
79 }
80 qint64 convertedTaskProcessorCount = taskProcessorCountString.toInt(&configErrorFlag);
81 if (configErrorFlag) {
82 taskProcessorCount = convertedTaskProcessorCount;
83 }
84 }
85
86
87 CCentralizedAllocationConfigProvidedDependendLimitation* allocLimitation = new CCentralizedAllocationConfigProvidedDependendLimitation(configProvider,"Konclude.Calculation.Memory");
88 taskContext->setAllocationLimitation(allocLimitation);
89
90 CSatisfiableCalculationTaskJobCallbackExecuter* callbackExecuter = new CSatisfiableCalculationTaskJobCallbackExecuter();
91 taskContext->initCallbackExecuter(callbackExecuter);
92 CSatisfiableCalculationTaskStatusPropagator* taskStatusPropagator = new CSatisfiableCalculationTaskStatusPropagator();
93 taskContext->initStatusPropagator(taskStatusPropagator);
94
95 if (taskProcessorCount <= 1) {
96 CTaskHandleAlgorithm* taskHandleAlg = mTaskHandleAlgBuilder->createTaskHandleAlgorithm();
97 CConsiderateMemoryPoolProvider* memProv = new CNewCentralizedLimitedAllocationMemoryPoolProvider(allocLimitation->getLimitator());
98 CSingleThreadTaskProcessorUnit* singlePocessUnit = new CSingleThreadTaskProcessorUnit(taskHandleAlg,memProv);
99 singlePocessUnit->startProcessing();
100 taskContext->initSingleTaskProcessor(singlePocessUnit);
101 singlePocessUnit->installCallbackExecuter(callbackExecuter);
102 singlePocessUnit->installStatusPropagator(taskStatusPropagator);
103 } else {
104 CTaskHandleAlgorithm* completorTaskHandleAlg = mTaskHandleAlgBuilder->createTaskHandleAlgorithm();
105 CTaskHandleAlgorithm* schedulerTaskHandleAlg = mTaskHandleAlgBuilder->createTaskHandleAlgorithm();
106 CConsiderateMemoryPoolProvider* compMemProv = new CNewCentralizedLimitedAllocationMemoryPoolProvider(allocLimitation->getLimitator());
107 CConsiderateMemoryPoolProvider* schedMemProv = new CNewCentralizedLimitedAllocationMemoryPoolProvider(allocLimitation->getLimitator());
108 CTaskProcessorCompletorThread* completorUnit = new CTaskProcessorCompletorThread(completorTaskHandleAlg,compMemProv);
109 CTaskProcessorSchedulerThread* schedulerUnit = new CTaskProcessorSchedulerThread(schedulerTaskHandleAlg,completorUnit,schedMemProv);
110 completorUnit->installScheduler(schedulerUnit);
111 schedulerUnit->installScheduler(schedulerUnit);
112 completorUnit->installCallbackExecuter(callbackExecuter);
113 schedulerUnit->installCallbackExecuter(callbackExecuter);
114 completorUnit->installStatusPropagator(taskStatusPropagator);
115 schedulerUnit->installStatusPropagator(taskStatusPropagator);
116 completorUnit->startProcessing();
117 schedulerUnit->startProcessing();
118 taskContext->initMultiTaskProcessor(schedulerUnit,completorUnit);
119
120 while (taskProcessorCount-- > 2) {
121 CTaskHandleAlgorithm* taskHandleAlg = mTaskHandleAlgBuilder->createTaskHandleAlgorithm();
122 CConsiderateMemoryPoolProvider* memProv = new CNewCentralizedLimitedAllocationMemoryPoolProvider(allocLimitation->getLimitator());
123 CTaskProcessorThread* taskProcessor = new CTaskProcessorThread(taskHandleAlg,completorUnit,memProv);
124 taskProcessor->installScheduler(schedulerUnit);
125 taskProcessor->installCallbackExecuter(callbackExecuter);
126 taskProcessor->installStatusPropagator(taskStatusPropagator);
127 taskProcessor->startProcessing();
128 taskContext->appendMultiTaskProcessor(taskProcessor);
129 }
130 }
131
132
133 context = taskContext;
134
135
136
137 }
138 return context;
139 }
140
141
142
143 }; // end namespace Calculation
144
145 }; // end namespace Kernel
146
147 }; // end namespace Reasoner
148
149 }; // end namespace Konclude