[CppAD] using CppAD threadsafe

Brad Bell bradbell at seanet.com
Fri Sep 2 09:57:27 EDT 2011


The CppAD multi-threading interface has been abstracted so that it 
should work with most any threading system. The connection between an 
arbitrary threading system and CppAD is made with the routine
     http://www.coin-or.org/CppAD/Doc/ta_parallel_setup.xml

  AD examples are included for OpenMP and Pthreads; see
     http://www.coin-or.org/CppAD/Doc/openmp_ad.cpp.xml
     http://www.coin-or.org/CppAD/Doc/pthread_ad.cpp.xml

The Pthread example is more complicated than to OpenMP example mainly 
due to creating the function
     size_t thread_num(void)
which returns a unique integer that identifies the current thread 
(between zero and the number of threads minus one).

The general instructions for using CppAD with multiple threads can be 
found at
     http://www.coin-or.org/CppAD/Doc/multi_thread.xml

The omp_alloc memory allocator has  been deprecated and one should use 
thread_alloc instead
     http://www.coin-or.org/CppAD/Doc/thread_alloc.xml


On 8/10/2011 12:44 PM, Stefan Vigerske wrote:
> Hi,
>
> I try to use CppAD 20110101.0 in a multithreading environment (pthreads).
> That is, there are several threads running in parallel, each one using
> CppAD to compute derivatives.
> If I use CppAD like I am used to, I get strange error messages or
> segmentation faults.
> Looking a bit around, it seems that CppAD stores the tapes in some
> static global variable.
>
> But I saw these OpenMP stuff, so I compiled my code with -fopenmp.
> Now I still get an error:
> CppAD 20110101.0 error from a known source:
> Independent: cannot create a new tape because
> a previous tape is still active (for this thread).
> AD<Base>::abort_recording() would abort this previous recording.
> Error detected by false result for
>       ADBase::tape_ptr() == CPPAD_NULL
> at line 172 in the file
>       lib/cppad/local/independent.hpp
> OpenMP: thread_num = 0
>
> I guess this is because CppAD cannot distinguish the different threads
> that I'm using, thus always assumes it is called from thread 0.
> It seems to use omp_get_thread_num to get the thread number, but a
> printf tells me that this is always 0, which isn't suprising, since the
> program I'm using uses the pthreads library for parallelization, so how
> should omp_get_thread_num() know?
>
> I also printed omp_get_max_threads() and this gives me 4, on a quad-core
> machine. Seems ok. So I doubt that calling
> omp_alloc::set_max_num_threads(number) would make any difference.
> In the documentation it says something about calling
> CppAD::omp_alloc::set_max_num_threads, but this function, or the whole
> omp_alloc, does not seem to exists in the CppAD version that I'm using.
>
> Any idea how to make CppAD threadsafe?
>
> Stefan
>



More information about the CppAD mailing list