heap corruption?
Frederik Eaton
frederik at a5.repetae.net
Fri Jun 2 10:15:40 EDT 2006
Hi Alberto,
I'm experiencing a problem which may be a bug in GHC. Here I take the
dot product of two vectors. I put debugging traces in the 'dotR' C
function (which I call in 'dot'), so one can see the addresses of the
memory blocks. When the vectors are below a certain size, the function
seems to give the correct result. However, when they are above a
certain size, the same pointers are sometimes used for both arguments
(ap and bp), and the result is of course incorrect. The incorrect
result is consistent with both pointers referring to the first vector
(vconst 2). Also, with other arguments I sometimes get heap
corruption. Any idea what might be the problem?
Prelude Vector> print $ (dot $ useFast $ $(dim 1000) $ (vconst 2)) (ones)
ap=0xb1b0a008, bp=0xb1b0c008, rp=0xb1b036c8
an=1000, bn=1000, rn=1
res=2000.000000
2000.0
Prelude Vector> print $ (dot $ useFast $ $(dim 1000000) $ (vconst 2)) (ones)
ap=0xb3402008, bp=0xb3402008, rp=0xb1b0c770
an=1000000, bn=1000000, rn=1
res=4000000.000000
4000000.0
Prelude Vector> print $ (dot $ useFast $ $(dim 1000000) $ (vconst 2)) (vconst 2)
ap=0xb2c02008, bp=0xb2302008, rp=0xb1b08780
an=1000000, bn=1000000, rn=1
ap[130055]=2.000000 bp[130055]=-0.000000
ap[130056]=2.000000 bp[130056]=2.000001
ap[130058]=2.000000 bp[130058]=2.000000
ap[130059]=2.000000 bp[130059]=0.000000
ap[130060]=2.000000 bp[130060]=2.000001
...
res=3992888.004728
3992888.004727577
Here is dotR:
int dotR(DVEC(a), DVEC(b), DVEC(r)) {
REQUIRES(an == bn && rn == 1, BAD_SIZE);
DEBUGMSG("dotR");
fprintf(stderr,"ap=%p, bp=%p, rp=%p\n",ap,bp,rp);
fprintf(stderr,"an=%d, bn=%d, rn=%d\n",an,bn,rn);
double res=0.0;
int i;
for(i=0; i<an; i++) {
if(ap[i] != floor(ap[i]) || bp[i] != floor(bp[i]))
fprintf(stderr,"ap[%d]=%lf bp[%d]=%lf\n",i,ap[i],i,bp[i]);
res += ap[i]*bp[i];
}
fprintf(stderr,"res=%lf\n",res);
rp[0] = res;
OK
}
I switched to not using gsl here, so that I could be sure the bug
wasn't in gsl. However, when using gsl the same problems occur. So I
think it may be heap corruption in ghc. But I don't know how to debug
that. I tried running in valgrind but it doesn't show anything
obvious. If you want to run what I have, here is my code:
http://ofb.net/~frederik/GSLHaskell2.tar.gz
The problem is exposed by building and running:
print -l ':m Vector' 'print $ (dot $ useFast $ $(dim 1000000) $ (vconst 2)) (vconst 2)' | =ghci -package GSL -fglasgow-exts
Thanks,
Frederik
--
http://ofb.net/~frederik/
More information about the Glasgow-haskell-bugs
mailing list