-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendclass1.cpp
More file actions
130 lines (104 loc) · 3.39 KB
/
sendclass1.cpp
File metadata and controls
130 lines (104 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//#############################################################
// $Source: /home/mann/Dropbox/PJM/research/1/examples/c++11_mpi/boostWebinar2018/RCS/sendclass1.cpp,v $
// $Revision: 1.1 $
// $Date: 2018/05/04 16:04:21 $
/*
MPI Example: Package up a class and send it off. USES BOOST MPI.
Output: just to keep each process separate
* the master (sender 0) uses cout
* the receiver outputs to a separate file
The recommended approach would be to send output messages
back to the master (or at least an output process) and let
that single process handle all error messages, output, etc.
*/
//##############################################################
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <fstream>
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
namespace mpi = boost::mpi;
using namespace std;
const string PROGRAM = "sendclass1";
//==============================================================
// Simple class containing a few data items
// Includes serialize (from Boost template)
class TestClass
{
private:
int i;
double a;
string desc;
public:
TestClass();
TestClass( int i_in, double a_in, string desc_in ) :
i(i_in), a(a_in), desc(desc_in) {}
~TestClass(){}
friend class boost::serialization::access;
template<class Archive>
void serialize( Archive &ar, const unsigned int version )
{
ar & i;
ar & a;
ar & desc;
}
friend ostream& operator << ( ostream&, TestClass& );
};
ostream& operator << ( ostream& s, TestClass& tclass )
{
s << "i: " << tclass.i << '\n'
<< "a: " << tclass.a << '\n'
<< "desc: \"" << tclass.desc << "\"\n";
return s;
}
//==============================================================
TestClass::TestClass()
{
i = 9876;
a = -1.234;
desc = "Test string from TestClass constructor";
}
//==============================================================
int main(int argc, char *argv[])
{
mpi::environment env(argc,argv);
mpi::communicator world;
int rank = world.rank();
int n_processes = world.size();
// We need at least 2 processes
if( n_processes < 2 ){
cerr << PROGRAM << ": ERROR: n_processes = "
<< n_processes << '\n';
exit(1);
}
if( rank == 0 ){
cout << PROGRAM << rank << ": INFO: "
<< "n_processes = " << n_processes << endl;
}
// send/receive pair
const int sender = 0;
const int receiver = 1;
stringstream receiver_filename;
receiver_filename << "sendclass1_from" << receiver << ".out";
int tag = 0;
if( rank == sender ){
cout << "********************************************\n"
<< "sendclass1: Output from the receiver is in file \"" << receiver_filename.str() << "\"\n"
<< "********************************************\n";
cout << "sendclass1 " << rank << " (sender): n_processes=" << n_processes << '\n';
std::vector<TestClass> tvector;
tvector.push_back( TestClass(1,2.0,"hi") );
tvector.push_back( TestClass(10,10.0, "hi again") );
world.send( receiver, tag, tvector );
cout << "sendclass1 " << rank << ": sent message." << endl;
} else if( rank == receiver ){
ofstream s( receiver_filename.str().c_str() );
s << "sendclass1 " << rank << ": receiver started. Waiting for message." << endl;
std::vector<TestClass> treceive;
world.recv( sender, tag, treceive );
for( auto &tvalue: treceive ){
s << tvalue << '\n';
}
}
}