-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_compress.cpp
More file actions
606 lines (520 loc) · 20 KB
/
matrix_compress.cpp
File metadata and controls
606 lines (520 loc) · 20 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#include "matrix_compress.hpp"
#include <algorithm>
#include <fstream>
#include <future>
#include <random>
#include <thread>
#include <spdlog/spdlog.h>
#include "atom/algorithm/rust_numeric.hpp"
#include "atom/error/exception.hpp"
#ifdef __AVX2__
#define USE_SIMD 2 // AVX2
#include <immintrin.h>
#elif defined(__SSE4_1__)
#define USE_SIMD 1 // SSE4.1
#include <smmintrin.h>
#else
#define USE_SIMD 0
#endif
#ifdef ATOM_USE_BOOST
#include <boost/exception/all.hpp>
#include <boost/filesystem.hpp>
#endif
namespace atom::algorithm {
// Define default number of threads for compression/decompression
static usize getDefaultThreadCount() noexcept {
return std::max(1u, std::thread::hardware_concurrency());
}
auto MatrixCompressor::compress(const Matrix& matrix) -> CompressedData {
// Input validation
if (matrix.empty() || matrix[0].empty()) {
return {};
}
try {
// Use SIMD optimized version if available
#if USE_SIMD > 0
return compressWithSIMD(matrix);
#else
CompressedData compressed;
compressed.reserve(
std::min<usize>(1000, matrix.size() * matrix[0].size() / 2));
char currentChar = matrix[0][0];
i32 count = 0;
// Use C++20 ranges
for (const auto& row : matrix) {
for (const char ch : row) {
if (ch == currentChar) {
count++;
} else {
compressed.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
}
if (count > 0) {
compressed.emplace_back(currentChar, count);
}
return compressed;
#endif
} catch (const std::exception& e) {
THROW_MATRIX_COMPRESS_EXCEPTION("Error during matrix compression: " +
std::string(e.what()));
}
}
auto MatrixCompressor::compressParallel(const Matrix& matrix, i32 thread_count)
-> CompressedData {
if (matrix.empty() || matrix[0].empty()) {
return {};
}
usize num_threads = thread_count > 0 ? static_cast<usize>(thread_count)
: getDefaultThreadCount();
if (matrix.size() < num_threads ||
matrix.size() * matrix[0].size() < 10000) {
return compress(matrix);
}
try {
usize rows_per_thread = matrix.size() / num_threads;
std::vector<std::future<CompressedData>> futures;
futures.reserve(num_threads);
for (usize t = 0; t < num_threads; ++t) {
usize start_row = t * rows_per_thread;
usize end_row = (t == num_threads - 1) ? matrix.size()
: (t + 1) * rows_per_thread;
futures.push_back(
std::async(std::launch::async, [&matrix, start_row, end_row]() {
CompressedData result;
if (start_row >= end_row)
return result;
char currentChar = matrix[start_row][0];
i32 count = 0;
for (usize i = start_row; i < end_row; ++i) {
for (char ch : matrix[i]) {
if (ch == currentChar) {
count++;
} else {
result.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
}
if (count > 0) {
result.emplace_back(currentChar, count);
}
return result;
}));
}
CompressedData result;
for (auto& future : futures) {
auto partial = future.get();
if (result.empty()) {
result = std::move(partial);
} else if (!partial.empty()) {
if (result.back().first == partial.front().first) {
result.back().second += partial.front().second;
result.insert(result.end(), std::next(partial.begin()),
partial.end());
} else {
result.insert(result.end(), partial.begin(), partial.end());
}
}
}
return result;
} catch (const std::exception& e) {
THROW_MATRIX_COMPRESS_EXCEPTION(
"Error during parallel matrix compression: " +
std::string(e.what()));
}
}
auto MatrixCompressor::decompress(const CompressedData& compressed, i32 rows,
i32 cols) -> Matrix {
if (rows <= 0 || cols <= 0) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Invalid dimensions: rows and cols must be positive");
}
if (compressed.empty()) {
return Matrix(rows, std::vector<char>(cols, 0));
}
try {
#if USE_SIMD > 0
return decompressWithSIMD(compressed, rows, cols);
#else
Matrix matrix(rows, std::vector<char>(cols));
i32 index = 0;
i32 totalElements = rows * cols;
usize elementCount = 0;
for (const auto& [ch, count] : compressed) {
elementCount += count;
}
if (elementCount != static_cast<usize>(totalElements)) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Decompression error: Element count mismatch - expected " +
std::to_string(totalElements) + ", got " +
std::to_string(elementCount));
}
for (const auto& [ch, count] : compressed) {
for (i32 i = 0; i < count; ++i) {
i32 row = index / cols;
i32 col = index % cols;
if (row >= rows || col >= cols) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Decompression error: Index out of bounds at " +
std::to_string(index) + " (row=" + std::to_string(row) +
", col=" + std::to_string(col) + ")");
}
matrix[row][col] = ch;
index++;
}
}
return matrix;
#endif
} catch (const std::exception& e) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Error during matrix decompression: " + std::string(e.what()));
}
}
auto MatrixCompressor::decompressParallel(const CompressedData& compressed,
i32 rows, i32 cols, i32 thread_count)
-> Matrix {
if (rows <= 0 || cols <= 0) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Invalid dimensions: rows and cols must be positive");
}
if (compressed.empty()) {
return Matrix(rows, std::vector<char>(cols, 0));
}
if (rows * cols < 10000) {
return decompress(compressed, rows, cols);
}
try {
usize num_threads = thread_count > 0 ? static_cast<usize>(thread_count)
: getDefaultThreadCount();
num_threads = std::min(num_threads, static_cast<usize>(rows));
Matrix result(rows, std::vector<char>(cols));
std::vector<std::pair<usize, usize>> row_ranges;
std::vector<std::pair<usize, usize>> element_ranges;
usize rows_per_thread = rows / num_threads;
usize elements_per_row = cols;
for (usize t = 0; t < num_threads; ++t) {
usize start_row = t * rows_per_thread;
usize end_row =
(t == num_threads - 1) ? rows : (t + 1) * rows_per_thread;
row_ranges.emplace_back(start_row, end_row);
usize start_element = start_row * elements_per_row;
usize end_element = end_row * elements_per_row;
element_ranges.emplace_back(start_element, end_element);
}
std::vector<usize> element_offsets = {0};
for (const auto& [ch, count] : compressed) {
element_offsets.push_back(element_offsets.back() + count);
}
std::vector<std::future<void>> futures;
for (usize t = 0; t < num_threads; ++t) {
futures.push_back(std::async(std::launch::async, [&, t]() {
usize start_element = element_ranges[t].first;
usize end_element = element_ranges[t].second;
usize block_index = 0;
while (block_index < element_offsets.size() - 1 &&
element_offsets[block_index + 1] <= start_element) {
block_index++;
}
usize current_element = start_element;
while (current_element < end_element &&
block_index < compressed.size()) {
char ch = compressed[block_index].first;
usize block_start = element_offsets[block_index];
usize block_end = element_offsets[block_index + 1];
usize process_start =
std::max(current_element, block_start);
usize process_end = std::min(end_element, block_end);
for (usize i = process_start; i < process_end; ++i) {
i32 row = static_cast<i32>(i / cols);
i32 col = static_cast<i32>(i % cols);
result[row][col] = ch;
}
current_element = process_end;
if (current_element >= block_end) {
block_index++;
}
}
}));
}
for (auto& future : futures) {
future.get();
}
return result;
} catch (const std::exception& e) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Error during parallel matrix decompression: " +
std::string(e.what()));
}
}
auto MatrixCompressor::compressWithSIMD(const Matrix& matrix)
-> CompressedData {
CompressedData compressed;
compressed.reserve(
std::min<usize>(1000, matrix.size() * matrix[0].size() / 4));
char currentChar = matrix[0][0];
i32 count = 0;
#if USE_SIMD == 2 // AVX2
for (const auto& row : matrix) {
usize i = 0;
for (; i + 32 <= row.size(); i += 32) {
__m256i chars1 =
_mm256_load_si256(reinterpret_cast<const __m256i*>(&row[i]));
__m256i chars2 = _mm256_load_si256(
reinterpret_cast<const __m256i*>(&row[i + 16]));
for (i32 j = 0; j < 16; ++j) {
char ch = reinterpret_cast<const char*>(&chars1)[j];
if (ch == currentChar) {
count++;
} else {
compressed.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
for (i32 j = 0; j < 16; ++j) {
char ch = reinterpret_cast<const char*>(&chars2)[j];
if (ch == currentChar) {
count++;
} else {
compressed.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
}
for (; i < row.size(); ++i) {
char ch = row[i];
if (ch == currentChar) {
count++;
} else {
compressed.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
}
#elif USE_SIMD == 1
for (const auto& row : matrix) {
usize i = 0;
for (; i + 16 <= row.size(); i += 16) {
__m128i chars =
_mm_load_si128(reinterpret_cast<const __m128i*>(&row[i]));
for (i32 j = 0; j < 16; ++j) {
char ch = reinterpret_cast<const char*>(&chars)[j];
if (ch == currentChar) {
count++;
} else {
compressed.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
}
for (; i < row.size(); ++i) {
char ch = row[i];
if (ch == currentChar) {
count++;
} else {
compressed.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
}
#else
for (const auto& row : matrix) {
for (char ch : row) {
if (ch == currentChar) {
count++;
} else {
compressed.emplace_back(currentChar, count);
currentChar = ch;
count = 1;
}
}
}
#endif
if (count > 0) {
compressed.emplace_back(currentChar, count);
}
return compressed;
}
auto MatrixCompressor::decompressWithSIMD(const CompressedData& compressed,
i32 rows, i32 cols) -> Matrix {
Matrix matrix(rows, std::vector<char>(cols));
i32 index = 0;
i32 total_elements = rows * cols;
usize elementCount = 0;
for (const auto& [ch, count] : compressed) {
elementCount += count;
}
if (elementCount != static_cast<usize>(total_elements)) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Decompression error: Element count mismatch - expected " +
std::to_string(total_elements) + ", got " +
std::to_string(elementCount));
}
#if USE_SIMD == 2 // AVX2
for (const auto& [ch, count] : compressed) {
__m256i chars = _mm256_set1_epi8(ch);
for (i32 i = 0; i < count; i += 32) {
i32 remaining = std::min(32, count - i);
for (i32 j = 0; j < remaining; ++j) {
i32 row = index / cols;
i32 col = index % cols;
if (row >= rows || col >= cols) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Decompression error: Index out of bounds at " +
std::to_string(index) + " (row=" + std::to_string(row) +
", col=" + std::to_string(col) + ")");
}
matrix[row][col] = reinterpret_cast<const char*>(&chars)[j];
index++;
}
}
}
#elif USE_SIMD == 1 // SSE4.1
for (const auto& [ch, count] : compressed) {
__m128i chars = _mm_set1_epi8(ch);
for (i32 i = 0; i < count; i += 16) {
i32 remaining = std::min(16, count - i);
for (i32 j = 0; j < remaining; ++j) {
i32 row = index / cols;
i32 col = index % cols;
if (row >= rows || col >= cols) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Decompression error: Index out of bounds at " +
std::to_string(index) + " (row=" + std::to_string(row) +
", col=" + std::to_string(col) + ")");
}
matrix[row][col] = reinterpret_cast<const char*>(&chars)[j];
index++;
}
}
}
#else
for (const auto& [ch, count] : compressed) {
for (i32 i = 0; i < count; ++i) {
i32 row = index / cols;
i32 col = index % cols;
if (row >= rows || col >= cols) {
THROW_MATRIX_DECOMPRESS_EXCEPTION(
"Decompression error: Index out of bounds at " +
std::to_string(index) + " (row=" + std::to_string(row) +
", col=" + std::to_string(col) + ")");
}
matrix[row][col] = ch;
index++;
}
}
#endif
return matrix;
}
auto MatrixCompressor::generateRandomMatrix(i32 rows, i32 cols,
std::string_view charset)
-> Matrix {
std::random_device randomDevice;
std::mt19937 generator(randomDevice());
std::uniform_int_distribution<i32> distribution(
0, static_cast<i32>(charset.length()) - 1);
Matrix matrix(rows, std::vector<char>(cols));
for (auto& row : matrix) {
std::ranges::generate(row.begin(), row.end(), [&]() {
return charset[distribution(generator)];
});
}
return matrix;
}
void MatrixCompressor::saveCompressedToFile(const CompressedData& compressed,
std::string_view filename) {
#ifdef ATOM_USE_BOOST
boost::filesystem::path filepath(filename);
std::ofstream file(filepath.string(), std::ios::binary);
#else
std::ofstream file(std::string(filename), std::ios::binary);
#endif
if (!file) {
#ifdef ATOM_USE_BOOST
throw boost::enable_error_info(FileOpenException())
<< boost::errinfo_api_function("Unable to open file for writing: " +
std::string(filename));
#else
THROW_FAIL_TO_OPEN_FILE("Unable to open file for writing: " +
std::string(filename));
#endif
}
for (const auto& [ch, count] : compressed) {
file.write(reinterpret_cast<const char*>(&ch), sizeof(ch));
file.write(reinterpret_cast<const char*>(&count), sizeof(count));
}
}
auto MatrixCompressor::loadCompressedFromFile(std::string_view filename)
-> CompressedData {
#ifdef ATOM_USE_BOOST
boost::filesystem::path filepath(filename);
std::ifstream file(filepath.string(), std::ios::binary);
#else
std::ifstream file(std::string(filename), std::ios::binary);
#endif
if (!file) {
#ifdef ATOM_USE_BOOST
throw boost::enable_error_info(FileOpenException())
<< boost::errinfo_api_function("Unable to open file for reading: " +
std::string(filename));
#else
THROW_FAIL_TO_OPEN_FILE("Unable to open file for reading: " +
std::string(filename));
#endif
}
CompressedData compressed;
char ch;
i32 count;
while (file.read(reinterpret_cast<char*>(&ch), sizeof(ch)) &&
file.read(reinterpret_cast<char*>(&count), sizeof(count))) {
compressed.emplace_back(ch, count);
}
return compressed;
}
#if ATOM_ENABLE_DEBUG
void performanceTest(i32 rows, i32 cols, bool runParallel) {
auto matrix = MatrixCompressor::generateRandomMatrix(rows, cols);
auto start = std::chrono::high_resolution_clock::now();
auto compressed = MatrixCompressor::compress(matrix);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<f64, std::milli> compression_time = end - start;
start = std::chrono::high_resolution_clock::now();
auto decompressed = MatrixCompressor::decompress(compressed, rows, cols);
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<f64, std::milli> decompression_time = end - start;
f64 compression_ratio =
MatrixCompressor::calculateCompressionRatio(matrix, compressed);
spdlog::info("Matrix size: {}x{}", rows, cols);
spdlog::info("Compression time: {} ms", compression_time.count());
spdlog::info("Decompression time: {} ms", decompression_time.count());
spdlog::info("Compression ratio: {}", compression_ratio);
spdlog::info("Compressed size: {} elements", compressed.size());
if (runParallel) {
start = std::chrono::high_resolution_clock::now();
compressed = MatrixCompressor::compressParallel(matrix);
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<f64, std::milli> parallel_compression_time =
end - start;
start = std::chrono::high_resolution_clock::now();
decompressed =
MatrixCompressor::decompressParallel(compressed, rows, cols);
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<f64, std::milli> parallel_decompression_time =
end - start;
spdlog::info("\nParallel processing:");
spdlog::info("Compression time: {} ms",
parallel_compression_time.count());
spdlog::info("Decompression time: {} ms",
parallel_decompression_time.count());
}
}
#endif
} // namespace atom::algorithm