[eemumu_AV] Use unique_ptr and RAII to automate memory management.#47
Conversation
|
In general I like the approach as it reduces the numbers of ifdefs. I have two questions on the CudaSetUpTearDown struct. Where is it initialised? If its initialised it will go out of scope probably only at the very end of the main. But the idea is to also dump the information how long the reset took in the timermap.dump(); (very bottom of the main). Also the numbering would change 9c <-> 9d but that's a minor one. Not even sure we need to time the cuda reset? @valassi ? |
Directly where it is declared:
Very correct. I thought that once you saw it clean up once, you have seen it all. It can obviously be reverted by
I did it because it's exactly the same logic as the memory management, and cuda frees after a device reset just lead to a crash. I've grown to love these |
ah sorry didn't see that, ok
I guess another way of solving it would be to put the dump into the destructor of the struct, then it's guaranteed that it will always go last (as long as there is only one such struct). |
|
Thanks @hageboeck for requesting a review. My personal taste: some of these things could be useful, some look like an overhead. And there are a few subtleties to keep in mind.
So all in all, nice suggestion, but I would keep only the unique pointers for simple arrays, and would make sure there are two separate functions for cudamallochost and new on the host in CUDA. |
|
PS Sorry forgot to mention, this has now some conflicts after I merged PR #48 |
|
Hi both, thanks for the discussion. Just for the record and so I do not forget, @hageboeck pointed out that the problem with adding unique pointers and not adding a struct with a destructor calling cudaDeviceReset is that cudaDeviceReset must be called AFTER all cudaMalloc. A code like would instead first call cudaDeviceReset() and later call the destructors of unique pointers that go out of scope. I am ok to keep Stephan's suggested struct then (with or without cudaFree). As discussed, this is any case temporary playground code, not production code. Eventually the memory allocations will be in MadGraph methods called by main, rather than by main. Another alternative I could suggest is to restructure check.cc so that it actually contains something like but it is quite ugly. I do not mind, lets keep CudaSetUpTearDown as it is and move on with adding the missing functionality to the rest of the code. |
3234ebb to
c0ee68c
Compare
valassi
left a comment
There was a problem hiding this comment.
Thanks @hageboeck
The main thing I suggest to change is to remove all appearance of devMakeUnique on c++ rathe than define a no-op one.
This morning we had discussed having hstMakeUnique and hstMakeUniquePinned, which are still not there. Initially I commented that we should have them, but then I took it back. So it's ok to leave these as they are, sorry for some hesitations...
Then the other are minor suggestions, but have a look, eg alphabetically ordered headers, why not memcpy, and add a printout on cudadevicereset.
Thanks! Andrea
c0ee68c to
ae58f23
Compare
BUG FIX in PR #47: arrays were uninitialised in RAII constructors
I wanted to test if memory / resource management can be automated. With unique_ptr and a few helper functions, a lot of
ifdefanddelete[] / frees can be removed. Is this helpful for us?