harmonic_worker

View page source

Do One Thread’s Work for Sum of 1/i

Syntax

harmonic_worker ()

Purpose

This routines computes the sum the summation that defines the harmonic series

1/ start + 1/( start +1) + … + 1/( end -1 )

start

This is the value of the harmonic_common information

start = work_all_ [ thread_num ] ->start

end

This is the value of the harmonic_common information

end = work_all_ [ thread_num ] ->end

thread_num

This is the number for the current thread; see thread_num .

Source

namespace {
void harmonic_worker(void)
{  // sum =  1/(stop-1) + 1/(stop-2) + ... + 1/start
   size_t thread_num  = thread_alloc::thread_num();
   size_t num_threads = std::max(num_threads_, size_t(1));
   bool   ok          = thread_num < num_threads;
   size_t start       = work_all_[thread_num]->start;
   size_t stop        = work_all_[thread_num]->stop;
   double sum         = 0.;

   ok &= stop > start;
   size_t i = stop;
   while( i > start )
   {  i--;
      sum += 1. / double(i);
   }

   work_all_[thread_num]->sum = sum;
   work_all_[thread_num]->ok  = ok;
}
}