1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 | #include <vector>
|
---|
4 | #include <boost/mpi/communicator.hpp>
|
---|
5 | #include <boost/mpi/collectives.hpp>
|
---|
6 | #include "scatterv.hpp"
|
---|
7 | #include "gatherv.hpp"
|
---|
8 |
|
---|
9 | using std::vector;
|
---|
10 | using std::ifstream;
|
---|
11 | using std::cout;
|
---|
12 | using std::cin;
|
---|
13 | using std::endl;
|
---|
14 |
|
---|
15 | void print( vector<int>* M, int m, int n ) {
|
---|
16 |
|
---|
17 | for ( int i = 0; i < m; ++ i ) {
|
---|
18 | for ( int j = 0; j < n; ++j ) {
|
---|
19 | cout << M[i][j] << " ";
|
---|
20 | } cout << endl;
|
---|
21 | }
|
---|
22 | } // ---------- end of function print ----------
|
---|
23 |
|
---|
24 | int main ( int argc, char *argv[] )
|
---|
25 | {
|
---|
26 | boost::mpi::environment env( argc, argv );
|
---|
27 | boost::mpi::communicator world;
|
---|
28 |
|
---|
29 | const int nprocs = world.size();
|
---|
30 | const int myRank = world.rank();
|
---|
31 | const int root = 0;
|
---|
32 | const int dim = 5;
|
---|
33 |
|
---|
34 | int ninputs;
|
---|
35 | ifstream file;
|
---|
36 | if ( myRank == root ) {
|
---|
37 |
|
---|
38 | file.open( "input.txt", ifstream::in );
|
---|
39 | file >> ninputs;
|
---|
40 | file.close();
|
---|
41 | }
|
---|
42 | boost::mpi::broadcast( world, ninputs, root );
|
---|
43 |
|
---|
44 | int remainder = ninputs % nprocs;
|
---|
45 | vector<int> pieceFor( nprocs, ninputs/nprocs );
|
---|
46 | for ( int rank = 0; rank < remainder; rank++ ) ++pieceFor[rank];
|
---|
47 |
|
---|
48 | int myPiece = pieceFor[myRank];
|
---|
49 |
|
---|
50 | vector<int> displs(nprocs);
|
---|
51 | for ( int i = 0, aux = 0; i < nprocs; i++ ) {
|
---|
52 | displs[i] = aux;
|
---|
53 | aux += pieceFor[i];
|
---|
54 | }
|
---|
55 |
|
---|
56 | //----------------------------------------------------------------------
|
---|
57 | // C-like DATA
|
---|
58 | //----------------------------------------------------------------------
|
---|
59 | vector<int>* bigPtr = new vector<int>[ninputs];
|
---|
60 | for ( int i = 0; i < ninputs; ++i ) bigPtr[i].resize(dim);
|
---|
61 |
|
---|
62 | for ( int i = 0; i < ninputs; ++i ) {
|
---|
63 | for ( int j = 0; j < dim; ++j ) {
|
---|
64 | bigPtr[i][j] = i+1;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | //----------------------------------------------------------------------
|
---|
69 | // C++-like DATA
|
---|
70 | //----------------------------------------------------------------------
|
---|
71 | vector<vector<int> > V( ninputs, vector<int>(dim) );
|
---|
72 | for ( int i = 0; i < ninputs; ++i ) {
|
---|
73 | for ( int j = 0; j < dim; ++j ) {
|
---|
74 | V[i][j] = i+1;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | //----------------------------------------------------------------------
|
---|
79 | // BOOST::MPI::SCATTERV
|
---|
80 | //----------------------------------------------------------------------
|
---|
81 | vector<int>* v = new vector<int>[myPiece];
|
---|
82 |
|
---|
83 | cout << "***** Scatterv - Prototype 1 *****" << endl;
|
---|
84 | boost::mpi::scatterv( world, bigPtr, pieceFor, displs, v, myPiece, root );
|
---|
85 | print( v, myPiece, dim );
|
---|
86 |
|
---|
87 | // cout << "***** Scatterv - Prototype 2 *****" << endl;
|
---|
88 | // boost::mpi::scatterv( world, V, pieceFor, displs, v, myPiece, root );
|
---|
89 | // print( v, myPiece, dim );
|
---|
90 |
|
---|
91 | // cout << "***** Scatterv - Prototype 3 *****" << endl;
|
---|
92 | // boost::mpi::scatterv( world, bigPtr, ninputs, v, root );
|
---|
93 | // print( v, myPiece, dim );
|
---|
94 |
|
---|
95 | // cout << "***** Scatterv - Prototype 4 *****" << endl;
|
---|
96 | // boost::mpi::scatterv( world, V, v, root );
|
---|
97 | // print( v, myPiece, dim );
|
---|
98 |
|
---|
99 | // cout << "***** Scatterv - Prototype 5 *****" << endl;
|
---|
100 | // if ( myRank == root )
|
---|
101 | // boost::mpi::scatterv( world, bigPtr, pieceFor, displs, v, myPiece, root );
|
---|
102 | // else
|
---|
103 | // boost::mpi::scatterv( world, v, myPiece, root );
|
---|
104 | // print( v, myPiece, dim );
|
---|
105 |
|
---|
106 | //----------------------------------------------------------------------
|
---|
107 | // BOOST::MPI::GATHERV
|
---|
108 | //----------------------------------------------------------------------
|
---|
109 | for ( int i = 0; i < ninputs; ++i ) {
|
---|
110 | for ( int j = 0; j < dim; ++j ) {
|
---|
111 | V[i][j] = 0;
|
---|
112 | bigPtr[i][j] = 0;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | if ( myRank == root ) cout << "***** Gatherv - Prototype 1 *****" << endl;
|
---|
117 | boost::mpi::gatherv( world, v, myPiece, bigPtr, pieceFor, displs, root );
|
---|
118 | if ( myRank == root ) print( bigPtr, ninputs, dim );
|
---|
119 |
|
---|
120 | // if ( myRank == root ) cout << "***** Gatherv - Prototype 2 *****" << endl;
|
---|
121 | // boost::mpi::gatherv( world, v, myPiece, V, pieceFor, displs, root );
|
---|
122 | // if ( myRank == root ) print( &V[0], ninputs, dim );
|
---|
123 |
|
---|
124 | // if ( myRank == root ) cout << "***** Gatherv - Prototype 3 *****" << endl;
|
---|
125 | // boost::mpi::gatherv( world, v, myPiece, bigPtr, root );
|
---|
126 | // if ( myRank == root ) print( bigPtr, ninputs, dim );
|
---|
127 |
|
---|
128 | // if ( myRank == root ) cout << "***** Gatherv - Prototype 4 *****" << endl;
|
---|
129 | // boost::mpi::gatherv( world, v, myPiece, bigPtr, root );
|
---|
130 | // if ( myRank == root ) print( bigPtr, ninputs, dim );
|
---|
131 |
|
---|
132 | // if ( myRank == root ) cout << "***** Gatherv - Prototype 5 *****" << endl;
|
---|
133 | // boost::mpi::gatherv( world, v, myPiece, V, root );
|
---|
134 | // if ( myRank == root ) print( &V[0], ninputs, dim );
|
---|
135 |
|
---|
136 | // if ( myRank == root ) cout << "***** Gatherv - Prototype 6 *****" << endl;
|
---|
137 | // if ( myRank == root )
|
---|
138 | // boost::mpi::gatherv( world, v, myPiece, V, pieceFor, displs, root );
|
---|
139 | // else
|
---|
140 | // boost::mpi::gatherv( world, v, myPiece, root );
|
---|
141 | // if ( myRank == root ) print( &V[0], ninputs, dim );
|
---|
142 |
|
---|
143 | // ---------------------------------------------------------------------
|
---|
144 |
|
---|
145 | delete[] bigPtr; delete[] v;
|
---|
146 | V.clear();
|
---|
147 |
|
---|
148 | cout << "----- PROCESS " << myRank << " -----" << endl;
|
---|
149 | cout << myPiece << " of " << ninputs << endl;
|
---|
150 | cin.get();
|
---|
151 | return 0;
|
---|
152 | } // ---------- end of function main ----------
|
---|