I wrote a recursive function that exhaustively generates matrices of certain characteristics. The function is as such:
def heavies(rowSums,colSums,colIndex,matH):
if colIndex == len(colSums) - 1:
for stuff in heavy_col_permutations(rowSums,colSums,colIndex):
matH[:,colIndex] = stuff[0]
yield matH.copy()
return
for stuff in heavy_col_permutations(rowSums,colSums,colIndex):
matH[:,colIndex] = stuff[0]
rowSums = stuff[1]
for matrix in heavies(rowSums,colSums,colIndex+1,matH):
yield matrix
and heavy_col_permutations is a function that just returns a column of a matrix with characteristics I need as well.
The problem is that as heavies is yielding a lot of matrices, it takes up too much memory. I end up calling this from another function one by one, and eventually I take up too much RAM and my process is killed (I'm running this on a server with memory caps). How can I write this to make it use less memory?
The program looks something like:
r = int(argv[1])
n = int(argv[2])
m = numpy.zeros((r,r),numpy.dtype=int32)
for row,col in heavy_listing(r,n):
for matrix in heavies(row,col,0,m):
# do more stuff with matrix
And I know that the function heavies is where the large amount of memory sucking is happening, I just need to lessen it.
Things you can try:
heavies() are not kept referenced in memory.gc module, call collect() and play around with set_threshold()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