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, &amp;jac_nnz, &amp;jac_rind, &amp;jac_cind, &amp;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">&lt;<a href="mailto:kshitij@math.upb.de">kshitij@math.upb.de</a>&gt;</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&#39;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>
&gt; Hello everyone,<br>
&gt;<br>
&gt; Using valgrind I found a bunch of memory leaks with the Sparse Jacobian<br>
&gt; features in ADOL-C. I noticed in the mailing list archives this problem<br>
&gt; has been previously reported; The leaks still persist after I upgraded<br>
&gt; to ADOL-C 2.2.0 and ColPack 1.0.3 today (the recommended action).<br>
&gt;<br>
&gt; Below is an excerpt of the valgrind report.<br>
&gt;<br>
&gt; ==21732==<br>
&gt; ==21732== HEAP SUMMARY:<br>
&gt; ==21732==     in use at exit: 29,627,156 bytes in 157,000 blocks<br>
&gt; ==21732==   total heap usage: 229,529,359 allocs, 229,372,359 frees,<br>
&gt; 762,459,196,535 bytes allocated<br>
&gt; ==21732==<br>
&gt; ==21732== 45 bytes in 1 blocks are possibly lost in loss record 2 of 105<br>
&gt; ==21732==    at 0x4C28B42: operator new(unsigned long)<br>
&gt; (vg_replace_malloc.c:261)<br>
&gt; ==21732==    by 0x5413E6C: std::string::_Rep::_S_create(unsigned long,<br>
&gt; unsigned long, std::allocator&lt;char&gt; const&amp;) (in<br>
&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
&gt; ==21732==    by 0x5415904: char* std::string::_S_construct&lt;char<br>
&gt; const*&gt;(char const*, char const*, std::allocator&lt;char&gt; const&amp;,<br>
&gt; std::forward_iterator_tag) (in<br>
&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
&gt; ==21732==    by 0x5415DF1: std::basic_string&lt;char,<br>
&gt; std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char<br>
&gt; const*, std::allocator&lt;char&gt; const&amp;) (in<br>
&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
&gt; ==21732==    by 0x4C3DEF:<br>
&gt; ColPack::BipartiteGraphPartialOrdering::ColumnSmallestLastOrdering() (in<br>
&gt; /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
&gt; ==21732==    by 0x4C4BB7:<br>
&gt; ColPack::BipartiteGraphPartialOrdering::OrderVertices(std::string,<br>
&gt; std::string) (in<br>
&gt; /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
&gt; ==21732==    by 0x47BEC8:<br>
&gt; ColPack::BipartiteGraphPartialColoringInterface::PartialDistanceTwoColoring(std::string,<br>
&gt; std::string) (in<br>
&gt; /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
&gt; ==21732==    by 0x47C9B3:<br>
&gt; ColPack::BipartiteGraphPartialColoringInterface::GenerateSeedJacobian(double***,<br>
&gt; int*, int*, std::string, std::string) (in<br>
&gt; /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
&gt; ==21732==    by 0x516721A: sparse_jac (sparsedrivers.cpp:314)<br>
&gt; ==21732==    by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
&gt; ==21732==    by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
&gt; ==21732==    by 0x466228: cvStgrNlsNewton (cvodes.c:6561)<br>
&gt; ==21732==<br>
&gt; ==21732== 48 bytes in 1 blocks are possibly lost in loss record 3 of 105<br>
&gt; ==21732==    at 0x4C28FAC: malloc (vg_replace_malloc.c:236)<br>
&gt; ==21732==    by 0x515A679: indopro_forward_safe (uni5_for.c:1341)<br>
&gt; ==21732==    by 0x5166016: jac_pat (sparsedrivers.cpp:91)<br>
&gt; ==21732==    by 0x51670BF: sparse_jac (sparsedrivers.cpp:268)<br>
&gt; ==21732==    by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
&gt; ==21732==    by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
&gt; ==21732==    by 0x465C10: cvNlsNewton (cvodes.c:6117)<br>
&gt; ==21732==    by 0x467D88: cvStep (cvodes.c:5873)<br>
&gt; ==21732==    by 0x469EA9: CVode (cvodes.c:3029)<br>
&gt; ==21732==    by 0x41442E: eval_f_c(double*, double*, double*)<br>
&gt; (ss_model.c:207)<br>
&gt; ==21732==    by 0x4117BA: main (ssnew.c:146)<br>
&gt; ==21732==<br>
&gt; ==21732== 52 bytes in 1 blocks are possibly lost in loss record 4 of 105<br>
&gt; ==21732==    at 0x4C28B42: operator new(unsigned long)<br>
&gt; (vg_replace_malloc.c:261)<br>
&gt; ==21732==    by 0x5413E6C: std::string::_Rep::_S_create(unsigned long,<br>
&gt; unsigned long, std::allocator&lt;char&gt; const&amp;) (in<br>
&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
&gt; ==21732==    by 0x5415904: char* std::string::_S_construct&lt;char<br>
&gt; const*&gt;(char const*, char const*, std::allocator&lt;char&gt; const&amp;,<br>
&gt; std::forward_iterator_tag) (in<br>
&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
&gt; ==21732==    by 0x5415DF1: std::basic_string&lt;char,<br>
&gt; std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char<br>
&gt; const*, std::allocator&lt;char&gt; const&amp;) (in<br>
&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14)<br>
&gt; ==21732==    by 0x4C7FE6:<br>
&gt; ColPack::BipartiteGraphPartialColoring::PartialDistanceTwoColumnColoring()<br>
&gt; (in /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
&gt; ==21732==    by 0x47C0D7:<br>
&gt; ColPack::BipartiteGraphPartialColoringInterface::PartialDistanceTwoColoring(std::string,<br>
&gt; std::string) (in<br>
&gt; /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
&gt; ==21732==    by 0x47C9B3:<br>
&gt; ColPack::BipartiteGraphPartialColoringInterface::GenerateSeedJacobian(double***,<br>
&gt; int*, int*, std::string, std::string) (in<br>
&gt; /home/awdowlin/Documents/svn/research/ssPowerOptimTightBounds/debug)<br>
&gt; ==21732==    by 0x516721A: sparse_jac (sparsedrivers.cpp:314)<br>
&gt; ==21732==    by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
&gt; ==21732==    by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
&gt; ==21732==    by 0x466228: cvStgrNlsNewton (cvodes.c:6561)<br>
&gt; ==21732==    by 0x46979C: cvStep (cvodes.c:6406)<br>
&gt;<br>
&gt; Note: Errors 5 through 105 are virtually the same.<br>
&gt;<br>
&gt; ==21732== 4,838,562 (95,232 direct, 4,743,330 indirect) bytes in 186<br>
&gt; blocks are definitely lost in loss record 105 of 105<br>
&gt; ==21732==    at 0x4C28B42: operator new(unsigned long)<br>
&gt; (vg_replace_malloc.c:261)<br>
&gt; ==21732==    by 0x516715C: sparse_jac (sparsedrivers.cpp:302)<br>
&gt; ==21732==    by 0x411BE5: Jac(int, double, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _DlsMat*, void*, _generic_N_Vector*,<br>
&gt; _generic_N_Vector*, _generic_N_Vector*) (ss_model.c:364)<br>
&gt; ==21732==    by 0x47606D: cvDenseSetup (cvodes_dense.c:269)<br>
&gt; ==21732==    by 0x465C10: cvNlsNewton (cvodes.c:6117)<br>
&gt; ==21732==    by 0x467D88: cvStep (cvodes.c:5873)<br>
&gt; ==21732==    by 0x469EA9: CVode (cvodes.c:3029)<br>
&gt; ==21732==    by 0x41442E: eval_f_c(double*, double*, double*)<br>
&gt; (ss_model.c:207)<br>
&gt; ==21732==    by 0x4117BA: main (ssnew.c:146)<br>
&gt; ==21732==<br>
&gt; ==21732== LEAK SUMMARY:<br>
&gt; ==21732==    definitely lost: 2,101,704 bytes in 3,135 blocks<br>
&gt; ==21732==    indirectly lost: 27,442,563 bytes in 153,610 blocks<br>
&gt; ==21732==      possibly lost: 35,785 bytes in 6 blocks<br>
&gt; ==21732==    still reachable: 47,104 bytes in 249 blocks<br>
&gt; ==21732==         suppressed: 0 bytes in 0 blocks<br>
&gt; ==21732== Reachable blocks (those to which a pointer was found) are not<br>
&gt; shown.<br>
&gt; ==21732== To see them, rerun with: --leak-check=full --show-reachable=yes<br>
&gt;<br>
&gt; main, eval_f_c and Jac all refer to function I (or my predecessor) have<br>
&gt; written. The functions beginning with cv belong to the numeric<br>
&gt; integrator CVODES. ADOL-C is called in the function Jac.<br>
&gt;<br>
&gt; Are there any recommendations on resolving this issue? I&#39;m happy to<br>
&gt; help, but my C/C++ is a bit rusty.<br>
&gt;<br>
&gt; Alex Dowling<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div></div>&gt; _______________________________________________<br>
&gt; ADOL-C mailing list<br>
&gt; <a href="mailto:ADOL-C@list.coin-or.org">ADOL-C@list.coin-or.org</a><br>
&gt; <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>