Basically, I am getting a memory error in python when trying to perform an algebraic operation on a numpy matrix. The variable u
, is a large matrix of double (in the failing case its a 288x288x156 matrix of doubles. I only get this error in this huge case, but I am able to do this on other large matrices, just not this big). Here is the Python error:
Traceback (most recent call last): File "S:\3D_Simulation_Data\Patient SPM Segmentation\20 pc t perim erosion flattop\SwSim.py", line 121, in __init__ self.mainSimLoop() File "S:\3D_Simulation_Data\Patient SPM Segmentation\20 pc t perim erosion flattop\SwSim.py", line 309, in mainSimLoop u = solver.solve_cg(u,b,tensors,param,fdHold,resid) # Solve the left hand si de of the equation Au=b with conjugate gradient method to approximate u File "S:\3D_Simulation_Data\Patient SPM Segmentation\20 pc t perim erosion flattop\conjugate_getb.py", line 47, in solv e_cg u = u + alpha*p MemoryError
u = u + alpha*p
is the line of code that fails.
alpha
is just a double, while u
and r
are the large matrices described above (both of the same size).
I don't know that much about memory errors especially in Python. Any insight/tips into solving this would be very appreciated!
Thanks
the MemoryError in Python. A memory error is raised when a Python script fills all the available memory in a computer system. One of the most obvious ways to fix this issue is to increase the machine's RAM . But buying a new RAM stick is not the only solution for such a situation.
There is no general maximum array size in numpy. Of course there is, it is the size of np. intp datatype. Which for 32bit versions may only be 32bits...
Rewrite to
p *= alpha u += p
and this will use much less memory. Whereas p = p*alpha
allocates a whole new matrix for the result of p*alpha
and then discards the old p
; p*= alpha
does the same thing in place.
In general, with big matrices, try to use op=
assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With