[CppAD] multithreading

Dominik Skanda Dominik.Skanda at biologie.uni-freiburg.de
Mon Feb 27 04:27:46 EST 2012


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;
}






More information about the CppAD mailing list