Mantid
Loading...
Searching...
No Matches
AlgorithmManager.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include "MantidAPI/Algorithm.h"
14#include <thread>
15
16namespace Mantid::API {
17namespace {
19Kernel::Logger g_log("AlgorithmManager");
20} // namespace
21
23AlgorithmManagerImpl::AlgorithmManagerImpl() : m_managed_algs() { g_log.debug() << "Algorithm Manager created.\n"; }
24
30
39Algorithm_sptr AlgorithmManagerImpl::createUnmanaged(const std::string &algName, const int &version) const {
40 return AlgorithmFactory::Instance().create(algName,
41 version); // Throws on fail:
42}
43
56IAlgorithm_sptr AlgorithmManagerImpl::create(const std::string &algName, const int &version) {
57 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
59 try {
60 alg = AlgorithmFactory::Instance().create(algName,
61 version); // Throws on fail:
63 g_log.debug() << count << " Finished algorithms removed from the managed algorithms list. " << m_managed_algs.size()
64 << " remaining.\n";
65
66 // Add to list of managed ones
67 m_managed_algs.emplace_back(alg);
68 alg->initialize();
69 } catch (std::runtime_error &ex) {
70 g_log.error() << "AlgorithmManager:: Unable to create algorithm " << algName << ' ' << ex.what() << '\n';
71 throw std::runtime_error("AlgorithmManager:: Unable to create algorithm " + algName + ' ' + ex.what());
72 }
73 return alg;
74}
75
80 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
81 for (auto itAlg = m_managed_algs.begin(); itAlg != m_managed_algs.end();) {
82 if (!(*itAlg)->isRunning()) {
83 itAlg = m_managed_algs.erase(itAlg);
84 } else {
85 ++itAlg;
86 }
87 }
88}
89
90std::size_t AlgorithmManagerImpl::size() const { return m_managed_algs.size(); }
91
98 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
99 const auto found = std::find_if(m_managed_algs.cbegin(), m_managed_algs.cend(),
100 [id](const auto &algorithm) { return algorithm->getAlgorithmID() == id; });
101 if (found == m_managed_algs.cend()) {
102 return IAlgorithm_sptr();
103 } else {
104 return *found;
105 }
106}
107
113 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
114 const auto it = std::find_if(m_managed_algs.cbegin(), m_managed_algs.cend(),
115 [&id](const auto &alg) { return alg->getAlgorithmID() == id; });
116 if (it == m_managed_algs.cend()) {
117 return;
118 }
119 if (!(*it)->isRunning()) {
120 g_log.debug() << "Removing algorithm " << (*it)->name() << '\n';
121 m_managed_algs.erase(it);
122 } else {
123 g_log.debug() << "Unable to remove algorithm " << (*it)->name() << ". The algorithm is running.\n";
124 }
125}
126
133 auto alg = this->getAlgorithm(id);
134 if (!alg)
135 return;
136 notificationCenter.postNotification(new AlgorithmStartingNotification(alg));
137}
138
141std::vector<IAlgorithm_const_sptr> AlgorithmManagerImpl::runningInstancesOf(const std::string &algorithmName) const {
142 std::vector<IAlgorithm_const_sptr> theRunningInstances;
143 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
144 std::copy_if(
145 m_managed_algs.cbegin(), m_managed_algs.cend(), std::back_inserter(theRunningInstances),
146 [&algorithmName](const auto &algorithm) { return algorithm->name() == algorithmName && algorithm->isRunning(); });
147 return theRunningInstances;
148}
149
152std::vector<IAlgorithm_const_sptr> AlgorithmManagerImpl::runningInstances() const {
153 std::vector<IAlgorithm_const_sptr> theRunningInstances;
154 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
155 std::copy_if(m_managed_algs.cbegin(), m_managed_algs.cend(), std::back_inserter(theRunningInstances),
156 [](const auto &algorithm) { return algorithm->isRunning(); });
157 return theRunningInstances;
158}
159
162 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
163 for (auto &managed_alg : m_managed_algs) {
164 if (managed_alg->isRunning())
165 managed_alg->cancel();
166 }
167}
168
171 std::vector<IAlgorithm_const_sptr> theCompletedInstances;
172 std::lock_guard<std::recursive_mutex> _lock(this->m_managedMutex);
173 std::copy_if(m_managed_algs.cbegin(), m_managed_algs.cend(), std::back_inserter(theCompletedInstances),
174 [](const auto &algorithm) { return (algorithm->isReadyForGarbageCollection()); });
175 for (auto completedAlg : theCompletedInstances) {
176 const auto it = std::find_if(m_managed_algs.begin(), m_managed_algs.end(), [&completedAlg](const auto &alg) {
177 return alg->getAlgorithmID() == completedAlg->getAlgorithmID();
178 });
179 if (it != m_managed_algs.end()) {
180 m_managed_algs.erase(it);
181 }
182 }
183 return theCompletedInstances.size();
184}
185
187 cancelAll();
188 while (runningInstances().size() > 0) {
189 std::this_thread::sleep_for(std::chrono::milliseconds(100));
190 }
191 clear();
192}
193} // namespace Mantid::API
int count
counter
Definition Matrix.cpp:37
std::shared_ptr< Algorithm > createUnmanaged(const std::string &algName, const int &version=-1) const
Creates an unmanaged algorithm with the option of choosing a version.
Poco::NotificationCenter notificationCenter
Sends notifications to observers.
void notifyAlgorithmStarting(AlgorithmID id)
Called by an algorithm that is executing asynchronously This sends out the notification.
IAlgorithm_sptr getAlgorithm(AlgorithmID id) const
Returns a shared pointer by algorithm id.
void removeById(AlgorithmID id)
Removes the given algorithm from the managed list.
std::recursive_mutex m_managedMutex
Mutex for modifying/accessing the m_managed_algs member.
IAlgorithm_sptr create(const std::string &algName, const int &version=-1)
Creates a managed algorithm with the option of choosing a version.
AlgorithmManagerImpl()
Private Constructor for singleton class.
std::vector< IAlgorithm_const_sptr > runningInstances() const
Returns all running (& managed) occurances of any algorithm, oldest first.
std::vector< IAlgorithm_const_sptr > runningInstancesOf(const std::string &algorithmName) const
Returns all running (& managed) occurances of the named algorithm, oldest first.
~AlgorithmManagerImpl()
Private destructor Prevents client from calling 'delete' on the pointer handed out by Instance.
std::deque< IAlgorithm_sptr > m_managed_algs
The list of managed algorithms.
void cancelAll()
Requests cancellation of all running algorithms.
size_t removeFinishedAlgorithms()
Removes any finished algorithms from the list of managed algorithms.
void clear()
Clears all managed algorithm objects that are not currently running.
Class for when an algorithm is starting asynchronously.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
Kernel::Logger g_log("ExperimentInfo")
static logger object
void * AlgorithmID
As we have multiple interfaces to the same logical algorithm we need a way of uniquely identifying ma...
Definition IAlgorithm.h:28
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition Algorithm.h:52