Update of /cvsroot/boost/boost/libs/parallel/src/mpi/python
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv4446/src/mpi/python
Added Files:
py_communicator.cpp py_environment.cpp py_request.cpp
py_timer.cpp
Removed Files:
communicator.cpp environment.cpp request.cpp timer.cpp
Log Message:
Add support for building the Python MPI module
--- NEW FILE: py_communicator.cpp ---
// (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
/** @file communicator.cpp
*
* This file reflects the Boost.MPI @c communicator class into
* Python.
*/
#include <boost/python.hpp>
#include <boost/parallel/mpi.hpp>
#include <boost/parallel/mpi/python/serialize.hpp>
using namespace boost::python;
using namespace boost::parallel::mpi;
namespace boost { namespace parallel { namespace mpi { namespace python {
extern const char* communicator_docstring;
extern const char* communicator_default_constructor_docstring;
extern const char* communicator_rank_docstring;
extern const char* communicator_size_docstring;
extern const char* communicator_send_docstring;
extern const char* communicator_recv_docstring;
extern const char* communicator_isend_docstring;
extern const char* communicator_irecv_docstring;
extern const char* communicator_probe_docstring;
extern const char* communicator_iprobe_docstring;
extern const char* communicator_barrier_docstring;
extern const char* communicator_split_docstring;
extern const char* communicator_split_key_docstring;
extern const char* communicator_abort_docstring;
object
communicator_recv(const communicator& comm, int source, int tag,
bool return_status)
{
using boost::python::make_tuple;
object result;
status stat = comm.recv(source, tag, result);
if (return_status)
return make_tuple(result, stat);
else
return result;
}
object
communicator_irecv(const communicator& comm, int source, int tag)
{
using boost::python::make_tuple;
object result;
object req(comm.irecv(source, tag, result));
req.attr("value") = result;
return req;
}
object
communicator_iprobe(const communicator& comm, int source, int tag)
{
if (boost::optional<status> result = comm.iprobe(source, tag))
return object(*result);
else
return object();
}
extern void export_skeleton_and_content(class_<communicator>&);
void export_communicator()
{
using boost::python::arg;
using boost::python::object;
class_<communicator> comm("communicator", communicator_docstring);
comm
.def(init<>())
.add_property("rank", &communicator::rank, communicator_rank_docstring)
.add_property("size", &communicator::size, communicator_size_docstring)
.def("send",
(void (communicator::*)(int, int, const object&) const)
&communicator::send<object>,
(arg("dest"), arg("tag") = 0, arg("value") = object()),
communicator_send_docstring)
.def("recv", &communicator_recv,
(arg("source") = any_source, arg("tag") = any_tag,
arg("return_status") = false),
communicator_recv_docstring)
.def("isend",
(request (communicator::*)(int, int, const object&) const)
&communicator::isend<object>,
(arg("dest"), arg("tag") = 0, arg("value") = object()),
communicator_isend_docstring)
.def("irecv", &communicator_irecv,
(arg("source") = any_source, arg("tag") = any_tag),
communicator_irecv_docstring)
.def("probe", &communicator::probe,
(arg("source") = any_source, arg("tag") = any_tag),
communicator_probe_docstring)
.def("iprobe", &communicator_iprobe,
(arg("source") = any_source, arg("tag") = any_tag),
communicator_iprobe_docstring)
.def("barrier", &communicator::barrier, communicator_barrier_docstring)
.def("__nonzero__", &communicator::operator bool)
.def("split",
(communicator (communicator::*)(int) const)&communicator::split,
(arg("color")), communicator_split_docstring)
.def("split",
(communicator (communicator::*)(int, int) const)&communicator::split,
(arg("color"), arg("key")))
.def("abort", &communicator::abort, arg("errcode"),
communicator_abort_docstring)
;
// Module-level attributes
scope().attr("any_source") = any_source;
scope().attr("any_tag") = any_tag;
{
communicator world;
scope().attr("world") = world;
scope().attr("rank") = world.rank();
scope().attr("size") = world.size();
}
// Export skeleton and content
export_skeleton_and_content(comm);
}
} } } } // end namespace boost::parallel::mpi::python
--- NEW FILE: py_environment.cpp ---
// (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
/** @file environment.cpp
*
* This file reflects the Boost.MPI "environment" class into Python
* methods at module level.
*/
#include <boost/python.hpp>
#include <boost/parallel/mpi.hpp>
using namespace boost::python;
using namespace boost::parallel::mpi;
namespace boost { namespace parallel { namespace mpi { namespace python {
extern const char* environment_init_docstring;
extern const char* environment_finalize_docstring;
extern const char* environment_abort_docstring;
extern const char* environment_initialized_docstring;
extern const char* environment_finalized_docstring;
/**
* The environment used by the Boost.MPI Python module. This will be
* zero-initialized before it is used.
*/
static environment* env;
bool mpi_init(list python_argv, bool abort_on_exception)
{
// If MPI is already initialized, do nothing.
if (environment::initialized())
return false;
// Convert Python argv into C-style argc/argv.
int my_argc = extract<int>(python_argv.attr("__len__")());
char** my_argv = new char*[my_argc];
for (int arg = 0; arg < my_argc; ++arg)
my_argv[arg] = strdup(extract<const char*>(python_argv[arg]));
// Initialize MPI
int mpi_argc = my_argc;
char** mpi_argv = my_argv;
env = new environment(mpi_argc, mpi_argv, abort_on_exception);
// If anything changed, convert C-style argc/argv into Python argv
if (mpi_argv != my_argv)
PySys_SetArgv(mpi_argc, mpi_argv);
for (int arg = 0; arg < my_argc; ++arg)
free(my_argv[arg]);
delete [] my_argv;
return true;
}
void mpi_finalize()
{
if (env) {
delete env;
env = 0;
}
}
void export_environment()
{
using boost::python::arg;
def("init", mpi_init, (arg("argv"), arg("abort_on_exception") = true),
environment_init_docstring);
def("finalize", mpi_finalize, environment_finalize_docstring);
// Setup initialization and finalization code
if (!environment::initialized()) {
// MPI_Init from sys.argv
object sys = object(handle<>(PyImport_ImportModule("sys")));
mpi_init(extract<list>(sys.attr("argv")), true);
// Setup MPI_Finalize call when the program exits
object atexit = object(handle<>(PyImport_ImportModule("atexit")));
object finalize = scope().attr("finalize");
atexit.attr("register")(finalize);
}
def("abort", &environment::abort, arg("errcode"),
environment_abort_docstring);
def("initialized", &environment::initialized,
environment_initialized_docstring);
def("finalized", &environment::finalized,
environment_finalized_docstring);
scope().attr("max_tag") = environment::max_tag();
scope().attr("collectives_tag") = environment::collectives_tag();
scope().attr("processor_name") = environment::processor_name();
if (optional<int> host_rank = environment::host_rank())
scope().attr("host_rank") = *host_rank;
else
scope().attr("host_rank") = object();
if (optional<int> io_rank = environment::io_rank())
scope().attr("io_rank") = *io_rank;
else
scope().attr("io_rank") = object();
}
} } } } // end namespace boost::parallel::mpi::python
--- NEW FILE: py_request.cpp ---
// (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
/** @file request.cpp
*
* This file reflects the Boost.MPI @c request class into
* Python.
*/
#include <boost/python.hpp>
#include <boost/parallel/mpi.hpp>
using namespace boost::python;
using namespace boost::parallel::mpi;
namespace boost { namespace parallel { namespace mpi { namespace python {
extern const char* request_docstring;
extern const char* request_wait_docstring;
extern const char* request_test_docstring;
extern const char* request_cancel_docstring;
object request_wait(object req_obj)
{
request& req = extract<request&>(req_obj)();
status stat = req.wait();
if (PyObject_HasAttrString(req_obj.ptr(), "value"))
return boost::python::make_tuple(stat, req_obj.attr("value"));
else
return object(stat);
}
object request_test(object req_obj)
{
request& req = extract<request&>(req_obj)();
if (optional<status> stat = req.test())
{
if (PyObject_HasAttrString(req_obj.ptr(), "value"))
return boost::python::make_tuple(stat, req_obj.attr("value"));
else
return object(stat);
}
else
return object();
}
void export_request()
{
using boost::python::arg;
using boost::python::object;
class_<request>("request", request_docstring, no_init)
.def("wait", &request_wait, request_wait_docstring)
.def("test", &request_test, request_test_docstring)
.def("cancel", &request::cancel, request_cancel_docstring)
;
}
} } } } // end namespace boost::parallel::mpi::python
--- NEW FILE: py_timer.cpp ---
// (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Douglas Gregor
/** @file timer.cpp
*
* This file reflects the Boost.MPI @c timer class into
* Python.
*/
#include <boost/python.hpp>
#include <boost/parallel/mpi/timer.hpp>
using namespace boost::python;
using namespace boost::parallel::mpi;
namespace boost { namespace parallel { namespace mpi { namespace python {
extern const char* timer_docstring;
extern const char* timer_default_constructor_docstring;
extern const char* timer_restart_docstring;
extern const char* timer_elapsed_docstring;
extern const char* timer_elapsed_min_docstring;
extern const char* timer_elapsed_max_docstring;
extern const char* timer_time_is_global_docstring;
void export_timer()
{
using boost::python::arg;
using boost::python::object;
class_<timer>("timer", timer_docstring)
.def(init<>())
.def("restart", &timer::restart, timer_restart_docstring)
.add_property("elapsed", &timer::elapsed, timer_elapsed_docstring)
.add_property("elapsed_min", &timer::elapsed_min,
timer_elapsed_min_docstring)
.add_property("elapsed_max", &timer::elapsed_max,
timer_elapsed_max_docstring)
.add_property("time_is_global", &timer::time_is_global,
timer_time_is_global_docstring)
;
}
} } } } // end namespace boost::parallel::mpi::python
--- communicator.cpp DELETED ---
--- environment.cpp DELETED ---
--- request.cpp DELETED ---
--- timer.cpp DELETED ---
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs