OPAL (Object Oriented Parallel Accelerator Library)  2024.1
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
27 #include "PyOpal/PyCore/Globals.h"
28 
29 namespace {
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 
39 namespace PyOpal {
40 namespace Globals {
41 void 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 
50 void 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 }
void Initialise()
Definition: Globals.cpp:50
void printArgv(char **argv)
Definition: Globals.cpp:41
Ippl * ippl
Definition: Main.cpp:69
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is and will automatically terminate your rights under this License parties who have received or from you under this License will not have their licenses terminated so long as such parties remain in full compliance You are not required to accept this since you have not signed it nothing else grants you permission to modify or distribute the Program or its derivative works These actions are prohibited by law if you do not accept this License by modifying or distributing the you indicate your acceptance of this License to do and all its terms and conditions for distributing or modifying the Program or works based on it Each time you redistribute the the recipient automatically receives a license from the original licensor to distribute or modify the Program subject to these terms and conditions You may not impose any further restrictions on the recipients exercise of the rights granted herein You are not responsible for enforcing compliance by third parties to this License as a consequence of a court judgment or allegation of patent infringement or for any other reason(not limited to patent issues)
The base class for all OPAL exceptions.
Definition: OpalException.h:28
Definition: Inform.h:42
Inform * gmsgALL
Definition: Main.cpp:71
#define INFORM_ALL_NODES
Definition: Inform.h:39
static void instantiateGlobals()
Definition: IpplInfo.cpp:53
IpplInfo Ippl
Definition: IpplInfo.h:353
Inform * gmsg
Definition: Main.cpp:70