# Sage 8.1 eats memory until system freeze

Hi, I have a quite long code (which uses arbitrary precision real numbers) which runs perfectly on Sage 7.5.1 (ppa for Mint 17.3 - Ubuntu 14.04). On Sage 8.1 (sage-8.1-Ubuntu_14.04-x86_64.tar.bz2) it starts to eat the memory until it freezes the system. I would like to help in debugging. Is there something that I can try/run/test? I can also upload the code, if necessary.

Finally, I have a minimal working code

def test(m,c,precision):
M = 3*m
RRR = RealField(prec = precision)
coef02 = [RRR(1/i) for i in [1..M+1]]
g = coef02[M]
for i in [M-1..2,step=-1]: # Horner
g = x*g+coef02[i]
ME = 32
disk = [exp (2*pi.n(precision)*I*i/ME) for i in range(ME)]
gamma = abs(c)/2
ellipse = [(gamma*(w+c^2/(4*gamma^2)/w)) for w in disk]
epsilon1 = max([abs(g(x=z)) for z in ellipse])
return
m = 40
for c in [1/2..10,step=1/2]:
for ell in [1..10]:
test(m,c,165)


If I run this in 7.5.1, I see (in top) the memory percentage stable around 2.5. If I run in 8.1, it grows up to 7.3 before code termination. If I increase the length of the loops, memory usage continues to grow.

edit retag close merge delete

Use a profiler, e.g.

https://docs.python.org/2/library/profile.html

to see where the code spends the most time. (Implement somehow hard breaks to make it finish.) Then look exactly at the corresponding points. It is hard to do this without the code.

Some IDEs may have a profiler functionality, e.g.

https://www.jetbrains.com/help/pycharm/optimizing-your-code-using-profilers.html

(Never tried it with sage.)

It is also a good idea to run the code in the debugger and follow the jumps a while till the run freezes...

Note: We only know that approximate values are used. Make sure, that all sage algorithms used do not require exact algebra to terminate.

( 2018-02-07 12:38:18 -0500 )edit

Sort by » oldest newest most voted

After inserting some prints, i decided to perform exact arithmetics as long as i could.

(Expanding the "polynomial" g was showing a much simpler "polynomial". Then i made a true polynomial out of it, in order to use the relations satisfied by $\xi$, the primitive root of order $32$ with the complex embedding $\exp(2\pi i/32)$. Computations could be then instantly done w.r.t. the wanted precision.)

Slightly changed code:

PREC   = 165
coef02 = [ 1/k for k in [1..10**3] ]
RRR    = RealField(prec = PREC)
ME     = 32
m      = 40

F.<xi> = CyclotomicField(ME)

def test(m, c, precision):
M = 3*m
g = coef02[M]
for k in [ M-1..2, step=-1 ]:    # Horner
g = x*g + coef02[k]

g = g.expand()
g = g.polynomial(QQ)

disk     = [ xi^k for k in range(ME) ]
gamma    = abs(c)/2
ellipse  = [ gamma*(1+c^2/4/gamma^2/w) for w in disk ]

gvals    = [ g(x=z) for z in ellipse ]
gabsvals = [ abs( a.complex_embedding( PREC ) ) for a in gvals ]

epsilon1 = max( gabsvals )
return epsilon1

for c in [1/2..10, step=1/2]:
# for ell in [1..10]:
if True:
print "c = %4s -> %s" % ( c, test(m, c, PREC) )


Results:

c =  1/2 -> 0.5451774444795624753378569716654125445795323421817
c =    1 -> 3.877132750163312268194055765884982005629693463214
c =  3/2 -> 1.515428179650201079251313202796991675611785000224e19
c =    2 -> 5.539239522130613509638371201851936011597427063094e33
c =  5/2 -> 1.254347365744115869605098307063077516259244950897e45
c =    3 -> 2.485622007123562697065591352164619348140520368265e54
c =  7/2 -> 1.840038623607799316523292031320351173032744393937e62
c =    4 -> 1.220236022233173046095362930456016538046036458529e69
c =  9/2 -> 1.277831294399228135782148313824375408273976668569e75
c =    5 -> 3.115249330363268000457437128132669088002585033222e80
c = 11/2 -> 2.333289819982865723165430200076136015690563123553e85
c =    6 -> 6.591269725218075592564330647475925557514987984406e89
c = 13/2 -> 8.207311589847995050609430607861848821939923069317e93
c =    7 -> 5.085511066693174056991376957615614330916905538649e97
c = 15/2 -> 1.726432056041238224907347764787556059619499068072e101
c =    8 -> 3.470108034398171310085902662737894563375023273487e104
c = 17/2 -> 4.400366924707123401372401158677839663730509791993e107
c =    9 -> 3.710533637845394896264923862712625167668124571656e110
c = 19/2 -> 2.174358483775499706197907609995433166935905001082e113
c =   10 -> 9.191254911989823075908386272607123115557791656833e115


I hope this answers practically the question, although it deviates from RRR.

more

Thanks for the code optimization. But the problem remains: a working code in 7.5.1 makes 8.1 to use more and more memory. I think this is a regression. Shall I open a bug report?

( 2018-02-08 06:42:16 -0500 )edit