Ticket #5292: Main(fixed version 2).cpp

File Main(fixed version 2).cpp, 5.3 KB (added by Júlio Hoffimann <julio.hoffimann@…>, 12 years ago)

Fixed version 2

Line 
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
9using std::vector;
10using std::ifstream;
11using std::cout;
12using std::cin;
13using std::endl;
14
15void 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
24int 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 to be scattered
58 //----------------------------------------------------------------------
59 vector<int>* PTR = new vector<int>[ninputs];
60 for ( int i = 0; i < ninputs; ++i ) PTR[i].resize(dim);
61
62 for ( int i = 0; i < ninputs; ++i ) {
63 for ( int j = 0; j < dim; ++j ) {
64 PTR[i][j] = i+1;
65 }
66 }
67
68 //----------------------------------------------------------------------
69 // C++-like data to be scattered
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>* ptr = new vector<int>[myPiece];
82
83 cout << "***** Scatterv - Prototype 1 *****" << endl;
84 boost::mpi::scatterv( world, PTR, pieceFor, displs, ptr, myPiece, root );
85 print( ptr, myPiece, dim );
86
87// cout << "***** Scatterv - Prototype 2 *****" << endl;
88// boost::mpi::scatterv( world, V, pieceFor, displs, ptr, myPiece, root );
89// print( ptr, myPiece, dim );
90
91// cout << "***** Scatterv - Prototype 3 *****" << endl;
92// if ( myRank == root )
93// boost::mpi::scatterv( world, PTR, pieceFor, displs, ptr, myPiece, root );
94// else
95// boost::mpi::scatterv( world, ptr, myPiece, root );
96// print( ptr, myPiece, dim );
97
98// cout << "***** Scatterv - Prototype 4 *****" << endl;
99// boost::mpi::scatterv( world, PTR, pieceFor, ptr, root );
100// print( ptr, myPiece, dim );
101
102// cout << "***** Scatterv - Prototype 5 *****" << endl;
103// boost::mpi::scatterv( world, V, pieceFor, ptr, root );
104// print( ptr, myPiece, dim );
105
106 //----------------------------------------------------------------------
107 // C-like data to be gathered
108 //----------------------------------------------------------------------
109 // ptr will be gathered
110
111 //----------------------------------------------------------------------
112 // C++-like data to be gathered
113 //----------------------------------------------------------------------
114 vector< vector<int> > v( myPiece );
115 for ( int i = 0; i < myPiece; ++i ) v[i] = ptr[i];
116
117 //----------------------------------------------------------------------
118 // BOOST::MPI::GATHERV
119 //----------------------------------------------------------------------
120 for ( int i = 0; i < ninputs; ++i ) {
121 for ( int j = 0; j < dim; ++j ) {
122 PTR[i][j] = 0;
123 }
124 }
125
126 if ( myRank == root ) cout << "***** Gatherv - Prototype 1 *****" << endl;
127 boost::mpi::gatherv( world, ptr, myPiece, PTR, pieceFor, displs, root );
128 if ( myRank == root ) print( PTR, ninputs, dim );
129
130// if ( myRank == root ) cout << "***** Gatherv - Prototype 2 *****" << endl;
131// boost::mpi::gatherv( world, v, PTR, pieceFor, displs, root );
132// if ( myRank == root ) print( PTR, ninputs, dim );
133
134// if ( myRank == root ) cout << "***** Gatherv - Prototype 3 *****" << endl;
135// if ( myRank == root )
136// boost::mpi::gatherv( world, ptr, myPiece, PTR, pieceFor, displs, root );
137// else
138// boost::mpi::gatherv( world, ptr, myPiece, root );
139// if ( myRank == root ) print( PTR, ninputs, dim );
140
141// if ( myRank == root ) cout << "***** Gatherv - Prototype 4 *****" << endl;
142// if ( myRank == root )
143// boost::mpi::gatherv( world, v, PTR, pieceFor, displs, root );
144// else
145// boost::mpi::gatherv( world, v, root );
146// if ( myRank == root ) print( PTR, ninputs, dim );
147
148// if ( myRank == root ) cout << "***** Gatherv - Prototype 5 *****" << endl;
149// boost::mpi::gatherv( world, ptr, myPiece, PTR, pieceFor, root );
150// if ( myRank == root ) print( PTR, ninputs, dim );
151
152// if ( myRank == root ) cout << "***** Gatherv - Prototype 6 *****" << endl;
153// boost::mpi::gatherv( world, v, PTR, pieceFor, root );
154// if ( myRank == root ) print( PTR, ninputs, dim );
155
156 // ---------------------------------------------------------------------
157
158 delete[] PTR;
159 delete[] ptr;
160 V.clear();
161 v.clear();
162
163 cout << "----- PROCESS " << myRank << " -----" << endl;
164 cout << myPiece << " of " << ninputs << endl;
165 cin.get();
166 return 0;
167} // ---------- end of function main ----------