Kshitij,<div><br></div><div>Thank you for the prompt response.</div><div><br></div><div>Memory (de)allocation in Jac(...) is based on the sparse Jacobian example included with ADOL-C. Below is a simplified version:</div><div>
<br></div><div>int Jac(...)</div><div>{</div><div><br></div><div>// Declare some variables not seen by ADOL-C</div><div><div><br></div><div><div>double *ud;</div><div>double *dud;</div></div><div><br></div><div>// Allocate memory for x and p</div>
<div>double *x = myalloc(NEQ+6);</div><div>double *p = myalloc(6);</div></div><div><br></div><div>// Populate ud and dud</div><div>// Code not shown in e-mail</div><div><br></div><div><div>int jac_nnz = 0;</div><div>int repeat = 0;</div>
<div>int options[4];</div><div> </div><div>static unsigned int *jac_rind = NULL;</div><div>static unsigned int *jac_cind = NULL;</div><div>static double *jac_values = NULL;</div><div><br></div><div>options[0] = 0;</div>
<div>options[1] = 0;</div><div>options[2] = 0;</div><div>options[3] = 0;</div></div><div><br></div><div>// Copy values from the inputs into x and p</div><div>// Code not shown in e-mail</div><div><br></div><div>// ADOL-C tape</div>
<div>tracerhs(t,x,dud,p);</div><div><br></div><div>// Calculate sparse Jacobian - Note: NEQ is a constant</div><div>rc = sparse_jac(1, NEQ, NEQ+6, repeat, x, &jac_nnz, &jac_rind, &jac_cind, &jac_values, options);</div>
<div><br></div><div>// Copy values from jac_values into an input variable to this function</div><div>// Code not shown in e-mail</div><div><br></div><div><div>myfree(x);</div><div>myfree(p);</div><div> </div><div>free(jac_rind);</div>
<div>free(jac_cind);</div><div>free(jac_values);</div></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br></div><div>}</div><div><br></div><div>Is this the correct way to free the pointers used by ADOL-C?</div>
<div><br></div><div>I compiled everything with ColPack debug flag active. Valgrind is running right now; it will take a while...</div><div><br></div><div>Alex Dowling<br><br><div class="gmail_quote">On Wed, Jun 1, 2011 at 7:13 AM, Kshitij Kulshreshtha <span dir="ltr"><<a href="mailto:kshitij@math.upb.de">kshitij@math.upb.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hello Alex,<br>
<br>
ADOL-C (and also ColPack) follows the rule that any pointer that is the<br>
output of an ADOL-C function like jac_pat or sparse_jac, which gets<br>
allocated by ADOL-C but is returned to the user's code, should be<br>
deallocated by the user when he does not need it anymore.<br>
<br>
ADOL-C will not delete pointers that were given out to the user at any<br>
time. ADOL-C deallocates only the memory that it allocated internally<br>
and did not give out to the user.<br>
<br>
Please check that in your code all pointers that you get back from<br>
ADOL-C are deallocated by your code. Hopefully this should take care of<br>
loss records 3 and 105.<br>
<br>
I am not sure why you are getting so many string allocations. Strings<br>
are only used as constants inside ColPack as far as I see in the code.<br>
You should try to add -g in the FLAGS for building ColPack inside the<br>
<a href="http://common_variables.in" target="_blank">common_variables.in</a> file. This will enable line numbers inside ColPack<br>
code too, which will help debug better.<br>
<div><div></div><div class="h5"><br>
As on 2011-06-01 01:17, Alex Dowling did write:<br>
> Hello everyone,<br>
><br>
> Using valgrind I found a bunch of memory leaks with the Sparse Jacobian<br>
> features in ADOL-C. I noticed in the mailing list archives this problem<br>
> has been previously reported; The leaks still persist after I upgraded<br>
> to ADOL-C 2.2.0 and ColPack 1.0.3 today (the recommended action).<br>
><br>
> Below is an excerpt of the valgrind report.<br>
><br>
> ==21732==<br>
> ==21732== HEAP SUMMARY:<br>
> ==21732== in use at exit: 29,627,156 bytes in 157,000 blocks<br>
> ==21732== total heap usage: 229,529,359 allocs, 229,372,359 frees,<br>
> 762,459,196,535 bytes allocated<br>
> ==21732==<br>
> ==21732== 45 bytes in 1 blocks are possibly lost in loss record 2 of 105<br>
> ==21732== at 0x4C28B42: operator new(unsigned long)<br>
> (vg_replace_malloc.c:261)<br>
> ==21732== by 0x5413E6C: std::string::_Rep::_S_create(unsigned long,<br>
> unsigned long, std::allocator<char> const&) (in<br>
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
> ==21732== by 0x5415904: char* std::string::_S_construct<char<br>
> const*>(char const*, char const*, std::allocator<char> const&,<br>
> std::forward_iterator_tag) (in<br>
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
> ==21732== by 0x5415DF1: std::basic_string<char,<br>
> std::char_traits<char>, std::allocator<char> >::basic_string(char<br>
> const*, std::allocator<char> const&) (in<br>
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
> ==21732== by 0x4C3DEF:<br>
> ColPack::BipartiteGraphPartialOrdering::ColumnSmallestLastOrdering() (in<br>
> /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
> ==21732== by 0x4C4BB7:<br>
> ColPack::BipartiteGraphPartialOrdering::OrderVertices(std::string,<br>
> std::string) (in<br>
> /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
> ==21732== by 0x47BEC8:<br>
> ColPack::BipartiteGraphPartialColoringInterface::PartialDistanceTwoColoring(std::string,<br>
> std::string) (in<br>
> /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
> ==21732== by 0x47C9B3:<br>
> ColPack::BipartiteGraphPartialColoringInterface::GenerateSeedJacobian(double***,<br>
> int*, int*, std::string, std::string) (in<br>
> /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
> ==21732== by 0x516721A: sparse_jac (sparsedrivers.cpp:314)<br>
> ==21732== by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
> _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
> _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
> ==21732== by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
> ==21732== by 0x466228: cvStgrNlsNewton (cvodes.c:6561)<br>
> ==21732==<br>
> ==21732== 48 bytes in 1 blocks are possibly lost in loss record 3 of 105<br>
> ==21732== at 0x4C28FAC: malloc (vg_replace_malloc.c:236)<br>
> ==21732== by 0x515A679: indopro_forward_safe (uni5_for.c:1341)<br>
> ==21732== by 0x5166016: jac_pat (sparsedrivers.cpp:91)<br>
> ==21732== by 0x51670BF: sparse_jac (sparsedrivers.cpp:268)<br>
> ==21732== by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
> _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
> _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
> ==21732== by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
> ==21732== by 0x465C10: cvNlsNewton (cvodes.c:6117)<br>
> ==21732== by 0x467D88: cvStep (cvodes.c:5873)<br>
> ==21732== by 0x469EA9: CVode (cvodes.c:3029)<br>
> ==21732== by 0x41442E: eval_f_c(double*, double*, double*)<br>
> (ss_model.c:207)<br>
> ==21732== by 0x4117BA: main (ssnew.c:146)<br>
> ==21732==<br>
> ==21732== 52 bytes in 1 blocks are possibly lost in loss record 4 of 105<br>
> ==21732== at 0x4C28B42: operator new(unsigned long)<br>
> (vg_replace_malloc.c:261)<br>
> ==21732== by 0x5413E6C: std::string::_Rep::_S_create(unsigned long,<br>
> unsigned long, std::allocator<char> const&) (in<br>
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
> ==21732== by 0x5415904: char* std::string::_S_construct<char<br>
> const*>(char const*, char const*, std::allocator<char> const&,<br>
> std::forward_iterator_tag) (in<br>
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
> ==21732== by 0x5415DF1: std::basic_string<char,<br>
> std::char_traits<char>, std::allocator<char> >::basic_string(char<br>
> const*, std::allocator<char> const&) (in<br>
> /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
> ==21732== by 0x4C7FE6:<br>
> ColPack::BipartiteGraphPartialColoring::PartialDistanceTwoColumnColoring()<br>
> (in /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
> ==21732== by 0x47C0D7:<br>
> ColPack::BipartiteGraphPartialColoringInterface::PartialDistanceTwoColoring(std::string,<br>
> std::string) (in<br>
> /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
> ==21732== by 0x47C9B3:<br>
> ColPack::BipartiteGraphPartialColoringInterface::GenerateSeedJacobian(double***,<br>
> int*, int*, std::string, std::string) (in<br>
> /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
> ==21732== by 0x516721A: sparse_jac (sparsedrivers.cpp:314)<br>
> ==21732== by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
> _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
> _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
> ==21732== by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
> ==21732== by 0x466228: cvStgrNlsNewton (cvodes.c:6561)<br>
> ==21732== by 0x46979C: cvStep (cvodes.c:6406)<br>
><br>
> Note: Errors 5 through 105 are virtually the same.<br>
><br>
> ==21732== 4,838,562 (95,232 direct, 4,743,330 indirect) bytes in 186<br>
> blocks are definitely lost in loss record 105 of 105<br>
> ==21732== at 0x4C28B42: operator new(unsigned long)<br>
> (vg_replace_malloc.c:261)<br>
> ==21732== by 0x516715C: sparse_jac (sparsedrivers.cpp:302)<br>
> ==21732== by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
> _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
> _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
> ==21732== by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
> ==21732== by 0x465C10: cvNlsNewton (cvodes.c:6117)<br>
> ==21732== by 0x467D88: cvStep (cvodes.c:5873)<br>
> ==21732== by 0x469EA9: CVode (cvodes.c:3029)<br>
> ==21732== by 0x41442E: eval_f_c(double*, double*, double*)<br>
> (ss_model.c:207)<br>
> ==21732== by 0x4117BA: main (ssnew.c:146)<br>
> ==21732==<br>
> ==21732== LEAK SUMMARY:<br>
> ==21732== definitely lost: 2,101,704 bytes in 3,135 blocks<br>
> ==21732== indirectly lost: 27,442,563 bytes in 153,610 blocks<br>
> ==21732== possibly lost: 35,785 bytes in 6 blocks<br>
> ==21732== still reachable: 47,104 bytes in 249 blocks<br>
> ==21732== suppressed: 0 bytes in 0 blocks<br>
> ==21732== Reachable blocks (those to which a pointer was found) are not<br>
> shown.<br>
> ==21732== To see them, rerun with: --leak-check=full --show-reachable=yes<br>
><br>
> main, eval_f_c and Jac all refer to function I (or my predecessor) have<br>
> written. The functions beginning with cv belong to the numeric<br>
> integrator CVODES. ADOL-C is called in the function Jac.<br>
><br>
> Are there any recommendations on resolving this issue? I'm happy to<br>
> help, but my C/C++ is a bit rusty.<br>
><br>
> Alex Dowling<br>
><br>
><br>
><br>
</div></div>> _______________________________________________<br>
> ADOL-C mailing list<br>
> <a href="mailto:ADOL-C@list.coin-or.org">ADOL-C@list.coin-or.org</a><br>
> <a href="http://list.coin-or.org/mailman/listinfo/adol-c" target="_blank">http://list.coin-or.org/mailman/listinfo/adol-c</a><br>
<br>
--<br>
Dr. Kshitij Kulshreshtha<br>
<br>
Institut für Mathematik,<br>
Universität Paderborn,<br>
Warburger Straße 100,<br>
33098 Paderborn.<br>
<br>
Büro: A3.235<br>
<br>
Privatanschrift:<br>
Arnikaweg 62<br>
33100 Paderborn.<br>
</blockquote></div><br></div>