-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsource_MPI_standard.cpp
150 lines (140 loc) · 4.04 KB
/
source_MPI_standard.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <unordered_map>
#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <cmath>
#include <sys/stat.h>
#include "mpi.h"
using namespace std;
long long get_file_size(string file_name) {
struct stat stat_buf;
int rc = stat(file_name.c_str(), &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
}
std::vector<int> encoding(ifstream& ifile, long long len)
{
// 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 += ifile.get();
int code = 256;
std::vector<int> output_code;
// std::cout << "String\tOutput_Code\tAddition\n";
unsigned long long cnt = 1;
char ch;
while (!ifile.fail()) {
ch = ifile.get();
if (!ifile.eof())
c += ch;
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]);
cnt++;
table[p + c] = code;
code++;
p = c;
}
c = "";
if (len > 0 && cnt > len) {
break;
}
}
// 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)
{
string input_file_name = "data/EnglishBible.txt";
string output_file_name = "MPIEncoded.out";
int len = get_file_size(input_file_name);
if (len == -1) {
cerr << "Cannot open input file" << endl;
return -1;
}
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 l = len/2 ;
if(rank == 0){
ifstream ifile(input_file_name);
std::vector<int> output_code0 = encoding(ifile, l);
// 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];
// }
ofstream ofile(output_file_name);
for (unsigned long long i = 0; i < output_code0.size(); i++) {
ofile << output_code0[i] << endl;
}
ofile.close();
MPI_Send( &signal, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
}
if(rank == 1){
ifstream ifile(input_file_name);
ifile.seekg(l);
std::vector<int> output_code1 = encoding(ifile, -1);
// std::vector<string> outp =decoding(output_code1);
MPI_Recv( &signal, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
ofstream ofile;
ofile.open(output_file_name, std::ios::out | std::ios::app);
for (unsigned long long i = 0; i < output_code1.size(); i++) {
ofile << output_code1[i] << endl;
}
ofile.close();
// for(int i=0; i<outp.size();i++){
// cout<<outp[i];
// }
}
cout<<endl;
MPI_Finalize();
}