-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsource_MPI.cpp
More file actions
112 lines (107 loc) · 2.95 KB
/
Copy pathsource_MPI.cpp
File metadata and controls
112 lines (107 loc) · 2.95 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
#include <unordered_map>
#include <vector>
#include <map>
#include <iostream>
#include "mpi.h"
using namespace std;
std::vector<int> encoding(std::string s1)
{
std::cout << "Encoding\n";
std::unordered_map<std::string, int> table;
for (int i = 0; i <= 255; i++) {
std::string ch = "";
ch += char(i);
table[ch] = i;
}
std::string p = "", c = "";
p += s1[0];
int code = 256;
std::vector<int> output_code;
std::cout << "String\tOutput_Code\tAddition\n";
for (int i = 0; i < s1.length(); i++) {
if (i != s1.length() - 1)
c += s1[i + 1];
if (table.find(p + c) != table.end()) {
p = p + c;
}
else {
std::cout << p << "\t" << table[p] << "\t\t"
<< p + c << "\t" << code << std::endl;
output_code.push_back(table[p]);
table[p + c] = code;
code++;
p = c;
}
c = "";
}
std::cout << p << "\t" << table[p] << std::endl;
output_code.push_back(table[p]);
return output_code;
}
std::vector<string> decoding(std::vector<int> op)
{
std::cout << "Decoding\n";
std::unordered_map<int, std::string> table;
std::vector<string> output_ori ;
for (int i = 0; i <= 255; i++) {
std::string ch = "";
ch += char(i);
table[i] = ch;
}
int old = op[0], n;
std::string s = table[old];
std::string c = "";
c += s[0];
//std::cout << s;
output_ori.push_back(s) ;
int count = 256;
for (int i = 0; i < op.size() - 1; i++) {
n = op[i + 1];
if (table.find(n) == table.end()) {
s = table[old];
s = s + c;
}
else {
s = table[n];
}
output_ori.push_back(s);
c = "";
c += s[0];
table[count] = table[old] + c;
count++;
old = n;
}
return output_ori ;
}
int main(int argc, char** argv)
{
int size, rank;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int signal=1;
std::string s = "WYS*WYGWYS*WYSWYSG";
int len = s.size();
int l = len/2 ;
if(rank == 0){
std::string s0= s.substr(0,l);
std::vector<int> output_code0 = encoding(s0);
std::vector<string> outp =decoding(output_code0);
std::cout <<"the decodeing result is \n";
for(int i=0; i<outp.size();i++){
cout<<outp[i];
}
MPI_Send( &signal, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
}
if(rank == 1){
std::string s1= s.substr(l);
std::vector<int> output_code1 = encoding(s1);
std::vector<string> outp =decoding(output_code1);
MPI_Recv( &signal, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for(int i=0; i<outp.size();i++){
cout<<outp[i];
}
}
cout<<endl;
MPI_Finalize();
}