[CppAD] Use of global var for tape

schattenpflanze at arcor.de schattenpflanze at arcor.de
Thu Aug 11 10:09:08 EDT 2011


Dear Iakov,

> I've just started to use CppAD and so the answer to my question might
> be obvious.
> It seems that use of global variable for storing tape is giving a lot
> of troubles in multithreading case. Have you considered other options?
> For instance:
> 1) Use ref counting, e.g., boost::shared_ptr
Some time ago, I actually had the very same idea. From a design point of
view, this seemed to be the optimal solution to avoid static variables
and ensure that threads can operate independently. Hence, I coded an
experimental implementation by modifying existing CppAD sources to this
concept.
Unfortunately, it turned out that using shared pointers (or even better
weak pointers) in each AD<double> instance slows down everything
considerably. The reason is most definitely that copying a smart pointer
is always a synchronized operation. It is therefore prohibitive to copy
smart pointers inside the most frequently called functions (arithmetic
operators on AD<double>).

Using a shared pointer to a tape in tape_link.hpp is still an option,
though. In particular for transferring a tape from the recorder to the
player.

> 2) Make the tape creation explicit to the user
> - tape_this pointer in the AD<Base> is a normal pointer to ADTape<Base>
> - Independent(X) either returns a tape pointer or the user is expected
> to construct ADTape and then call Independent on it
> - All the assignment operators transfer the tape_ptr from arguments to
> the left hand side
> - Dependent() might clone the tape object or just store a pointer to
> it in ADFunc
Using ordinary pointers instead would work with regard to performance.
However, this might cause trouble if a tape is deleted but the recorded
AD<double> variables are not. There is then no obvious way to check that
the tape pointed to still exists (and thus to decide, whether an
AD<double> pointing to that tape is still a variable of a specific tape
or a parameter).
Moreover, the memory allocator in omp_alloc.hpp currently keeps track of
used memory per thread. One would have to do the memory management
within the tape (or attach a memory manager object to the tape) in order
to avoid checking the thread number explicitly.


Best regards,
Peter




More information about the CppAD mailing list