-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlzw_parallel_decode.cpp
More file actions
192 lines (151 loc) · 4.95 KB
/
lzw_parallel_decode.cpp
File metadata and controls
192 lines (151 loc) · 4.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <sys/stat.h>
#include <pthread.h>
#define DEBUG 1
using std::endl;
using std::cerr;
using std::cout;
int num_thread;
pthread_attr_t* thread_attr;
pthread_t* threads;
std::vector<long long> codes;
struct thread_args_t {
int id;
long long start;
long long end;
std::unordered_map<long long, std::string>* table;
long long code_begin;
std::vector<std::string> output;
};
thread_args_t* thread_args;
long long get_file_size(std::string file_name) {
struct stat stat_buf;
int rc = stat(file_name.c_str(), &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
}
void* decode(void* args)
{
thread_args_t& curr_arg = *((thread_args_t*)args);
long long old;
long long n;
old = codes[curr_arg.start];
std::string s = curr_arg.table->at(old);
std::string c;
c += s[0];
curr_arg.output.push_back(s);
int count = curr_arg.code_begin;
for (int i = curr_arg.start+1; i <= curr_arg.end; i++) {
n = codes[i];
if (curr_arg.table->find(n) == curr_arg.table->end()) {
s = curr_arg.table->at(old);
s = s + c;
} else {
s = curr_arg.table->at(n);
}
curr_arg.output.push_back(s);
c = "";
c += s[0];
curr_arg.table->insert(std::make_pair(count++, curr_arg.table->at(old)+c));
old = n;
}
// start new threads
if (curr_arg.id*2 <= num_thread) {
int next_id = curr_arg.id*2 - 1;
thread_args[next_id].code_begin = count;
thread_args[next_id].table = curr_arg.table;
}
if (curr_arg.id*2+1 <= num_thread) {
int next_id = curr_arg.id*2;
thread_args[next_id].code_begin = count;
thread_args[next_id].table = new std::unordered_map<long long, std::string>(*(curr_arg.table));
pthread_create(&threads[next_id], thread_attr, decode, (void*)&thread_args[next_id]);
}
if (curr_arg.id*2 <= num_thread) {
decode((void*)&thread_args[curr_arg.id*2-1]);
}
if (curr_arg.id*2+1 <= num_thread) {
pthread_join(threads[curr_arg.id*2], NULL);
delete thread_args[curr_arg.id*2].table;
}
return NULL;
}
int main(int argc, char** argv)
{
if (argc < 3) {
cerr << "usage: ./lzw_parallel_decode encoded_file extracted_file" << endl;
return 1;
}
std::string input_file = argv[1];
std::string output_file = argv[2];
long long input_file_size = get_file_size(input_file);
if (input_file_size < 0) {
cerr << "Error: cannot read input file or input file is corrupted" << endl;
return 1;
}
std::ifstream ifile(input_file);
long long buf;
ifile >> buf;
if (ifile.fail() || buf != -2) {
cerr << "Error: input file is corrupted" << endl;
ifile.close();
return 1;
}
ifile >> num_thread;
threads = new pthread_t[num_thread];
thread_args = new thread_args_t[num_thread];
int curr_block = 0;
int code_cnt = 0;
int block_size = -1;
while (ifile >> buf) {
if (buf == -1) { // new block
thread_args[curr_block].start = code_cnt;
ifile >> block_size; // The number of elements to come
for (int i = 0; i < block_size; i++) {
ifile >> buf;
codes.push_back(buf);
code_cnt++;
}
thread_args[curr_block].end = code_cnt-1;
curr_block++;
} else { // corruption
cerr << "Warning: corruption" << endl;
return 2;
}
}
struct timespec start, stop;
double time = 0;
if( clock_gettime(CLOCK_REALTIME, &start) == -1) { perror("clock gettime");}
for (int i = 0; i < num_thread; i++) {
thread_args[i].id = i+1;
// the start point is defined in the file
}
thread_args[0].table = new std::unordered_map<long long, std::string>;
for (int i = 0; i <= 255; i++) {
std::string ch;
ch += char(i);
thread_args[0].table->insert(std::make_pair(i, ch));
}
thread_args[0].code_begin = 256;
thread_attr = new pthread_attr_t;
pthread_attr_init(thread_attr);
pthread_attr_setdetachstate(thread_attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[0], thread_attr, decode, (void*)&thread_args[0]);
pthread_join(threads[0], NULL);
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) { perror("clock gettime");}
time = (stop.tv_sec - start.tv_sec)+ (double)(stop.tv_nsec - start.tv_nsec)/1e9;
std::cout << "Decoding time = " << time << " sec " <<std::endl;
std::ofstream ofile(output_file);
for (int i = 0; i < num_thread; i++) {
for (size_t j = 0; j < thread_args[i].output.size(); j++) {
ofile << thread_args[i].output[j];
}
}
delete thread_args[0].table;
delete [] threads;
delete [] thread_args;
delete thread_attr;
return 0;
}