<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Timing Test of Multi-Threaded Summation of 1/i</title>
<meta name="description" id="description" content="Timing Test of Multi-Threaded Summation of 1/i"/>
<meta name="keywords" id="keywords" content=" harmonic_time summation multi_thread speed time "/>
<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_time.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.cpp.xml" target="_top">Prev</a>
</td><td><a href="harmonic_work.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_time.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_time.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Syntax</option>
<option>Purpose</option>
<option>ok</option>
<option>time_out</option>
<option>test_time</option>
<option>num_threads</option>
<option>mega_sum</option>
<option>Source</option>
</select>
</td>
</tr></table><br/>









<center><b><big><big>Timing Test of Multi-Threaded Summation of 1/i</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_time(</span></font><i><font color="black"><span style='white-space: nowrap'>time_out</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'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>mega_sum</span></font></i><font color="blue"><span style='white-space: nowrap'>)</span></font></code>


<br/>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
Runs a correctness and timing test for a multi-threaded 
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>

This routine must be called in sequential execution mode,
even though <a href="ta_in_parallel.xml" target="_top"><span style='white-space: nowrap'>in_parallel</span></a>
 may return true.

<br/>
<br/>
<b><big><a name="ok" id="ok">ok</a></big></b>
<br/>
This return value has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;bool&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>ok</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
If it is true,
<code><font color="blue">harmonic_time</font></code> passed the correctness test.
Otherwise it is false.

<br/>
<br/>
<b><big><a name="time_out" id="time_out">time_out</a></big></b>
<br/>
This argument has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;double&amp;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>time_out</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
The input value of the argument does not matter.
Upon return it is the number of wall clock seconds required for
to compute the 
<a href="harmonic_time.cpp.xml#Purpose" target="_top"><span style='white-space: nowrap'>summation</span></a>
.

<br/>
<br/>
<b><big><a name="test_time" id="test_time">test_time</a></big></b>
<br/>
Is the minimum amount of wall clock time that the test should take.
The number of repeats for the test will be increased until this time
is reached. 
The reported 
<code><i><font color="black"><span style='white-space: nowrap'>time_out</span></font></i></code>
 is the total wall clock time divided by the
number of repeats.

<br/>
<br/>
<b><big><a name="num_threads" id="num_threads">num_threads</a></big></b>
<br/>
This argument 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 are available for this test.
If it is zero, the test is run without the multi-threading environment and

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;1&#xA0;==&#xA0;CppAD::thread_alloc::num_threads()<br/>
</span></font></code>
when <code><font color="blue">harmonic_time</font></code> is called.
If it is non-zero, the test is run with the multi-threading and

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#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'>&#xA0;=&#xA0;CppAD::thread_alloc::num_threads()<br/>
</span></font></code>
when <code><font color="blue">harmonic_time</font></code> is called.

<br/>
<br/>
<b><big><a name="mega_sum" id="mega_sum">mega_sum</a></big></b>
<br/>
This argument has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;size_t&amp;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>mega_sum</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
and is greater than zero.
The value 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>n</mi>
</mrow></math>

 in the 
<a href="harmonic_time.cpp.xml#Purpose" target="_top"><span style='white-space: nowrap'>summation</span></a>
.
is equal to 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msup><mn>10</mn>
<mn>6</mn>
</msup>
</mrow></math>

 times 
<code><i><font color="black"><span style='white-space: nowrap'>mega_sum</span></font></i></code>
. 

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

<code><font color="blue">
<br/>
<pre style='display:inline'> 
# include &lt;omp.h&gt;
# include &lt;cstring&gt;
# include &lt;limits&gt;
# include &lt;vector&gt;
# include &lt;iostream&gt;
# include &lt;cstdlib&gt;

// Note there is no mention of parallel mode in the documentation for
// speed_test (so it is safe to use without special consideration).
# include &lt;cppad/time_test.hpp&gt;
# include &quot;harmonic.hpp&quot;

namespace { // empty namespace

	// value of num_threads in previous call to harmonic_time.
	size_t num_threads_;

	// value of mega_sum in previous call to harmonic_time.
	size_t mega_sum_;

	// value of sum resulting from most recent call to test_once
	double sum_ = 0.;

	void test_once(void)
	{	if( mega_sum_ &lt; 1 )
		{	std::cerr &lt;&lt; &quot;harmonic_time: mega_sum &lt; 1&quot; &lt;&lt; std::endl;
			exit(1);
		}
		size_t num_sum = mega_sum_ * 1000000;
		bool ok = harmonic(sum_, num_sum, num_threads_); 
		if( ! ok )
		{	std::cerr &lt;&lt; &quot;harmonic: error&quot; &lt;&lt; std::endl;
			exit(1);
		}
		return;
	}

	void test_repeat(size_t repeat)
	{	size_t i;
		for(i = 0; i &lt; repeat; i++)
			test_once();
		return;
	}
} // end empty namespace

bool harmonic_time(
	double&amp; time_out, double test_time, size_t num_threads, size_t mega_sum)
{	bool ok  = true;
	using std::vector;

	// arguments passed to harmonic_time
	num_threads_ = num_threads;
	mega_sum_    = mega_sum;

	// convert zero to actual number of threads
	num_threads  = std::max(num_threads_, size_t(1));

	// expect number of threads to already be set up
	ok &amp;= num_threads == CppAD::thread_alloc::num_threads();

	// run the test case and set the time return value
	time_out = CppAD::time_test(test_repeat, test_time);

	// Correctness check
	double eps   = mega_sum_ * 1e3 * std::numeric_limits&lt;double&gt;::epsilon();
	size_t i     = mega_sum_ * 1000000;
	double check = 0.;
	while(i)
		check += 1. / double(i--); 
	ok &amp;= std::fabs(sum_ - check) &lt;= eps;

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
