sz_double.c 21.1 KB
Newer Older
T
tickduan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/**
 *  @file sz_double.c
 *  @author Sheng Di, Dingwen Tao, Xin Liang, Xiangyu Zou, Tao Lu, Wen Xia, Xuan Wang, Weizhe Zhang
 *  @date Aug, 2016
 *  @brief SZ_Init, Compression and Decompression functions
 *  (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "sz.h"
#include "CompressElement.h"
#include "DynamicByteArray.h"
#include "DynamicIntArray.h"
#include "TightDataPointStorageD.h"
#include "sz_double.h"
#include "sz_double_pwr.h"
#include "szd_double.h"
#include "szd_double_pwr.h"
#include "zlib.h"
#include "rw.h"
#include "sz_double_ts.h"
#include "utility.h"
#include "CacheTable.h"
#include "MultiLevelCacheTableWideInterval.h"
#include "sz_stats.h"

unsigned char* SZ_skip_compress_double(double* data, size_t dataLength, size_t* outSize)
{
	*outSize = dataLength*sizeof(double);
	unsigned char* out = (unsigned char*)malloc(dataLength*sizeof(double));
	memcpy(out, data, dataLength*sizeof(double));
	return out;
}

inline void computeReqLength_double(double realPrecision, short radExpo, int* reqLength, double* medianValue)
{
	short reqExpo = getPrecisionReqLength_double(realPrecision);
	*reqLength = 12+radExpo - reqExpo; //radExpo-reqExpo == reqMantiLength
	if(*reqLength<12)
		*reqLength = 12;
	if(*reqLength>64)
	{
		*reqLength = 64;
		*medianValue = 0;
	}
}

inline short computeReqLength_double_MSST19(double realPrecision)
{
	short reqExpo = getPrecisionReqLength_double(realPrecision);
	return 12-reqExpo;
}

unsigned int optimize_intervals_double_1D(double *oriData, size_t dataLength, double realPrecision)
{	
	size_t i = 0, radiusIndex;
	double pred_value = 0, pred_err;
	size_t *intervals = (size_t*)malloc(confparams_cpr->maxRangeRadius*sizeof(size_t));
	memset(intervals, 0, confparams_cpr->maxRangeRadius*sizeof(size_t));
	size_t totalSampleSize = dataLength/confparams_cpr->sampleDistance;
	for(i=2;i<dataLength;i++)
	{
		if(i%confparams_cpr->sampleDistance==0)
		{
			//pred_value = 2*oriData[i-1] - oriData[i-2];
			pred_value = oriData[i-1];
			pred_err = fabs(pred_value - oriData[i]);
			radiusIndex = (unsigned long)((pred_err/realPrecision+1)/2);
			if(radiusIndex>=confparams_cpr->maxRangeRadius)
				radiusIndex = confparams_cpr->maxRangeRadius - 1;
			intervals[radiusIndex]++;
		}
	}
	//compute the appropriate number
	size_t targetCount = totalSampleSize*confparams_cpr->predThreshold;
	size_t sum = 0;
	for(i=0;i<confparams_cpr->maxRangeRadius;i++)
	{
		sum += intervals[i];
		if(sum>targetCount)
			break;
	}

	if(i>=confparams_cpr->maxRangeRadius)
		i = confparams_cpr->maxRangeRadius-1;
	unsigned int accIntervals = 2*(i+1);
	unsigned int powerOf2 = roundUpToPowerOf2(accIntervals);

	if(powerOf2<32)
		powerOf2 = 32;

	free(intervals);
	//printf("accIntervals=%d, powerOf2=%d\n", accIntervals, powerOf2);
	return powerOf2;
}

TightDataPointStorageD* SZ_compress_double_1D_MDQ(double *oriData, 
size_t dataLength, double realPrecision, double valueRangeSize, double medianValue_d)
{
	unsigned int quantization_intervals;
	if(exe_params->optQuantMode==1)
		quantization_intervals = optimize_intervals_double_1D_opt(oriData, dataLength, realPrecision);
	else
		quantization_intervals = exe_params->intvCapacity;
	//updateQuantizationInfo(quantization_intervals);	
	int intvRadius = quantization_intervals/2;

	size_t i;
	int reqLength;
	double medianValue = medianValue_d;
	short radExpo = getExponent_double(valueRangeSize/2);

	computeReqLength_double(realPrecision, radExpo, &reqLength, &medianValue);	

	int* type = (int*) malloc(dataLength*sizeof(int));
		
	double* spaceFillingValue = oriData; //
	
	DynamicIntArray *exactLeadNumArray;
	new_DIA(&exactLeadNumArray, DynArrayInitLen);
	
	DynamicByteArray *exactMidByteArray;
	new_DBA(&exactMidByteArray, DynArrayInitLen);
	
	DynamicIntArray *resiBitArray;
	new_DIA(&resiBitArray, DynArrayInitLen);

	unsigned char preDataBytes[8];
	longToBytes_bigEndian(preDataBytes, 0);
	
	int reqBytesLength = reqLength/8;
	int resiBitsLength = reqLength%8;
	double last3CmprsData[3] = {0};

	DoubleValueCompressElement *vce = (DoubleValueCompressElement*)malloc(sizeof(DoubleValueCompressElement));
	LossyCompressionElement *lce = (LossyCompressionElement*)malloc(sizeof(LossyCompressionElement));			
				
	//add the first data	
	type[0] = 0;
	compressSingleDoubleValue(vce, spaceFillingValue[0], realPrecision, medianValue, reqLength, reqBytesLength, resiBitsLength);
	updateLossyCompElement_Double(vce->curBytes, preDataBytes, reqBytesLength, resiBitsLength, lce);
	memcpy(preDataBytes,vce->curBytes,8);
	addExactData(exactMidByteArray, exactLeadNumArray, resiBitArray, lce);
T
tickduan 已提交
150
	listAdd_double(last3CmprsData, vce->data);	
T
tickduan 已提交
151 152 153 154 155 156 157 158
		
	//add the second data
	type[1] = 0;
	compressSingleDoubleValue(vce, spaceFillingValue[1], realPrecision, medianValue, reqLength, reqBytesLength, resiBitsLength);
	updateLossyCompElement_Double(vce->curBytes, preDataBytes, reqBytesLength, resiBitsLength, lce);
	memcpy(preDataBytes,vce->curBytes,8);
	addExactData(exactMidByteArray, exactLeadNumArray, resiBitArray, lce);
	listAdd_double(last3CmprsData, vce->data);
T
tickduan 已提交
159

T
tickduan 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	int state;
	double checkRadius;
	double curData;
	double pred = last3CmprsData[0];
	double predAbsErr;
	checkRadius = (quantization_intervals-1)*realPrecision;
	double interval = 2*realPrecision;

	double recip_realPrecision = 1/realPrecision;
	for(i=2;i<dataLength;i++)
	{				
		//printf("%.30G\n",last3CmprsData[0]);
		curData = spaceFillingValue[i];
		//pred = 2*last3CmprsData[0] - last3CmprsData[1];
		//pred = last3CmprsData[0];
		predAbsErr = fabs(curData - pred);	
		if(predAbsErr<checkRadius)
		{
			state = (predAbsErr*recip_realPrecision+1)*0.5;
			if(curData>=pred)
			{
				type[i] = intvRadius+state;
				pred = pred + state*interval;
			}
			else //curData<pred
			{
				type[i] = intvRadius-state;
				pred = pred - state*interval;
			}
			//listAdd_double(last3CmprsData, pred);
			continue;
		}
		
		//unpredictable data processing
		type[i] = 0;		
		compressSingleDoubleValue(vce, curData, realPrecision, medianValue, reqLength, reqBytesLength, resiBitsLength);
		updateLossyCompElement_Double(vce->curBytes, preDataBytes, reqBytesLength, resiBitsLength, lce);
		memcpy(preDataBytes,vce->curBytes,8);
		addExactData(exactMidByteArray, exactLeadNumArray, resiBitArray, lce);
							
		//listAdd_double(last3CmprsData, vce->data);
		pred = vce->data;
T
tickduan 已提交
202
			
T
tickduan 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
		
	}//end of for
		
	size_t exactDataNum = exactLeadNumArray->size;
	
	TightDataPointStorageD* tdps;
			
	new_TightDataPointStorageD(&tdps, dataLength, exactDataNum, 
			type, exactMidByteArray->array, exactMidByteArray->size,  
			exactLeadNumArray->array,  
			resiBitArray->array, resiBitArray->size, 
			resiBitsLength, 
			realPrecision, medianValue, (char)reqLength, quantization_intervals, NULL, 0, 0);
	
//	printf("exactDataNum=%d, expSegmentsInBytes_size=%d, exactMidByteArray->size=%d\n", 
//			exactDataNum, expSegmentsInBytes_size, exactMidByteArray->size);
	
	//free memory
	free_DIA(exactLeadNumArray);
	free_DIA(resiBitArray);
	free(type);
	free(vce);
	free(lce);	
	free(exactMidByteArray); //exactMidByteArray->array has been released in free_TightDataPointStorageF(tdps);	
	
	return tdps;	
}

T
tickduan 已提交
231
void SZ_compress_args_double_StoreOriData(double* oriData, size_t dataLength, unsigned char* newByteData, size_t *outSize)
T
tickduan 已提交
232 233 234 235 236 237 238 239 240
{	
	int doubleSize = sizeof(double);
	size_t k = 0, i;
	size_t totalByteLength = 3 + MetaDataByteLength_double + exe_params->SZ_SIZE_TYPE + 1 + doubleSize*dataLength;
	/*No need to malloc because newByteData should always already be allocated with no less totalByteLength.*/
	//*newByteData = (unsigned char*)malloc(totalByteLength);
	
	unsigned char dsLengthBytes[8];
	for (i = 0; i < 3; i++)//3
T
tickduan 已提交
241
		newByteData[k++] = versionNumber[i];
T
tickduan 已提交
242 243
	
	if(exe_params->SZ_SIZE_TYPE==4)//1
T
tickduan 已提交
244
		newByteData[k++] = 16; //00010000
T
tickduan 已提交
245
	else
T
tickduan 已提交
246
		newByteData[k++] = 80;	//01010000: 01000000 indicates the SZ_SIZE_TYPE=8
T
tickduan 已提交
247

T
tickduan 已提交
248
	convertSZParamsToBytes(confparams_cpr, &(newByteData[k]));
T
tickduan 已提交
249 250 251 252
	k = k + MetaDataByteLength_double;

	sizeToBytes(dsLengthBytes,dataLength);
	for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++)//ST: 4 or 8
T
tickduan 已提交
253
		newByteData[k++] = dsLengthBytes[i];
T
tickduan 已提交
254 255

	if(sysEndianType==BIG_ENDIAN_SYSTEM)
T
tickduan 已提交
256
		memcpy(newByteData+4+MetaDataByteLength_double+exe_params->SZ_SIZE_TYPE, oriData, dataLength*doubleSize);
T
tickduan 已提交
257 258
	else
	{
T
tickduan 已提交
259
		unsigned char* p = newByteData+4+MetaDataByteLength_double+exe_params->SZ_SIZE_TYPE;
T
tickduan 已提交
260 261 262 263 264 265 266
		for(i=0;i<dataLength;i++,p+=doubleSize)
			doubleToBytes(p, oriData[i]);
	}
	*outSize = totalByteLength;
}


T
tickduan 已提交
267
bool SZ_compress_args_double_NoCkRngeNoGzip_1D(unsigned char* newByteData, double *oriData, 
T
tickduan 已提交
268 269 270 271
size_t dataLength, double realPrecision, size_t *outSize, double valueRangeSize, double medianValue_d)
{
	char compressionType = 0;	
	TightDataPointStorageD* tdps = NULL; 	
T
tickduan 已提交
272
	tdps = SZ_compress_double_1D_MDQ(oriData, dataLength, realPrecision, valueRangeSize, medianValue_d);			
T
tickduan 已提交
273
	
T
tickduan 已提交
274 275 276 277 278
	if(!convertTDPStoFlatBytes_double(tdps, newByteData, outSize))
	{
		free_TightDataPointStorageD(tdps);	
		return false;
	} 
T
tickduan 已提交
279
	
T
tickduan 已提交
280 281
	//if(*outSize>3 + MetaDataByteLength_double + exe_params->SZ_SIZE_TYPE + 1 + sizeof(double)*dataLength)
	//	SZ_compress_args_double_StoreOriData(oriData, dataLength, newByteData, outSize);
T
tickduan 已提交
282 283
	
	free_TightDataPointStorageD(tdps);	
T
tickduan 已提交
284
	return true;
T
tickduan 已提交
285 286
}

T
tickduan 已提交
287 288 289
/*MSST19*/
TightDataPointStorageD* SZ_compress_double_1D_MDQ_MSST19(double *oriData, 
size_t dataLength, double realPrecision, double valueRangeSize, double medianValue_f)
T
tickduan 已提交
290
{
T
tickduan 已提交
291 292 293

	//struct ClockPoint clockPointBuild;
	//TimeDurationStart("build", &clockPointBuild);
T
tickduan 已提交
294 295
	unsigned int quantization_intervals;
	if(exe_params->optQuantMode==1)
T
tickduan 已提交
296
		quantization_intervals = optimize_intervals_double_1D_opt_MSST19(oriData, dataLength, realPrecision);
T
tickduan 已提交
297 298
	else
		quantization_intervals = exe_params->intvCapacity;
T
tickduan 已提交
299
	//updateQuantizationInfo(quantization_intervals);
T
tickduan 已提交
300 301
	int intvRadius = quantization_intervals/2;
	
T
tickduan 已提交
302 303 304 305 306 307 308 309 310 311 312
	double* precisionTable = (double*)malloc(sizeof(double) * quantization_intervals);
	double inv = 2.0-pow(2, -(confparams_cpr->plus_bits));
    for(int i=0; i<quantization_intervals; i++){
        double test = pow((1+realPrecision), inv*(i - intvRadius));
        precisionTable[i] = test;
    }
    
	struct TopLevelTableWideInterval levelTable;
    MultiLevelCacheTableWideIntervalBuild(&levelTable, precisionTable, quantization_intervals, realPrecision, confparams_cpr->plus_bits);

	size_t i;
T
tickduan 已提交
313
	int reqLength;
T
tickduan 已提交
314 315 316
	double medianValue = medianValue_f;
	//double medianInverse = 1 / medianValue_f;
	//short radExpo = getExponent_double(realPrecision);
T
tickduan 已提交
317
	
T
tickduan 已提交
318
	reqLength = computeReqLength_double_MSST19(realPrecision);	
T
tickduan 已提交
319 320 321 322 323 324

	int* type = (int*) malloc(dataLength*sizeof(int));
		
	double* spaceFillingValue = oriData; //
	
	DynamicIntArray *exactLeadNumArray;
T
tickduan 已提交
325
	new_DIA(&exactLeadNumArray, dataLength/2/8);
T
tickduan 已提交
326 327
	
	DynamicByteArray *exactMidByteArray;
T
tickduan 已提交
328
	new_DBA(&exactMidByteArray, dataLength/2);
T
tickduan 已提交
329 330 331 332 333
	
	DynamicIntArray *resiBitArray;
	new_DIA(&resiBitArray, DynArrayInitLen);
	
	unsigned char preDataBytes[8];
T
tickduan 已提交
334
	intToBytes_bigEndian(preDataBytes, 0);
T
tickduan 已提交
335 336 337
	
	int reqBytesLength = reqLength/8;
	int resiBitsLength = reqLength%8;
T
tickduan 已提交
338 339 340
	double last3CmprsData[3] = {0};

	//size_t miss=0, hit=0;
T
tickduan 已提交
341 342 343

	DoubleValueCompressElement *vce = (DoubleValueCompressElement*)malloc(sizeof(DoubleValueCompressElement));
	LossyCompressionElement *lce = (LossyCompressionElement*)malloc(sizeof(LossyCompressionElement));
T
tickduan 已提交
344 345
				
	//add the first data	
T
tickduan 已提交
346
	type[0] = 0;
T
tickduan 已提交
347
	compressSingleDoubleValue_MSST19(vce, spaceFillingValue[0], realPrecision, reqLength, reqBytesLength, resiBitsLength);
T
tickduan 已提交
348 349 350
	updateLossyCompElement_Double(vce->curBytes, preDataBytes, reqBytesLength, resiBitsLength, lce);
	memcpy(preDataBytes,vce->curBytes,8);
	addExactData(exactMidByteArray, exactLeadNumArray, resiBitArray, lce);
T
tickduan 已提交
351
	listAdd_double(last3CmprsData, vce->data);
T
tickduan 已提交
352
	//miss++;	
T
tickduan 已提交
353 354 355 356 357 358 359 360 361
		
	//add the second data
	type[1] = 0;
	compressSingleDoubleValue_MSST19(vce, spaceFillingValue[1], realPrecision, reqLength, reqBytesLength, resiBitsLength);
	updateLossyCompElement_Double(vce->curBytes, preDataBytes, reqBytesLength, resiBitsLength, lce);
	memcpy(preDataBytes,vce->curBytes,8);
	addExactData(exactMidByteArray, exactLeadNumArray, resiBitArray, lce);
	listAdd_double(last3CmprsData, vce->data);
	//miss++;
T
tickduan 已提交
362

T
tickduan 已提交
363 364 365 366
	int state;
	//double checkRadius;
	double curData;
	double pred = vce->data;
T
tickduan 已提交
367

T
tickduan 已提交
368
    double predRelErrRatio;
T
tickduan 已提交
369

T
tickduan 已提交
370 371 372 373 374 375 376 377 378
	const uint64_t top = levelTable.topIndex, base = levelTable.baseIndex;
	const uint64_t range = top - base;
	const int bits = levelTable.bits;
	uint64_t* const buffer = (uint64_t*)&predRelErrRatio;
	const int shift = 52-bits;
	uint64_t expoIndex, mantiIndex;
	uint16_t* tables[range+1];
	for(int i=0; i<=range; i++){
		tables[i] = levelTable.subTables[i].table;
T
tickduan 已提交
379 380
	}

T
tickduan 已提交
381 382 383 384
	for(i=2;i<dataLength;i++)
	{
		curData = spaceFillingValue[i];
		predRelErrRatio = curData / pred;
T
tickduan 已提交
385

T
tickduan 已提交
386 387 388 389 390 391
		expoIndex = ((*buffer & 0x7fffffffffffffff) >> 52) - base;
		if(expoIndex <= range){
			mantiIndex = (*buffer & 0x000fffffffffffff) >> shift;
			state = tables[expoIndex][mantiIndex];
		}else{
			state = 0;
T
tickduan 已提交
392 393
		}

T
tickduan 已提交
394 395 396 397 398 399
		if(state)
		{
			type[i] = state;
			pred *= precisionTable[state];
			//hit++;
			continue;
T
tickduan 已提交
400 401
		}

T
tickduan 已提交
402 403 404 405 406 407 408 409
		//unpredictable data processing
		type[i] = 0;
		compressSingleDoubleValue_MSST19(vce, curData, realPrecision, reqLength, reqBytesLength, resiBitsLength);
		updateLossyCompElement_Double(vce->curBytes, preDataBytes, reqBytesLength, resiBitsLength, lce);
		memcpy(preDataBytes,vce->curBytes,8);
		addExactData(exactMidByteArray, exactLeadNumArray, resiBitArray, lce);
		pred =  vce->data;
		//miss++;
T
tickduan 已提交
410
		
T
tickduan 已提交
411 412 413 414
	}//end of for
		
//	printf("miss:%d, hit:%d\n", miss, hit);

T
tickduan 已提交
415 416 417 418 419 420 421 422
	size_t exactDataNum = exactLeadNumArray->size;
	
	TightDataPointStorageD* tdps;
			
	new_TightDataPointStorageD(&tdps, dataLength, exactDataNum, 
			type, exactMidByteArray->array, exactMidByteArray->size,  
			exactLeadNumArray->array,  
			resiBitArray->array, resiBitArray->size, 
T
tickduan 已提交
423
			resiBitsLength,
T
tickduan 已提交
424
			realPrecision, medianValue, (char)reqLength, quantization_intervals, NULL, 0, 0);
T
tickduan 已提交
425
    tdps->plus_bits = confparams_cpr->plus_bits;
T
tickduan 已提交
426 427 428 429 430 431 432 433
	
	//free memory
	free_DIA(exactLeadNumArray);
	free_DIA(resiBitArray);
	free(type);	
	free(vce);
	free(lce);	
	free(exactMidByteArray); //exactMidByteArray->array has been released in free_TightDataPointStorageF(tdps);
T
tickduan 已提交
434 435
	free(precisionTable);
	freeTopLevelTableWideInterval(&levelTable);
T
tickduan 已提交
436 437 438
	return tdps;
}

T
tickduan 已提交
439

T
tickduan 已提交
440
void SZ_compress_args_double_withinRange(unsigned char* newByteData, double *oriData, size_t dataLength, size_t *outSize)
T
tickduan 已提交
441
{
T
tickduan 已提交
442 443 444 445 446
	TightDataPointStorageD* tdps = (TightDataPointStorageD*) malloc(sizeof(TightDataPointStorageD));
	tdps->rtypeArray = NULL;
	tdps->typeArray = NULL;
	tdps->leadNumArray = NULL;
	tdps->residualMidBits = NULL;
T
tickduan 已提交
447
	
T
tickduan 已提交
448 449 450 451 452 453 454 455
	tdps->allSameData = 1;
	tdps->dataSeriesLength = dataLength;
	tdps->exactMidBytes = (unsigned char*)malloc(sizeof(unsigned char)*8);
	tdps->pwrErrBoundBytes = NULL;
	tdps->isLossless = 0;
	double value = oriData[0];
	doubleToBytes(tdps->exactMidBytes, value);
	tdps->exactMidBytes_size = 8;
T
tickduan 已提交
456
	
T
tickduan 已提交
457 458 459 460 461 462 463 464 465
	size_t tmpOutSize;
	//unsigned char *tmpByteData;
	convertTDPStoFlatBytes_double(tdps, newByteData, &tmpOutSize);
	//convertTDPStoFlatBytes_double(tdps, &tmpByteData, &tmpOutSize);

	//*newByteData = (unsigned char*)malloc(sizeof(unsigned char)*16); //for floating-point data (1+3+4+4)
	//memcpy(*newByteData, tmpByteData, 16);
	*outSize = tmpOutSize;//12==3+1+8(double_size)+MetaDataByteLength_double
	free_TightDataPointStorageD(tdps);	
T
tickduan 已提交
466 467
}

T
tickduan 已提交
468 469 470 471
int SZ_compress_args_double(double *oriData, size_t r1, unsigned char* newByteData, size_t *outSize, sz_params* params)
{		
	int status = SZ_SUCCESS;
	size_t dataLength = r1;
T
tickduan 已提交
472
	
T
tickduan 已提交
473

T
tickduan 已提交
474 475 476 477 478 479
	double valueRangeSize = 0, medianValue = 0;
	
	unsigned char * signs = NULL;
	bool positive = true;
	double nearZero = 0.0;
	double min = 0;
T
tickduan 已提交
480 481 482 483 484 485 486 487 488
	if(params->pw_relBoundRatio < 0.000009999)
		params->accelerate_pw_rel_compression = 0;

	// check at least elements count  
	if(dataLength <= MIN_NUM_OF_ELEMENTS)
	{
		printf("error, double input elements count=%d less than %d, so need not do compress.\n", dataLength, MIN_NUM_OF_ELEMENTS);
		return SZ_LITTER_ELEMENT;
	}
T
tickduan 已提交
489
		
T
tickduan 已提交
490
	if(params->errorBoundMode == PW_REL && params->accelerate_pw_rel_compression == 1)
T
tickduan 已提交
491
	{
T
tickduan 已提交
492 493 494
		signs = (unsigned char *) malloc(dataLength);
		memset(signs, 0, dataLength);
		min = computeRangeSize_double_MSST19(oriData, dataLength, &valueRangeSize, &medianValue, signs, &positive, &nearZero);
T
tickduan 已提交
495 496
	}
	else
T
tickduan 已提交
497 498
		min = computeRangeSize_double(oriData, dataLength, &valueRangeSize, &medianValue);	
	double max = min+valueRangeSize;
T
tickduan 已提交
499 500
	params->dmin = min;
	params->dmax = max;
T
tickduan 已提交
501 502 503
	
	double realPrecision = 0; 
	
T
tickduan 已提交
504
	if(params->errorBoundMode==PSNR)
T
tickduan 已提交
505
	{
T
tickduan 已提交
506 507
		params->errorBoundMode = SZ_ABS;
		realPrecision = params->absErrBound = computeABSErrBoundFromPSNR(params->psnr, (double)params->predThreshold, valueRangeSize);
T
tickduan 已提交
508
	}
T
tickduan 已提交
509
	else if(params->errorBoundMode==NORM) //norm error = sqrt(sum((xi-xi_)^2))
T
tickduan 已提交
510
	{
T
tickduan 已提交
511 512
		params->errorBoundMode = SZ_ABS;
		realPrecision = params->absErrBound = computeABSErrBoundFromNORM_ERR(params->normErr, dataLength);
T
tickduan 已提交
513 514 515 516
		//printf("realPrecision=%lf\n", realPrecision);				
	}	
	else
	{
T
tickduan 已提交
517 518
		realPrecision = getRealPrecision_double(valueRangeSize, params->errorBoundMode, params->absErrBound, params->relBoundRatio, &status);
		params->absErrBound = realPrecision;
T
tickduan 已提交
519 520 521
	}	
	if(valueRangeSize <= realPrecision)
	{
T
tickduan 已提交
522
		if(params->errorBoundMode>=PW_REL && params->accelerate_pw_rel_compression == 1)
T
tickduan 已提交
523 524
			free(signs);		
		SZ_compress_args_double_withinRange(newByteData, oriData, dataLength, outSize);
T
tickduan 已提交
525
	}
T
tickduan 已提交
526
	else
T
tickduan 已提交
527
	{
T
tickduan 已提交
528
		size_t tmpOutSize = 0;
T
tickduan 已提交
529
		unsigned char* tmpByteData = newByteData;
T
tickduan 已提交
530 531
		bool twoStage = params->szMode != SZ_BEST_SPEED;
		if(twoStage)
T
tickduan 已提交
532
		{
T
tickduan 已提交
533 534 535 536 537 538 539
			tmpByteData = (unsigned char*)malloc(r1*sizeof(double)*1.2);
		}

		if(params->errorBoundMode>=PW_REL)
		{
			if(params->accelerate_pw_rel_compression && params->maxRangeRadius <= 32768)
				SZ_compress_args_double_NoCkRngeNoGzip_1D_pwr_pre_log_MSST19(tmpByteData, oriData, params->pw_relBoundRatio, r1, &tmpOutSize, valueRangeSize, medianValue, signs, &positive, min, max, nearZero);
T
tickduan 已提交
540
			else
T
tickduan 已提交
541
				SZ_compress_args_double_NoCkRngeNoGzip_1D_pwr_pre_log(tmpByteData, oriData, params->pw_relBoundRatio, r1, &tmpOutSize, min, max);
T
tickduan 已提交
542 543 544
		}
		else
		{
T
tickduan 已提交
545 546 547 548 549 550 551 552 553
			if(!SZ_compress_args_double_NoCkRngeNoGzip_1D(tmpByteData, oriData, r1, realPrecision, &tmpOutSize, valueRangeSize, medianValue))
			{
				if(twoStage)
				   free(tmpByteData);

				return SZ_ALGORITHM_ERR;
			}
			//if(tmpOutSize>=dataLength*sizeof(double) + 3 + MetaDataByteLength_double + exe_params->SZ_SIZE_TYPE + 1)
			//	SZ_compress_args_double_StoreOriData(oriData, dataLength, tmpByteData, &tmpOutSize);
T
tickduan 已提交
554
		}
T
tickduan 已提交
555
					
T
tickduan 已提交
556 557 558
		//		
		//Call Gzip to do the further compression.
		//
T
tickduan 已提交
559
		if(twoStage)
T
tickduan 已提交
560
		{
T
tickduan 已提交
561
			*outSize = sz_lossless_compress(params->losslessCompressor, params->gzipMode, tmpByteData, tmpOutSize, newByteData);
T
tickduan 已提交
562
			free(tmpByteData);
T
tickduan 已提交
563 564 565
		}
		else
		{
T
tickduan 已提交
566
			*outSize = tmpOutSize;
T
tickduan 已提交
567
		}
T
tickduan 已提交
568
	}
T
tickduan 已提交
569

T
tickduan 已提交
570 571
	return status;
}
T
tickduan 已提交
572 573


T
tickduan 已提交
574 575 576 577 578 579 580 581
unsigned int optimize_intervals_double_1D_opt_MSST19(double *oriData, size_t dataLength, double realPrecision)
{	
	size_t i = 0, radiusIndex;
	double pred_value = 0;
	double pred_err;
	size_t *intervals = (size_t*)malloc(confparams_cpr->maxRangeRadius*sizeof(size_t));
	memset(intervals, 0, confparams_cpr->maxRangeRadius*sizeof(size_t));
	size_t totalSampleSize = 0;//dataLength/confparams_cpr->sampleDistance;
T
tickduan 已提交
582

T
tickduan 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	double * data_pos = oriData + 2;
	double divider = log2(1+realPrecision)*2;
	int tempIndex = 0;
	while(data_pos - oriData < dataLength){
		if(*data_pos == 0){
        		data_pos += confparams_cpr->sampleDistance;
        		continue;
		}			
		tempIndex++;
		totalSampleSize++;
		pred_value = data_pos[-1];
		pred_err = fabs((double)*data_pos / pred_value);
		radiusIndex = (unsigned long)fabs(log2(pred_err)/divider+0.5);
		if(radiusIndex>=confparams_cpr->maxRangeRadius)
			radiusIndex = confparams_cpr->maxRangeRadius - 1;			
		intervals[radiusIndex]++;
T
tickduan 已提交
599

T
tickduan 已提交
600
		data_pos += confparams_cpr->sampleDistance;
T
tickduan 已提交
601
	}
T
tickduan 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615
	//compute the appropriate number
	size_t targetCount = totalSampleSize*confparams_cpr->predThreshold;
	size_t sum = 0;
	for(i=0;i<confparams_cpr->maxRangeRadius;i++)
	{
		sum += intervals[i];
		if(sum>targetCount)
			break;
	}
	if(i>=confparams_cpr->maxRangeRadius)
		i = confparams_cpr->maxRangeRadius-1;
		
	unsigned int accIntervals = 2*(i+1);
	unsigned int powerOf2 = roundUpToPowerOf2(accIntervals);
T
tickduan 已提交
616
	
T
tickduan 已提交
617 618
	if(powerOf2<64)
		powerOf2 = 64;
T
tickduan 已提交
619
	
T
tickduan 已提交
620 621 622
	free(intervals);
	return powerOf2;
}
T
tickduan 已提交
623 624


T
tickduan 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
unsigned int optimize_intervals_double_1D_opt(double *oriData, size_t dataLength, double realPrecision)
{	
	size_t i = 0, radiusIndex;
	double pred_value = 0, pred_err;
	size_t *intervals = (size_t*)malloc(confparams_cpr->maxRangeRadius*sizeof(size_t));
	memset(intervals, 0, confparams_cpr->maxRangeRadius*sizeof(size_t));
	size_t totalSampleSize = 0;

	double * data_pos = oriData + 2;
	while(data_pos - oriData < dataLength){
		totalSampleSize++;
		pred_value = data_pos[-1];
		pred_err = fabs(pred_value - *data_pos);
		radiusIndex = (unsigned long)((pred_err/realPrecision+1)/2);
		if(radiusIndex>=confparams_cpr->maxRangeRadius)
			radiusIndex = confparams_cpr->maxRangeRadius - 1;			
		intervals[radiusIndex]++;

		data_pos += confparams_cpr->sampleDistance;
	}
	//compute the appropriate number
	size_t targetCount = totalSampleSize*confparams_cpr->predThreshold;
	size_t sum = 0;
	for(i=0;i<confparams_cpr->maxRangeRadius;i++)
	{
		sum += intervals[i];
		if(sum>targetCount)
			break;
T
tickduan 已提交
653
	}
T
tickduan 已提交
654 655 656 657 658
	if(i>=confparams_cpr->maxRangeRadius)
		i = confparams_cpr->maxRangeRadius-1;
		
	unsigned int accIntervals = 2*(i+1);
	unsigned int powerOf2 = roundUpToPowerOf2(accIntervals);
T
tickduan 已提交
659
	
T
tickduan 已提交
660 661
	if(powerOf2<32)
		powerOf2 = 32;
T
tickduan 已提交
662
	
T
tickduan 已提交
663 664
	free(intervals);
	return powerOf2;
T
tickduan 已提交
665
}