1 | #include <iostream>
|
---|
2 | #include <boost/mpi.hpp>
|
---|
3 | #include <boost/mpi/packed_oarchive.hpp>
|
---|
4 | #include <boost/mpi/packed_iarchive.hpp>
|
---|
5 | #include <boost/serialization/string.hpp>
|
---|
6 | #include <boost/serialization/export.hpp>
|
---|
7 |
|
---|
8 | namespace boost
|
---|
9 | {
|
---|
10 | namespace mpi
|
---|
11 | {
|
---|
12 | template<> struct is_mpi_datatype<std::string> : public mpl::true_ {};
|
---|
13 | }
|
---|
14 | }
|
---|
15 |
|
---|
16 | class Base
|
---|
17 | {
|
---|
18 | public:
|
---|
19 | Base() {}
|
---|
20 |
|
---|
21 | virtual void print() = 0;
|
---|
22 |
|
---|
23 | private:
|
---|
24 | friend class boost::serialization::access;
|
---|
25 |
|
---|
26 | template<class Archive>
|
---|
27 | void serialize(Archive & ar, const unsigned int version)
|
---|
28 | {
|
---|
29 | }
|
---|
30 | };
|
---|
31 |
|
---|
32 | class Foo : public Base
|
---|
33 | {
|
---|
34 | public:
|
---|
35 | Foo() {}
|
---|
36 | Foo(const std::string& text) : _text(text) {}
|
---|
37 |
|
---|
38 | void print() { std::cout << _text << std::endl; }
|
---|
39 |
|
---|
40 |
|
---|
41 | private:
|
---|
42 | friend class boost::serialization::access;
|
---|
43 |
|
---|
44 | template<class Archive>
|
---|
45 | void serialize(Archive & ar, const unsigned int version)
|
---|
46 | {
|
---|
47 | ar & boost::serialization::base_object<Base>(*this);
|
---|
48 | ar & _text;
|
---|
49 | }
|
---|
50 |
|
---|
51 | std::string _text;
|
---|
52 | };
|
---|
53 |
|
---|
54 | BOOST_CLASS_EXPORT(Foo);
|
---|
55 |
|
---|
56 | int main(int argc, char * argv[])
|
---|
57 | {
|
---|
58 | boost::mpi::environment env(argc, argv);
|
---|
59 | boost::mpi::communicator world;
|
---|
60 |
|
---|
61 | if (world.rank() == 0)
|
---|
62 | {
|
---|
63 | Base * a = new Foo("Hello");
|
---|
64 | world.send(1, 0, a);
|
---|
65 | delete a;
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | Base * a;
|
---|
70 | world.recv(0, 0, a);
|
---|
71 | a->print();
|
---|
72 | delete a;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return 0;
|
---|
76 | }
|
---|