<?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 Parallel Pthread Example and Test</title>
<meta name="description" id="description" content="A Simple Parallel Pthread Example and Test"/>
<meta name="keywords" id="keywords" content=" Openmp example A.1.1c thread "/>
<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='_a11c_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="a11c_bthread.cpp.xml" target="_top">Prev</a>
</td><td><a href="simple_ad_openmp.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>a11c_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>multi_thread</option>
<option>library</option>
<option>cppad_ipopt_nlp</option>
<option>Example</option>
<option>preprocessor</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>a11c_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 Parallel Pthread Example and Test</big></big></b></center>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
This example just demonstrates pthreads and does not use CppAD at all.

<br/>
<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;pthread.h&gt;
# include &lt;limits&gt;
# include &lt;cmath&gt;
# include &lt;cassert&gt;
# define NUMBER_THREADS 4

# ifdef NDEBUG
# define CHECK_ZERO(expression) expression
# else
# define CHECK_ZERO(expression) assert( expression == 0 );
# endif
namespace {
	// Beginning of Example A.1.1.1c of OpenMP 2.5 standard document ---------
	void a1(int n, float *a, float *b)
	{	int i;
		// for some reason this function is missing on some systems
		// assert( pthread_is_multithreaded_np() &gt; 0 );
		for(i = 1; i &lt; n; i++) 
			b[i] = (a[i] + a[i-1]) / 2.0;
		return;
	}
	// End of Example A.1.1.1c of OpenMP 2.5 standard document ---------------
	struct start_arg { int  n; float* a; float* b; };
	void* start_routine(void* arg_vptr)
	{	start_arg* arg = static_cast&lt;start_arg*&gt;( arg_vptr );
		a1(arg-&gt;n, arg-&gt;a, arg-&gt;b);

		void* no_status = 0;
		pthread_exit(no_status);

		return no_status;
	}
}

bool a11c(void)
{	bool ok = true;

	// Test setup
	int i, j, n_total = 10;
	float *a = new float[n_total];
	float *b = new float[n_total];
	for(i = 0; i &lt; n_total; i++)
		a[i] = float(i);

	// number of threads
	int n_thread = NUMBER_THREADS;
	// the threads
	pthread_t thread[NUMBER_THREADS];
 	// arguments to start_routine
	struct start_arg arg[NUMBER_THREADS];
	// attr
	pthread_attr_t attr;
	CHECK_ZERO( pthread_attr_init( &amp;attr ) );
	CHECK_ZERO( pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE) );
	//
	// Break the work up into sub work for each thread
	int n = n_total / n_thread;
	arg[0].n = n;
	arg[0].a = a;
	arg[0].b = b;
	for(j = 1; j &lt; n_thread; j++)
	{	arg[j].n = n + 1;
		arg[j].a = arg[j-1].a + n - 1;
		arg[j].b = arg[j-1].b + n - 1;
		if( j == (n_thread - 1) )
			arg[j].n = n_total - j * n + 1;
	}
	for(j = 0; j &lt; n_thread; j++)
	{	// inform each thread of which block it is working on
		void* arg_vptr = static_cast&lt;void*&gt;( &amp;arg[j] );
		CHECK_ZERO( pthread_create(
			&amp;thread[j], &amp;attr, start_routine, arg_vptr
		) );
	}
	for(j = 0; j &lt; n_thread; j++)
	{	void* no_status = 0;
		CHECK_ZERO( pthread_join(thread[j], &amp;no_status) );
	}

	// check the result
	float eps = 100. * std::numeric_limits&lt;float&gt;::epsilon();
	for(i = 1; i &lt; n ; i++)
		ok &amp;= std::fabs( (2. * b[i] - a[i] - a[i-1]) / b[i] ) &lt;= eps; 

	delete [] a;
	delete [] b;

	return ok;
}
</pre>

</font></code>

<hr/>Input File: multi_thread/pthread/a11c_pthread.cpp

</body>
</html>

