Nektar++
Functions
Python/BasicUtils/SessionReader.cpp File Reference
#include <LibUtilities/BasicUtils/SessionReader.h>
#include <LibUtilities/Python/NekPyConfig.hpp>

Go to the source code of this file.

Functions

SessionReaderSharedPtr SessionReader_CreateInstance (py::list &ns)
 Thin wrapper around SessionReader to provide a nicer Pythonic interface. More...
 
void export_SessionReader ()
 SessionReader exports. More...
 

Function Documentation

◆ export_SessionReader()

void export_SessionReader ( )

SessionReader exports.

Currently wrapped functions:

  • SessionReader::CreateInstance for creating objects
  • SessionReader::GetSessionName to return the session name
  • SessionReader::Finalise to deal with finalising things

Definition at line 122 of file Python/BasicUtils/SessionReader.cpp.

123 {
124  py::class_<SessionReader, std::shared_ptr<SessionReader>,
125  boost::noncopyable>("SessionReader", py::no_init)
126 
127  .def("CreateInstance", SessionReader_CreateInstance)
128  .staticmethod("CreateInstance")
129 
130  .def("GetSessionName", &SessionReader::GetSessionName,
131  py::return_value_policy<py::copy_const_reference>())
132 
133  .def("Finalise", &SessionReader::Finalise)
134 
135  .def("DefinesParameter", &SessionReader::DefinesParameter)
136  .def("GetParameter", &SessionReader::GetParameter,
137  py::return_value_policy<py::return_by_value>())
138 
139  .def("GetVariable", &SessionReader::GetVariable,
140  py::return_value_policy<py::copy_const_reference>())
141 
142  .def("GetComm", &SessionReader::GetComm)
143 
144  ;
145 }
SessionReaderSharedPtr SessionReader_CreateInstance(py::list &ns)
Thin wrapper around SessionReader to provide a nicer Pythonic interface.
static void Finalise(gs_data *pGsh)
Deallocates the GSLib mapping data.
Definition: GsLib.hpp:251

References Nektar::LibUtilities::SessionReader::DefinesParameter(), Nektar::LibUtilities::SessionReader::Finalise(), Nektar::LibUtilities::SessionReader::GetComm(), Nektar::LibUtilities::SessionReader::GetParameter(), Nektar::LibUtilities::SessionReader::GetSessionName(), Nektar::LibUtilities::SessionReader::GetVariable(), and SessionReader_CreateInstance().

Referenced by BOOST_PYTHON_MODULE().

◆ SessionReader_CreateInstance()

SessionReaderSharedPtr SessionReader_CreateInstance ( py::list &  ns)

Thin wrapper around SessionReader to provide a nicer Pythonic interface.

This allows us to do, for example

session = SessionReader.CreateInstance(sys.argv)

which is more natural in Python.

Definition at line 55 of file Python/BasicUtils/SessionReader.cpp.

56 {
57  int i, argc = py::len(ns), bufSize = 0;
58  char **argv = new char *[argc + 1], *p;
59 
60  // Create argc, argv to give to the session reader. Note that this needs to
61  // be a contiguous block in memory, otherwise MPI (specifically OpenMPI)
62  // will likely segfault.
63  for (i = 0; i < argc; ++i)
64  {
65  std::string tmp = py::extract<std::string>(ns[i]);
66  bufSize += tmp.size() + 1;
67  }
68 
69  std::vector<char> buf(bufSize);
70  for (i = 0, p = &buf[0]; i < argc; ++i)
71  {
72  std::string tmp = py::extract<std::string>(ns[i]);
73  std::copy(tmp.begin(), tmp.end(), p);
74  p[tmp.size()] = '\0';
75  argv[i] = p;
76  p += tmp.size() + 1;
77  }
78 
79  // Also make sure we set argv[argc] = NULL otherwise OpenMPI will also
80  // segfault.
81  argv[argc] = NULL;
82 
83 #ifdef NEKTAR_USE_MPI
84  // In the case we're using MPI, it may already have been initialised. So to
85  // handle this, we'll construct our own CommMpi object and pass through to
86  // the SessionReader. This will persist indefinitely, or at least until the
87  // library is unloaded by Python.
88 
89  if (!MPICOMM)
90  {
91  MPICOMM = GetCommFactory().CreateInstance("ParallelMPI", argc, argv);
92  }
93 
94  std::vector<std::string> filenames(argc - 1);
95  for (i = 1; i < argc; ++i)
96  {
97  filenames[i - 1] = std::string(argv[i]);
98  }
99 
100  // Create session reader.
102  SessionReader::CreateInstance(argc, argv, filenames, MPICOMM);
103 #else
104  // Create session reader.
105  SessionReaderSharedPtr sr = SessionReader::CreateInstance(argc, argv);
106 #endif
107 
108  // Clean up.
109  delete[] argv;
110 
111  return sr;
112 }
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144
def copy(self)
Definition: pycml.py:2663
std::shared_ptr< SessionReader > SessionReaderSharedPtr
CommFactory & GetCommFactory()

References CellMLToNektar.pycml::copy(), Nektar::LibUtilities::SessionReader::CreateInstance(), Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance(), Nektar::LibUtilities::GetCommFactory(), and CellMLToNektar.cellml_metadata::p.

Referenced by export_SessionReader().