# workstealing.h

Go to the documentation of this file.

    00001 // -*- C++ -*-
    00002
    00003 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the terms
    00007 // of the GNU General Public License as published by the Free Software
    00008 // Foundation; either version 3, or (at your option) any later
    00009 // version.
    00010
    00011 // This library is distributed in the hope that it will be useful, but
    00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014 // General Public License for more details.
    00015
    00016 // Under Section 7 of GPL version 3, you are granted additional
    00017 // permissions described in the GCC Runtime Library Exception, version
    00018 // 3.1, as published by the Free Software Foundation.
    00019
    00020 // You should have received a copy of the GNU General Public License and
    00021 // a copy of the GCC Runtime Library Exception along with this program;
    00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00023 // <http://www.gnu.org/licenses/>.
    00024
    00025 /** @file parallel/workstealing.h
    00026  *  @brief Parallelization of embarrassingly parallel execution by
    00027  *  means of work-stealing.
    00028  *
    00029  *  Work stealing is described in
    00030  *
    00031  *  R. D. Blumofe and C. E. Leiserson.
    00032  *  Scheduling multithreaded computations by work stealing.
    00033  *  Journal of the ACM, 46(5):720–748, 1999.
    00034  *
    00035  *  This file is a GNU parallel extension to the Standard C++ Library.
    00036  */
    00037
    00038 // Written by Felix Putze.
    00039
    00040 #ifndef _GLIBCXX_PARALLEL_WORKSTEALING_H
    00041 #define _GLIBCXX_PARALLEL_WORKSTEALING_H 1
    00042
    00043 #include <parallel/parallel.h>
    00044 #include <parallel/random_number.h>
    00045 #include <parallel/compatibility.h>
    00046
    00047 namespace __gnu_parallel
    00048 {
    00049
    00050 #define _GLIBCXX_JOB_VOLATILE volatile
    00051
    00052 /** @brief One job for a certain thread. */
    00053 template<typename _DifferenceTp>
    00054   struct Job
    00055   {
    00056     typedef _DifferenceTp difference_type;
    00057
    00058     /** @brief First element.
    00059      *
    00060      *  Changed by owning and stealing thread. By stealing thread,
    00061      *  always incremented. */
    00062     _GLIBCXX_JOB_VOLATILE difference_type first;
    00063
    00064     /** @brief Last element.
    00065      *
    00066      *  Changed by owning thread only. */
    00067     _GLIBCXX_JOB_VOLATILE difference_type last;
    00068
    00069     /** @brief Number of elements, i. e. @c last-first+1.
    00070      *
    00071      *  Changed by owning thread only. */
    00072     _GLIBCXX_JOB_VOLATILE difference_type load;
    00073   };
    00074
    00075 /** @brief Work stealing algorithm for random access iterators.
    00076   *
    00077   *  Uses O(1) additional memory. Synchronization at job lists is
    00078   *  done with atomic operations.
    00079   *  @param begin Begin iterator of element sequence.
    00080   *  @param end End iterator of element sequence.
    00081   *  @param op User-supplied functor (comparator, predicate, adding
    00082   *  functor, ...).
    00083   *  @param f Functor to "process" an element with op (depends on
    00084   *  desired functionality, e. g. for std::for_each(), ...).
    00085   *  @param r Functor to "add" a single result to the already
    00086   *  processed elements (depends on functionality).
    00087   *  @param base Base value for reduction.
    00088   *  @param output Pointer to position where final result is written to
    00089   *  @param bound Maximum number of elements processed (e. g. for
    00090   *  std::count_n()).
    00091   *  @return User-supplied functor (that may contain a part of the result).
    00092   */
    00093 template<typename RandomAccessIterator,
    00094      typename Op,
    00095      typename Fu,
    00096      typename Red,
    00097      typename Result>
    00098   Op
    00099   for_each_template_random_access_workstealing(RandomAccessIterator begin,
    00100                            RandomAccessIterator end,
    00101                            Op op, Fu& f, Red r,
    00102                            Result base, Result& output,
    00103                            typename std::iterator_traits
    00104                            <RandomAccessIterator>::
    00105                            difference_type bound)
    00106   {
    00107     _GLIBCXX_CALL(end - begin)
    00108
    00109     typedef std::iterator_traits<RandomAccessIterator> traits_type;
    00110     typedef typename traits_type::difference_type difference_type;
    00111
    00112     const _Settings& __s = _Settings::get();
    00113
    00114     difference_type chunk_size = static_cast<difference_type>(__s.workstealing_chunk_size);
    00115
    00116     // How many jobs?
    00117     difference_type length = (bound < 0) ? (end - begin) : bound;
    00118
    00119     // To avoid false sharing in a cache line.
    00120     const int stride = __s.cache_line_size * 10 / sizeof(Job<difference_type>) + 1;
    00121
    00122     // Total number of threads currently working.
    00123     thread_index_t busy = 0;
    00124
    00125     Job<difference_type> *job;
    00126
    00127     omp_lock_t output_lock;
    00128     omp_init_lock(&output_lock);
    00129
    00130     // Write base value to output.
    00131     output = base;
    00132
    00133     // No more threads than jobs, at least one thread.
    00134     thread_index_t num_threads =
    00135         __gnu_parallel::max<thread_index_t>(1,
    00136             __gnu_parallel::min<difference_type>(length, get_max_threads()));
    00137
    00138 #   pragma omp parallel shared(busy) num_threads(num_threads)
    00139       {
    00140
    00141 #       pragma omp single
    00142           {
    00143             num_threads = omp_get_num_threads();
    00144
    00145             // Create job description array.
    00146             job = new Job<difference_type>[num_threads * stride];
    00147           }
    00148
    00149         // Initialization phase.
    00150
    00151         // Flags for every thread if it is doing productive work.
    00152         bool iam_working = false;
    00153
    00154         // Thread id.
    00155         thread_index_t iam = omp_get_thread_num();
    00156
    00157         // This job.
    00158         Job<difference_type>& my_job = job[iam * stride];
    00159
    00160         // Random number (for work stealing).
    00161         thread_index_t victim;
    00162
    00163         // Local value for reduction.
    00164         Result result = Result();
    00165
    00166         // Number of elements to steal in one attempt.
    00167         difference_type steal;
    00168
    00169         // Every thread has its own random number generator
    00170         // (modulo num_threads).
    00171         random_number rand_gen(iam, num_threads);
    00172
    00173         // This thread is currently working.
    00174 #       pragma omp atomic
    00175           ++busy;
    00176
    00177         iam_working = true;
    00178
    00179         // How many jobs per thread? last thread gets the rest.
    00180         my_job.first =
    00181             static_cast<difference_type>(iam * (length / num_threads));
    00182
    00183         my_job.last = (iam == (num_threads - 1)) ?
    00184             (length - 1) : ((iam + 1) * (length / num_threads) - 1);
    00185         my_job.load = my_job.last - my_job.first + 1;
    00186
    00187         // Init result with first value (to have a base value for reduction).
    00188         if (my_job.first <= my_job.last)
    00189           {
    00190             // Cannot use volatile variable directly.
    00191             difference_type my_first = my_job.first;
    00192             result = f(op, begin + my_first);
    00193             ++my_job.first;
    00194             --my_job.load;
    00195           }
    00196
    00197         RandomAccessIterator current;
    00198
    00199 #       pragma omp barrier
    00200
    00201         // Actual work phase
    00202         // Work on own or stolen start
    00203         while (busy > 0)
    00204           {
    00205             // Work until no productive thread left.
    00206 #           pragma omp flush(busy)
    00207
    00208             // Thread has own work to do
    00209             while (my_job.first <= my_job.last)
    00210               {
    00211                 // fetch-and-add call
    00212                 // Reserve current job block (size chunk_size) in my queue.
    00213                 difference_type current_job =
    00214                   fetch_and_add<difference_type>(&(my_job.first), chunk_size);
    00215
    00216                 // Update load, to make the three values consistent,
    00217                 // first might have been changed in the meantime
    00218                 my_job.load = my_job.last - my_job.first + 1;
    00219                 for (difference_type job_counter = 0;
    00220                      job_counter < chunk_size && current_job <= my_job.last;
    00221                      ++job_counter)
    00222                   {
    00223                     // Yes: process it!
    00224                     current = begin + current_job;
    00225                     ++current_job;
    00226
    00227                     // Do actual work.
    00228                     result = r(result, f(op, current));
    00229                   }
    00230
    00231 #               pragma omp flush(busy)
    00232               }
    00233
    00234             // After reaching this point, a thread's job list is empty.
    00235             if (iam_working)
    00236               {
    00237                 // This thread no longer has work.
    00238 #               pragma omp atomic
    00239                 --busy;
    00240
    00241                 iam_working = false;
    00242               }
    00243
    00244             difference_type supposed_first, supposed_last, supposed_load;
    00245             do
    00246               {
    00247                 // Find random nonempty deque (not own), do consistency check.
    00248                 yield();
    00249 #               pragma omp flush(busy)
    00250                 victim = rand_gen();
    00251                 supposed_first = job[victim * stride].first;
    00252                 supposed_last = job[victim * stride].last;
    00253                 supposed_load = job[victim * stride].load;
    00254               }
    00255             while (busy > 0
    00256               && ((supposed_load <= 0)
    00257                 || ((supposed_first + supposed_load - 1) != supposed_last)));
    00258
    00259             if (busy == 0)
    00260               break;
    00261
    00262             if (supposed_load > 0)
    00263               {
    00264                 // Has work and work to do.
    00265                 // Number of elements to steal (at least one).
    00266                 steal = (supposed_load < 2) ? 1 : supposed_load / 2;
    00267
    00268                 // Push victim's start forward.
    00269                 difference_type stolen_first =
    00270                     fetch_and_add<difference_type>(
    00271                         &(job[victim * stride].first), steal);
    00272                 difference_type stolen_try =
    00273                     stolen_first + steal - difference_type(1);
    00274
    00275                 my_job.first = stolen_first;
    00276                 my_job.last = __gnu_parallel::min(stolen_try, supposed_last);
    00277                 my_job.load = my_job.last - my_job.first + 1;
    00278
    00279                 // Has potential work again.
    00280 #               pragma omp atomic
    00281                   ++busy;
    00282                 iam_working = true;
    00283
    00284 #               pragma omp flush(busy)
    00285               }
    00286 #           pragma omp flush(busy)
    00287           } // end while busy > 0
    00288             // Add accumulated result to output.
    00289         omp_set_lock(&output_lock);
    00290         output = r(output, result);
    00291         omp_unset_lock(&output_lock);
    00292       }
    00293
    00294     delete[] job;
    00295
    00296     // Points to last element processed (needed as return value for
    00297     // some algorithms like transform)
    00298     f.finish_iterator = begin + length;
    00299
    00300     omp_destroy_lock(&output_lock);
    00301
    00302     return op;
    00303   }
    00304 } // end namespace
    00305
    00306 #endif /* _GLIBCXX_PARALLEL_WORKSTEALING_H */