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
15namespace Mantid::API {
16namespace {
18Kernel::Logger g_log("AlgorithmManager");
19} // namespace
20
22AlgorithmManagerImpl::AlgorithmManagerImpl() : m_managed_algs() { g_log.debug() << "Algorithm Manager created.\n"; }
23
29
38Algorithm_sptr AlgorithmManagerImpl::createUnmanaged(const std::string &algName, const int &version) const {
39 return AlgorithmFactory::Instance().create(algName,
40 version); // Throws on fail:
41}
42
55IAlgorithm_sptr AlgorithmManagerImpl::create(const std::string &algName, const int &version) {
56 std::lock_guard<std::mutex> _lock(this->m_managedMutex);
58 try {
59 alg = AlgorithmFactory::Instance().create(algName,
60 version); // Throws on fail:
62 g_log.debug() << count << " Finished algorithms removed from the managed algorithms list. " << m_managed_algs.size()
63 << " remaining.\n";
64
65 // Add to list of managed ones
66 m_managed_algs.emplace_back(alg);
67 alg->initialize();
68 } catch (std::runtime_error &ex) {
69 g_log.error() << "AlgorithmManager:: Unable to create algorithm " << algName << ' ' << ex.what() << '\n';
70 throw std::runtime_error("AlgorithmManager:: Unable to create algorithm " + algName + ' ' + ex.what());
71 }
72 return alg;
73}
74
79 std::lock_guard<std::mutex> _lock(this->m_managedMutex);
80 for (auto itAlg = m_managed_algs.begin(); itAlg != m_managed_algs.end();) {
81 if (!(*itAlg)->isRunning()) {
82 itAlg = m_managed_algs.erase(itAlg);
83 } else {
84 ++itAlg;
85 }
86 }
87}
88
89std::size_t AlgorithmManagerImpl::size() const { return m_managed_algs.size(); }
90
97 std::lock_guard<std::mutex> _lock(this->m_managedMutex);
98 const auto found = std::find_if(m_managed_algs.cbegin(), m_managed_algs.cend(),
99 [id](const auto &algorithm) { return algorithm->getAlgorithmID() == id; });
100 if (found == m_managed_algs.cend()) {
101 return IAlgorithm_sptr();
102 } else {
103 return *found;
104 }
105}
106
112 std::lock_guard<std::mutex> _lock(this->m_managedMutex);
113 auto itend = m_managed_algs.end();
114 for (auto it = m_managed_algs.begin(); it != itend; ++it) {
115 if ((**it).getAlgorithmID() == id) {
116 if (!(*it)->isRunning()) {
117 g_log.debug() << "Removing algorithm " << (*it)->name() << '\n';
118 m_managed_algs.erase(it);
119 } else {
120 g_log.debug() << "Unable to remove algorithm " << (*it)->name() << ". The algorithm is running.\n";
121 }
122 break;
123 }
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::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::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::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
173 std::vector<IAlgorithm_const_sptr> theCompletedInstances;
174 std::copy_if(m_managed_algs.cbegin(), m_managed_algs.cend(), std::back_inserter(theCompletedInstances),
175 [](const auto &algorithm) { return (algorithm->isReadyForGarbageCollection()); });
176 for (auto completedAlg : theCompletedInstances) {
177 auto itend = m_managed_algs.end();
178 for (auto it = m_managed_algs.begin(); it != itend; ++it) {
179 if ((**it).getAlgorithmID() == completedAlg->getAlgorithmID()) {
180 m_managed_algs.erase(it);
181 break;
182 }
183 }
184 }
185 return theCompletedInstances.size();
186}
187
189} // 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.
IAlgorithm_sptr create(const std::string &algName, const int &version=-1)
Creates a managed algorithm with the option of choosing a version.
std::mutex m_managedMutex
Mutex for modifying/accessing the m_managed_algs member.
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:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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:61