OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
Globals.cpp
Go to the documentation of this file.
1//
2// Python API for PolynomialCoefficient (part of the multidimensional polynomial fitting routines)
3//
4// Copyright (c) 2008-2023, Chris Rogers, STFC Rutherford Appleton Laboratory, Didcot, UK
5//
6// This file is part of OPAL.
7//
8// OPAL is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// You should have received a copy of the GNU General Public License
14// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
15//
16
17#include <cstring>
18#include <gsl/gsl_errno.h>
19#include <Python.h>
20#include <boost/python.hpp>
21
22#include "Utility/IpplInfo.h" // that is ippl land
24
25// Note the gymnastics here - we only want to define gmsg and ippl once
26#define PYOPAL_GLOBALS_C
28
29namespace {
30 void errorHandlerGSL(const char *reason,
31 const char *file,
32 int line,
33 int gsl_errno) {
34 throw OpalException(file, reason);
35 if (line || gsl_errno) {;} // disable gcc warning; does nothing
36 }
37}
38
39namespace PyOpal {
40namespace Globals {
41void printArgv(char** argv) { // debugging
42 int i = 0;
43 while (argv[i] != nullptr) {
44 std::cerr << i << " " << std::string(argv[i]) << std::endl;
45 ++i;
46 }
47
48}
49
50void Initialise() {
51 if (ippl == nullptr) {
52 PyObject* pyargv = PySys_GetObject("argv"); // this is a borrowed ref
53 boost::python::handle<> wrapper(boost::python::borrowed(pyargv)); // now wrapper owns the ref
54 boost::python::list myList(wrapper);
55
56 int argc = int(boost::python::len(myList));
57 // I am not strong on the C-style strings, but if I understand correctly
58 // there is a secret null pointer at the end of each string, hence the
59 // char arrays have to be one character longer than you might think.
60 char* argvr[argc+1];
61 argvr[0] = new char[7];
62 strcpy(argvr[0], "pyopal");
63 for (int i = 1; i < argc; ++i) {
64 int stringLength(boost::python::len(myList[i]));
65 const char* value = boost::python::extract<const char*>(
66 boost::python::str(myList[i]));
67 argvr[i] = new char[stringLength+1];
68 strcpy(argvr[i], value);
69 }
70 argvr[argc] = nullptr;
71 // and here is another secret nullptr to mark the end of the array of strings
72 // don't forget it, you might spend days debugging the segv otherwise...
73 char** argv = argvr;
74 // Ippl is a typedef of IpplInfo in ippl/Utilities
75 ippl = new Ippl(argc, argv);
76 }
77 if (gmsg == nullptr) {
79 gmsg = new Inform("OPAL");
80 gmsgALL = new Inform("OPAL", INFORM_ALL_NODES);
81 }
82 gsl_set_error_handler(&errorHandlerGSL);
83}
84}
85}
Inform * gmsgALL
Definition Main.cpp:71
Inform * gmsg
Definition Main.cpp:70
Ippl * ippl
Definition Main.cpp:69
#define INFORM_ALL_NODES
Definition Inform.h:39
IpplInfo Ippl
Definition IpplInfo.h:353
void printArgv(char **argv)
Definition Globals.cpp:41
void Initialise()
Definition Globals.cpp:50
The base class for all OPAL exceptions.
static void instantiateGlobals()
Definition IpplInfo.cpp:53