Skip to content

Commit

Permalink
Added tests target to Makefile
Browse files Browse the repository at this point in the history
Classdesc/Graphcode refs updated
Parallel python object to enable Master/Worker pattern to be scriptable in python
ecolab_mpi python interpreter to enable parallel pythons scripts
  • Loading branch information
highperformancecoder committed Sep 30, 2024
1 parent 8a7d254 commit 6fb1c21
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ src/getContext.o: src/getContext.cc
# ./generate_nauty_sizes >$@
#endif

tests:
tests: all
-cd test; $(MAKE)

sure: all tests
sure: tests
sh runtests test/00/*.sh

# install documentation on SourceForge
Expand Down
2 changes: 1 addition & 1 deletion classdesc
Submodule classdesc updated 1 files
+5 −1 pythonBuffer.h
2 changes: 1 addition & 1 deletion graphcode
5 changes: 5 additions & 0 deletions include/ecolab.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ namespace ecolab
unsigned myid();
unsigned nprocs();

/// adds the EcoLab module path to the python interpreter
int addEcoLabPath();
/// initialisation of the parallel type object
void registerParallel();

using classdesc::pack_t;
using classdesc::unpack_t;
using classdesc::xdr_pack;
Expand Down
8 changes: 0 additions & 8 deletions models/ecolab_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ using namespace classdesc;
// TODO - move this into main library
namespace
{
int addEcoLabPath()
{
if (auto path=PySys_GetObject("path"))
PyList_Append(path,PyUnicode_FromString(ECOLAB_HOME"/lib"));
return 0;
}
int setPath=addEcoLabPath();

/* Rounding function, randomly round up or down, in the range 0..INT_MAX */
inline int ROUND(double x)
{
Expand Down
80 changes: 36 additions & 44 deletions src/ecolab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,31 @@ namespace ecolab
{return Tcl_GetString(pop_arg());}
#endif

int addEcoLabPath()
{
if (Py_IsInitialized())
if (auto path=PySys_GetObject("path"))
PyList_Append(path,PyUnicode_FromString(ECOLAB_HOME"/lib"));
return 0;
}

namespace
{
int setPath=addEcoLabPath();

/// Python object to implement MPI process control from Python
struct Parallel: public CppPyObject
{
PyObject* target=nullptr;
Parallel();
};

static PyObject* exit(Parallel* self, PyObject*)
{
if (self->target)
{
#ifdef MPI_SUPPORT
MPIbuf b; b<<string("return")<<bcast(0);
if (myid()==0)
MPIbuf()<<string("return")<<bcast(0);
#endif
self->target=nullptr;
}
self->target=nullptr;
return Py_None;
}

Expand All @@ -82,7 +89,7 @@ namespace ecolab
PyErr_SetString(PyExc_RuntimeError, "No target object supplied");
return nullptr;
}
if (!PySequence_Check(args) || PySequence_Size(args)<2)
if (!PySequence_Check(args) || PySequence_Size(args)<1)
{
PyErr_SetString(PyExc_RuntimeError, "Incorrect arguments");
return nullptr;
Expand All @@ -108,12 +115,22 @@ namespace ecolab
PyErr_SetString(PyExc_RuntimeError, "Incorrect arguments");
return -1;
}
static_cast<Parallel*>(self)->target=PySequence_GetItem(args,0);
auto target=static_cast<Parallel*>(self)->target=PySequence_GetItem(args,0);
#ifdef MPI_SUPPORT
for (;myid()>0;)
{
MPIbuf b; b.bcast(0);
std::string method; b>>method;
if (method=="return") break;
// we need to pickle the arguments and kwargs, so leave argument support until later
PyObject_Call(PyObject_GetAttrString(target,method.c_str()),PyTuple_New(0),nullptr);
}
#endif
return 0;
}

static void finalize(PyObject* self) {exit(static_cast<Parallel*>(self),nullptr);}

ParallelType()
{
memset(this,0,sizeof(PyTypeObject));
Expand All @@ -122,42 +139,25 @@ namespace ecolab
tp_methods=parallelMethods;
tp_call=call;
tp_init=init;
tp_alloc=PyType_GenericAlloc;
tp_new=PyType_GenericNew;
tp_finalize=finalize;
tp_basicsize=sizeof(Parallel);
PyType_Ready(this);
}

};

ParallelType parallelType;

Parallel::Parallel()
{
ob_refcnt=1;
ob_type=&parallelType;

#ifdef MPI_SUPPORT
for (;myid()>0;)
{
MPIbuf b; b.bcast(0);
std::string method; b>>method;
if (method=="return") break;
// we need to pickle the arguments and kwargs, so leave argument support until later
PyObject_Call(PyObject_GetAttrString(target,method.c_str()),PyTuple_New(0),nullptr);
}
#endif
}

void registerParallel()
{
PyModule_AddObject(pythonModule,"Parallel",reinterpret_cast<PyObject*>(&parallelType));
}

CLASSDESC_ADD_FUNCTION(registerParallel);

}

void registerParallel()
{
static ParallelType parallelType;
PyModule_AddObject(pythonModule,"Parallel",reinterpret_cast<PyObject*>(&parallelType));
}

CLASSDESC_ADD_FUNCTION(registerParallel);


std::string ecolabHome=ECOLAB_HOME;
CLASSDESC_ADD_GLOBAL(ecolabHome);
CLASSDESC_ADD_GLOBAL(array_urand);
Expand All @@ -167,14 +167,6 @@ namespace ecolab
CLASSDESC_DECLARE_TYPE(Plot);
}

//namespace
//{
// unsigned (*myid)()=ecolab::myid;
// unsigned (*nprocs)()=ecolab::nprocs;
// CLASSDESC_ADD_GLOBAL(myid);
// CLASSDESC_ADD_GLOBAL(nprocs);
//}

CLASSDESC_PYTHON_MODULE(ecolab);


Expand Down
10 changes: 10 additions & 0 deletions src/pythonMain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <Python.h>
#include <boost/locale.hpp>
#include "ecolab.h"

#ifdef MPI_SUPPORT
#include "classdescMP.h"

Expand All @@ -31,6 +33,7 @@ void throw_MPI_errors(MPI_Comm * c, int *code, ...)
#include <string>
#include <vector>
using namespace std;
using namespace ecolab;
using boost::locale::conv::utf_to_utf;

struct Python
Expand Down Expand Up @@ -61,6 +64,9 @@ int main(int argc, char* argv[])
return 1; // invalid file
}
Python python;
addEcoLabPath();
PyImport_ImportModule("ecolab");
registerParallel();
if (auto path=PySys_GetObject("path"))
{
PyList_Append(path,PyUnicode_FromString("."));
Expand All @@ -74,5 +80,9 @@ int main(int argc, char* argv[])
int err=0;
if (err=PyRun_SimpleFile(script,argv[1]))
PyErr_Print();
#ifdef MPI_SUPPORT
// terminate any parallel region
if (myid()==0) MPIbuf()<<string("return")<<bcast(0);
#endif
return err;
}
11 changes: 7 additions & 4 deletions test/testParallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ struct Test
classdesc::MPIbuf buf;
buf<<ecolab::myid();
buf.gather(0);
while (buf.pos()<buf.size())
if (ecolab::myid()==0)
{
unsigned p; buf>>p;
procs.insert(p);
while (buf.pos()<buf.size())
{
unsigned p; buf>>p;
procs.insert(p);
}
assert(procs.size()==ecolab::nprocs());
}
assert(procs.size()==ecolab::nprocs());
#else
procs.insert(0);
#endif
Expand Down

0 comments on commit 6fb1c21

Please sign in to comment.