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 +
11
12#include <boost/python/class.hpp>
13#include <boost/python/def.hpp>
14#include <boost/python/list.hpp>
15#include <boost/python/overloads.hpp>
16
17using namespace Mantid::API;
20using namespace boost::python;
21
22namespace {
23
24std::once_flag INIT_FLAG;
25
32AlgorithmManagerImpl &instance() {
33 // start the framework (if necessary)
34 auto &mgr = AlgorithmManager::Instance();
35 std::call_once(INIT_FLAG, []() {
36 PyRun_SimpleString("import atexit\n"
37 "def cleanupAlgorithmManager():\n"
38 " from mantid.api import AlgorithmManager\n"
39 " AlgorithmManager.shutdown()\n"
40 "atexit.register(cleanupAlgorithmManager)");
41 });
42 return mgr;
43}
44
45IAlgorithm_sptr create(AlgorithmManagerImpl *self, const std::string &algName, const int &version = -1) {
47 return self->create(algName, version);
48}
49
50std::shared_ptr<Algorithm> createUnmanaged(AlgorithmManagerImpl *self, const std::string &algName,
51 const int &version = -1) {
53 return self->createUnmanaged(algName, version);
54}
55
56void clear(AlgorithmManagerImpl *self) {
60 // ReleaseGlobalInterpreterLock releaseGIL;
61 return self->clear();
62}
63
64void shutdown(AlgorithmManagerImpl *self) {
65 // See comment above for clear()
66 // ReleaseGlobalInterpreterLock releaseGIL;
67 return self->shutdown();
68}
69
70void cancelAll(AlgorithmManagerImpl *self) {
72 return self->cancelAll();
73}
74
82IAlgorithm_sptr getAlgorithm(AlgorithmManagerImpl const *const self, AlgorithmIDProxy idHolder) {
84 return self->getAlgorithm(idHolder.id);
85}
86
94void removeById(AlgorithmManagerImpl &self, AlgorithmIDProxy idHolder) {
96 return self.removeById(idHolder.id);
97}
98
104boost::python::list runningInstancesOf(AlgorithmManagerImpl const *const self, const std::string &algName) {
105 std::vector<IAlgorithm_const_sptr> mgrAlgs;
106
107 {
109 mgrAlgs = self->runningInstancesOf(algName);
110 }
111
112 boost::python::list algs;
113 for (auto &mgrAlg : mgrAlgs) {
114 algs.append(mgrAlg);
115 }
116
117 return algs;
118}
119
121//------------------------------------------------------------------------------------------------------
122GNU_DIAG_OFF("unused-local-typedef")
123// Ignore -Wconversion warnings coming from boost::python
124// Seen with GCC 7.1.1 and Boost 1.63.0
125GNU_DIAG_OFF("conversion")
127// cppcheck-suppress unknownMacro
128BOOST_PYTHON_FUNCTION_OVERLOADS(create_overloads, create, 2, 3)
129BOOST_PYTHON_FUNCTION_OVERLOADS(createUnmanaged_overloads, createUnmanaged, 2, 3)
130
131GNU_DIAG_ON("conversion")
132GNU_DIAG_ON("unused-local-typedef")
134} // namespace
135
137
138 class_<AlgorithmManagerImpl, boost::noncopyable>("AlgorithmManagerImpl", no_init)
139 .def("create", &create, create_overloads((arg("name"), arg("version")), "Creates a managed algorithm."))
140 .def("createUnmanaged", &createUnmanaged,
141 createUnmanaged_overloads((arg("name"), arg("version")), "Creates an unmanaged algorithm."))
142 .def("size", &AlgorithmManagerImpl::size, arg("self"), "Returns the number of managed algorithms")
143 .def("getAlgorithm", &getAlgorithm, (arg("self"), arg("id_holder")),
144 "Return the algorithm instance identified by the given id.")
145 .def("removeById", &removeById, (arg("self"), arg("id_holder")), "Remove an algorithm from the managed list")
146 .def("runningInstancesOf", &runningInstancesOf, (arg("self"), arg("algorithm_name")),
147 "Returns a list of managed algorithm instances that are "
148 "currently executing")
149 .def("clear", &clear, arg("self"), "Clears the current list of managed algorithms")
150 .def("shutdown", &shutdown, arg("self"), "Cancels all running algorithms and waits for them to exit")
151 .def("cancelAll", &cancelAll, arg("self"), "Requests that all currently running algorithms be cancelled")
152 .def("Instance", instance, return_value_policy<reference_existing_object>(),
153 "Return a reference to the singleton instance")
154 .staticmethod("Instance");
155}
void export_AlgorithmManager()
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
The AlgorithmManagerImpl class is responsible for controlling algorithm instances.
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.
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::vector< IAlgorithm_const_sptr > runningInstancesOf(const std::string &algorithmName) const
Returns all running (& managed) occurances of the named algorithm, oldest first.
void cancelAll()
Requests cancellation of all running algorithms.
void clear()
Clears all managed algorithm objects that are not currently running.
Defines a structure for releasing the Python GIL using the RAII pattern.
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
Provides a concrete type to wrap & return AlgorithmIDs that are actually just typedefs for void*.