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

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

adolc_mat_mul.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: Matrix Multiplication</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Adolc Speed: Matrix Multiplication"/>
<meta name="keywords" id="keywords" content=" adolc speed: matrix multiplication link_mat_mul speed multiply 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_mat_mul.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_det_lu.cpp.xml" target="_top">Prev</a>
</td><td><a href="adolc_ode.cpp.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_mat_mul.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_mat_mul.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: Matrix Multiplication</big></big></b></center>
<br/>
<b><big><a name="Specifications" id="Specifications">Specifications</a></big></b>
<br/>
See <a href="link_mat_mul.xml" target="_top"><span style='white-space: nowrap'>link_mat_mul</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;cppad/vector.hpp&gt;
# include &lt;cppad/speed/mat_sum_sq.hpp&gt;
# include &lt;cppad/speed/uniform_01.hpp&gt;
# include &lt;cppad/vector.hpp&gt;

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

bool link_mat_mul(
	size_t                           size     , 
	size_t                           repeat   , 
	CppAD::vector&lt;double&gt;&amp;           x        ,
	CppAD::vector&lt;double&gt;&amp;           z        ,
	CppAD::vector&lt;double&gt;&amp;           dz       )
{
	// speed test global option values
	if( global_memory || global_atomic || global_optimize )
		return false;
	// -----------------------------------------------------
	// setup
	typedef adouble    ADScalar;
	typedef ADScalar*  ADVector;

	int tag  = 0;         // tape identifier
	int m    = 1;         // number of dependent variables
	int n    = size*size; // number of independent variables
	double f;             // function value
	int j;                // temporary index

	// 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

	// AD domain space vector
	ADVector X = thread_alloc::create_array&lt;ADScalar&gt;(size_t(n), capacity);

	// Product matrix
	ADVector Y = thread_alloc::create_array&lt;ADScalar&gt;(size_t(n), capacity);

	// AD range space vector
	ADVector Z = thread_alloc::create_array&lt;ADScalar&gt;(size_t(m), capacity);

	// vector with matrix value
	double* mat = thread_alloc::create_array&lt;double&gt;(size_t(n), capacity);

	// vector of reverse mode weights
	double* u  = thread_alloc::create_array&lt;double&gt;(size_t(m), capacity);
	u[0] = 1.;

	// gradient
	double* grad = thread_alloc::create_array&lt;double&gt;(size_t(n), capacity);

	// ----------------------------------------------------------------------
	if( ! global_onetape ) while(repeat--)
	{	// choose a matrix
		CppAD::uniform_01(n, mat);

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

		// do computations
		CppAD::mat_sum_sq(size, X, Y, Z);

		// create function object f : X -&gt; Z
		Z[0] &gt;&gt;= f;
		trace_off();

		// evaluate and return gradient using reverse mode
		fos_reverse(tag, m, n, u, grad);
	}
	else
	{	// choose a matrix
		CppAD::uniform_01(n, mat);

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

		// do computations
		CppAD::mat_sum_sq(size, X, Y, Z);

		// create function object f : X -&gt; Z
		Z[0] &gt;&gt;= f;
		trace_off();

		while(repeat--)
		{	// choose a matrix
			CppAD::uniform_01(n, mat);

			// evaluate the determinant at the new matrix value
			keep = 1; // keep this forward mode result
			zos_forward(tag, m, n, keep, mat, &amp;f); 

			// evaluate and return gradient using reverse mode
			fos_reverse(tag, m, n, u, grad);
		}
	}
	// return function, matrix, and gradient
	z[0] = f;
	for(j = 0; j &lt; n; j++)
	{	x[j]  = mat[j];
		dz[j] = grad[j];
	}

	// tear down
	thread_alloc::delete_array(X);
	thread_alloc::delete_array(Y);
	thread_alloc::delete_array(Z);
	thread_alloc::delete_array(mat);
	thread_alloc::delete_array(u);
	thread_alloc::delete_array(grad);

	return true;
}


</pre></font></code>


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

</body>
</html>