Skip to content

Commit

Permalink
improved error handing: throw exception and print to stderr instead o…
Browse files Browse the repository at this point in the history
…f exit(0)
  • Loading branch information
ayzk committed Dec 7, 2024
1 parent 49a2694 commit 4dafde0
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 41 deletions.
4 changes: 2 additions & 2 deletions include/SZ3/api/impl/SZAlgoLorenzoReg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ std::shared_ptr<concepts::CompressorInterface<T>> make_compressor_typetwo_lorenz
int methodCnt = (conf.lorenzo + conf.lorenzo2 + conf.regression + conf.regression2);
int use_single_predictor = (methodCnt == 1);
if (methodCnt == 0) {
printf("All lorenzo and regression methods are disabled.\n");
exit(0);
fprintf(stderr, "All lorenzo and regression methods are disabled.\n");
throw std::invalid_argument("All lorenzo and regression methods are disabled.");
}
if (conf.lorenzo) {
if (use_single_predictor) {
Expand Down
12 changes: 7 additions & 5 deletions include/SZ3/api/impl/SZDispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ size_t SZ_compress_dispatcher(Config &conf, const T *data, uchar *cmpData, size_
} else if (conf.cmprAlgo == ALGO_NOPRED) {
cmpSize = SZ_compress_nopred<T, N>(conf, dataCopy.data(), cmpData, cmpCap);
} else {
printf("SZ_compress_dispatcher, Method not supported\n");
exit(0);
fprintf(stderr, "Unknown compression algorithm\n");
throw std::invalid_argument("Unknown compression algorithm");
}

} catch (std::length_error &e) {
Expand Down Expand Up @@ -58,6 +58,7 @@ size_t SZ_compress_dispatcher(Config &conf, const T *data, uchar *cmpData, size_
if (zstdCmpSize < cmpSize) {
conf.cmprAlgo = ALGO_LOSSLESS;
if (zstdCmpSize > cmpCap) {
fprintf(stderr, SZ_ERROR_COMP_BUFFER_NOT_LARGE_ENOUGH);
throw std::length_error(SZ_ERROR_COMP_BUFFER_NOT_LARGE_ENOUGH);
}
memcpy(cmpData, zstdCmpData, zstdCmpSize);
Expand All @@ -78,7 +79,8 @@ void SZ_decompress_dispatcher(Config &conf, const uchar *cmpData, size_t cmpSize
auto decDataPos = reinterpret_cast<uchar *>(decData);
zstd.decompress(cmpData, cmpSize, decDataPos, decDataSize);
if (decDataSize != conf.num * sizeof(T)) {
throw std::runtime_error("Decompressed data size does not match the original data size\n");
fprintf(stderr, "Decompressed data size does not match the original data size\n");
throw std::runtime_error("Decompressed data size does not match the original data size");
}
} else if (conf.cmprAlgo == ALGO_LORENZO_REG) {
SZ_decompress_LorenzoReg<T, N>(conf, cmpData, cmpSize, decData);
Expand All @@ -87,8 +89,8 @@ void SZ_decompress_dispatcher(Config &conf, const uchar *cmpData, size_t cmpSize
} else if (conf.cmprAlgo == ALGO_NOPRED) {
SZ_decompress_nopred<T, N>(conf, cmpData, cmpSize, decData);
} else {
printf("SZ_decompress_dispatcher, Method not supported\n");
exit(0);
fprintf(stderr, "Unknown compression algorithm\n");
throw std::invalid_argument("Unknown compression algorithm");
}
}
} // namespace SZ3
Expand Down
8 changes: 4 additions & 4 deletions include/SZ3/api/sz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ size_t SZ_compress(const SZ3::Config &config, const T *data, char *cmpData, size
} else if (conf.N == 4) {
cmpDataLen = SZ_compress_impl<T, 4>(conf, data, cmpDataPos, cmpDataCap);
} else {
printf("Data dimension higher than 4 is not supported.\n");
exit(0);
fprintf(stderr, "Data dimension higher than 4 is not supported.\n");
throw std::invalid_argument("Data dimension higher than 4 is not supported.");
}

auto cmpConfPos = reinterpret_cast<uchar *>(cmpData);
Expand Down Expand Up @@ -134,8 +134,8 @@ void SZ_decompress(SZ3::Config &config, char *cmpData, size_t cmpSize, T *&decDa
} else if (config.N == 4) {
SZ_decompress_impl<T, 4>(config, cmpDataPos, cmpDataSize, decData);
} else {
printf("Data dimension higher than 4 is not supported.\n");
exit(0);
fprintf(stderr, "Data dimension higher than 4 is not supported.\n");
throw std::invalid_argument("Data dimension higher than 4 is not supported.");
}
}

Expand Down
1 change: 1 addition & 0 deletions include/SZ3/compressor/SZGenericCompressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SZGenericCompressor : public concepts::CompressorInterface<T> {
std::vector<int> quant_inds = decomposition.compress(conf, data);

if (decomposition.get_out_range().first != 0) {
fprintf(stderr, "The output range of the decomposition must start from 0 for this compressor\n");
throw std::runtime_error("The output range of the decomposition must start from 0 for this compressor");
}
encoder.preprocess_encode(quant_inds, decomposition.get_out_range().second);
Expand Down
1 change: 1 addition & 0 deletions include/SZ3/compressor/SZIterateCompressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class SZIterateCompressor : public concepts::CompressorInterface<T> {
quantizer.postcompress_data();

if (quantizer.get_out_range().first != 0) {
fprintf(stderr, "The output range of the quantizer must start from 0 for this compressor\n");
throw std::runtime_error("The output range of the quantizer must start from 0 for this compressor");
}
encoder.preprocess_encode(quant_inds, quantizer.get_out_range().second);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class SZBlockInterpolationCompressor {
// predictor.print();

if (quantizer.get_out_range().first != 0) {
fprintf(stderr, "The output range of the quantizer must start from 0 for this compressor\n");
throw std::runtime_error("The output range of the quantizer must start from 0 for this compressor");
}
encoder.preprocess_encode(quant_inds, quantizer.get_out_range().second);
Expand Down
1 change: 1 addition & 0 deletions include/SZ3/compressor/specialized/SZExaaltCompressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class SZExaaltCompressor : public SZ3::concepts::CompressorInterface<T> {


if (quantizer.get_out_range().first != 0) {
fprintf(stderr, "The output range of the quantizer must start from 0 for this compressor\n");
throw std::runtime_error("The output range of the quantizer must start from 0 for this compressor");
}
encoder.preprocess_encode(quant_inds, quantizer.get_out_range().second);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LorenzoRegressionDecomposition : public concepts::DecompositionInterface<T
precision(conf.absErrorBound),
conf(conf) {
if (N != 1 && N != 3) {
fprintf(stderr, "SZMeta Front only support 1D or 3D data\n");
throw std::invalid_argument("SZMeta Front only support 1D or 3D data");
}
static_assert(std::is_base_of<concepts::QuantizerInterface<T, int>, Quantizer>::value,
Expand Down
4 changes: 2 additions & 2 deletions include/SZ3/encoder/HuffmanEncoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class HuffmanEncoder : public concepts::EncoderInterface<T> {
void preprocess_encode(const T *bins, size_t num_bin, int stateNum) {
nodeCount = 0;
if (num_bin == 0) {
printf("Huffman bins should not be empty\n");
exit(0);
fprintf(stderr, "Huffman bins should not be empty\n");
throw std::invalid_argument("Huffman bins should not be empty");
}
init(bins, num_bin);
for (int i = 0; i < huffmanTree->stateNum; i++)
Expand Down
1 change: 1 addition & 0 deletions include/SZ3/lossless/Lossless_zstd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Lossless_zstd : public concepts::LosslessInterface {
size_t compress(const uchar *src, size_t srcLen, uchar *dst, size_t dstCap) override {
write(srcLen, dst);
if (dstCap < ZSTD_compressBound(srcLen)) {
fprintf(stderr, SZ_ERROR_COMP_BUFFER_NOT_LARGE_ENOUGH);
throw std::length_error(SZ_ERROR_COMP_BUFFER_NOT_LARGE_ENOUGH);
}
size_t dstLen = ZSTD_compress(dst, dstCap, src, srcLen, compression_level);
Expand Down
1 change: 0 additions & 1 deletion include/SZ3/predictor/MetaRegressionPredictor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ inline void regression_predict_quantize_3d(const T *data_pos, const float *reg_p
}
type_pos += size_y * size_z;
}
// exit(0);
}

template <typename T, class Quantizer>
Expand Down
8 changes: 4 additions & 4 deletions include/SZ3/preprocessor/Wavelet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace SZ3 {
int status = gsl_wavelet_transform_forward(w, dwtdata.data(), 1, m, work);

if (status != GSL_SUCCESS) {
printf("Error: wavelets transform failed.\n");
exit(0);
fprintf(stderr, "Error: wavelets transform failed.\n");
throw std::runtime_error("Error: wavelets transform failed.");
}

for (size_t i = 0; i < n; i++) {
Expand Down Expand Up @@ -70,8 +70,8 @@ namespace SZ3 {
int status = gsl_wavelet_transform_inverse(w, dwtdata.data(), 1, m, work);

if (status != GSL_SUCCESS) {
printf("Error: wavelets transform failed.\n");
exit(0);
fprintf(stderr, "Error: wavelets transform failed.\n");
throw std::runtime_error("Error: wavelets transform failed.");
}

for (size_t i = 0; i < n; i++) {
Expand Down
18 changes: 10 additions & 8 deletions include/SZ3/utils/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const char *enum2Str(T e) {
} else if (std::is_same<T, EB>::value) {
return EB_STR[e];
} else {
printf("invalid enum type for enum2Str()\n ");
exit(0);
fprintf(stderr, "invalid enum type for enum2Str()\n ");
throw std::invalid_argument("invalid enum type for enum2Str()");
}
}

Expand Down Expand Up @@ -90,8 +90,8 @@ class Config {
INIReader cfg(cfgpath);

if (cfg.ParseError() != 0) {
std::cout << "Can't load cfg file " << cfgpath << std::endl;
exit(0);
std::cerr << "Can't load cfg file " << cfgpath << std::endl;
throw std::invalid_argument("Can't load cfg file");
}

auto cmprAlgoStr = cfg.Get("GlobalSettings", "CmprAlgo", "");
Expand All @@ -107,8 +107,8 @@ class Config {
} else if (cmprAlgoStr == ALGO_STR[ALGO_LOSSLESS]) {
cmprAlgo = ALGO_LOSSLESS;
} else {
printf("Unknown compression algorithm %s\n", cmprAlgoStr.data());
exit(0);
fprintf(stderr, "Unknown compression algorithm %s\n", cmprAlgoStr.data());
throw std::invalid_argument("Unknown compression algorithm");
}
}
auto ebModeStr = cfg.Get("GlobalSettings", "ErrorBoundMode", "");
Expand All @@ -126,8 +126,8 @@ class Config {
} else if (ebModeStr == EB_STR[EB_ABS_OR_REL]) {
errorBoundMode = EB_ABS_OR_REL;
} else {
printf("Unknown error bound mode %s\n", ebModeStr.data());
exit(0);
fprintf(stderr, "Unknown error bound mode %s\n", ebModeStr.data());
throw std::invalid_argument("Unknown error bound mode");
}
}
absErrorBound = cfg.GetReal("GlobalSettings", "AbsErrorBound", absErrorBound);
Expand Down Expand Up @@ -205,6 +205,7 @@ class Config {
// auto c0 = c;
read(sz3MagicNumber, c);
if (sz3MagicNumber != SZ3_MAGIC_NUMBER) {
fprintf(stderr, "magic number mismatch, the input data is not compressed by SZ3\n");
throw std::invalid_argument("magic number mismatch, the input data is not compressed by SZ3");
}
read(sz3DataVer, c);
Expand All @@ -213,6 +214,7 @@ class Config {
printf("program v%s , program-data %s , input data v%s\n", SZ3_VER, SZ3_DATA_VER,
versionStr(sz3DataVer).data());
ss << "Please use SZ3 v" << versionStr(sz3DataVer) << " to decompress the data" << std::endl;
std::cerr<< ss.str();
throw std::invalid_argument(ss.str());
}

Expand Down
13 changes: 7 additions & 6 deletions include/SZ3/utils/FileUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ template <typename Type>
void readfile(const char *file, const size_t num, Type *data) {
std::ifstream fin(file, std::ios::binary);
if (!fin) {
std::cout << " Error, Couldn't find the file: " << file << "\n";
exit(0);
std::cerr << " Error, Couldn't find the file: " << file << "\n";
throw std::invalid_argument("Couldn't find the file");
}
fin.seekg(0, std::ios::end);
if (fin.tellg() / sizeof(Type) != num) {
fprintf(stderr, "File size is not equal to the input setting\n");
throw std::invalid_argument("File size is not equal to the input setting");
}
fin.seekg(0, std::ios::beg);
Expand All @@ -49,8 +50,8 @@ template <typename Type>
std::unique_ptr<Type[]> readfile(const char *file, size_t &num) {
std::ifstream fin(file, std::ios::binary);
if (!fin) {
std::cout << " Error, Couldn't find the file: " << file << std::endl;
exit(0);
std::cerr << " Error, Couldn't find the file: " << file << std::endl;
throw std::invalid_argument("Couldn't find the file");
}
fin.seekg(0, std::ios::end);
num = fin.tellg() / sizeof(Type);
Expand Down Expand Up @@ -78,8 +79,8 @@ void writeTextFile(const char *file, Type *data, size_t num_elements) {
}
fout.close();
} else {
std::cout << "Error, unable to open file for output: " << file << std::endl;
exit(0);
std::cerr << "Error, unable to open file for output: " << file << std::endl;
throw std::invalid_argument("Couldn't open the file for output");
}
}

Expand Down
4 changes: 2 additions & 2 deletions include/SZ3/utils/Iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ class multi_dimensional_range : public std::enable_shared_from_this<multi_dimens
"ForwardIt1 must be convertible to std::size_t");
if (global_dims_end - global_dims_begin != N) {
std::cout << global_dims_end - global_dims_begin << " " << N << std::endl;
std::cerr << "#dimensions does not match!\n";
exit(0);
std::cerr << "#dimensions does not match\n";
throw std::invalid_argument("#dimensions does not match");
}
set_access_stride(stride_);
// set global dimensions
Expand Down
1 change: 0 additions & 1 deletion include/SZ3/utils/KmeansUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ void get_cluster(T *data, size_t num, float &level_start, float &level_offset, i
// std::cout << "No clusters are found." << std::endl;
level_num = 0;
return;
// exit(0);
}

// std::cout << "centers : ";
Expand Down
4 changes: 2 additions & 2 deletions include/SZ3/utils/Statistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void calAbsErrorBound(Config &conf, const T *data, T range = 0) {
conf.absErrorBound =
std::max(conf.absErrorBound, conf.relErrorBound * ((range > 0) ? range : data_range(data, conf.num)));
} else {
printf("Error, error bound mode not supported\n");
exit(0);
fprintf(stderr, "Error bound mode not supported\n");
throw std::invalid_argument("Error bound mode not supported");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions tools/mdz/include/mdz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ template <typename T, uint N>
float *VQ(Config conf, size_t ts, T *data, size_t &compressed_size, bool decom, int method, float level_start,
float level_offset, int level_num) {
if (level_num == 0) {
printf("VQ/VQT not availble on current dataset, please use ADP or MT\n");
exit(0);
fprintf(stderr, "VQ/VQT not availble on current dataset, please use ADP or MT\n");
throw std::runtime_error("VQ/VQT not availble on current dataset, please use ADP or MT");
}

auto sz = make_compressor_exaalt<T, N>(LinearQuantizer<float>(conf.absErrorBound, conf.quantbinCnt / 2),
Expand Down Expand Up @@ -284,8 +284,8 @@ template <typename T, uint N>
uchar *LAMMPS_compress(Config conf, T *data, int method, size_t &compressed_size, float level_start, float level_offset,
int level_num, T *ts0) {
if ((method == 0 || method == 1) && level_num == 0) {
printf("VQ/VQT not available on current dataset, please use ADP or MT\n");
exit(0);
fprintf(stderr, "VQ/VQT not available on current dataset, please use ADP or MT\n");
throw std::runtime_error("VQ/VQT not available on current dataset, please use ADP or MT");
}
compressed_size = conf.num * sizeof(T);
auto compressed_data = new uchar[compressed_size];
Expand Down

0 comments on commit 4dafde0

Please sign in to comment.