Ticket #3261: main.cpp

File main.cpp, 1.3 KB (added by mat087@…, 13 years ago)
Line 
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
8namespace boost
9{
10 namespace mpi
11 {
12 template<> struct is_mpi_datatype<std::string> : public mpl::true_ {};
13 }
14}
15
16class Base
17{
18public:
19 Base() {}
20
21 virtual void print() = 0;
22
23private:
24 friend class boost::serialization::access;
25
26 template<class Archive>
27 void serialize(Archive & ar, const unsigned int version)
28 {
29 }
30};
31
32class Foo : public Base
33{
34public:
35 Foo() {}
36 Foo(const std::string& text) : _text(text) {}
37
38 void print() { std::cout << _text << std::endl; }
39
40
41private:
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
54BOOST_CLASS_EXPORT(Foo);
55
56int 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}