1 // (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
2
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // Authors: Douglas Gregor
8
9 /** @file communicator.cpp
10 *
11 * This file reflects the Boost.MPI @c communicator class into
12 * Python.
13 */
14 #include <boost/python.hpp>
15 #include <boost/mpi.hpp>
16 #include <boost/mpi/python/serialize.hpp>
17 #include "request_with_value.hpp"
18
19 using namespace boost::python;
20 using namespace boost::mpi;
21
22 namespace boost { namespace mpi { namespace python {
23
24 extern const char* communicator_docstring;
25 extern const char* communicator_default_constructor_docstring;
26 extern const char* communicator_rank_docstring;
27 extern const char* communicator_size_docstring;
28 extern const char* communicator_send_docstring;
29 extern const char* communicator_recv_docstring;
30 extern const char* communicator_isend_docstring;
31 extern const char* communicator_irecv_docstring;
32 extern const char* communicator_probe_docstring;
33 extern const char* communicator_iprobe_docstring;
34 extern const char* communicator_barrier_docstring;
35 extern const char* communicator_split_docstring;
36 extern const char* communicator_split_key_docstring;
37 extern const char* communicator_abort_docstring;
38
39 object
communicator_recv(const communicator & comm,int source,int tag,bool return_status)40 communicator_recv(const communicator& comm, int source, int tag,
41 bool return_status)
42 {
43 using boost::python::make_tuple;
44
45 object result;
46 status stat = comm.recv(source, tag, result);
47 if (return_status)
48 return make_tuple(result, stat);
49 else
50 return result;
51 }
52
53 request_with_value
communicator_irecv(const communicator & comm,int source,int tag)54 communicator_irecv(const communicator& comm, int source, int tag)
55 {
56 boost::shared_ptr<object> result(new object());
57 request_with_value req(comm.irecv(source, tag, *result));
58 req.m_internal_value = result;
59 return req;
60 }
61
62 object
communicator_iprobe(const communicator & comm,int source,int tag)63 communicator_iprobe(const communicator& comm, int source, int tag)
64 {
65 if (boost::optional<status> result = comm.iprobe(source, tag))
66 return object(*result);
67 else
68 return object();
69 }
70
71 extern void export_skeleton_and_content(class_<communicator>&);
72
export_communicator()73 void export_communicator()
74 {
75 using boost::python::arg;
76 using boost::python::object;
77
78 class_<communicator> comm("Communicator", communicator_docstring);
79 comm
80 .def(init<>())
81 .add_property("rank", &communicator::rank, communicator_rank_docstring)
82 .add_property("size", &communicator::size, communicator_size_docstring)
83 .def("send",
84 (void (communicator::*)(int, int, const object&) const)
85 &communicator::send<object>,
86 (arg("dest"), arg("tag") = 0, arg("value") = object()),
87 communicator_send_docstring)
88 .def("recv", &communicator_recv,
89 (arg("source") = any_source, arg("tag") = any_tag,
90 arg("return_status") = false),
91 communicator_recv_docstring)
92 .def("isend",
93 (request (communicator::*)(int, int, const object&) const)
94 &communicator::isend<object>,
95 (arg("dest"), arg("tag") = 0, arg("value") = object()),
96 communicator_isend_docstring)
97 .def("irecv", &communicator_irecv,
98 (arg("source") = any_source, arg("tag") = any_tag),
99 communicator_irecv_docstring)
100 .def("probe", &communicator::probe,
101 (arg("source") = any_source, arg("tag") = any_tag),
102 communicator_probe_docstring)
103 .def("iprobe", &communicator_iprobe,
104 (arg("source") = any_source, arg("tag") = any_tag),
105 communicator_iprobe_docstring)
106 .def("barrier", &communicator::barrier, communicator_barrier_docstring)
107 .def("__nonzero__", &communicator::operator bool)
108 .def("split",
109 (communicator (communicator::*)(int) const)&communicator::split,
110 (arg("color")), communicator_split_docstring)
111 .def("split",
112 (communicator (communicator::*)(int, int) const)&communicator::split,
113 (arg("color"), arg("key")))
114 .def("abort", &communicator::abort, arg("errcode"),
115 communicator_abort_docstring)
116 ;
117
118 // Module-level attributes
119 scope().attr("any_source") = any_source;
120 scope().attr("any_tag") = any_tag;
121
122 {
123 communicator world;
124 scope().attr("world") = world;
125 scope().attr("rank") = world.rank();
126 scope().attr("size") = world.size();
127 }
128
129 // Export skeleton and content
130 export_skeleton_and_content(comm);
131 }
132
133 } } } // end namespace boost::mpi::python
134