LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
python.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_UTILS_PYTHON_HPP_INCLUDED
28 #define LBANN_UTILS_PYTHON_HPP_INCLUDED
29 
30 #include "lbann/base.hpp"
31 #ifdef LBANN_HAS_EMBEDDED_PYTHON
32 
33 // Forward declarations of Python objects to avoid including Python.h
34 struct _object;
35 typedef struct _object PyObject;
36 
37 #include <mutex>
38 #include <string>
39 
40 namespace lbann {
41 namespace python {
42 
52 void initialize();
53 
59 void finalize();
60 
62 bool is_active();
63 
72 void check_error(bool force_error = false);
73 
84 class global_interpreter_lock
85 {
86 public:
87  global_interpreter_lock();
88  ~global_interpreter_lock();
89 
90 private:
91  global_interpreter_lock(const global_interpreter_lock&) = delete;
92  global_interpreter_lock& operator=(const global_interpreter_lock&) = delete;
93  int m_gil_state;
94 };
95 
116 class object
117 {
118 public:
122  object(PyObject* ptr);
123 
125  object(const std::string& val);
127  object(long val);
129  object(double val);
130 
131  object() {}
133  object(const object& other);
135  object& operator=(const object& other);
137  object(object&& other) noexcept;
139  object& operator=(object&& other);
140  ~object();
141 
143  inline PyObject* get() noexcept { return m_ptr; }
145  inline const PyObject* get() const noexcept { return m_ptr; }
147  inline operator PyObject*() noexcept { return get(); }
149  inline operator const PyObject*() const noexcept { return get(); }
150 
154  PyObject* release() noexcept;
155 
157  operator std::string();
159  operator long();
161  operator double();
162 
163 private:
165  PyObject* m_ptr = nullptr;
166 };
167 
168 } // namespace python
169 } // namespace lbann
170 
171 #endif // LBANN_HAS_EMBEDDED_PYTHON
172 #endif // LBANN_UTILS_PYTHON_HPP_INCLUDED
world_comm_ptr initialize(int &argc, char **&argv)
void finalize(lbann_comm *comm=nullptr)