• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-Sn / opensn / 18300593117

06 Oct 2025 10:47PM UTC coverage: 74.862% (-0.2%) from 75.031%
18300593117

push

github

web-flow
Merge pull request #759 from wdhawkins/performance

Sweep performance optimizations

294 of 302 new or added lines in 15 files covered. (97.35%)

334 existing lines in 80 files now uncovered.

17788 of 23761 relevant lines covered (74.86%)

61852783.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.0
/python/lib/context.cc
1
// SPDX-FileCopyrightText: 2025 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "python/lib/py_wrappers.h"
5
#include "python/lib/py_env.h"
6
#include "framework/logging/log.h"
7
#include "framework/runtime.h"
8
#include "caliper/cali.h"
9
#include "petscsys.h"
10
#include <pybind11/eval.h>
11
#include <stdexcept>
12

13
namespace opensn
14
{
15

16
// Wrap Finalize
17
static void
18
WrapFinalize(py::module& context)
62✔
19
{
20
  // clang-format off
21
  // finalize
22
  context.def(
62✔
23
    "Finalize",
24
    []()
62✔
25
    {
26
      PyEnv::p_default_env.reset();
×
27
    },
×
28
    R"(
29
    Finalize OpenSn context.
30

31
    This function can only be invoked **once at the end of the runtime process**.
32

33
    In standard Python module mode, the context is automatically finalized when the interpreter ends
34
    the lifecycle of variables. However, in environments like IPython within Jupyter, functions
35
    registered with `atexit` may not be executed.
36

37
    In such cases, users must explicitly call this finalize function followed by MPI Finalize to
38
    properly terminate the context; otherwise, an MPI error will occur. This process can be
39
    registered to the ``post_execute`` event of IPython console as follows:
40

41
    .. code-block::
42

43
       from IPython import get_ipython
44

45
       def finalize_env():
46
           Finalize()
47
           MPI.Finalize()
48

49
       ipython_instance = get_ipython()
50
       if ipython_instance is not None:
51
           ipython_instance.events.register("post_execute", finalize_env)
52

53
    This approach allows the module to safely execute the Python script generated by ``nbconvert``.
54
    )"
55
  );
56
  // clang-format on
57
}
62✔
58

59
// Wrap settings
60
static void
61
WrapSettings(py::module& context)
62✔
62
{
63
  // clang-format off
64
  // log settings
65
  context.def(
62✔
66
    "SetVerbosityLevel",
UNCOV
67
    [](unsigned int level)
×
68
    {
69
      log.SetVerbosity(level);
×
UNCOV
70
    },
×
71
    "Set verbosity level (0 to 3). Default is 0.",
72
    py::arg("level")
62✔
73
  );
74
  context.def(
62✔
75
    "UseColor",
UNCOV
76
    [](bool cfg)
×
77
    {
78
      suppress_color = (!cfg);
34✔
79
    },
34✔
80
    "Enable/disable color output. Default is True.",
81
    py::arg("config") = true
124✔
82
  );
83
  // PETSc error handler
84
  context.def(
62✔
85
    "EnablePETScErrorHandler",
UNCOV
86
    []()
×
87
    {
88
      ::PetscOptionsSetValue(nullptr, "-no_signal_handler", "false");
×
UNCOV
89
    },
×
90
    "Allow PETSc error handler."
91
  );
92
  // Caliper reporting
93
  context.def(
62✔
94
    "SetCaliperConfig",
UNCOV
95
    [](const std::string& config)
×
96
    {
97
      if (use_caliper)
×
98
      {
99
        throw std::runtime_error("This function can only be called before enabling Cailper.");
×
100
      }
101
      cali_config = config;
×
102
    },
×
103
    R"(
104
    Set configuration to the Caliper manager.
105

106
    This function can only be called before using :py:func:`pyopensn.context.EnableCaliper`. Note
107
    that this function does not start the Caliper manager immediately.
108

109
    Parameters
110
    ----------
111
    config: str, default='runtime-report(calc.inclusive=true),max_column_width=80'
112
        Configuration.
113
    )",
114
    py::arg("config") = "runtime-report(calc.inclusive=true),max_column_width=80"
124✔
115
  );
116
  context.def(
62✔
117
    "EnableCaliper",
118
    []()
62✔
119
    {
120
      // check if caliper is already initialized
121
      if (use_caliper)
×
122
      {
123
        throw std::runtime_error("Caliper is already set.");
×
124
      }
125
      use_caliper = true;
×
126
      // initialize Caliper
127
      cali_mgr.add(cali_config.c_str());
×
128
      cali_set_global_string_byname("opensn.version", GetVersionStr().c_str());
×
129
      cali_set_global_string_byname("opensn.input", input_path.c_str());
×
130
      cali_mgr.start();
×
131
    },
×
132
    R"(
133
    Start the Caliper manager and mark the program begin.
134
    )"
135
  );
136
  // clang-format on
137
}
62✔
138

139
// Wrap sys.argv translator
140
static void
141
WrapSysArgv(py::module& context)
62✔
142
{
143
  // clang-format off
144
  context.def(
62✔
145
    "InitializeWithArgv",
UNCOV
146
    [](py::list sys_argv)
×
147
    {
148
      // looping over each argument of the list
149
      for (int i_arg = 0; i_arg < sys_argv.size(); ++i_arg)
×
150
      {
151
        // skip for the first argument (Python script name)
152
        if (i_arg == 0)
×
153
        {
154
          continue;
×
155
        }
156
        // cast argument to string
157
        auto arg = sys_argv[i_arg].cast<std::string>();
×
158
        // color
159
        if (arg == "-c" || arg == "--suppress-color")
×
160
        {
161
          suppress_color = true;
×
162
          continue;
×
163
        }
164
        // verbosity
165
        if (arg == "-v" || arg == "--verbose")
×
166
        {
167
          auto next_arg = sys_argv[++i_arg].cast<std::string>();
×
168
          log.SetVerbosity(std::atoi(next_arg.c_str()));
×
169
          continue;
×
170
        }
×
171
        // caliper
172
        if (arg == "--caliper")
×
173
        {
174
          auto next_arg = sys_argv[++i_arg].cast<std::string>();
×
175
          use_caliper = true;
×
176
          cali_config = next_arg;
×
177
          cali_mgr.add(cali_config.c_str());
×
178
          cali_set_global_string_byname("opensn.version", GetVersionStr().c_str());
×
179
          cali_set_global_string_byname("opensn.input", input_path.c_str());
×
180
          cali_mgr.start();
×
181
          continue;
×
UNCOV
182
        }
×
183
        // PETSc handler
184
        if (arg == "--allow-petsc-error-handler")
×
185
        {
186
          ::PetscOptionsSetValue(nullptr, "-no_signal_handler", "false");
×
187
          continue;
×
188
        }
189
        // execute Python statement
190
        if (arg == "--py")
×
191
        {
192
          auto next_arg = sys_argv[++i_arg].cast<std::string>();
×
193
          try
194
          {
195
            py::exec(next_arg);
×
196
          }
197
          catch (py::error_already_set& e)
×
198
          {
199
            py::object exc_type = e.type();
×
200
            std::string type_name = py::str(exc_type.attr("__name__"));
×
201
            log.LogAllError() << "Caught an exception of type: [" << type_name
×
202
              << "] when executing code of the sys.argv.\nException message: " << e.what() << "\n";
×
203
            e.restore();
×
204
          }
×
205
          continue;
×
206
        }
×
207
        // throw error for invalid argument
208
        throw std::runtime_error("Invalid argument: " + arg + "\n");
×
209
      }
×
210
    },
×
211
    R"(
212
    Overwrite OpenSn settings using ``sys.argv``.
213

214
    This is a temporary solution for dealing with command line argument mode of the console. Once
215
    the console is gone, this functionality will also be killed.
216

217
    Parameters
218
    ----------
219
    sys_argv: List[str], default=sys.argv
220
        Argument vector to be used. Default to ``sys.argv``.
221
    )",
222
    py::arg_v("sys_argv", py::module::import("sys").attr("argv").cast<py::list>(), "sys.argv")
124✔
223
  );
224
  // clang-format on
225
}
62✔
226

227
// Wrap the context components of OpenSn
228
void
229
py_context(py::module& pyopensn)
62✔
230
{
231
  py::module context = pyopensn.def_submodule("context", "Context manager module.");
62✔
232
  WrapFinalize(context);
62✔
233
  WrapSettings(context);
62✔
234
  WrapSysArgv(context);
62✔
235
}
62✔
236

237
} // namespace opensn
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc