-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_mpi.cpp
More file actions
74 lines (49 loc) · 1.37 KB
/
example_mpi.cpp
File metadata and controls
74 lines (49 loc) · 1.37 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
#include <iostream>
#include "MPI_Handler.hpp"
#include <vector>
using namespace std;
int main(int argc,char* argv[]){
///declare MPI Handler:
MPI_Handler mpih;
///initialize first :
mpih.Initialize(argc,argv);
/*
Some parallel section
*/
// Synchronize:
mpih.Synchronize();
/// Gather
// combine all small segments to rank 1 :
// only the destination will return an valid vector
// other rank will return a empty vector
vector<int> Segs(12);
vector<int> result;
int dest = 1;
result = mpih.Gather(Segs,Segs.size(),Segs.size(),dest);
// Sequential_Print (handy for debug using):
mpih.Sequencial_Print(result.size());
//output :
//+=====================+
//[Rank 00] 0
//[Rank 01] 12*mpih.NProc
//[Rank 02] 0
// ....
//+=====================+
/// Broadcast
// copy Seg from rank 1 to all ranks :
vector<int> Seg;
int source = 1;
if(mpih.Rank==source) Seg.resize(10); // only rank 1 have 10 elems
mpih.Boradcast(Seg,source);
// Sequential_Print (handy for debug using):
mpih.Sequencial_Print(result.size());
//output :
//+=====================+
//[Rank 00] 10
//[Rank 01] 10
//[Rank 02] 10
// ....
//+=====================+
///finalize before end program
mpih.Finalize();
}