<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Multi-threading Sum of 1/i Utility Routines</title>
<meta name="description" id="description" content="Multi-threading Sum of 1/i Utility Routines"/>
<meta name="keywords" id="keywords" content=" harmonic_work harmonic_setup harmonic_worker harmonic_combine setup harmonic worker combine "/>
<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='_harmonic_work.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="harmonic_time.cpp.xml" target="_top">Prev</a>
</td><td><a href="multi_newton.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>harmonic.cpp</option>
<option>harmonic_work.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>multi_thread-&gt;</option>
<option>parallel_ad</option>
<option>thread_test.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down2(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>
<select onchange='choose_down1(this)'>
<option>harmonic.cpp-&gt;</option>
<option>harmonic_time.cpp</option>
<option>harmonic_work.cpp</option>
</select>
</td>
<td>harmonic_work.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Syntax</option>
<option>Purpose</option>
<option>harmonic_setup</option>
<option>---..num_sum</option>
<option>---..num_threads</option>
<option>harmonic_worker</option>
<option>harmonic_combine</option>
<option>Source</option>
</select>
</td>
</tr></table><br/>




<center><b><big><big>Multi-threading Sum of 1/i Utility Routines</big></big></b></center>
<br/>
<b><big><a name="Syntax" id="Syntax">Syntax</a></big></b>

<br/>

<code><i><font color="black"><span style='white-space: nowrap'>ok</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;=&#xA0;harmonic_setup(</span></font><i><font color="black"><span style='white-space: nowrap'>num_sum</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>num_threads</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>

<code><font color="blue"><span style='white-space: nowrap'>harmonic_worker()<br/>
</span></font></code>

<code><i><font color="black"><span style='white-space: nowrap'>ok</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;=&#xA0;harmonic_combine(</span></font><i><font color="black"><span style='white-space: nowrap'>sum</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
These routines aid in the multi-threading computation of
the summation that defines the harmonic series

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mn>1</mn>
<mo stretchy="false">+</mo>
<mn>1</mn>
<mo stretchy="false">/</mo>
<mn>2</mn>
<mo stretchy="false">+</mo>
<mn>1</mn>
<mo stretchy="false">/</mo>
<mn>3</mn>
<mo stretchy="false">+</mo>
<mo stretchy="false">.</mo>
<mo stretchy="false">.</mo>
<mo stretchy="false">.</mo>
<mo stretchy="false">+</mo>
<mn>1</mn>
<mo stretchy="false">/</mo>
<mi mathvariant='italic'>n</mi>
</mrow></math>

<br/>
<b><big><a name="harmonic_setup" id="harmonic_setup">harmonic_setup</a></big></b>
<br/>
Calling this function splits up the computation of the summation
into different parts for each thread.

<br/>
<br/>
<b><a name="harmonic_setup.num_sum" id="harmonic_setup.num_sum">num_sum</a></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>num_sum</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;size_t&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>num_sum</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It specifies the value of 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>n</mi>
</mrow></math>

 in the summation.

<br/>
<br/>
<b><a name="harmonic_setup.num_threads" id="harmonic_setup.num_threads">num_threads</a></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>num_threads</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;size_t&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>num_threads</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It specifies the number of threads that will be used for the summation.
In the special case where 
<code><i><font color="black"><span style='white-space: nowrap'>num_thread</span></font></i></code>
 is zero,
the summation should be done by one thread, but without the overhead
of the multi_threading system.

<br/>
<br/>
<b><big><a name="harmonic_worker" id="harmonic_worker">harmonic_worker</a></big></b>
<br/>
Calling this function does the computation for one thread.
Following a call to <code><font color="blue">harmonic_setup</font></code>,
this function should be called by each of the 
<code><i><font color="black"><span style='white-space: nowrap'>num_threads</span></font></i></code>
 threads. 

<br/>
<br/>
<b><big><a name="harmonic_combine" id="harmonic_combine">harmonic_combine</a></big></b>
<br/>
After the 
<code><i><font color="black"><span style='white-space: nowrap'>num_threads</span></font></i></code>
 threads have completed their
calls to <code><font color="blue">harmonic_worker</font></code>,
this function call will combine the results and return the final summation.

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

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

namespace {
	using CppAD::thread_alloc;

	// number of threads specified by previous call to harmonic_setup
	size_t num_threads_ = 0;

	// structure with information for one thread
	typedef struct {
		// index to start summation at (worker input)
		size_t start;
		// index to end summation at (worker input)
		size_t stop;
		// summation for this thread (worker output)
		double sum;
		// 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_[MAX_NUMBER_THREADS];
}
// -----------------------------------------------------------------------
// do the work for one thread
void harmonic_worker(void)
{	// sum =  1/(stop-1) + 1/(stop-2) + ... + 1/start
	size_t thread_num  = thread_alloc::thread_num();
	size_t num_threads = std::max(num_threads_, size_t(1));
	bool   ok          = thread_num &lt; num_threads;
	size_t start       = work_all_[thread_num]-&gt;start;
	size_t stop        = work_all_[thread_num]-&gt;stop;
	double sum         = 0.;

	ok &amp;= stop &gt; start;
	size_t i = stop;
	while( i &gt; start )
	{	i--;
		sum += 1. / double(i);	
	}

	work_all_[thread_num]-&gt;sum = sum;
	work_all_[thread_num]-&gt;ok  = ok;
}
// -----------------------------------------------------------------------
// setup the work up for multiple threads
bool harmonic_setup(size_t num_sum, size_t num_threads)
{	// sum = 1/num_sum + 1/(num_sum-1) + ... + 1
	num_threads_ = num_threads;
	num_threads  = std::max(num_threads_, size_t(1));
	bool ok      = num_threads == thread_alloc::num_threads();
	ok          &amp;= num_sum &gt;= num_threads;

	size_t thread_num;
	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);
		// in case this thread's worker does not get called
		work_all_[thread_num]-&gt;ok = false;
		// parameters that define the work for this and previous thread
		if( thread_num == 0 )
			work_all_[0]-&gt;start = 1;
		else
		{	size_t index  = (num_sum * thread_num) / num_threads;
			work_all_[thread_num-1]-&gt;stop = index; 
			work_all_[thread_num]-&gt;start  = index;
		}
	}
	work_all_[num_threads-1]-&gt;stop = num_sum + 1;
	return ok;
}
// -----------------------------------------------------------------------
// get the result of the work 
bool harmonic_combine(double&amp; sum)
{	// sum = 1/num_sum + 1/(num_sum-1) + ... + 1
	bool ok            = true;
	size_t num_threads = std::max(num_threads_, size_t(1));
	sum                = 0.;

	// go down so that free memory for other threads before memory for master
	size_t thread_num = num_threads;
	while(thread_num--)
	{	// check that this tread was ok with the work it did
		ok  &amp;= work_all_[thread_num]-&gt;ok;
		// add this threads contribution to the sum
		sum += work_all_[thread_num]-&gt;sum;
		// 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/harmonic_work.cpp

</body>
</html>
