|
| 1 | +#include <sstream> |
| 2 | +#include "sz_autotuning_3d.hpp" |
| 3 | +#include "sz_def.hpp" |
| 4 | +#include "sz_utils.hpp" |
| 5 | +#include "libpressio_ext/cpp/data.h" //for access to pressio_data structures |
| 6 | +#include "libpressio_ext/cpp/compressor.h" //for the libpressio_compressor_plugin class |
| 7 | +#include "libpressio_ext/cpp/options.h" // for access to pressio_options |
| 8 | +#include "libpressio_ext/cpp/pressio.h" //for the plugin registries |
| 9 | +#include "pressio_options.h" |
| 10 | +#include "pressio_data.h" |
| 11 | +#include "pressio_compressor.h" |
| 12 | +#include "std_compat/memory.h" |
| 13 | + |
| 14 | +class sz_auto_plugin: public libpressio_compressor_plugin { |
| 15 | + public: |
| 16 | + sz_auto_plugin() { |
| 17 | + std::stringstream ss; |
| 18 | + ss << sz_auto_plugin::major_version() << "." << sz_auto_plugin::minor_version() << "." << sz_auto_plugin::patch_version() << "." << sz_auto_plugin::revision_version(); |
| 19 | + sz_auto_version = ss.str(); |
| 20 | + }; |
| 21 | + struct pressio_options get_options_impl() const override { |
| 22 | + struct pressio_options options; |
| 23 | + set(options, "SZauto:error_bounds", error_bounds); |
| 24 | + set(options, "SZauto:sample_ratio", sample_ratio); |
| 25 | + return options; |
| 26 | + } |
| 27 | + |
| 28 | + struct pressio_options get_configuration_impl() const override { |
| 29 | + struct pressio_options options; |
| 30 | + options.set("pressio:thread_safe", static_cast<int>(pressio_thread_safety_multiple)); |
| 31 | + return options; |
| 32 | + } |
| 33 | + |
| 34 | + int set_options_impl(struct pressio_options const& options) override { |
| 35 | + get(options, "SZauto:error_bounds", &error_bounds); |
| 36 | + get(options, "SZauto:sample_ratio", &sample_ratio); |
| 37 | + return 0; |
| 38 | + } |
| 39 | + |
| 40 | + int compress_impl(const pressio_data *input, struct pressio_data* output) override { |
| 41 | + enum pressio_dtype type = pressio_data_dtype(input); |
| 42 | + unsigned long outSize; |
| 43 | + void* data = pressio_data_ptr(input, nullptr); |
| 44 | + unsigned char* compressed_data; |
| 45 | + |
| 46 | + size_t ndims = pressio_data_num_dimensions(input); |
| 47 | + size_t r1 = pressio_data_get_dimension(input, 0); |
| 48 | + size_t r2 = pressio_data_get_dimension(input, 1); |
| 49 | + size_t r3 = pressio_data_get_dimension(input, 2); |
| 50 | + |
| 51 | + if(ndims != 3) |
| 52 | + { |
| 53 | + return set_error(2, "Error: SZauto only supports 3d compression"); |
| 54 | + } |
| 55 | + |
| 56 | + if(type == pressio_float_dtype) |
| 57 | + { |
| 58 | + compressed_data = sz_compress_autotuning_3d<float>((float*)data, r1, r2, r3, error_bounds, outSize, false, false, false, sample_ratio); |
| 59 | + } |
| 60 | + else if(type == pressio_double_dtype) |
| 61 | + { |
| 62 | + compressed_data = sz_compress_autotuning_3d<double>((double*)data, r1, r2, r3, error_bounds, outSize, false, false, false, sample_ratio); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + return set_error(2, "Error: SZauto only supports float or double"); |
| 67 | + } |
| 68 | + |
| 69 | + //that means the compressor is complaining about the parameter |
| 70 | + if(compressed_data == NULL) |
| 71 | + { |
| 72 | + return set_error(2, "Error when SZauto is compressing the data"); |
| 73 | + } |
| 74 | + |
| 75 | + *output = pressio_data::move(pressio_byte_dtype, compressed_data, 1, &outSize, pressio_data_libc_free_fn, nullptr); |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | + int decompress_impl(const pressio_data *input, struct pressio_data* output) override { |
| 80 | + enum pressio_dtype dtype = pressio_data_dtype(output); |
| 81 | + |
| 82 | + size_t r[] = { |
| 83 | + pressio_data_get_dimension(output, 0), |
| 84 | + pressio_data_get_dimension(output, 1), |
| 85 | + pressio_data_get_dimension(output, 2), |
| 86 | + }; |
| 87 | + size_t ndims = pressio_data_num_dimensions(output); |
| 88 | + if(ndims != 3) |
| 89 | + { |
| 90 | + return set_error(2, "Error: SZauto only supports 3d decompression"); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + size_t compressed_size; |
| 95 | + void* compressedBytes = pressio_data_ptr(input, &compressed_size); |
| 96 | + |
| 97 | + void* decompressed_data; |
| 98 | + if(dtype == pressio_float_dtype) |
| 99 | + { |
| 100 | + decompressed_data = sz_decompress_autotuning_3d<float>((unsigned char*)compressedBytes, compressed_size, r[0], r[1], r[2]); |
| 101 | + } |
| 102 | + else if(dtype == pressio_double_dtype) |
| 103 | + { |
| 104 | + decompressed_data = sz_decompress_autotuning_3d<double>((unsigned char*)compressedBytes, compressed_size, r[0], r[1], r[2]); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + return set_error(2, "Error: SZauto only supports float or double"); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + *output = pressio_data::move(dtype, decompressed_data, ndims, r, pressio_data_libc_free_fn, nullptr); |
| 113 | + return 0; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + //the author of SZauto does not release their version info. |
| 118 | + int major_version() const override { |
| 119 | + return SZAUTO_MAJOR_VERSION; |
| 120 | + } |
| 121 | + int minor_version() const override { |
| 122 | + return SZAUTO_MINOR_VERSION; |
| 123 | + } |
| 124 | + int patch_version() const override { |
| 125 | + return SZAUTO_PATCH_VERSION; |
| 126 | + } |
| 127 | + int revision_version () const { |
| 128 | + return SZAUTO_REVISION_VERSION; |
| 129 | + } |
| 130 | + |
| 131 | + const char* version() const override { |
| 132 | + return sz_auto_version.c_str(); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + const char* prefix() const override { |
| 137 | + return "SZauto"; |
| 138 | + } |
| 139 | + |
| 140 | + std::shared_ptr<libpressio_compressor_plugin> clone() override { |
| 141 | + return compat::make_unique<sz_auto_plugin>(*this); |
| 142 | + } |
| 143 | + private: |
| 144 | + |
| 145 | + std::string sz_auto_version; |
| 146 | + double error_bounds = 0.1; //params for compressing the sz-auto |
| 147 | + float sample_ratio = 0.05; |
| 148 | +}; |
| 149 | + |
| 150 | +static pressio_register compressor_sz_auto_plugin(compressor_plugins(), "SZauto", [](){return compat::make_unique<sz_auto_plugin>(); }); |
| 151 | + |
0 commit comments