<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Controlling Taylor Coefficient Memory Allocation: Example and Test</title>
<meta name="description" id="description" content="Controlling Taylor Coefficient Memory Allocation: Example and Test"/>
<meta name="keywords" id="keywords" content=" capacity_order example "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_capacity_order.cpp_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="capacity_order.xml" target="_top">Prev</a>
</td><td><a href="number_skip.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>ADFun</option>
<option>FunEval</option>
<option>Forward</option>
<option>capacity_order</option>
<option>capacity_order.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>FunEval-&gt;</option>
<option>Forward</option>
<option>Reverse</option>
<option>Sparse</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>Forward-&gt;</option>
<option>forward_zero</option>
<option>forward_one</option>
<option>forward_two</option>
<option>forward_order</option>
<option>size_order</option>
<option>CompareChange</option>
<option>capacity_order</option>
<option>number_skip</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>capacity_order-&gt;</option>
<option>capacity_order.cpp</option>
</select>
</td>
<td>capacity_order.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Controlling Taylor Coefficient Memory Allocation: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;
	
namespace {
	bool test(void)
	{	bool ok = true;
		using CppAD::AD;
		using CppAD::NearEqual;
		using CppAD::thread_alloc;
	
		// domain space vector
		size_t n(1), m(1);
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) ax(n), ay(n);
	
		// declare independent variables and start tape recording
		ax[0]  = 1.0;
		CppAD::<a href="independent.xml" target="_top">Independent</a>(ax);
	
		// Set y = x^3, use enough variables so more that the minimal amount 
		// of memory is allocated for Taylor coefficients
		ay[0] = 0.;
		for( size_t i = 0; i &lt; 10; i++)
			ay[0] += ax[0] * ax[0] * ax[0];
		ay[0] = ay[0] / 10.;
	
		// create f: x -&gt; y and stop tape recording
		// (without running zero order forward mode).
		CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f;
		f.Dependent(ax, ay); 

		// check that this is master thread
		size_t thread = thread_alloc::thread_num();
		ok           &amp;= thread == 0; // this should be master thread

		// The highest order forward mode calculation below is first order.
		// This corresponds to two Taylor coefficient per variable,direction
		// (orders zero and one). Preallocate memory for speed.
		size_t inuse  = thread_alloc::inuse(thread);
		f.capacity_order(2);
		ok &amp;= thread_alloc::inuse(thread) &gt; inuse;

		// zero order forward mode
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x(n), y(m);
		x[0] = 0.5;
		y    = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
		double eps = 10. * CppAD::numeric_limits&lt;double&gt;::epsilon();
		ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y[0], x[0] * x[0] * x[0], eps, eps);
	
		// forward computation of partials w.r.t. x
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n), dy(m);
		dx[0] = 1.;
		dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
		ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], 3. * x[0] * x[0], eps, eps);
	
		// Suppose we no longer need the first order Taylor coefficients.
		inuse = thread_alloc::inuse(thread);
		f.capacity_order(1); // just keep zero order coefficients
		ok   &amp;= thread_alloc::inuse(thread) &lt; inuse;
	
		// Suppose we no longer need the zero order Taylor coefficients
		// (could have done this first and not used f.capacity_order(1)).
		inuse = thread_alloc::inuse(thread);
		f.capacity_order(0);
		ok   &amp;= thread_alloc::inuse(thread) &lt; inuse;
	
		// turn off memory holding
		thread_alloc::hold_memory(false);
	
		return ok;
	}
}
bool capacity_order(void)
{	bool ok = true;
	using CppAD::thread_alloc;

	// original amount of memory inuse
	size_t thread = thread_alloc::thread_num();
	ok           &amp;= thread == 0; // this should be master thread
	size_t inuse  = thread_alloc::inuse(thread);

	// do test in separate routine so all objects are destroyed
	ok &amp;= test();

	// check that the amount of memroy inuse has not changed
	ok &amp;= thread_alloc::inuse(thread) == inuse;

	// Test above uses hold_memory, so return available memory 
	thread_alloc::free_available(thread); 

	return ok;
}

</pre>

</font></code>


<hr/>Input File: example/capacity_order.cpp

</body>
</html>
