| 1 |
#include "SZ3/compressor/SZTruncateCompressor.hpp" |
| 2 |
#include "SZ3/lossless/Lossless_zstd.hpp" |
| 3 |
#include "SZ3/lossless/Lossless_bypass.hpp" |
| 4 |
#include "SZ3/utils/FileUtil.hpp" |
| 5 |
#include "SZ3/utils/Config.hpp" |
| 6 |
#include "SZ3/utils/Statistic.hpp" |
| 7 |
#include "SZ3/utils/Timer.hpp" |
| 8 |
#include "SZ3/def.hpp" |
| 9 |
#include <cstdio> |
| 10 |
#include <iostream> |
| 11 |
#include <memory> |
| 12 |
#include <sstream> |
| 13 |
#include <random> |
| 14 |
#include <array> |
| 15 |
|
| 16 |
std::string src_file_name; |
| 17 |
float relative_error_bound = 0; |
| 18 |
float compression_time = 0; |
| 19 |
int byteLen = 2; |
| 20 |
|
| 21 |
template<typename T, uint N, class Lossless> |
| 22 |
float SZ_compress(std::unique_ptr<T[]> const &data, |
| 23 |
const SZ::Config &conf, Lossless lossless) { |
| 24 |
|
| 25 |
std::cout << "****************** Options ********************" << std::endl; |
| 26 |
std::cout << "dimension = " << N |
| 27 |
<< ", error bound = " << conf.absErrorBound |
| 28 |
<< ", byteLen = " << byteLen |
| 29 |
<< ", lossless = " << conf.lossless |
| 30 |
<< std::endl; |
| 31 |
|
| 32 |
std::vector<T> data_ = std::vector<T>(data.get(), data.get() + conf.num); |
| 33 |
|
| 34 |
auto sz = SZ::make_sz_truncate_compressor<T, N>(conf, lossless, byteLen); |
| 35 |
|
| 36 |
SZ::Timer timer; |
| 37 |
timer.start(); |
| 38 |
std::cout << "****************** Compression ******************" << std::endl; |
| 39 |
|
| 40 |
|
| 41 |
size_t compressed_size = 0; |
| 42 |
std::unique_ptr<SZ::uchar[]> compressed; |
| 43 |
compressed.reset(sz.compress(conf,data.get(), compressed_size)); |
| 44 |
|
| 45 |
compression_time = timer.stop("Compression"); |
| 46 |
|
| 47 |
auto ratio = conf.num * sizeof(T) * 1.0 / compressed_size; |
| 48 |
std::cout << "Compression Ratio = " << ratio << std::endl; |
| 49 |
std::cout << "Compressed size = " << compressed_size << std::endl; |
| 50 |
|
| 51 |
std::random_device rd; //Will be used to obtain a seed for the random number engine |
| 52 |
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() |
| 53 |
std::uniform_int_distribution<> dis(0, 10000); |
| 54 |
std::stringstream ss; |
| 55 |
ss << src_file_name.substr(src_file_name.rfind('/') + 1) |
| 56 |
<< "." << relative_error_bound << "." << dis(gen) << ".sz3"; |
| 57 |
auto compressed_file_name = ss.str(); |
| 58 |
SZ::writefile(compressed_file_name.c_str(), compressed.get(), compressed_size); |
| 59 |
std::cout << "Compressed file = " << compressed_file_name << std::endl; |
| 60 |
|
| 61 |
std::cout << "****************** Decompression ****************" << std::endl; |
| 62 |
compressed = SZ::readfile<SZ::uchar>(compressed_file_name.c_str(), compressed_size); |
| 63 |
|
| 64 |
timer.start(); |
| 65 |
T *dec_data = sz.decompress(compressed.get(), compressed_size, conf.num); |
| 66 |
timer.stop("Decompression"); |
| 67 |
|
| 68 |
SZ::verify<T>(data_.data(), dec_data, conf.num); |
| 69 |
|
| 70 |
remove(compressed_file_name.c_str()); |
| 71 |
// auto decompressed_file_name = compressed_file_name + ".out"; |
| 72 |
// SZ::writefile(decompressed_file_name.c_str(), dec_data, conf.num); |
| 73 |
// std::cout << "Decompressed file = " << decompressed_file_name << std::endl; |
| 74 |
|
| 75 |
delete[] dec_data; |
| 76 |
return ratio; |
| 77 |
} |
| 78 |
|
| 79 |
template<class T, uint N> |
| 80 |
float SZ_compress_parse_args(int argc, char **argv, int argp, std::unique_ptr<T[]> &data, float eb, |
| 81 |
std::array<size_t, N> dims) { |
| 82 |
SZ::Config conf; |
| 83 |
conf.setDims(dims.begin(), dims.end()); |
| 84 |
conf.absErrorBound = eb; |
| 85 |
if (argp < argc) { |
| 86 |
conf.lossless = atoi(argv[argp++]); |
| 87 |
} |
| 88 |
if (conf.lossless > 0) { |
| 89 |
return SZ_compress<T, N>(data, conf, SZ::Lossless_zstd(conf.lossless)); |
| 90 |
} else { |
| 91 |
return SZ_compress<T, N>(data, conf, SZ::Lossless_bypass()); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
int main(int argc, char **argv) { |
| 97 |
if (argc < 4) { |
| 98 |
std::cout << "usage: " << argv[0] << " data_file -num_dim dim0 .. dimn byteLens " << std::endl; |
| 99 |
std::cout << "example: " << argv[0] << " qmcpack.dat -3 33120 69 69 2" << std::endl; |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
size_t num = 0; |
| 104 |
auto data = SZ::readfile<float>(argv[1], num); |
| 105 |
src_file_name = argv[1]; |
| 106 |
std::cout << "Read " << num << " elements\n"; |
| 107 |
|
| 108 |
int dim = atoi(argv[2] + 1); |
| 109 |
assert(1 <= dim && dim <= 4); |
| 110 |
int argp = 3; |
| 111 |
std::vector<size_t> dims(dim); |
| 112 |
for (int i = 0; i < dim; i++) { |
| 113 |
dims[i] = atoi(argv[argp++]); |
| 114 |
} |
| 115 |
if (argp < argc) { |
| 116 |
byteLen = atoi(argv[argp++]); |
| 117 |
} |
| 118 |
|
| 119 |
double eb = 0; |
| 120 |
|
| 121 |
|
| 122 |
if (dim == 1) { |
| 123 |
SZ_compress_parse_args<float, 1>(argc, argv, argp, data, eb, std::array<size_t, 1>{dims[0]}); |
| 124 |
} else if (dim == 2) { |
| 125 |
SZ_compress_parse_args<float, 2>(argc, argv, argp, data, eb, std::array<size_t, 2>{dims[0], dims[1]}); |
| 126 |
} else if (dim == 3) { |
| 127 |
SZ_compress_parse_args<float, 3>(argc, argv, argp, data, eb, |
| 128 |
std::array<size_t, 3>{dims[0], dims[1], dims[2]}); |
| 129 |
} else if (dim == 4) { |
| 130 |
SZ_compress_parse_args<float, 4>(argc, argv, argp, data, eb, |
| 131 |
std::array<size_t, 4>{dims[0], dims[1], dims[2], dims[3]}); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
return 0; |
| 136 |
} |