Nektar++
Python/BasicUtils/SessionReader.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: SessionReader.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Python wrapper for SessionReader.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
37 
38 using namespace Nektar::LibUtilities;
39 
40 #ifdef NEKTAR_USE_MPI
42 CommSharedPtr MPICOMM = CommSharedPtr();
43 #endif
44 
45 /*
46  * @brief Thin wrapper around SessionReader to provide a nicer Pythonic
47  * interface.
48  *
49  * This allows us to do, for example
50  *
51  * session = SessionReader.CreateInstance(sys.argv)
52  *
53  * which is more natural in Python.
54  */
55 
57 {
58  int i, argc = py::len(ns), bufSize = 0;
59  char **argv = new char *[argc + 1], *p;
60 
61  // Create argc, argv to give to the session reader. Note that this needs to
62  // be a contiguous block in memory, otherwise MPI (specifically OpenMPI)
63  // will likely segfault.
64  for (i = 0; i < argc; ++i)
65  {
66  std::string tmp = py::extract<std::string>(ns[i]);
67  bufSize += tmp.size() + 1;
68  }
69 
70  std::vector<char> buf(bufSize);
71  for (i = 0, p = &buf[0]; i < argc; ++i)
72  {
73  std::string tmp = py::extract<std::string>(ns[i]);
74  std::copy(tmp.begin(), tmp.end(), p);
75  p[tmp.size()] = '\0';
76  argv[i] = p;
77  p += tmp.size() + 1;
78  }
79 
80  // Also make sure we set argv[argc] = NULL otherwise OpenMPI will also
81  // segfault.
82  argv[argc] = NULL;
83 
84 #ifdef NEKTAR_USE_MPI
85  // In the case we're using MPI, it may already have been initialised. So to
86  // handle this, we'll construct our own CommMpi object and pass through to
87  // the SessionReader. This will persist indefinitely, or at least until the
88  // library is unloaded by Python.
89 
90  if (!MPICOMM)
91  {
92  MPICOMM = GetCommFactory().CreateInstance("ParallelMPI", argc, argv);
93  }
94 
95  std::vector<std::string> filenames(argc - 1);
96  for (i = 1; i < argc; ++i)
97  {
98  filenames[i - 1] = std::string(argv[i]);
99  }
100 
101  // Create session reader.
103  SessionReader::CreateInstance(argc, argv, filenames, MPICOMM);
104 #else
105  // Create session reader.
107 #endif
108 
109  // Clean up.
110  delete[] argv;
111 
112  return sr;
113 }
114 
116  std::string paramName, int paramValue)
117 {
118  session->SetParameter(paramName, paramValue);
119 }
120 
122  std::string paramName, double paramValue)
123 {
124  session->SetParameter(paramName, paramValue);
125 }
126 
127 /**
128  * @brief SessionReader exports.
129  *
130  * Currently wrapped functions:
131  * - SessionReader::CreateInstance for creating objects
132  * - SessionReader::GetSessionName to return the session name
133  * - SessionReader::Finalise to deal with finalising things
134  */
135 
137 {
138  py::class_<SessionReader, std::shared_ptr<SessionReader>,
139  boost::noncopyable>("SessionReader", py::no_init)
140 
141  .def("CreateInstance", SessionReader_CreateInstance)
142  .staticmethod("CreateInstance")
143 
144  .def("GetSessionName", &SessionReader::GetSessionName,
145  py::return_value_policy<py::copy_const_reference>())
146 
147  .def("Finalise", &SessionReader::Finalise)
148 
149  .def("DefinesParameter", &SessionReader::DefinesParameter)
150  .def("GetParameter", &SessionReader::GetParameter,
151  py::return_value_policy<py::return_by_value>())
152 
153  .def("SetParameter", SessionReader_SetParameterInt)
154  .def("SetParameter", SessionReader_SetParameterDouble)
155 
156  .def("GetVariable", &SessionReader::GetVariable,
157  py::return_value_policy<py::copy_const_reference>())
158 
159  .def("GetComm", &SessionReader::GetComm)
160 
161  .def("GetSharedFilesystem", &SessionReader::GetSharedFilesystem);
162 }
void SessionReader_SetParameterDouble(SessionReaderSharedPtr session, std::string paramName, double paramValue)
SessionReaderSharedPtr SessionReader_CreateInstance(py::list &ns)
void SessionReader_SetParameterInt(SessionReaderSharedPtr session, std::string paramName, int paramValue)
void export_SessionReader()
SessionReader exports.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144
const std::string & GetSessionName() const
Returns the session name of the loaded XML document.
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
CommSharedPtr GetComm()
Returns the communication object.
const NekDouble & GetParameter(const std::string &pName) const
Returns the value of the specified parameter.
const std::string & GetVariable(const unsigned int &idx) const
Returns the name of the variable specified by the given index.
bool DefinesParameter(const std::string &name) const
Checks if a parameter is specified in the XML document.
bool GetSharedFilesystem()
Returns if file system shared.
def copy(self)
Definition: pycml.py:2663
std::shared_ptr< SessionReader > SessionReaderSharedPtr
CommFactory & GetCommFactory()
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:54