Mantid
Loading...
Searching...
No Matches
FrameworkManager.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/import.hpp>
14#include <boost/python/reference_existing_object.hpp>
15#include <boost/python/return_value_policy.hpp>
16
17#include <mutex>
18
23using namespace boost::python;
24
25namespace {
26
27std::once_flag INIT_FLAG;
28bool INSTANCE_CALLED = false;
29constexpr auto PYTHONPATHS_KEY = "pythonscripts.directories";
30
37void declareCPPAlgorithms() { AlgorithmFactory::Instance().subscribe<Mantid::PythonInterface::RunPythonScript>(); }
38
43void updatePythonPaths() {
44 auto packagesetup = import("mantid.kernel.packagesetup");
45 packagesetup.attr("update_sys_paths")(
46 ConfigService::Instance().getValue<std::string>(PYTHONPATHS_KEY).get_value_or(""));
47}
48
61FrameworkManagerImpl &instance() {
62 // start the framework (if necessary)
63 auto &frameworkMgr = FrameworkManager::Instance();
64 std::call_once(INIT_FLAG, []() {
65 INSTANCE_CALLED = true;
66 declareCPPAlgorithms();
67 updatePythonPaths();
68 import("mantid.simpleapi");
69 // Without a python-based exit handler the singletons are only cleaned
70 // up after main() and this is too late to acquire the GIL to be able to
71 // delete any python objects still stored in other singletons like the
72 // ADS or AlgorithmManager.
73 PyRun_SimpleString("import atexit\n"
74 "from mantid.api import FrameworkManager\n"
75 "atexit.register(lambda: FrameworkManager.shutdown())");
76 });
77 return frameworkMgr;
78}
79
83bool hasInstance() { return INSTANCE_CALLED; }
84} // namespace
85
87 class_<FrameworkManagerImpl, boost::noncopyable>("FrameworkManagerImpl", no_init)
88 .def("setNumOMPThreadsToConfigValue", &FrameworkManagerImpl::setNumOMPThreadsToConfigValue, arg("self"),
89 "Sets the number of OpenMP threads to the value "
90 "specified in the "
91 "config file")
92
93 .def("setNumOMPThreads", &FrameworkManagerImpl::setNumOMPThreads, (arg("self"), arg("nthread")),
94 "Set the number of OpenMP threads to the given value")
95
96 .def("getNumOMPThreads", &FrameworkManagerImpl::getNumOMPThreads, arg("self"),
97 "Returns the number of OpenMP threads that will be used.")
98
99 .def("clear", &FrameworkManagerImpl::clear, arg("self"), "Clear all memory held by Mantid")
100
101 .def("clearAlgorithms", &FrameworkManagerImpl::clearAlgorithms, arg("self"),
102 "Clear memory held by algorithms (does not include workspaces)")
103
104 .def("clearData", &FrameworkManagerImpl::clearData, arg("self"),
105 "Clear memory held by the data service (essentially all "
106 "workspaces, "
107 "including hidden)")
108
109 .def("clearInstruments", &FrameworkManagerImpl::clearInstruments, arg("self"),
110 "Clear memory held by the cached instruments")
111
112 .def("clearPropertyManagers", &FrameworkManagerImpl::clearPropertyManagers, arg("self"),
113 "Clear memory held by the PropertyManagerDataService")
114
115 .def("shutdown", &FrameworkManagerImpl::shutdown, arg("self"), "Effectively shutdown this service")
116
117 .def("hasInstance", hasInstance, "Returns True if Instance has been called, false otherwise")
118 .staticmethod("hasInstance")
119
120 .def("Instance", instance, "Return a reference to the singleton instance",
121 return_value_policy<reference_existing_object>())
122 .staticmethod("Instance");
123}
void export_FrameworkManager()
The main public API via which users interact with the Mantid framework.
void clearPropertyManagers()
Clear memory associated with the PropertyManagers.
void setNumOMPThreads(const int nthreads)
Set the number of OpenMP threads to the given value.
void clearInstruments()
Clear memory associated with the IDS.
void clearData()
Clear memory associated with the ADS.
void shutdown()
shuts down and performs clean up tasks
int getNumOMPThreads() const
Returns the number of OpenMP threads that will be used.
void setNumOMPThreadsToConfigValue()
Set the number of OpenMP threads to use based on the config value.
void clear()
Clears all memory associated with the AlgorithmManager, ADS & IDS.
void clearAlgorithms()
Clear memory associated with the AlgorithmManager.
Manage the lifetime of a class intended to be a singleton.