summaryrefslogtreecommitdiff
path: root/src/compression
diff options
context:
space:
mode:
authorMagnus Lundborg <lundborg.magnus@gmail.com>2013-05-28 14:04:15 (GMT)
committerMagnus Lundborg <lundborg.magnus@gmail.com>2013-05-28 14:04:15 (GMT)
commit3d6794f4d8bfdf0f0a38b704a1e2a2c17026e824 (patch)
tree595211f4c69570493cb57c4047877a828b644711 /src/compression
parent583a3deb5bd37d5e81e17761b32831a190192281 (diff)
Allocate enough memory in Ptngc_pack_array_xtc3
When simulating with very few particles too little memory was allocated. Add a minimum value to allocate. Removed earlier debugging outputs.
Diffstat (limited to 'src/compression')
-rw-r--r--src/compression/tng_compress.c60
-rw-r--r--src/compression/xtc3.c91
2 files changed, 72 insertions, 79 deletions
diff --git a/src/compression/tng_compress.c b/src/compression/tng_compress.c
index cccab04..31818ee 100644
--- a/src/compression/tng_compress.c
+++ b/src/compression/tng_compress.c
@@ -63,16 +63,6 @@ static int verify_input_data_float(float *x, int natoms, int nframes, float prec
#if 1
for (iframe=0; iframe<nframes; iframe++)
for (i=0; i<natoms; i++)
- {
- printf("vel test: %d %d:",iframe,i);
- for (j=0; j<3; j++)
- printf(" %g",x[iframe*natoms*3+i*3+j]);
- printf("\n");
- }
-#endif
-#if 1
- for (iframe=0; iframe<nframes; iframe++)
- for (i=0; i<natoms; i++)
for (j=0; j<3; j++)
if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL)
printf("ERROR. Too large value: %d %d %d: %g %g %g\n",iframe,i,j,x[iframe*natoms*3+i*3+j],precision,x[iframe*natoms*3+i*3+j]/precision/MAX_FVAL);
@@ -116,7 +106,7 @@ static void quant_inter_differences(int *quant, int natoms, int nframes,
for (iframe=1; iframe<nframes; iframe++)
for (i=0; i<natoms; i++)
for (j=0; j<3; j++)
- quant_inter[iframe*natoms*3+i*3+j]=quant[iframe*natoms*3+i*3+j]-quant[(iframe-1)*natoms*3+i*3+j];
+ quant_inter[iframe*natoms*3+i*3+j]=quant[iframe*natoms*3+i*3+j]-quant[(iframe-1)*natoms*3+i*3+j];
}
static void quant_intra_differences(int *quant, int natoms, int nframes,
@@ -232,7 +222,7 @@ static void unquant_intra_differences_first_frame(int *quant, int natoms)
}
#endif
}
-
+
static void unquantize_intra_differences(double *x, int natoms, int nframes,
double precision,
int *quant)
@@ -334,7 +324,7 @@ static void compress_quantized_pos(int *quant, int *quant_inter, int *quant_intr
/* Information needed for decompression. */
if (data)
bufferfix((unsigned char*)data+bufloc,(fix_t)MAGIC_INT_POS,4);
- bufloc+=4;
+ bufloc+=4;
/* Number of atoms. */
if (data)
bufferfix((unsigned char*)data+bufloc,(fix_t)natoms,4);
@@ -384,7 +374,7 @@ static void compress_quantized_pos(int *quant, int *quant_inter, int *quant_intr
length=natoms*3;
datablock=(char*)Ptngc_pack_array(coder,quant_intra,&length,
initial_coding,initial_coding_parameter,natoms,speed);
- Ptngc_coder_deinit(coder);
+ Ptngc_coder_deinit(coder);
}
/* Block length. */
if (data)
@@ -408,7 +398,7 @@ static void compress_quantized_pos(int *quant, int *quant_inter, int *quant_intr
length=natoms*3*(nframes-1);
datablock=(char*)Ptngc_pack_array(coder,quant_inter+natoms*3,&length,
coding,coding_parameter,natoms,speed);
- Ptngc_coder_deinit(coder);
+ Ptngc_coder_deinit(coder);
}
/* One-to-one compression? */
else if ((coding==TNG_COMPRESS_ALGO_POS_XTC2) ||
@@ -459,7 +449,7 @@ static void compress_quantized_vel(int *quant, int *quant_inter,
/* Information needed for decompression. */
if (data)
bufferfix((unsigned char*)data+bufloc,(fix_t)MAGIC_INT_VEL,4);
- bufloc+=4;
+ bufloc+=4;
/* Number of atoms. */
if (data)
bufferfix((unsigned char*)data+bufloc,(fix_t)natoms,4);
@@ -666,7 +656,7 @@ static void determine_best_pos_initial_coding(int *quant, int *quant_intra, int
}
}
Ptngc_coder_deinit(coder);
-
+
if (speed>=2)
{
current_coding=TNG_COMPRESS_ALGO_POS_XTC3;
@@ -931,7 +921,7 @@ static void determine_best_vel_initial_coding(int *quant, int natoms, int speed,
best_code_size=current_code_size;
}
Ptngc_coder_deinit(coder);
-
+
/* Determine best parameter for triplet one-to-one. */
current_coding=TNG_COMPRESS_ALGO_VEL_TRIPLET_ONETOONE;
coder=Ptngc_coder_init();
@@ -1020,7 +1010,7 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms,
best_coding=current_coding;
best_code_size=current_code_size;
best_coding_parameter=current_coding_parameter;
-
+
/* Test triplet interframe */
current_coding=TNG_COMPRESS_ALGO_VEL_TRIPLET_INTER;
current_code_size=natoms*3*(nframes-1);
@@ -1033,7 +1023,7 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms,
{
best_coding=current_coding;
best_code_size=current_code_size;
- best_coding_parameter=current_coding_parameter;
+ best_coding_parameter=current_coding_parameter;
}
}
Ptngc_coder_deinit(coder);
@@ -1050,11 +1040,11 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms,
{
best_coding=current_coding;
best_code_size=current_code_size;
- best_coding_parameter=current_coding_parameter;
+ best_coding_parameter=current_coding_parameter;
}
}
Ptngc_coder_deinit(coder);
-
+
/* Test stopbit interframe */
current_coding=TNG_COMPRESS_ALGO_VEL_STOPBIT_INTER;
current_code_size=natoms*3*(nframes-1);
@@ -1067,11 +1057,11 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms,
{
best_coding=current_coding;
best_code_size=current_code_size;
- best_coding_parameter=current_coding_parameter;
- }
+ best_coding_parameter=current_coding_parameter;
+ }
}
Ptngc_coder_deinit(coder);
-
+
if (speed>=4)
{
/* Test BWLZH inter */
@@ -1086,7 +1076,7 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms,
{
best_coding=current_coding;
best_code_size=current_code_size;
- best_coding_parameter=current_coding_parameter;
+ best_coding_parameter=current_coding_parameter;
}
/* Test BWLZH one-to-one */
@@ -1101,7 +1091,7 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms,
{
best_coding=current_coding;
best_code_size=current_code_size;
- best_coding_parameter=current_coding_parameter;
+ best_coding_parameter=current_coding_parameter;
}
}
*coding=best_coding;
@@ -1201,7 +1191,7 @@ char DECLSPECDLLEXPORT *tng_compress_pos_int(int *pos, int natoms, int nframes,
{
coding_parameter=-1;
determine_best_pos_coding(quant,quant_inter,quant_intra,natoms,nframes,speed,prec_hi,prec_lo,
- &coding,&coding_parameter);
+ &coding,&coding_parameter);
}
else if (coding_parameter==-1)
{
@@ -1228,7 +1218,7 @@ char DECLSPECDLLEXPORT *tng_compress_pos_int(int *pos, int natoms, int nframes,
}
char DECLSPECDLLEXPORT *tng_compress_pos(double *pos, int natoms, int nframes,
- double desired_precision,
+ double desired_precision,
int speed,int *algo,
int *nitems)
{
@@ -1246,7 +1236,7 @@ char DECLSPECDLLEXPORT *tng_compress_pos(double *pos, int natoms, int nframes,
}
char DECLSPECDLLEXPORT *tng_compress_pos_float(float *pos, int natoms, int nframes,
- float desired_precision,
+ float desired_precision,
int speed,int *algo,
int *nitems)
{
@@ -1352,20 +1342,20 @@ char DECLSPECDLLEXPORT *tng_compress_vel_int(int *vel, int natoms, int nframes,
determine_best_vel_initial_coding(quant,natoms,speed,prec_hi,prec_lo,
&initial_coding,&initial_coding_parameter);
}
-
+
if (nframes==1)
{
coding=0;
coding_parameter=0;
}
-
+
if (nframes>1)
{
if (coding==-1)
{
coding_parameter=-1;
determine_best_vel_coding(quant,quant_inter,natoms,nframes,speed,prec_hi,prec_lo,
- &coding,&coding_parameter);
+ &coding,&coding_parameter);
}
else if (coding_parameter==-1)
{
@@ -1562,7 +1552,7 @@ static int tng_compress_uncompress_pos_gen(char *data,double *posd,float *posf,i
coder=Ptngc_coder_init();
rval=Ptngc_unpack_array(coder,(unsigned char*)data+bufloc,quant,natoms*3,
initial_coding,initial_coding_parameter,natoms);
- Ptngc_coder_deinit(coder);
+ Ptngc_coder_deinit(coder);
if (rval)
goto error;
/* Skip past the actual data block. */
@@ -1711,7 +1701,7 @@ static int tng_compress_uncompress_vel_gen(char *data,double *veld,float *velf,i
coder=Ptngc_coder_init();
rval=Ptngc_unpack_array(coder,(unsigned char*)data+bufloc,quant,natoms*3,
initial_coding,initial_coding_parameter,natoms);
- Ptngc_coder_deinit(coder);
+ Ptngc_coder_deinit(coder);
if (rval)
goto error;
/* Skip past the actual data block. */
diff --git a/src/compression/xtc3.c b/src/compression/xtc3.c
index 8c742a2..c26d7aa 100644
--- a/src/compression/xtc3.c
+++ b/src/compression/xtc3.c
@@ -10,9 +10,9 @@
* of the License, or (at your option) any later version.
*/
-/* This code is heavily influenced by
+/* This code is heavily influenced by
http://hpcv100.rc.rug.nl/xdrf.html
- Based on coordinate compression (c) by Frans van Hoesel.
+ Based on coordinate compression (c) by Frans van Hoesel.
and GROMACS xtc files (http://www.gromacs.org)
(c) Copyright (c) Erik Lindahl, David van der Spoel
*/
@@ -101,9 +101,9 @@ static int unpositive_int(int val)
struct xtc3_context
{
unsigned int *instructions;
- int ninstr, ninstr_alloc;
+ int ninstr, ninstr_alloc;
unsigned int *rle;
- int nrle, nrle_alloc;
+ int nrle, nrle_alloc;
unsigned int *large_direct;
int nlargedir, nlargedir_alloc;
unsigned int *large_intra_delta;
@@ -117,17 +117,17 @@ struct xtc3_context
int has_large_ints[MAX_LARGE_RLE*3]; /* Large cache. */
int has_large_type[MAX_LARGE_RLE]; /* What kind of type this large
int is. */
- int current_large_type;
+ int current_large_type;
};
static void init_xtc3_context(struct xtc3_context *xtc3_context)
{
xtc3_context->instructions=NULL;
xtc3_context->ninstr=0;
- xtc3_context->ninstr_alloc=0;
+ xtc3_context->ninstr_alloc=0;
xtc3_context->rle=NULL;
xtc3_context->nrle=0;
- xtc3_context->nrle_alloc=0;
+ xtc3_context->nrle_alloc=0;
xtc3_context->large_direct=NULL;
xtc3_context->nlargedir=0;
xtc3_context->nlargedir_alloc=0;
@@ -208,7 +208,7 @@ static void insert_value_in_array(unsigned int **ptr, int *nele, int *nele_alloc
allocate_enough_memory(ptr,nele,nele_alloc);
#ifdef SHOWIT
fprintf(stderr,"Inserting value %u into array %s @ %d\n",value,arrayname,(*nele)-1);
-#endif
+#endif
(*ptr)[(*nele)-1]=value;
}
@@ -228,7 +228,7 @@ static void swapdecide(struct xtc3_context *xtc3_context, int *input,int *swapat
*/
#ifdef SHOWIT
fprintf(stderr,"Trying Flip: %g %g\n",(double)swapped/normal, (double)normal/swapped);
-#endif
+#endif
if (((swapped<normal) && (fabs((double)swapped/normal)<iflipgaincheck)) ||
((normal<swapped) && (fabs((double)normal/swapped)<iflipgaincheck)))
{
@@ -291,7 +291,7 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord, int
{
int nencode=startenc*3;
int tmp_prevcoord[3];
-
+
tmp_prevcoord[0]=prevcoord[0];
tmp_prevcoord[1]=prevcoord[1];
tmp_prevcoord[2]=prevcoord[2];
@@ -313,13 +313,13 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord, int
positive_int(encode_ints[i*3]),
positive_int(encode_ints[i*3+1]),
positive_int(encode_ints[i*3+2]));
-#endif
+#endif
}
}
#ifdef SHOWIT
fprintf(stderr,"New batch\n");
-#endif
+#endif
while ((nencode<3+MAX_SMALL_RLE*3) && (nencode<ntriplets_left*3))
{
encode_ints[nencode]=input_ptr[nencode]-tmp_prevcoord[0];
@@ -336,7 +336,7 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord, int
positive_int(encode_ints[nencode]),
positive_int(encode_ints[nencode+1]),
positive_int(encode_ints[nencode+2]));
-#endif
+#endif
tmp_prevcoord[0]=input_ptr[nencode];
tmp_prevcoord[1]=input_ptr[nencode+1];
tmp_prevcoord[2]=input_ptr[nencode+2];
@@ -663,7 +663,7 @@ static void base_compress(unsigned int *data, int len, unsigned char *output, in
{
#ifdef SHOWIT
fprintf(stderr,"Writing largeint: ");
-#endif
+#endif
for (j=0; j<numbytes; j++)
{
int ilarge=j/4;
@@ -671,11 +671,11 @@ static void base_compress(unsigned int *data, int len, unsigned char *output, in
output[nwrittenout++]=(unsigned char)((largeint[ilarge]>>(ibyte*8))&(0xFFU));
#ifdef SHOWIT
fprintf(stderr,"%02x",(unsigned int)output[nwrittenout-1]);
-#endif
+#endif
}
#ifdef SHOWIT
fprintf(stderr,"\n");
-#endif
+#endif
nvals=0;
for (j=0; j<MAXBASEVALS+1; j++)
largeint[j]=0U;
@@ -692,7 +692,7 @@ static void base_compress(unsigned int *data, int len, unsigned char *output, in
int ilarge=j/4;
int ibyte=j%4;
output[nwrittenout++]=(unsigned char)((largeint[ilarge]>>(ibyte*8))&(0xFFU));
- }
+ }
}
}
*outlen=nwrittenout;
@@ -750,7 +750,7 @@ static void base_decompress(unsigned char *input, int len, unsigned int *output)
largeint[j]=0U;
#ifdef SHOWIT
fprintf(stderr,"Reading largeint: ");
-#endif
+#endif
for (j=0; j<numbytes; j++)
{
int ilarge=j/4;
@@ -758,11 +758,11 @@ static void base_decompress(unsigned char *input, int len, unsigned int *output)
largeint[ilarge]|=((unsigned int)input[j])<<(ibyte*8);
#ifdef SHOWIT
fprintf(stderr,"%02x",(unsigned int)input[j]);
-#endif
+#endif
}
#ifdef SHOWIT
fprintf(stderr,"\n");
-#endif
+#endif
input+=numbytes;
/* Do the long division required to get the output values. */
n=maxbasevals;
@@ -800,10 +800,10 @@ static int heuristic_bwlzh(unsigned int *ints, int nints)
}
/* Speed selects how careful to try to find the most efficient compression. The BWLZH algo is expensive!
- Speed <=2 always avoids BWLZH everywhere it is possible.
+ Speed <=2 always avoids BWLZH everywhere it is possible.
Speed 3 and 4 and 5 use heuristics (check proportion of large value). This should mostly be safe.
Speed 5 enables the LZ77 component of BWLZH.
- Speed 6 always tests if BWLZH is better and if it is uses it. This can be very slow.
+ Speed 6 always tests if BWLZH is better and if it is uses it. This can be very slow.
*/
unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int speed)
{
@@ -833,7 +833,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
int bwlzh_buf_len;
unsigned char *base_buf=NULL;
int base_buf_len;
-
+
struct xtc3_context xtc3_context;
init_xtc3_context(&xtc3_context);
@@ -852,7 +852,10 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
nvalues_sum=0;
#endif
/* Allocate enough memory for output */
- output=warnmalloc(8* *length*sizeof *output);
+ if (*length < 48)
+ output=warnmalloc(8*48*sizeof *output);
+ else
+ output=warnmalloc(8* *length*sizeof *output);
for (i=1; i<ntriplets; i++)
@@ -955,7 +958,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
/* Insert the next batch of integers to be encoded into the buffer */
#ifdef SHOWIT
fprintf(stderr,"Initial batch\n");
-#endif
+#endif
insert_batch(input+inpdata,ntriplets_left,prevcoord,encode_ints,0,&nencode);
/* First we must decide if the next value is large (does not reasonably fit in current small encoding)
@@ -1044,8 +1047,8 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
positive_int(encode_ints[i*3]),
positive_int(encode_ints[i*3+1]),
positive_int(encode_ints[i*3+2]));
-
-#endif
+
+#endif
min_runlength=2;
}
}
@@ -1076,11 +1079,11 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
ntriplets_left--;
refused=0;
-
+
/* Insert the next batch of integers to be encoded into the buffer */
#ifdef SHOWIT
fprintf(stderr,"Update batch due to large int.\n");
-#endif
+#endif
if ((swapatoms) && (didswap))
{
/* Keep swapped values. */
@@ -1139,7 +1142,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
new_runlength=1;
new_small_index=small_index;
}
-
+
iter_runlength=new_runlength;
iter_small_index=new_small_index;
@@ -1152,7 +1155,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
do {
new_runlength=iter_runlength;
new_small_index=iter_small_index;
-
+
#ifdef SHOWIT
fprintf(stderr,"Test new_small_index=%d Base=%d\n",new_small_index,Ptngc_magic(new_small_index));
#endif
@@ -1205,7 +1208,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
else if (new_runlength<6)
rle_index_dep=QUITE_LARGE;
if ((min_runlength)
- || ((new_small_index<small_index+IS_LARGE) && (new_small_index+rle_index_dep<max_large_index))
+ || ((new_small_index<small_index+IS_LARGE) && (new_small_index+rle_index_dep<max_large_index))
#if 1
|| (new_small_index+IS_LARGE<max_large_index)
#endif
@@ -1254,7 +1257,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
if ((new_runlength!=runlength) || (new_small_index!=small_index))
{
int change=new_small_index-small_index;
-
+
if (new_small_index<=0)
change=0;
@@ -1276,12 +1279,12 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
rejected=0;
#ifdef SHOWIT
fprintf(stderr,"Tested decrease %d of index: %g>=%g?\n",change,isum,(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change));
-#endif
+#endif
if (isum>(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change))
{
#ifdef SHOWIT
fprintf(stderr,"Rejected decrease %d of index due to length of vector: %g>=%g\n",change,isum,(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change));
-#endif
+#endif
rejected=1;
change++;
}
@@ -1308,7 +1311,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
}
#ifdef SHOWIT
fprintf(stderr,"Current small index: %d Base=%d\n",small_index,Ptngc_magic(small_index));
-#endif
+#endif
}
/* If we have a large previous integer we can combine it with a sequence of small ints. */
if (xtc3_context.has_large)
@@ -1336,14 +1339,14 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
}
else
{
-
+
#ifdef SHOWIT
fprintf(stderr,"Sequence of one large and small integers (good compression).\n");
#endif
/* Flush all large atoms but one! */
if (xtc3_context.has_large>1)
flush_large(&xtc3_context,xtc3_context.has_large-1);
-
+
/* Here we must check if we should emit a large
type change instruction. */
large_instruction_change(&xtc3_context,0);
@@ -1352,7 +1355,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
&xtc3_context.ninstr,
&xtc3_context.ninstr_alloc,
INSTR_DEFAULT,"instr");
-
+
write_three_large(&xtc3_context,0);
xtc3_context.has_large=0;
}
@@ -1396,7 +1399,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
fprintf(stderr,"Prevcoord in packing: %d %d %d\n",
prevcoord[0],prevcoord[1],prevcoord[2]);
#endif
-
+
inpdata+=3*runlength;
ntriplets_left-=runlength;
#if 1
@@ -1408,13 +1411,13 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp
#ifdef SHOWIT
fprintf(stderr,"Refused value: %d old is %d max is %d\n",new_small_index,small_index,max_large_index);
fflush(stderr);
-#endif
+#endif
refused=1;
}
}
#ifdef SHOWIT
fprintf(stderr,"Number of triplets left is %d\n",ntriplets_left);
-#endif
+#endif
}
/* If we have large previous integers we must flush them now. */
@@ -1726,7 +1729,7 @@ static void unpack_one_large(struct xtc3_context *xtc3_context,
fprintf(stderr,"Unpack one large: %d %d %d\n",prevcoord[0],prevcoord[1],prevcoord[2]);
#endif
}
-
+
int Ptngc_unpack_array_xtc3(unsigned char *packed,int *output, int length, int natoms)
{
@@ -1812,7 +1815,7 @@ int Ptngc_unpack_array_xtc3(unsigned char *packed,int *output, int length, int n
else
decompress_base_block(&ptr,xtc3_context.nlargeinter,&xtc3_context.large_inter_delta);
}
-
+
xtc3_context.nsmallintra=(int)(((unsigned int)ptr[0]) |
(((unsigned int)ptr[1])<<8) |
(((unsigned int)ptr[2])<<16) |
contact: Jan Huwald // Impressum