forked from ZaidQureshi/zlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture_compressor.py
375 lines (329 loc) · 12.7 KB
/
texture_compressor.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/python
import sys
import zlib
import cv2
import os
import multiprocessing
import mmap
import zstandard as zstd
import mmap
import numpy as np
MIN_MATCH = 1;
#N_PROCS = 80-4;
N_PROCS = 1;
WARP_SIZE = 32;
PATH = "/home/zaid/.local/share/Steam/steamapps/common/dds/"
#PATH = "/mnt/707f56b3-4a3c-4769-aaea-6f3b497651dc/zaid/datasets/fin_textures/"
#PATH = "./imgs/"
#PATH = "/home/zaid/nfs/datasets/new_textures/"
def find_match(dict, input, input_offset):
max_len = 0;
max_off = 0;
cur_len = 0;
cur_off = 0;
dict_len = len(dict);
input_len = len(input);
while(cur_off < len(dict)):
if ((input_offset < input_len) and (input[input_offset] == dict[cur_off]).all()):
cur_len = 1;
while (((cur_off + cur_len) < dict_len) and ((input_offset + cur_len) < input_len) and (input[input_offset+cur_len] == dict[cur_off+cur_len]).all()):
cur_len = cur_len + 1;
if (cur_len > max_len):
max_len = cur_len;
max_off = cur_off;
cur_off = cur_off + 1;
return max_off,max_len;
def compress_texture(input, dict):
pointers = [];
match_len = 0;
match_off = 0;
prev_off = 0;
prev_len = 0;
#prev = false;
i = 0;
count = 0;
input_len = len(input);
while i < input_len:
match_off,match_len = find_match(dict, input, i);
if match_len < MIN_MATCH:
dict.append(input[i]);
#if (prev_len+prev_off) == len(dict):
# prev = true;
# pointers[-1][1] = pointers[-1][1] + 1;
i = i + 1;
else:
i = i + match_len;
if match_len == MIN_MATCH:
count = count + 1;
#if prev == false:
pointers.append([match_off,match_len])
#print(str(i));
print(i)
#prev_off = match_off;
#prev_len = match_len;
#prev = false;
#print(i)
return dict,pointers,count;
def merge_dict(fin_dict, dict):
match_len = 0;
match_off = 0;
i = 0;
dict_len = len(dict);
while i < dict_len:
match_off,match_len = find_match(fin_dict, dict, i);
if match_len < MIN_MATCH:
fin_dict.append(dict[i]);
#if (prev_len+prev_off) == len(dict):
# prev = true;
# pointers[-1][1] = pointers[-1][1] + 1;
i = i + 1;
else:
i = i + match_len;
#if prev == false:
#prev_off = match_off;
#prev_len = match_len;
#prev = false;
return fin_dict;
def compress_dicts(dicts):
fin_dict = dicts[0]
for dict in dicts[1:]:
if (dict != None):
fin_dict = merge_dict(fin_dict, dict);
return fin_dict;
#row major, single thread
def method1(input):
dict = [];
pointers = [];
count = 0;
input_reshaped = input.reshape(input.shape[0]*input.shape[1], -1);
dict,pointers,count = compress_texture(input_reshaped, dict);
#for i in range(input.shape[0]):
# pointers.append(None);
# dict,pointers[i],cnt = compress_texture(input[i], dict);
# count = count + cnt
dict_size = len(dict);
pointers_size = len(pointers);
return [dict_size, pointers_size, count];
#pointers_count = sum(map(len, pointers));
#row-major linear, warp 32 strided
def method2(input):
input_reshaped = input.reshape(input.shape[0]*input.shape[1], -1);
input_shfled = [None] * WARP_SIZE;
dicts = [None] * WARP_SIZE;
pointers = [None] * WARP_SIZE;
counts = [0] * WARP_SIZE;
for i in range(WARP_SIZE):
input_shfled[i] = input_reshaped[i::WARP_SIZE];
if (input_shfled[i].size == 0):
break;
dicts[i] = [];
dicts[i],pointers[i],counts[i] = compress_texture(input_shfled[i],dicts[i]);
ret = [sum(map(lambda x: len(x) if (x != None) else 0, dicts)), sum(map(lambda x: len(x) if (x != None) else 0,pointers)), sum(counts)];
print("Before Merge Dicts\n")
merged_dict = compress_dicts(dicts);
ret.append(len(merged_dict));
return ret;
#col-major linear, warp 32 strided
def method3(input):
input_reshaped = input.transpose(1,0,2).reshape(input.shape[0]*input.shape[1], -1);
input_shfled = [None] * WARP_SIZE;
dicts = [None] * WARP_SIZE;
pointers = [None] * WARP_SIZE;
counts = [0] * WARP_SIZE;
for i in range(WARP_SIZE):
input_shfled[i] = input_reshaped[i::WARP_SIZE];
if (input_shfled[i].size == 0):
break;
dicts[i] = [];
dicts[i],pointers[i],counts[i] = compress_texture(input_shfled[i],dicts[i]);
ret = [sum(map(lambda x: len(x) if (x != None) else 0, dicts)), sum(map(lambda x: len(x) if (x != None) else 0,pointers)), sum(counts)];
merged_dict = compress_dicts(dicts);
ret.append(len(merged_dict));
return ret;
#row-major matrix, warp 32 strided (each thread gets rows)
def method4(input):
dicts = [None] * WARP_SIZE;
pointers = [None] * WARP_SIZE;
counts = [0] * WARP_SIZE;
inputs = [None] * WARP_SIZE;
#input_reshaped = input.reshape(input.shape[0]*input.shape[1], -1);
#dict,pointers,count = compress_texture(input_reshaped, dict);
for i in range(WARP_SIZE):
inputs[i] = input[i::WARP_SIZE];
if (inputs[i].size == 0):
break;
inputs[i] = inputs[i].reshape(inputs[i].shape[0]*inputs[i].shape[1], -1);
dicts[i] = [];
dicts[i],pointers[i],counts[i] = compress_texture(inputs[i],dicts[i]);
ret = [sum(map(lambda x: len(x) if (x != None) else 0, dicts)), sum(map(lambda x: len(x) if (x != None) else 0,pointers)), sum(counts)];
merged_dict = compress_dicts(dicts);
ret.append(len(merged_dict));
return ret;
#col-major matrix, warp 32 strided (each thread gets cols)
def method5(input):
dicts = [None] * WARP_SIZE;
pointers = [None] * WARP_SIZE;
counts = [0] * WARP_SIZE;
inputs = [None] * WARP_SIZE;
#input_reshaped = input.reshape(input.shape[0]*input.shape[1], -1);
#dict,pointers,count = compress_texture(input_reshaped, dict);
for i in range(WARP_SIZE):
inputs[i] = input.transpose(1,0,2)[i::WARP_SIZE];
if (inputs[i].size == 0):
break;
inputs[i] = inputs[i].reshape(inputs[i].shape[0]*inputs[i].shape[1], -1);
dicts[i] = [];
dicts[i],pointers[i],counts[i] = compress_texture(inputs[i],dicts[i]);
ret = [sum(map(lambda x: len(x) if (x != None) else 0, dicts)), sum(map(lambda x: len(x) if (x != None) else 0,pointers)), sum(counts)];
merged_dict = compress_dicts(dicts);
ret.append(len(merged_dict));
return ret;
def zlib_compress(data):
zc = zlib.compress(data, level=9);
return ["zlib size: " + str(len(zc))];
level = 22;
def zstd_compress(data):
cctx = zstd.ZstdCompressor(level=level);
zstdc = cctx.compress(data);
return ["zstd size: " + str(len(zstdc))];
#strides = [4, 8, 16, 32];
#chunks = [4096, 32*1024, 64*1024];
strides = [32];
chunks = [128*1024];
def zlib_strided_compress(data):
np_arr = np.frombuffer(data, dtype=np.uint8);
data_copy = np.copy(np_arr);
ret = [];
for i in strides:
c_count = 0;
for k in chunks:
l_data = len(data_copy);
pad_needed = int(((int((l_data+k-1)/k)) * k) - l_data);
#print(l_data)
#print(pad_needed)
nz = np.zeros(pad_needed, dtype=np.uint8);
data_copy_n = np.concatenate((data_copy, nz));
n_chunks = int(len(data_copy_n)/k);
c_count = c_count + 1;
t_l = 0;
t_l_c = 0;
t_l_c_d = 0;
for p in range(n_chunks):
sys.stderr.write("chunk: " + str(p) + " Start --------------------------------------------\n")
#start = (0 if p == 0 else ((p)*k));
start = p * k;
end = (p+1) * k;
#sys.stderr.write("start: " + str(start) + "\tend: " + str(end) + "\n")
cur_data = data_copy_n[start:end];
data_copy_r = cur_data.reshape((-1,i));
input = [];
input_compressed = []
for j in range(WARP_SIZE):
sys.stderr.write("tid: " + str(p*WARP_SIZE+j) + "\tchunk: " + str(p) + "\tlane: " +str(j) + "\n");
sys.stderr.flush();
nd = (data_copy_r[j::WARP_SIZE]).reshape((-1)).tobytes();
input.append(nd);
#cctx = zstd.ZstdCompressor(level=level);
zc = zlib.compress(nd, level=9);
sys.stderr.flush();
#input_compressed.append(zstdc);
t_l_c = t_l_c + len(zc);
t_l = t_l + len(nd);
sys.stderr.write("chunk: " + str(p) + " End --------------------------------------------\n")
sys.stderr.write("zlib_" + str(i) + "_" + str(k) + " size: " + str(t_l_c) + "\tpadded size: " + str(len(data_copy_n)) + "\tt_l: " +str(t_l)+ "\torig size: " + str(len(data)) + "\n");
ret.append("zlib_" + str(i) + "_" + str(k) + " size: " + str(t_l_c) + "\tpadded size: " + str(t_l) + "\torig size: " + str(len(data)) + "\n");
return ret;
def zstd_strided_compress(data):
np_arr = np.frombuffer(data, dtype=np.uint8);
data_copy = np.copy(np_arr);
ret = [];
for i in strides:
l_data = len(data_copy);
pad_needed = int(((int((l_data+i-1)/i)) * i) - l_data);
#print(l_data)
#print(pad_needed)
nz = np.zeros(pad_needed, dtype=np.uint8);
data_copy_n = np.concatenate((data_copy, nz));
#print(len(data_copy_n))
data_copy_r = data_copy_n.reshape((-1,i));
input = [];
input_compressed = []
t_l = 0;
t_l_c = 0;
t_l_c_d = 0;
for j in range(WARP_SIZE):
nd = (data_copy_r[j::WARP_SIZE]).reshape((-1)).tobytes();
input.append(nd);
cctx = zstd.ZstdCompressor(level=level);
zstdc = cctx.compress(nd);
#input_compressed.append(zstdc);
t_l_c = t_l_c + len(zstdc);
t_l = t_l + len(nd);
ret.append("zstd_" + str(i) + " size: " + str(t_l_c));
try:
dict_data = zstd.train_dictionary(t_l, input, split_point=1.0, level=level);
cctx = zstd.ZstdCompressor(dict_data=dict_data, level=level);
for j in range(WARP_SIZE):
cc = cctx.compress(input[j]);
t_l_c_d = t_l_c_d + len(cc);
ret.append("zstd_dict_" + str(i) + " dict_size: " + str(len(dict_data)) + " size: " + str(t_l_c_d) + " total: " + str(t_l_c_d+len(dict_data)));
except:
ret.append("zstd_"+str(i)+"_err");
return ret;
def run(id, files):
output_f = open("output/"+str(id), 'w');
#output_err = open("output/"+str(id)+"_err", 'w');
#sys.stderr = output_err;
separator = '\t';
files.sort(key=lambda f: os.stat(f).st_size, reverse=True);
for file in files:
if (file[-4:] == ".dat"):
continue
line = [file];
f = open(file, "r+b");
dmap = mmap.mmap(f.fileno(), 0);
orig_size = os.path.getsize(file);
line.append("orig_size: " + str(orig_size));
b = bytes(dmap);
sys.stderr.write(file + " START ******************************************************\n");
#line = line + zlib_compress(b);
#line = line + zstd_compress(b);
line = line + zlib_strided_compress(b);
#img = cv2.imread(file);
#print(img.shape)
f.close();
sys.stderr.write(file + " END ******************************************************\n");
#line.append(jpg_size);
#pixel_size = img.shape[0]*img.shape[1]*img.shape[2];
#line.append(pixel_size);
#line = line + method1(img);
#print("here1")
#line = line + method2(img);
#print("here2")
#line = line + method3(img);
#print("here3")
#line = line + method4(img);
#print("here4")
#line = line + method5(img);
#print("here5")
out_string = separator.join(map(str, line)) + '\n';
output_f.write(out_string);
output_f.flush();
#break;
output_f.close();
#output_err.close();
if __name__ == '__main__':
files = [(PATH+f) for f in os.listdir(PATH) if os.path.isfile(os.path.join(PATH, f))];
n_files = len(files);
n_iters = n_files/N_PROCS;
n_rem = n_files%N_PROCS;
parent_conns = [None] * N_PROCS;
child_conns = [None] * N_PROCS;
processes = [None] * N_PROCS;
for i in range(N_PROCS):
begin = int(i*n_iters);
end = int((i+1)*n_iters);
processes[i] = multiprocessing.Process(target=run, args=(i, files[begin:end]));
processes[i].start();
for i in range(N_PROCS):
processes[i].join();