<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>A Simple pthread AD: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="A Simple pthread AD: Example and Test"/>
<meta name="keywords" id="keywords" content=" simple pthread ad: example test Ad purpose 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='_simple_ad_pthread.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_bthread.cpp.xml" target="_top">Prev</a>
</td><td><a href="team_example.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>simple_ad_pthread.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>utility</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_atomic.cpp</option>
<option>multi_newton.cpp</option>
<option>team_thread.hpp</option>
</select>
</td>
<td>simple_ad_pthread.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
<option>Source Code</option>
</select>
</td>
</tr></table><br/>
<center><b><big><big>A Simple pthread AD: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
This example demonstrates how CppAD can be used in a
pthread multi-threading environment.

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

<code><font color="blue">
<pre><tt>
<span style="font-weight: bold"><span style="color: #000080"># include</span></span> <span style="color: #FF0000">&lt;cppad/cppad.hpp&gt;</span>
<span style="font-weight: bold"><span style="color: #000080"># include</span></span> <span style="color: #FF0000">&lt;pthread.h&gt;</span>
<span style="font-weight: bold"><span style="color: #000080"># define</span></span> NUMBER_THREADS  <span style="color: #993399">4</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> <span style="color: #FF0000">{</span>
     <span style="font-style: italic"><span style="color: #9A1900">// structure with problem specific information</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #FF0000">{</span>
          <span style="font-style: italic"><span style="color: #9A1900">// function argument (worker input)</span></span>
          <span style="color: #009900">double</span>          x<span style="color: #990000">;</span>
          <span style="font-style: italic"><span style="color: #9A1900">// This structure would also have return information in it,</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// but this example only returns the ok flag</span></span>
     <span style="color: #FF0000">}</span> problem_specific<span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">// =====================================================================</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// General purpose code you can copy to your application</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// =====================================================================</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">using</span></span> CppAD<span style="color: #990000">::</span>thread_alloc<span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">// ------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// key for accessing thread specific information</span></span>
     <span style="color: #008080">pthread_key_t</span> thread_specific_key_<span style="color: #990000">;</span>

     <span style="font-style: italic"><span style="color: #9A1900">// no need to destroy thread specific information</span></span>
     <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">thread_specific_destructor</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">*</span> thread_num_vptr<span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span> <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// Are we in sequential mode; i.e., other threads are waiting for</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// master thread to set up next job ?</span></span>
     <span style="color: #009900">bool</span> sequential_execution_ <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

     <span style="font-style: italic"><span style="color: #9A1900">// used to inform CppAD when we are in parallel execution mode</span></span>
     <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">in_parallel</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #990000">!</span> sequential_execution_<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// used to inform CppAD of current thread number thread_number()</span></span>
     <span style="color: #008080">size_t</span> <span style="font-weight: bold"><span style="color: #000000">thread_number</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// get thread specific information</span></span>
          <span style="color: #009900">void</span><span style="color: #990000">*</span>   thread_num_vptr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">pthread_getspecific</span></span><span style="color: #990000">(</span>thread_specific_key_<span style="color: #990000">);</span>
          size_t<span style="color: #990000">*</span> thread_num_ptr  <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">static_cast</span></span><span style="color: #990000">&lt;</span>size_t<span style="color: #990000">*&gt;(</span>thread_num_vptr<span style="color: #990000">);</span>
          <span style="color: #008080">size_t</span>  thread_num      <span style="color: #990000">=</span> <span style="color: #990000">*</span>thread_num_ptr<span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> thread_num<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">// ---------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// structure with information for one thread</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #0000FF">struct</span></span> <span style="color: #FF0000">{</span>
          <span style="font-style: italic"><span style="color: #9A1900">// number for this thread (thread specific points here)</span></span>
          <span style="color: #008080">size_t</span>            thread_num<span style="color: #990000">;</span>
          <span style="font-style: italic"><span style="color: #9A1900">// pthread unique identifier for this thread</span></span>
          <span style="color: #008080">pthread_t</span>         pthread_id<span style="color: #990000">;</span>
          <span style="font-style: italic"><span style="color: #9A1900">// false if an error occurs, true otherwise</span></span>
          <span style="color: #009900">bool</span>              ok<span style="color: #990000">;</span>
          <span style="font-style: italic"><span style="color: #9A1900">// pointer to problem specific information</span></span>
          problem_specific<span style="color: #990000">*</span> info<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span> thread_one_t<span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">// vector with information for all threads</span></span>
     <span style="color: #008080">thread_one_t</span> thread_all_<span style="color: #990000">[</span>NUMBER_THREADS<span style="color: #990000">];</span>
     <span style="font-style: italic"><span style="color: #9A1900">// --------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// function that initializes the thread and then calls the actual worker</span></span>
     <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">worker</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> thread_num<span style="color: #990000">,</span> problem_specific<span style="color: #990000">*</span> info<span style="color: #990000">);</span>
     <span style="color: #009900">void</span><span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #000000">run_one_worker</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">*</span> thread_num_vptr<span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="color: #009900">bool</span> ok <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// thread_num for this thread</span></span>
          <span style="color: #008080">size_t</span> thread_num <span style="color: #990000">=</span> <span style="color: #990000">*</span><span style="font-weight: bold"><span style="color: #0000FF">static_cast</span></span><span style="color: #990000">&lt;</span>size_t<span style="color: #990000">*&gt;(</span>thread_num_vptr<span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// The master thread should call worker directly</span></span>
          ok <span style="color: #990000">&amp;=</span> thread_num <span style="color: #990000">!=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// This is not the master thread, so thread specific infromation</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// has not yet been set. We use it to inform other routines</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// of this threads number.</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// We must do this before calling thread_alloc::thread_num().</span></span>
          <span style="color: #009900">int</span> rc <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">pthread_setspecific</span></span><span style="color: #990000">(</span>
               thread_specific_key_<span style="color: #990000">,</span>
               thread_num_vptr
          <span style="color: #990000">);</span>
          ok <span style="color: #990000">&amp;=</span> rc <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// check the value of thread_alloc::thread_num().</span></span>
          ok <span style="color: #990000">=</span> thread_num <span style="color: #990000">==</span> thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">thread_num</span></span><span style="color: #990000">();</span>

          <span style="font-style: italic"><span style="color: #9A1900">// Now do the work</span></span>
          ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">worker</span></span><span style="color: #990000">(</span>thread_num<span style="color: #990000">,</span> thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>info<span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// pass back ok information for this thread</span></span>
          thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>ok <span style="color: #990000">=</span> ok<span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// no return value</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> CPPAD_NULL<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">// --------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// function that calls all the workers</span></span>
     <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">run_all_workers</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> num_threads<span style="color: #990000">,</span> problem_specific<span style="color: #990000">*</span> info_all<span style="color: #990000">[])</span>
     <span style="color: #FF0000">{</span>     <span style="color: #009900">bool</span> ok <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// initialize thread_all_ (execpt for pthread_id)</span></span>
          <span style="color: #008080">size_t</span> thread_num<span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>thread_num <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> thread_num <span style="color: #990000">&lt;</span> num_threads<span style="color: #990000">;</span> thread_num<span style="color: #990000">++)</span>
          <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// pointed to by thread specific info for this thread</span></span>
               thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>thread_num <span style="color: #990000">=</span> thread_num<span style="color: #990000">;</span>
               <span style="font-style: italic"><span style="color: #9A1900">// initialize as false to make sure worker gets called by other</span></span>
               <span style="font-style: italic"><span style="color: #9A1900">// threads. Note that thread_all_[0].ok does not get used</span></span>
               thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>ok         <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>
               <span style="font-style: italic"><span style="color: #9A1900">// problem specific information</span></span>
               thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>info       <span style="color: #990000">=</span> info_all<span style="color: #990000">[</span>thread_num<span style="color: #990000">];</span>
          <span style="color: #FF0000">}</span>

          <span style="font-style: italic"><span style="color: #9A1900">// master pthread_id</span></span>
          thread_all_<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>pthread_id <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">pthread_self</span></span><span style="color: #990000">();</span>

          <span style="font-style: italic"><span style="color: #9A1900">// error flag for calls to pthread library</span></span>
          <span style="color: #009900">int</span> rc<span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// create a key for thread specific information</span></span>
          rc <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">pthread_key_create</span></span><span style="color: #990000">(</span>
               <span style="color: #990000">&amp;</span>thread_specific_key_<span style="color: #990000">,</span> thread_specific_destructor
          <span style="color: #990000">);</span>
          ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>rc <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// set thread specific information for this (master thread)</span></span>
          <span style="color: #009900">void</span><span style="color: #990000">*</span> thread_num_vptr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">static_cast</span></span><span style="color: #990000">&lt;</span><span style="color: #009900">void</span><span style="color: #990000">*&gt;(</span>
               <span style="color: #990000">&amp;(</span>thread_all_<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>thread_num<span style="color: #990000">)</span>
          <span style="color: #990000">);</span>
          rc <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">pthread_setspecific</span></span><span style="color: #990000">(</span>thread_specific_key_<span style="color: #990000">,</span> thread_num_vptr<span style="color: #990000">);</span>
          ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>rc <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// Now thread_number() has necessary information for this thread</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// (number zero), and while still in sequential mode,</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// call setup for using CppAD::AD&lt;double&gt; in parallel mode.</span></span>
          thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">parallel_setup</span></span><span style="color: #990000">(</span>
               num_threads<span style="color: #990000">,</span> in_parallel<span style="color: #990000">,</span> thread_number
          <span style="color: #990000">);</span>
          thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">hold_memory</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>
          CppAD<span style="color: #990000">::</span>parallel_ad<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;();</span>

          <span style="font-style: italic"><span style="color: #9A1900">// inform CppAD that we now may be in parallel execution mode</span></span>
          sequential_execution_ <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// structure used to create the threads</span></span>
          <span style="color: #008080">pthread_t</span>       pthread_id<span style="color: #990000">;</span>
          <span style="font-style: italic"><span style="color: #9A1900">// default for pthread_attr_setdetachstate is PTHREAD_CREATE_JOINABLE</span></span>
          pthread_attr_t<span style="color: #990000">*</span> no_attr<span style="color: #990000">=</span> CPPAD_NULL<span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// This master thread is already running, we need to create</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// num_threads - 1 more threads</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>thread_num <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> thread_num <span style="color: #990000">&lt;</span> num_threads<span style="color: #990000">;</span> thread_num<span style="color: #990000">++)</span>
          <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// Create the thread with thread number equal to thread_num</span></span>
               thread_num_vptr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">static_cast</span></span><span style="color: #990000">&lt;</span><span style="color: #009900">void</span><span style="color: #990000">*&gt;</span> <span style="color: #990000">(</span>
                    <span style="color: #990000">&amp;(</span>thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>thread_num<span style="color: #990000">)</span>
               <span style="color: #990000">);</span>
               rc <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">pthread_create</span></span><span style="color: #990000">(</span>
                         <span style="color: #990000">&amp;</span>pthread_id <span style="color: #990000">,</span>
                         no_attr     <span style="color: #990000">,</span>
                         run_one_worker<span style="color: #990000">,</span>
                         thread_num_vptr
               <span style="color: #990000">);</span>
               thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>pthread_id <span style="color: #990000">=</span> pthread_id<span style="color: #990000">;</span>
               ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>rc <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
          <span style="color: #FF0000">}</span>

          <span style="font-style: italic"><span style="color: #9A1900">// now call worker for the master thread</span></span>
          thread_num <span style="color: #990000">=</span> thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">thread_num</span></span><span style="color: #990000">();</span>
          ok <span style="color: #990000">&amp;=</span> thread_num <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
          ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">worker</span></span><span style="color: #990000">(</span>thread_num<span style="color: #990000">,</span> thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>info<span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// now wait for the other threads to finish</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>thread_num <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> thread_num <span style="color: #990000">&lt;</span> num_threads<span style="color: #990000">;</span> thread_num<span style="color: #990000">++)</span>
          <span style="color: #FF0000">{</span>     <span style="color: #009900">void</span><span style="color: #990000">*</span> no_status <span style="color: #990000">=</span> CPPAD_NULL<span style="color: #990000">;</span>
               rc      <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">pthread_join</span></span><span style="color: #990000">(</span>
                    thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>pthread_id<span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>no_status
               <span style="color: #990000">);</span>
               ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>rc <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">);</span>
          <span style="color: #FF0000">}</span>

          <span style="font-style: italic"><span style="color: #9A1900">// Inform CppAD that we now are definately back to sequential mode</span></span>
          sequential_execution_ <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// now inform CppAD that there is only one thread</span></span>
          thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">parallel_setup</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> CPPAD_NULL<span style="color: #990000">,</span> CPPAD_NULL<span style="color: #990000">);</span>
          thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">hold_memory</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">);</span>
          CppAD<span style="color: #990000">::</span>parallel_ad<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;();</span>

          <span style="font-style: italic"><span style="color: #9A1900">// destroy the key for thread specific data</span></span>
          <span style="font-weight: bold"><span style="color: #000000">pthread_key_delete</span></span><span style="color: #990000">(</span>thread_specific_key_<span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// check to ok flag returned by during calls to work by other threads</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>thread_num <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> thread_num <span style="color: #990000">&lt;</span> num_threads<span style="color: #990000">;</span> thread_num<span style="color: #990000">++)</span>
               ok <span style="color: #990000">&amp;=</span> thread_all_<span style="color: #990000">[</span>thread_num<span style="color: #990000">].</span>ok<span style="color: #990000">;</span>

          <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ok<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">// =====================================================================</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// End of General purpose code</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// =====================================================================</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// function that does the work for one thread</span></span>
     <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">worker</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> thread_num<span style="color: #990000">,</span> problem_specific<span style="color: #990000">*</span> info<span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="color: #009900">bool</span> ok <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

          <span style="font-style: italic"><span style="color: #9A1900">// CppAD::vector uses the CppAD fast multi-threading allocator</span></span>
          CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt; CppAD::AD&lt;double&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">ax</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">ay</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
          ax<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> info<span style="color: #990000">-&gt;</span>x<span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #000000">Independent</span></span><span style="color: #990000">(</span>ax<span style="color: #990000">);</span>
          ay<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">sqrt</span></span><span style="color: #990000">(</span> ax<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> ax<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">);</span>
          CppAD<span style="color: #990000">::</span><span style="color: #008080">ADFun&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">f</span></span><span style="color: #990000">(</span>ax<span style="color: #990000">,</span> ay<span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// Check function value corresponds to the identity</span></span>
          <span style="color: #009900">double</span> eps <span style="color: #990000">=</span> <span style="color: #993399">10</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> CppAD<span style="color: #990000">::</span>numeric_limits<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;::</span><span style="font-weight: bold"><span style="color: #000000">epsilon</span></span><span style="color: #990000">();</span>
          ok        <span style="color: #990000">&amp;=</span> CppAD<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">NearEqual</span></span><span style="color: #990000">(</span>ay<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> ax<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> eps<span style="color: #990000">,</span> eps<span style="color: #990000">);</span>

          <span style="font-style: italic"><span style="color: #9A1900">// Check derivative value corresponds to the identity.</span></span>
          CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">d_x</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">d_y</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
          d_x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
          d_y    <span style="color: #990000">=</span> f<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Forward</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> d_x<span style="color: #990000">);</span>
          ok    <span style="color: #990000">&amp;=</span> CppAD<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">NearEqual</span></span><span style="color: #990000">(</span>d_x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #993399">1</span><span style="color: #990000">.,</span> eps<span style="color: #990000">,</span> eps<span style="color: #990000">);</span>

          <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ok<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>
<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">simple_ad</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>     <span style="color: #009900">bool</span> ok <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
     <span style="color: #008080">size_t</span> num_threads <span style="color: #990000">=</span> NUMBER_THREADS<span style="color: #990000">;</span>

     <span style="font-style: italic"><span style="color: #9A1900">// Check that no memory is in use or avialable at start</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// (using thread_alloc in sequential mode)</span></span>
     <span style="color: #008080">size_t</span> thread_num<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>thread_num <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> thread_num <span style="color: #990000">&lt;</span> num_threads<span style="color: #990000">;</span> thread_num<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     ok <span style="color: #990000">&amp;=</span> thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">inuse</span></span><span style="color: #990000">(</span>thread_num<span style="color: #990000">)</span> <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
          ok <span style="color: #990000">&amp;=</span> thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">available</span></span><span style="color: #990000">(</span>thread_num<span style="color: #990000">)</span> <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// initialize info_all</span></span>
     <span style="color: #008080">problem_specific</span> <span style="color: #990000">*</span>info<span style="color: #990000">,</span> <span style="color: #990000">*</span>info_all<span style="color: #990000">[</span>NUMBER_THREADS<span style="color: #990000">];</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>thread_num <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> thread_num <span style="color: #990000">&lt;</span> num_threads<span style="color: #990000">;</span> thread_num<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// problem specific information</span></span>
          <span style="color: #008080">size_t</span> <span style="font-weight: bold"><span style="color: #000000">min_bytes</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">sizeof</span></span><span style="color: #990000">(</span>info<span style="color: #990000">)),</span> cap_bytes<span style="color: #990000">;</span>
          <span style="color: #009900">void</span><span style="color: #990000">*</span>  v_ptr <span style="color: #990000">=</span> thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">get_memory</span></span><span style="color: #990000">(</span>min_bytes<span style="color: #990000">,</span> cap_bytes<span style="color: #990000">);</span>
          info         <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">static_cast</span></span><span style="color: #990000">&lt;</span>problem_specific<span style="color: #990000">*&gt;(</span>v_ptr<span style="color: #990000">);</span>
          info<span style="color: #990000">-&gt;</span>x      <span style="color: #990000">=</span> <span style="color: #009900">double</span><span style="color: #990000">(</span>thread_num<span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
          info_all<span style="color: #990000">[</span>thread_num<span style="color: #990000">]</span> <span style="color: #990000">=</span> info<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>

     ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">run_all_workers</span></span><span style="color: #990000">(</span>num_threads<span style="color: #990000">,</span> info_all<span style="color: #990000">);</span>

     <span style="font-style: italic"><span style="color: #9A1900">// go down so that free memory for other threads before memory for master</span></span>
     thread_num <span style="color: #990000">=</span> num_threads<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">while</span></span><span style="color: #990000">(</span>thread_num<span style="color: #990000">--)</span>
     <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// delete problem specific information</span></span>
          <span style="color: #009900">void</span><span style="color: #990000">*</span> v_ptr <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">static_cast</span></span><span style="color: #990000">&lt;</span><span style="color: #009900">void</span><span style="color: #990000">*&gt;(</span> info_all<span style="color: #990000">[</span>thread_num<span style="color: #990000">]</span> <span style="color: #990000">);</span>
          thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">return_memory</span></span><span style="color: #990000">(</span> v_ptr <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">// check that there is no longer any memory inuse by this thread</span></span>
          ok <span style="color: #990000">&amp;=</span> thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">inuse</span></span><span style="color: #990000">(</span>thread_num<span style="color: #990000">)</span> <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
          <span style="font-style: italic"><span style="color: #9A1900">// return all memory being held for future use by this thread</span></span>
          thread_alloc<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">free_available</span></span><span style="color: #990000">(</span>thread_num<span style="color: #990000">);</span>
     <span style="color: #FF0000">}</span>

     <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ok<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre>
</font></code>
<hr/>Input File: example/multi_thread/pthread/simple_ad_pthread.cpp

</body>
</html>
