Prev Next multi_atomic_takedown

@(@\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@
Multi-Threaded User Atomic Take Down

Syntax
ok = multi_atomic_takedown(square_root)

Purpose
This routine gathers up the results for each thread and frees memory that was allocated by multi_atomic_setup .

Thread
It is assumed that this function is called by thread zero and all the other threads are blocked (waiting).

square_root
This argument has prototype
     vector<double>& 
square_root
The input value of square_root does not matter. Upon return, it has the same size and is the element by element square root of y_squared .

ok
This return value has prototype
     bool 
ok
If it is false, multi_atomic_takedown detected an error.

Source

namespace {
bool multi_atomic_takedown(vector<double>& square_root)
{     bool ok            = true;
     ok                &= thread_alloc::thread_num() == 0;
     size_t num_threads = std::max(num_threads_, size_t(1));
     //
     // extract square roots in original order
     square_root.resize(0);
     for(size_t thread_num = 0; thread_num < num_threads; thread_num++)
     {     // results for this thread
          size_t n = work_all_[thread_num]->square_root->size();
          for(size_t i = 0; i < n; i++)
               square_root.push_back((* work_all_[thread_num]->square_root )[i]);
     }
     //
     // go down so that free memory for other threads before memory for master
     size_t thread_num = num_threads;
     while(thread_num--)
     {     // check that this tread was ok with the work it did
          ok  &= work_all_[thread_num]->ok;
          //
          // run destructor on vector object for this thread
          delete work_all_[thread_num]->y_squared;
          delete work_all_[thread_num]->square_root;
          //
          // run destructor on function object for this thread
          delete work_all_[thread_num]->fun;
          //
          // delete problem specific information
          void* v_ptr = static_cast<void*>( work_all_[thread_num] );
          thread_alloc::return_memory( v_ptr );
          //
          // check that there is no longer any memory inuse by this thread
          if( thread_num > 0 )
          {     ok &= 0 == thread_alloc::inuse(thread_num);
               //
               // return all memory being held for future use by this thread
               thread_alloc::free_available(thread_num);
          }
     }
     return ok;
}
}

Input File: example/multi_thread/multi_atomic.cpp