[CppAD] multithreading

Brad Bell bradbell at seanet.com
Fri Mar 2 09:00:10 EST 2012


Note that on the web page
http://www.coin-or.org/CppAD/Doc/multi_thread.xml
the following text appears:

"Extra steps and care must be taken to use CppAD  in parallel execution 
mode. This section collects this information in one place."

The example
http://www.coin-or.org/CppAD/Doc/team_pthread.cpp.xml
is intended to be an example of how to connect ptheads to CppAD. 
Unfortunately this example is more complicated than necessary because it 
allows for reusing threads for multiple operations.

When each thread is only usde once, and then a join is used to continue 
the main program, a simpler example is possible. I plan create such a 
simple example and add it to the CppAD multi-threading documentation.

Thanks for your question which points out the need for a simpler example.

Brad


On 02/27/2012 01:27 AM, Dominik Skanda wrote:
> Hello,
>
> I try to use CppAD in multi threading mode using posix threads.
> I don't want to tape parallel I only want to construct an ADFun Object
> and use copies of this ADFun object in several threads, whereby each
> copy of an ADFun object gets used in only one thread.
> I also try to copy the ADFun object within a thread.
> I have not found out how to resolve this operation by use of the
> documentation.
> I have added a short example (small as possible) so that it is hopefully
> clear what I'm trying to explain.
> I compiled it with: "g++ main.cpp -I./ -g -lpthread"
>
> The program performs but by using
>    "valgrind --tool=helgrind ./a.out"
> I get a lot of errors in the context of multi threading.
>
> Can someone help me to set it up correctly?
>
> Many thanks
>
> Dominik
>
>
> main.cpp:
>
> #include<pthread.h>
> #include<iostream>
> #include<vector>
> #include<cppad/cppad.hpp>
>
> using namespace std;
>
> pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
>
>
>
> struct THREAD_DATA{
> 	CppAD::ADFun<double>  fun;
> 	int threadNumber;
> };
>
>   void *start_CppAD(void *data)
> {
> 	int loop=10;
> 	vector<  double>  x(2);
> 	x[0]=3.78;
>      x[1]=7.33;
>
>      int i;
>
>      vector<  double>  y(1);
> 	y[0]=0.0;
>
>      for (i=0;i<loop;i++)
> 		{
> 			
> 			// perform CppAD_FUN_OBJECT FORWARD0
>
> 			y=((THREAD_DATA*)data)->fun.Forward(0,x);
> 			
> 			
> 			pthread_mutex_lock(&mutex);
> 			cout<<  i<<  " THREAD: "<<  ((THREAD_DATA*)data)->  threadNumber<<  " RESULT FORWARD 0: "<<  y[0]<<  endl;
> 			pthread_mutex_unlock(&mutex);
> 			sleep(1);
> 		}
>
>
> 	for (i=0;i<loop;i++)
> 		{
> 			// perform CppAD_FUN_OBJECT FORWARD1
> 			
> 			y=((THREAD_DATA*)data)->fun.Forward(1,x);
>              // ((THREAD_DATA*)data)->fun.capacity_taylor(1);
>
> 			pthread_mutex_lock(&mutex);
> 			cout<<  i<<  "THREAD: "<<  ((THREAD_DATA*)data)->  threadNumber<<  " RESULT FORWARD 1: "<<  y[0]<<  endl;
> 			pthread_mutex_unlock(&mutex);
> 			sleep(1);	
> 		}
>
>
>      // copy CppAD_FUN_OBJECT
>
>      CppAD::ADFun<double>  copyFun;
>      copyFun=((THREAD_DATA*)data)->fun;
>
>      y=copyFun.Forward(0,x);
>
>      pthread_mutex_lock(&mutex);
>      cout<<  "THREAD: "<<  ((THREAD_DATA*)data)->  threadNumber<<  " RESULT COPY FUN FORWARD 0: "<<  y[0]<<  endl;
>      pthread_mutex_unlock(&mutex);
>
>      y=copyFun.Forward(1,x);
>
>      pthread_mutex_lock(&mutex);
>      cout<<  "THREAD: "<<  ((THREAD_DATA*)data)->  threadNumber<<  " RESULT COPY FUN FORWARD 1: "<<  y[0]<<  endl;
>      pthread_mutex_unlock(&mutex);
>
> 	return NULL;
> }
>
>
>
> int main (int argc, char *argv[])
> {
> 	
>    // TAPING THE FUNCTION 	
> 	
>    vector<  CppAD::AD<double>  >  x(2);
>    x[0]=3.78;
>    x[1]=7.33;
>    CppAD::Independent(x);
>    vector<  CppAD::AD<double>  >  y(1);
>    y[0]=sin(x[0])+cos(x[1]);
>
>    CppAD::ADFun<double>  fun;
>    fun.Dependent(x,y);
>    fun.optimize();
>
>
>    // creating data for posix threads
>
>    struct THREAD_DATA dataThread1, dataThread2;
>
>    dataThread1.fun=fun;
>    dataThread1.threadNumber=1;
>    dataThread2.fun=fun;
>    dataThread2.threadNumber=2;
>
>    // creating threads
>
>    pthread_t executionThread[2];
>
>    int status;
>
>    // executing threads
>
>    status=pthread_create (&(executionThread[0]),NULL,start_CppAD,&dataThread1);
>    status=pthread_create (&(executionThread[1]),NULL,start_CppAD,&dataThread2);
>
>    // joining threads
>
>    status=pthread_join((executionThread[0]),NULL);
>    status=pthread_join((executionThread[1]),NULL);
>
>     return 0;
> }
>
>
>
>
> _______________________________________________
> CppAD mailing list
> CppAD at list.coin-or.org
> http://list.coin-or.org/mailman/listinfo/cppad
>



More information about the CppAD mailing list