[2c47b73] | 1 | /** |
---|
| 2 | * @file conf.c |
---|
| 3 | * @author Sheng Di ([email protected] or [email protected]) |
---|
| 4 | * @date 2015. |
---|
| 5 | * @brief Configuration loading functions for the SZ library. |
---|
| 6 | * (C) 2015 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
| 7 | * See COPYRIGHT in top-level directory. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <math.h> |
---|
| 11 | #include "string.h" |
---|
| 12 | #include "sz.h" |
---|
| 13 | #include "iniparser.h" |
---|
| 14 | #include "Huffman.h" |
---|
| 15 | #include "pastri.h" |
---|
| 16 | |
---|
| 17 | /*-------------------------------------------------------------------------*/ |
---|
| 18 | /** |
---|
| 19 | @brief It reads the configuration given in the configuration file. |
---|
| 20 | @return integer 1 if successfull. |
---|
| 21 | |
---|
| 22 | This function reads the configuration given in the SZ configuration |
---|
| 23 | file and sets other required parameters. |
---|
| 24 | |
---|
| 25 | **/ |
---|
| 26 | |
---|
| 27 | /*struct node_t *pool; |
---|
| 28 | node *qqq; |
---|
| 29 | node *qq; |
---|
| 30 | int n_nodes = 0, qend; |
---|
| 31 | unsigned long **code; |
---|
| 32 | unsigned char *cout; |
---|
| 33 | int n_inode;*/ |
---|
| 34 | |
---|
| 35 | unsigned int roundUpToPowerOf2(unsigned int base) |
---|
| 36 | { |
---|
| 37 | base -= 1; |
---|
| 38 | |
---|
| 39 | base = base | (base >> 1); |
---|
| 40 | base = base | (base >> 2); |
---|
| 41 | base = base | (base >> 4); |
---|
| 42 | base = base | (base >> 8); |
---|
| 43 | base = base | (base >> 16); |
---|
| 44 | |
---|
| 45 | return base + 1; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void updateQuantizationInfo(int quant_intervals) |
---|
| 49 | { |
---|
| 50 | exe_params->intvCapacity = quant_intervals; |
---|
| 51 | exe_params->intvRadius = quant_intervals/2; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | double computeABSErrBoundFromPSNR(double psnr, double threshold, double value_range) |
---|
| 55 | { |
---|
| 56 | double v1 = psnr + 10 * log10(1-2.0/3.0*threshold); |
---|
| 57 | double v2 = v1/(-20); |
---|
| 58 | double v3 = pow(10, v2); |
---|
| 59 | return value_range * v3; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /*-------------------------------------------------------------------------*/ |
---|
| 63 | /** |
---|
| 64 | * |
---|
| 65 | * |
---|
| 66 | * @return the status of loading conf. file: 1 (success) or 0 (error code); |
---|
| 67 | * */ |
---|
| 68 | int SZ_ReadConf(const char* sz_cfgFile) { |
---|
| 69 | // Check access to SZ configuration file and load dictionary |
---|
| 70 | //record the setting in confparams_cpr |
---|
| 71 | confparams_cpr = (sz_params*)malloc(sizeof(sz_params)); |
---|
| 72 | exe_params = (sz_exedata*)malloc(sizeof(sz_exedata)); |
---|
| 73 | |
---|
| 74 | int x = 1; |
---|
| 75 | char sol_name[256]; |
---|
| 76 | char *modeBuf; |
---|
| 77 | char *errBoundMode; |
---|
| 78 | char *endianTypeString; |
---|
| 79 | dictionary *ini; |
---|
| 80 | char *par; |
---|
| 81 | |
---|
| 82 | char *y = (char*)&x; |
---|
| 83 | |
---|
| 84 | if(*y==1) |
---|
| 85 | sysEndianType = LITTLE_ENDIAN_SYSTEM; |
---|
| 86 | else //=0 |
---|
| 87 | sysEndianType = BIG_ENDIAN_SYSTEM; |
---|
| 88 | |
---|
| 89 | if(sz_cfgFile == NULL) |
---|
| 90 | { |
---|
| 91 | dataEndianType = LITTLE_ENDIAN_DATA; |
---|
| 92 | confparams_cpr->sol_ID = SZ; |
---|
| 93 | confparams_cpr->max_quant_intervals = 65536; |
---|
| 94 | confparams_cpr->maxRangeRadius = confparams_cpr->max_quant_intervals/2; |
---|
| 95 | |
---|
| 96 | exe_params->intvCapacity = confparams_cpr->maxRangeRadius*2; |
---|
| 97 | exe_params->intvRadius = confparams_cpr->maxRangeRadius; |
---|
| 98 | |
---|
| 99 | confparams_cpr->quantization_intervals = 0; |
---|
| 100 | exe_params->optQuantMode = 1; |
---|
| 101 | confparams_cpr->predThreshold = 0.99; |
---|
| 102 | confparams_cpr->sampleDistance = 100; |
---|
| 103 | |
---|
| 104 | confparams_cpr->szMode = SZ_BEST_COMPRESSION; |
---|
| 105 | |
---|
| 106 | confparams_cpr->gzipMode = 1; //fast mode |
---|
| 107 | |
---|
| 108 | confparams_cpr->errorBoundMode = PSNR; |
---|
| 109 | confparams_cpr->psnr = 90; |
---|
| 110 | |
---|
| 111 | confparams_cpr->pw_relBoundRatio = 1E-3; |
---|
| 112 | confparams_cpr->segment_size = 36; |
---|
| 113 | |
---|
| 114 | confparams_cpr->pwr_type = SZ_PWR_MIN_TYPE; |
---|
| 115 | |
---|
| 116 | confparams_cpr->snapshotCmprStep = 5; |
---|
| 117 | |
---|
| 118 | return SZ_SCES; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if (access(sz_cfgFile, F_OK) != 0) |
---|
| 122 | { |
---|
| 123 | printf("[SZ] Configuration file NOT accessible.\n"); |
---|
| 124 | return SZ_NSCS; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | //printf("[SZ] Reading SZ configuration file (%s) ...\n", sz_cfgFile); |
---|
| 128 | ini = iniparser_load(sz_cfgFile); |
---|
| 129 | if (ini == NULL) |
---|
| 130 | { |
---|
| 131 | printf("[SZ] Iniparser failed to parse the conf. file.\n"); |
---|
| 132 | return SZ_NSCS; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | endianTypeString = iniparser_getstring(ini, "ENV:dataEndianType", "LITTLE_ENDIAN_DATA"); |
---|
| 136 | if(strcmp(endianTypeString, "LITTLE_ENDIAN_DATA")==0) |
---|
| 137 | dataEndianType = LITTLE_ENDIAN_DATA; |
---|
| 138 | else if(strcmp(endianTypeString, "BIG_ENDIAN_DATA")==0) |
---|
| 139 | dataEndianType = BIG_ENDIAN_DATA; |
---|
| 140 | else |
---|
| 141 | { |
---|
| 142 | printf("Error: Wrong dataEndianType: please set it correctly in sz.config.\n"); |
---|
| 143 | iniparser_freedict(ini); |
---|
| 144 | return SZ_NSCS; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | // Reading/setting detection parameters |
---|
| 148 | |
---|
| 149 | par = iniparser_getstring(ini, "ENV:sol_name", NULL); |
---|
| 150 | snprintf(sol_name, 256, "%s", par); |
---|
| 151 | |
---|
| 152 | if(strcmp(sol_name, "SZ")==0) |
---|
| 153 | confparams_cpr->sol_ID = SZ; |
---|
| 154 | else if(strcmp(sol_name, "PASTRI")==0) |
---|
| 155 | confparams_cpr->sol_ID = PASTRI; |
---|
| 156 | else{ |
---|
| 157 | printf("[SZ] Error: wrong solution name (please check sz.config file)\n"); |
---|
| 158 | iniparser_freedict(ini); |
---|
| 159 | return SZ_NSCS; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | if(confparams_cpr->sol_ID==SZ) |
---|
| 163 | { |
---|
| 164 | int max_quant_intervals = iniparser_getint(ini, "PARAMETER:max_quant_intervals", 65536); |
---|
| 165 | confparams_cpr->max_quant_intervals = max_quant_intervals; |
---|
| 166 | |
---|
| 167 | int quantization_intervals = (int)iniparser_getint(ini, "PARAMETER:quantization_intervals", 0); |
---|
| 168 | confparams_cpr->quantization_intervals = quantization_intervals; |
---|
| 169 | if(quantization_intervals>0) |
---|
| 170 | { |
---|
| 171 | updateQuantizationInfo(quantization_intervals); |
---|
| 172 | confparams_cpr->max_quant_intervals = max_quant_intervals = quantization_intervals; |
---|
| 173 | exe_params->optQuantMode = 0; |
---|
| 174 | } |
---|
| 175 | else //==0 |
---|
| 176 | { |
---|
| 177 | confparams_cpr->maxRangeRadius = max_quant_intervals/2; |
---|
| 178 | |
---|
| 179 | exe_params->intvCapacity = confparams_cpr->maxRangeRadius*2; |
---|
| 180 | exe_params->intvRadius = confparams_cpr->maxRangeRadius; |
---|
| 181 | |
---|
| 182 | exe_params->optQuantMode = 1; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if(quantization_intervals%2!=0) |
---|
| 186 | { |
---|
| 187 | printf("Error: quantization_intervals must be an even number!\n"); |
---|
| 188 | iniparser_freedict(ini); |
---|
| 189 | return SZ_NSCS; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | confparams_cpr->predThreshold = (float)iniparser_getdouble(ini, "PARAMETER:predThreshold", 0); |
---|
| 193 | confparams_cpr->sampleDistance = (int)iniparser_getint(ini, "PARAMETER:sampleDistance", 0); |
---|
| 194 | |
---|
| 195 | modeBuf = iniparser_getstring(ini, "PARAMETER:szMode", NULL); |
---|
| 196 | if(modeBuf==NULL) |
---|
| 197 | { |
---|
| 198 | printf("[SZ] Error: Null szMode setting (please check sz.config file)\n"); |
---|
| 199 | iniparser_freedict(ini); |
---|
| 200 | return SZ_NSCS; |
---|
| 201 | } |
---|
| 202 | else if(strcmp(modeBuf, "SZ_BEST_SPEED")==0) |
---|
| 203 | confparams_cpr->szMode = SZ_BEST_SPEED; |
---|
| 204 | else if(strcmp(modeBuf, "SZ_DEFAULT_COMPRESSION")==0) |
---|
| 205 | confparams_cpr->szMode = SZ_DEFAULT_COMPRESSION; |
---|
| 206 | else if(strcmp(modeBuf, "SZ_BEST_COMPRESSION")==0) |
---|
| 207 | confparams_cpr->szMode = SZ_BEST_COMPRESSION; |
---|
| 208 | else |
---|
| 209 | { |
---|
| 210 | printf("[SZ] Error: Wrong szMode setting (please check sz.config file)\n"); |
---|
| 211 | iniparser_freedict(ini); |
---|
| 212 | return SZ_NSCS; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | modeBuf = iniparser_getstring(ini, "PARAMETER:gzipMode", NULL); |
---|
| 216 | if(modeBuf==NULL) |
---|
| 217 | { |
---|
| 218 | printf("[SZ] Error: Null Gzip mode setting (please check sz.config file)\n"); |
---|
| 219 | iniparser_freedict(ini); |
---|
| 220 | return SZ_NSCS; |
---|
| 221 | } |
---|
| 222 | else if(strcmp(modeBuf, "Gzip_NO_COMPRESSION")==0) |
---|
| 223 | confparams_cpr->gzipMode = 0; |
---|
| 224 | else if(strcmp(modeBuf, "Gzip_BEST_SPEED")==0) |
---|
| 225 | confparams_cpr->gzipMode = 1; |
---|
| 226 | else if(strcmp(modeBuf, "Gzip_BEST_COMPRESSION")==0) |
---|
| 227 | confparams_cpr->gzipMode = 9; |
---|
| 228 | else if(strcmp(modeBuf, "Gzip_DEFAULT_COMPRESSION")==0) |
---|
| 229 | confparams_cpr->gzipMode = -1; |
---|
| 230 | else |
---|
| 231 | { |
---|
| 232 | printf("[SZ] Error: Wrong gzip Mode (please check sz.config file)\n"); |
---|
| 233 | return SZ_NSCS; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | //TODO |
---|
| 237 | confparams_cpr->snapshotCmprStep = (int)iniparser_getint(ini, "PARAMETER:snapshotCmprStep", 5); |
---|
| 238 | |
---|
| 239 | errBoundMode = iniparser_getstring(ini, "PARAMETER:errorBoundMode", NULL); |
---|
| 240 | if(errBoundMode==NULL) |
---|
| 241 | { |
---|
| 242 | printf("[SZ] Error: Null error bound setting (please check sz.config file)\n"); |
---|
| 243 | iniparser_freedict(ini); |
---|
| 244 | return SZ_NSCS; |
---|
| 245 | } |
---|
| 246 | else if(strcmp(errBoundMode,"ABS")==0||strcmp(errBoundMode,"abs")==0) |
---|
| 247 | confparams_cpr->errorBoundMode=ABS; |
---|
| 248 | else if(strcmp(errBoundMode, "REL")==0||strcmp(errBoundMode,"rel")==0) |
---|
| 249 | confparams_cpr->errorBoundMode=REL; |
---|
| 250 | else if(strcmp(errBoundMode, "ABS_AND_REL")==0||strcmp(errBoundMode, "abs_and_rel")==0) |
---|
| 251 | confparams_cpr->errorBoundMode=ABS_AND_REL; |
---|
| 252 | else if(strcmp(errBoundMode, "ABS_OR_REL")==0||strcmp(errBoundMode, "abs_or_rel")==0) |
---|
| 253 | confparams_cpr->errorBoundMode=ABS_OR_REL; |
---|
| 254 | else if(strcmp(errBoundMode, "PW_REL")==0||strcmp(errBoundMode, "pw_rel")==0) |
---|
| 255 | confparams_cpr->errorBoundMode=PW_REL; |
---|
| 256 | else if(strcmp(errBoundMode, "PSNR")==0||strcmp(errBoundMode, "psnr")==0) |
---|
| 257 | confparams_cpr->errorBoundMode=PSNR; |
---|
| 258 | else if(strcmp(errBoundMode, "ABS_AND_PW_REL")==0||strcmp(errBoundMode, "abs_and_pw_rel")==0) |
---|
| 259 | confparams_cpr->errorBoundMode=ABS_AND_PW_REL; |
---|
| 260 | else if(strcmp(errBoundMode, "ABS_OR_PW_REL")==0||strcmp(errBoundMode, "abs_or_pw_rel")==0) |
---|
| 261 | confparams_cpr->errorBoundMode=ABS_OR_PW_REL; |
---|
| 262 | else if(strcmp(errBoundMode, "REL_AND_PW_REL")==0||strcmp(errBoundMode, "rel_and_pw_rel")==0) |
---|
| 263 | confparams_cpr->errorBoundMode=REL_AND_PW_REL; |
---|
| 264 | else if(strcmp(errBoundMode, "REL_OR_PW_REL")==0||strcmp(errBoundMode, "rel_or_pw_rel")==0) |
---|
| 265 | confparams_cpr->errorBoundMode=REL_OR_PW_REL; |
---|
| 266 | else |
---|
| 267 | { |
---|
| 268 | printf("[SZ] Error: Wrong error bound mode (please check sz.config file)\n"); |
---|
| 269 | iniparser_freedict(ini); |
---|
| 270 | return SZ_NSCS; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | confparams_cpr->absErrBound = (double)iniparser_getdouble(ini, "PARAMETER:absErrBound", 0); |
---|
| 274 | confparams_cpr->relBoundRatio = (double)iniparser_getdouble(ini, "PARAMETER:relBoundRatio", 0); |
---|
| 275 | confparams_cpr->psnr = (double)iniparser_getdouble(ini, "PARAMETER:psnr", 0); |
---|
| 276 | confparams_cpr->pw_relBoundRatio = (double)iniparser_getdouble(ini, "PARAMETER:pw_relBoundRatio", 0); |
---|
| 277 | confparams_cpr->segment_size = (int)iniparser_getint(ini, "PARAMETER:segment_size", 0); |
---|
| 278 | |
---|
| 279 | modeBuf = iniparser_getstring(ini, "PARAMETER:pwr_type", "MIN"); |
---|
| 280 | |
---|
| 281 | if(strcmp(modeBuf, "MIN")==0) |
---|
| 282 | confparams_cpr->pwr_type = SZ_PWR_MIN_TYPE; |
---|
| 283 | else if(strcmp(modeBuf, "AVG")==0) |
---|
| 284 | confparams_cpr->pwr_type = SZ_PWR_AVG_TYPE; |
---|
| 285 | else if(strcmp(modeBuf, "MAX")==0) |
---|
| 286 | confparams_cpr->pwr_type = SZ_PWR_MAX_TYPE; |
---|
| 287 | else if(modeBuf!=NULL) |
---|
| 288 | { |
---|
| 289 | printf("[SZ] Error: Wrong pwr_type setting (please check sz.config file).\n"); |
---|
| 290 | iniparser_freedict(ini); |
---|
| 291 | return SZ_NSCS; |
---|
| 292 | } |
---|
| 293 | else //by default |
---|
| 294 | confparams_cpr->pwr_type = SZ_PWR_AVG_TYPE; |
---|
| 295 | |
---|
| 296 | //initialization for Huffman encoding |
---|
| 297 | //SZ_Reset(); |
---|
| 298 | } |
---|
| 299 | else if(confparams_cpr->sol_ID == PASTRI) |
---|
| 300 | {//load parameters for PSTRI |
---|
| 301 | pastri_par.bf[0] = (int)iniparser_getint(ini, "PARAMETER:basisFunction_0", 0); |
---|
| 302 | pastri_par.bf[1] = (int)iniparser_getint(ini, "PARAMETER:basisFunction_1", 0); |
---|
| 303 | pastri_par.bf[2] = (int)iniparser_getint(ini, "PARAMETER:basisFunction_2", 0); |
---|
| 304 | pastri_par.bf[3] = (int)iniparser_getint(ini, "PARAMETER:basisFunction_3", 0); |
---|
| 305 | pastri_par.numBlocks = (int)iniparser_getint(ini, "PARAMETER:numBlocks", 0); |
---|
| 306 | confparams_cpr->absErrBound = pastri_par.originalEb = (double)iniparser_getdouble(ini, "PARAMETER:absErrBound", 1E-3); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | iniparser_freedict(ini); |
---|
| 310 | return SZ_SCES; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | /*-------------------------------------------------------------------------*/ |
---|
| 314 | /** |
---|
| 315 | @brief It reads and tests the configuration given. |
---|
| 316 | @return integer 1 if successfull. |
---|
| 317 | |
---|
| 318 | This function reads the configuration file. Then test that the |
---|
| 319 | configuration parameters are correct (including directories). |
---|
| 320 | |
---|
| 321 | **/ |
---|
| 322 | /*-------------------------------------------------------------------------*/ |
---|
| 323 | int SZ_LoadConf(const char* sz_cfgFile) { |
---|
| 324 | int res = SZ_ReadConf(sz_cfgFile); |
---|
| 325 | if (res != SZ_SCES) |
---|
| 326 | { |
---|
| 327 | printf("[SZ] ERROR: Impossible to read configuration.\n"); |
---|
| 328 | return SZ_NSCS; |
---|
| 329 | } |
---|
| 330 | return SZ_SCES; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | int checkVersion(char* version) |
---|
| 334 | { |
---|
| 335 | int i = 0; |
---|
| 336 | for(;i<3;i++) |
---|
| 337 | if(version[i]!=versionNumber[i]) |
---|
| 338 | return 0; |
---|
| 339 | return 1; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | void initSZ_TSC() |
---|
| 343 | { |
---|
| 344 | sz_tsc = (sz_tsc_metadata*)malloc(sizeof(sz_tsc_metadata)); |
---|
| 345 | memset(sz_tsc, 0, sizeof(sz_tsc_metadata)); |
---|
| 346 | sprintf(sz_tsc->metadata_filename, "sz_tsc_metainfo.txt"); |
---|
| 347 | sz_tsc->metadata_file = fopen(sz_tsc->metadata_filename, "wb"); |
---|
| 348 | if (sz_tsc->metadata_file == NULL) |
---|
| 349 | { |
---|
| 350 | printf("Failed to open sz_tsc_metainfo.txt file for writing metainfo.\n"); |
---|
| 351 | exit(1); |
---|
| 352 | } |
---|
| 353 | fputs("#metadata of the time-step based compression\n", sz_tsc->metadata_file); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | /*double fabs(double value) |
---|
| 357 | { |
---|
| 358 | if(value<0) |
---|
| 359 | return -value; |
---|
| 360 | else |
---|
| 361 | return value; |
---|
| 362 | }*/ |
---|