+1 vote
asked by (130 points)

Hi Miles,

In my program, the vector<MPS> structure is used several times in some functions. I expect the memory to be automatically freed when exiting the functions, but it is not. I have tried the "swap" trick which indeed destructs the vectors, but the total memory taken by the process still keeps growing. Making the vectors global and reusing them also does not work.

Except for sum(vector <MPS>):

auto terms = vector<MPS>(N);
for (int q = 0; q < N; q++){
    terms.at(q) = psi;
    SqActOnOpenMPS(momentum + q, terms.at(q), Operator, sites, N, Cargs);
    SqActOnOpenMPS(q, terms.at(q), Operator, sites, N, Cargs);
}
Basis.at(0) = sum(terms, Cargs);

all the other operations are on only one or two MPS in the vector. Another strange thing is that the memory usage grows when calculating the overlap of two MPS in the same vector:

Nij[i][j] = overlap(Basis.at(i), Basis.at(j));

Hij[i][j] = overlap(Basis.at(i), H, Basis.at(j));

It takes ~ 20kB for each pair when the size of the (spinless) system is 20 and bond dimension ~ 200.

Is there any suggestion? Thank you.

Yilun

commented by (26.2k points)
Hi Yilun,
I think I'd need to know more to answer your questions. One question is: have you run your code in debug mode to test for any memory errors? For example I see that you are using code like Nij[i][j] without bounds checking, so you might have some out of bounds accesses happening. (Instead of using [i][j] on an array to store real numbers, I'd recommend using the Matrix class which is included with ITensor; it does bounds checking when the code is compiled in debug mode.)

Also, even though the memory usage is growing, it sounds like the amounts are very small (20kB). Is it causing a problem with running your code? I wonder if the memory actually is being freed correctly but the allocator system is just choosing to continue owning this memory, perhaps as an optimization in order to be able to more quickly allocate new memory later in the program? Of course if your system memory is filling up and you are getting crashes then that's a serious problem.

Worst case, there may be a memory leak somewhere in ITensor. If so it would be good if you could email me a minimal code that I can run which reproduces this leak. Thanks -

Miles

1 Answer

0 votes
answered by (26.2k points)

Hi Yilun,
To officially answer your question, all that should be necessary to free the memory used by an MPS is to let it go out of scope and/or overwrite it with a new MPS (such as a default-constructed MPS, or any other operation which calls the destructor of the MPS and its member objects).

However, it's important to know that all ITensors use a copy-on-write system where multiple ITensors can share the same underlying memory when copied, until one of them needs to make a change to the shared data, at which point the data is copied so as to be unique to that tensor. So there can be cases where an object is destructed but the memory usage doesn't go down because another tensor is still pointing to that data. It should be leak-proof, though, in the sense that once all of the tensors sharing data go out of scope the memory is freed.

Please also see my comment above asking some more questions about your code and what you are seeing happen -

Miles

Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.

Categories

...