multi_atomic_two_setup

View page source

Multi-Threaded atomic_two Set Up

Syntax

ok = multi_atomic_two_setup ( y_squared )

Purpose

This routine splits up the computation into the individual threads.

Thread

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

y_squared

This argument has prototype

const vector<double>& y_squared

and its size is equal to the number of equations to solve. It is the values that we are computing the square root of.

ok

This return value has prototype

bool ok

If it is false, multi_atomic_two_setup detected an error.

Source

namespace {
bool multi_atomic_two_setup(const vector<double>& y_squared)
{  using CppAD::AD;
   size_t num_threads = std::max(num_threads_, size_t(1));
   bool   ok          = num_threads == thread_alloc::num_threads();
   ok                &= thread_alloc::thread_num() == 0;
   //
   // declare independent variable variable vector
   vector< AD<double> > ax(1);
   ax[0] = 2.0;
   CppAD::Independent(ax);
   //
   // argument and result for atomic function
   vector< AD<double> > au(3), ay(1);
   au[0] = AD<double>( num_itr_ ); // num_itr
   au[1] = ax[0];                  // y_initial
   au[2] = ax[0];                  // y_squared
   // put atomic function operation in recording
   (*a_square_root_)(au, ay);
   //
   // f(u) = sqrt(u)
   CppAD::ADFun<double> fun(ax, ay);
   //
   // number of square roots for each thread
   size_t per_thread = (y_squared.size() + num_threads - 1) / num_threads;
   size_t y_index    = 0;
   //
   for(size_t thread_num = 0; thread_num < num_threads; thread_num++)
   {  // allocate separate memory for each thread to avoid false sharing
      size_t min_bytes(sizeof(work_one_t)), cap_bytes;
      void* v_ptr = thread_alloc::get_memory(min_bytes, cap_bytes);
      work_all_[thread_num] = static_cast<work_one_t*>(v_ptr);
      //
      // Run constructor on work_all_[thread_num]->fun
      work_all_[thread_num]->fun = new CppAD::ADFun<double>;
      //
      // Run constructor on work_all_[thread_num] vectors
      work_all_[thread_num]->y_squared = new vector<double>;
      work_all_[thread_num]->square_root = new vector<double>;
      //
      // Each worker gets a separate copy of fun. This is necessary because
      // the Taylor coefficients will be set by each thread.
      *(work_all_[thread_num]->fun) = fun;
      //
      // values we are computing square root of for this thread
      ok &=  0 == work_all_[thread_num]->y_squared->size();
      for(size_t i = 0; i < per_thread; i++)
      if( y_index < y_squared.size() )
         work_all_[thread_num]->y_squared->push_back(y_squared[y_index++]);
      //
      // set to false in case this thread's worker does not get called
      work_all_[thread_num]->ok = false;
   }
   ok &= y_index == y_squared.size();
   //
   return ok;
}
}