|
| 1 | +#include <cmath> //for exp and log |
| 2 | +#include <iterator> |
| 3 | +#include <map> |
| 4 | +#include <sstream> |
| 5 | +#include <bg/bg.h> |
| 6 | +#include "libpressio_ext/cpp/data.h" //for access to pressio_data structures |
| 7 | +#include "libpressio_ext/cpp/compressor.h" //for the libpressio_compressor_plugin class |
| 8 | +#include "libpressio_ext/cpp/options.h" // for access to pressio_options |
| 9 | +#include "libpressio_ext/cpp/pressio.h" //for the plugin registries |
| 10 | +#include "pressio_options.h" |
| 11 | +#include "pressio_data.h" |
| 12 | +#include "pressio_compressor.h" |
| 13 | +#include "libpressio_ext/compat/memory.h" |
| 14 | + |
| 15 | +#define INVALID_TYPE -1 |
| 16 | + |
| 17 | +namespace { |
| 18 | + struct bg_iless { |
| 19 | + bool operator()(std::string lhs, std::string rhs) const { |
| 20 | + std::transform(std::begin(lhs), std::end(lhs), std::begin(lhs), [](unsigned char c){return std::tolower(c);}); |
| 21 | + std::transform(std::begin(rhs), std::end(rhs), std::begin(rhs), [](unsigned char c){return std::tolower(c);}); |
| 22 | + return lhs < rhs; |
| 23 | + } |
| 24 | + }; |
| 25 | + static std::map<std::string, int, bg_iless> const bitgroom_mode_str_to_code { |
| 26 | + {"bitgroom", BITGROOM}, |
| 27 | + {"bitshave", BITSHAVE}, |
| 28 | + {"bitset", BITSET}, |
| 29 | + }; |
| 30 | + static std::map<std::string, int, bg_iless> const bitgroom_ec_mode_str_to_code { |
| 31 | + {"nsd", BG_NSD}, |
| 32 | + {"dsd", BG_DSD}, |
| 33 | + }; |
| 34 | + |
| 35 | + template <class Map> |
| 36 | + std::vector<std::string> bg_keys(Map const& map) { |
| 37 | + std::vector<std::string> keys; |
| 38 | + keys.reserve(map.size()); |
| 39 | + std::transform( |
| 40 | + std::begin(map), std::end(map), std::back_inserter(keys), |
| 41 | + [](typename Map::const_reference it) { return it.first; }); |
| 42 | + return keys; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +class bit_grooming_plugin: public libpressio_compressor_plugin { |
| 48 | + public: |
| 49 | + bit_grooming_plugin() { |
| 50 | + std::stringstream ss; |
| 51 | + ss << bit_grooming_plugin::major_version() << "." << bit_grooming_plugin::minor_version() << "." << bit_grooming_plugin::patch_version() << "." << bit_grooming_plugin::revision_version(); |
| 52 | + bg_version = ss.str(); |
| 53 | + }; |
| 54 | + struct pressio_options get_options_impl() const override { |
| 55 | + struct pressio_options options; |
| 56 | + |
| 57 | + set(options, "bit_grooming:mode", bgMode); |
| 58 | + set_type(options, "bit_grooming:mode_str", pressio_option_charptr_type); |
| 59 | + |
| 60 | + set(options, "bit_grooming:error_control_mode", errorControlMode); |
| 61 | + set_type(options, "bit_grooming:error_control_mode_str", pressio_option_charptr_type); |
| 62 | + |
| 63 | + set(options, "bit_grooming:n_sig_digits", nun_sig_digits); // number of significant digits |
| 64 | + set(options, "bit_grooming:n_sig_decimals", num_sig_decimals); // number of significant decimal digits |
| 65 | + return options; |
| 66 | + } |
| 67 | + |
| 68 | + struct pressio_options get_configuration_impl() const override { |
| 69 | + struct pressio_options options; |
| 70 | + set(options, "pressio:thread_safe", static_cast<int>(pressio_thread_safety_multiple)); |
| 71 | + set(options, "bit_grooming:mode", bg_keys(bitgroom_mode_str_to_code)); |
| 72 | + set(options, "bit_grooming:error_control_mode_mode", bg_keys(bitgroom_ec_mode_str_to_code)); |
| 73 | + return options; |
| 74 | + } |
| 75 | + |
| 76 | + int set_options_impl(struct pressio_options const& options) override { |
| 77 | + get(options, "bit_grooming:mode", &bgMode); |
| 78 | + |
| 79 | + std::string tmp_mode; |
| 80 | + if(get(options, "bit_grooming:mode_str", &tmp_mode) == pressio_options_key_set) { |
| 81 | + auto const& it = bitgroom_mode_str_to_code.find(tmp_mode); |
| 82 | + if(it != bitgroom_mode_str_to_code.end()) { |
| 83 | + bgMode = it->second; |
| 84 | + } |
| 85 | + } |
| 86 | + if(get(options, "bit_grooming:error_control_mode_str", &tmp_mode) == pressio_options_key_set) { |
| 87 | + auto const& it = bitgroom_ec_mode_str_to_code.find(tmp_mode); |
| 88 | + if(it != bitgroom_ec_mode_str_to_code.end()) { |
| 89 | + errorControlMode = it->second; |
| 90 | + } |
| 91 | + } |
| 92 | + get(options, "bit_grooming:error_control_mode", &errorControlMode); |
| 93 | + get(options, "bit_grooming:n_sig_digits", &nun_sig_digits); // number of significant digits |
| 94 | + get(options, "bit_grooming:n_sig_decimals", |
| 95 | + &num_sig_decimals); // number of significant decimal digits |
| 96 | + return 0; |
| 97 | + } |
| 98 | + |
| 99 | + int compress_impl(const pressio_data *input, struct pressio_data* output) override { |
| 100 | + int type = libpressio_type_to_bg_type(pressio_data_dtype(input)); |
| 101 | + if(type == INVALID_TYPE) { |
| 102 | + return INVALID_TYPE; |
| 103 | + } |
| 104 | + |
| 105 | + size_t nbEle = pressio_data_num_elements(input); |
| 106 | + unsigned long outSize; |
| 107 | + void* data = pressio_data_ptr(input, nullptr); |
| 108 | + unsigned char* compressed_data; |
| 109 | + |
| 110 | + compressed_data = BG_compress_args(type, data, &outSize, bgMode, errorControlMode, |
| 111 | + nun_sig_digits, num_sig_decimals, nbEle); |
| 112 | + |
| 113 | + if(compressed_data == NULL) |
| 114 | + { |
| 115 | + return set_error(2, "Error when bit grooming is compressing the data"); |
| 116 | + } |
| 117 | + |
| 118 | + *output = pressio_data::move(pressio_byte_dtype, compressed_data, 1, &outSize, pressio_data_libc_free_fn, nullptr); |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + int decompress_impl(const pressio_data *input, struct pressio_data* output) override { |
| 123 | + int type = libpressio_type_to_bg_type(pressio_data_dtype(output)); |
| 124 | + if(type == INVALID_TYPE) { |
| 125 | + return INVALID_TYPE; |
| 126 | + } |
| 127 | + unsigned char* bytes = (unsigned char*)pressio_data_ptr(input, nullptr); |
| 128 | + size_t nbEle = pressio_data_num_elements(output); |
| 129 | + size_t byteLength = pressio_data_get_bytes(input); |
| 130 | + |
| 131 | + void* decompressed_data = BG_decompress(type, bytes, byteLength, nbEle); |
| 132 | + *output = pressio_data::move(pressio_data_dtype(output), decompressed_data, 1, &nbEle, pressio_data_libc_free_fn, nullptr); |
| 133 | + return 0; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + int major_version() const override { |
| 139 | + return BG_VER_MAJOR; |
| 140 | + } |
| 141 | + int minor_version() const override { |
| 142 | + return BG_VER_MINOR; |
| 143 | + } |
| 144 | + int patch_version() const override { |
| 145 | + return BG_VER_BUILD; |
| 146 | + } |
| 147 | + int revision_version () const { |
| 148 | + return BG_VER_REVISION; |
| 149 | + } |
| 150 | + |
| 151 | + const char* version() const override { |
| 152 | + return bg_version.c_str(); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + const char* prefix() const override { |
| 157 | + return "bit_grooming"; |
| 158 | + } |
| 159 | + |
| 160 | + std::shared_ptr<libpressio_compressor_plugin> clone() override { |
| 161 | + return compat::make_unique<bit_grooming_plugin>(*this); |
| 162 | + } |
| 163 | + private: |
| 164 | + std::string bg_version; |
| 165 | + int libpressio_type_to_bg_type(pressio_dtype type) |
| 166 | + { |
| 167 | + if(type == pressio_float_dtype) |
| 168 | + { |
| 169 | + return BG_FLOAT; |
| 170 | + } |
| 171 | + else if(type == pressio_double_dtype) |
| 172 | + { |
| 173 | + return BG_FLOAT; |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + set_error(2, "Invalid data type"); |
| 178 | + return INVALID_TYPE; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + int bgMode = BITGROOM; |
| 183 | + int errorControlMode = BG_NSD; |
| 184 | + int nun_sig_digits = 5; |
| 185 | + int num_sig_decimals = 5; |
| 186 | +}; |
| 187 | + |
| 188 | +static pressio_register compressor_bit_grooming_plugin(compressor_plugins(), "bit_grooming", [](){return compat::make_unique<bit_grooming_plugin>(); }); |
0 commit comments