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

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

team_example.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>Using a Team of AD Threads: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Using a Team of AD Threads: Example and Test"/>
<meta name="keywords" id="keywords" content=" using a team of ad threads: example and test thread Ad purpose thread_team source code "/>
<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='_team_example.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="simple_ad_pthread.cpp.xml" target="_top">Prev</a>
</td><td><a href="harmonic.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>multi_thread</option>
<option>thread_test.cpp</option>
<option>team_example.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>multi_thread-&gt;</option>
<option>parallel_ad</option>
<option>thread_test.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>thread_test.cpp-&gt;</option>
<option>a11c_openmp.cpp</option>
<option>a11c_bthread.cpp</option>
<option>a11c_pthread.cpp</option>
<option>simple_ad_openmp.cpp</option>
<option>simple_ad_bthread.cpp</option>
<option>simple_ad_pthread.cpp</option>
<option>team_example.cpp</option>
<option>harmonic.cpp</option>
<option>multi_newton.cpp</option>
<option>team_thread.hpp</option>
</select>
</td>
<td>team_example.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
<option>thread_team</option>
<option>Source Code</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Using a Team of AD Threads: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
This example demonstrates how use a team threads with CppAD.

<br/>
<br/>
<b><big><a name="thread_team" id="thread_team">thread_team</a></big></b>
<br/>
The following three implementations of the 
<a href="team_thread.hpp.xml" target="_top"><span style='white-space: nowrap'>team_thread.hpp</span></a>
 specifications are included:
<table><tr><td align='left'  valign='top'>

<a href="team_openmp.cpp.xml" target="_top">team_openmp.cpp</a></td><td>
OpenMP Implementation of a Team of AD Threads</td></tr>
<tr><td>

<a href="team_bthread.cpp.xml" target="_top">team_bthread.cpp</a></td><td>
Boost Thread Implementation of a Team of AD Threads</td></tr>
<tr><td>

<a href="team_pthread.cpp.xml" target="_top">team_pthread.cpp</a></td><td>
Pthread Implementation of a Team of AD Threads</td></tr>
<tr><td>

</td></tr>
</table>
<br/>
<b><big><a name="Source Code" id="Source Code">Source Code</a></big></b>

<code><font color="blue">
<br/>
<pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;
# include &quot;team_thread.hpp&quot;
# define NUMBER_THREADS  4

namespace {
	using CppAD::thread_alloc;

	// structure with information for one thread
	typedef struct {
		// function argument (worker input)
		double          x;
		// false if an error occurs, true otherwise (worker output)
		bool            ok;
	} work_one_t;
	// vector with information for all threads
	// (use pointers instead of values to avoid false sharing)
	work_one_t* work_all_[NUMBER_THREADS];
	// --------------------------------------------------------------------
	// function that does the work for one thread
	void worker(void)
	{	using CppAD::NearEqual;
		using CppAD::AD;
		bool ok = true;
		size_t thread_num = thread_alloc::thread_num();

		// CppAD::vector uses the CppAD fast multi-threading allocator
		CppAD::vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; ax(1), ay(1);
		ax[0] = work_all_[thread_num]-&gt;x;
		<a href="independent.xml" target="_top">Independent</a>(ax);
		ay[0] = sqrt( ax[0] * ax[0] );
		CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(ax, ay); 

		// Check function value corresponds to the identity 
		double eps = 10. * CppAD::numeric_limits&lt;double&gt;::epsilon();
		ok        &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(ay[0], ax[0], eps, eps);

		// Check derivative value corresponds to the identity.
		CppAD::vector&lt;double&gt; d_x(1), d_y(1);
		d_x[0] = 1.;
		d_y    = f.<a href="forward.xml" target="_top">Forward</a>(1, d_x);
		ok    &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(d_x[0], 1., eps, eps);

		// pass back ok information for this thread
		work_all_[thread_num]-&gt;ok = ok;
	}
}

// This test routine is only called by the master thread (thread_num = 0).
bool team_example(void)
{	bool ok = true;

	size_t num_threads = NUMBER_THREADS;

	// Check that no memory is in use or avialable at start
	// (using thread_alloc in sequential mode)
	size_t thread_num;
	for(thread_num = 0; thread_num &lt; num_threads; thread_num++)
	{	ok &amp;= thread_alloc::inuse(thread_num) == 0; 
		ok &amp;= thread_alloc::available(thread_num) == 0; 
	}

	// initialize work_all_
	for(thread_num = 0; thread_num &lt; num_threads; thread_num++)
	{	// allocate separate memory for this thread to avoid false sharing
		size_t min_bytes(sizeof(work_one_t)), cap_bytes;
		void*  v_ptr = thread_alloc::get_memory(min_bytes, cap_bytes);
		work_all_[thread_num]     = static_cast&lt;work_one_t*&gt;(v_ptr);
		// incase this thread's worker does not get called
		work_all_[thread_num]-&gt;ok = false;
		// parameter that defines the work for this thread 
		work_all_[thread_num]-&gt;x  = double(thread_num) + 1.;
	}

	ok &amp;= team_create(num_threads);
	ok &amp;= team_work(worker);
	ok &amp;= team_destroy();

	// go down so that free memrory for other threads before memory for master
	thread_num = num_threads;
	while(thread_num--)
	{	// check that this thread was ok with the work it did
		ok &amp;= work_all_[thread_num]-&gt;ok;
		// delete problem specific information 
		void* v_ptr = static_cast&lt;void*&gt;( work_all_[thread_num] );
		thread_alloc::return_memory( v_ptr );
		// check that there is no longer any memory inuse by this thread
		// (for general applications, the master might still be using memory)
		ok &amp;= thread_alloc::inuse(thread_num) == 0;
		// return all memory being held for future use by this thread
		thread_alloc::free_available(thread_num);
	}
	return ok;
}
</pre>

</font></code>


<hr/>Input File: multi_thread/team_example.cpp

</body>
</html>