Codebase list cppad / upstream/2015.00.00.7 doc / adolc_sparse_jacobian.cpp.xml
upstream/2015.00.00.7

Tree @upstream/2015.00.00.7 (Download .tar.gz)

adolc_sparse_jacobian.cpp.xml @upstream/2015.00.00.7raw · history · blame

<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>adolc Speed: Sparse Jacobian</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="adolc Speed: Sparse Jacobian"/>
<meta name="keywords" id="keywords" content=" adolc speed: sparse jacobian link_sparse_jacobian speed specifications implementation "/>
<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='_adolc_sparse_jacobian.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="adolc_sparse_hessian.cpp.xml" target="_top">Prev</a>
</td><td><a href="adolc_alloc_mat.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>speed</option>
<option>speed_adolc</option>
<option>adolc_sparse_jacobian.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>speed-&gt;</option>
<option>speed_main</option>
<option>speed_utility</option>
<option>speed_double</option>
<option>speed_adolc</option>
<option>speed_cppad</option>
<option>speed_fadbad</option>
<option>speed_sacado</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>speed_adolc-&gt;</option>
<option>adolc_det_minor.cpp</option>
<option>adolc_det_lu.cpp</option>
<option>adolc_mat_mul.cpp</option>
<option>adolc_ode.cpp</option>
<option>adolc_poly.cpp</option>
<option>adolc_sparse_hessian.cpp</option>
<option>adolc_sparse_jacobian.cpp</option>
<option>adolc_alloc_mat</option>
</select>
</td>
<td>adolc_sparse_jacobian.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Specifications</option>
<option>Implementation</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>adolc Speed: Sparse Jacobian</big></big></b></center>
<br/>
<b><big><a name="Specifications" id="Specifications">Specifications</a></big></b>
<br/>
See <a href="link_sparse_jacobian.xml" target="_top"><span style='white-space: nowrap'>link_sparse_jacobian</span></a>
.

<br/>
<br/>
<b><big><a name="Implementation" id="Implementation">Implementation</a></big></b>


<code><font color='blue'><pre style='display:inline'> 
# include &lt;adolc/adolc.h&gt;
# include &lt;adolc/adolc_sparse.h&gt;
# include &lt;cppad/vector.hpp&gt;
# include &lt;cppad/speed/uniform_01.hpp&gt;
# include &lt;cppad/speed/sparse_jac_fun.hpp&gt;

// list of possible options
extern bool global_memory, global_onetape, global_atomic, global_optimize;
extern bool global_colpack, global_boolsparsity;

bool link_sparse_jacobian(
	size_t                           size     , 
	size_t                           repeat   , 
	size_t                           m        ,
	const CppAD::vector&lt;size_t&gt;&amp;     row      ,
	const CppAD::vector&lt;size_t&gt;&amp;     col      ,
	      CppAD::vector&lt;double&gt;&amp;     x_return ,
	      CppAD::vector&lt;double&gt;&amp;     jacobian ,
	      size_t&amp;                    n_sweep  )
{
	if( global_atomic || (! global_colpack) )
		return false; 
	if( global_memory || global_optimize )
		return false; 
	// -----------------------------------------------------
	// setup
	typedef unsigned int*    SizeVector;
	typedef double*          DblVector;
	typedef adouble          ADScalar;
	typedef ADScalar*        ADVector;

	size_t i, j;                // temporary indices
	size_t n = size;            // number of independent variables
	size_t order = 0;          // derivative order corresponding to function

	// set up for thread_alloc memory allocator (fast and checks for leaks)
	using CppAD::thread_alloc; // the allocator
	size_t capacity;           // capacity of an allocation

	// tape identifier
	int tag  = 0;
	// AD domain space vector
	ADVector a_x = thread_alloc::create_array&lt;ADScalar&gt;(n, capacity);
	// AD range space vector
	ADVector a_y = thread_alloc::create_array&lt;ADScalar&gt;(m, capacity);
	// argument value in double
	DblVector x = thread_alloc::create_array&lt;double&gt;(n, capacity);
	// function value in double
	DblVector y = thread_alloc::create_array&lt;double&gt;(m, capacity);

	
	// options that control sparse_jac
	int        options[4];
	extern bool global_boolsparsity;
	if( global_boolsparsity )
		options[0] = 1;  // sparsity by propagation of bit pattern
	else
		options[0] = 0;  // sparsity pattern by index domains
	options[1] = 0; // (0 = safe mode, 1 = tight mode)
	options[2] = 0; // see changing to -1 and back to 0 below
	options[3] = 0; // (0 = column compression, 1 = row compression)

	// structure that holds some of the work done by sparse_jac
	int        nnz;                   // number of non-zero values
	SizeVector rind   = CPPAD_NULL;   // row indices
	SizeVector cind   = CPPAD_NULL;   // column indices
	DblVector  values = CPPAD_NULL;   // Jacobian values

	// initialize all entries as zero
	for(i = 0; i &lt; m; i++)
	{	for(j = 0; j &lt; n; j++)
			jacobian[ i * n + j ] = 0.;
	}

	// choose a value for x
	CppAD::uniform_01(n, x);

	// declare independent variables
	int keep = 0; // keep forward mode results 
	trace_on(tag, keep);
	for(j = 0; j &lt; n; j++)
		a_x[j] &lt;&lt;= x[j];

	// AD computation of f (x) 
	CppAD::sparse_jac_fun&lt;ADScalar&gt;(m, n, a_x, row, col, order, a_y);

	// create function object f : x -&gt; y
	for(i = 0; i &lt; m; i++)
		a_y[i] &gt;&gt;= y[i];
	trace_off();

	// Retrieve n_sweep using undocumented feature of sparsedrivers.cpp
	int same_pattern = 0;
	options[2]       = -1;
	n_sweep = sparse_jac(tag, int(m), int(n), 
		same_pattern, x, &amp;nnz, &amp;rind, &amp;cind, &amp;values, options
	);
	options[2]       = 0;
	// ----------------------------------------------------------------------
	if( ! global_onetape ) while(repeat--)
	{	// choose a value for x
		CppAD::uniform_01(n, x);

		// declare independent variables
		trace_on(tag, keep);
		for(j = 0; j &lt; n; j++)
			a_x[j] &lt;&lt;= x[j];

		// AD computation of f (x) 
		CppAD::sparse_jac_fun&lt;ADScalar&gt;(m, n, a_x, row, col, order, a_y);

		// create function object f : x -&gt; y 
		for(i = 0; i &lt; m; i++)
			a_y[i] &gt;&gt;= y[i];
		trace_off();

		// is this a repeat call with the same sparsity pattern
		same_pattern = 0;

		// calculate the jacobian at this x
		rind   = CPPAD_NULL;
		cind   = CPPAD_NULL;
		values = CPPAD_NULL;
		sparse_jac(tag, int(m), int(n), 
			same_pattern, x, &amp;nnz, &amp;rind, &amp;cind, &amp;values, options
		);
		int int_n = int(n);
		for(int k = 0; k &lt; nnz; k++)
			jacobian[ rind[k] * int_n + cind[k] ] = values[k];

		// free raw memory allocated by sparse_jac
		free(rind);
		free(cind);
		free(values);
	}
	else
	{	while(repeat--)
		{	// choose a value for x
			CppAD::uniform_01(n, x);

			// calculate the jacobian at this x
			sparse_jac(tag, int(m), int(n), 
				same_pattern, x, &amp;nnz, &amp;rind, &amp;cind, &amp;values, options
			);
			same_pattern = 1;
		}
		int int_n = int(n);
		for(int k = 0; k &lt; nnz; k++)
			jacobian[ rind[k] * int_n + cind[k] ] = values[k];

		// free raw memory allocated by sparse_jac
		free(rind);
		free(cind);
		free(values);
	}
	// --------------------------------------------------------------------
	// return argument 
	for(j = 0; j &lt; n; j++)
		x_return[j] = x[j];

	// tear down
	thread_alloc::delete_array(a_x);
	thread_alloc::delete_array(a_y);
	thread_alloc::delete_array(x);
	thread_alloc::delete_array(y);
	return true;
}
</pre></font></code>


<hr/>Input File: speed/adolc/sparse_jacobian.cpp

</body>
</html>