parallel.qbk 12.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
[/
 / Copyright (c) 2014 Vicente J. Botet Escriba
 /
 / Distributed under the Boost Software License, Version 1.0. (See accompanying
 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 /]

[//////////////////////////////////////////////////////////]
[section:parallel Parallel - Fork-Join -- EXPERIMENTAL]

[section:fork_join Fork-Join]

[warning These features are experimental and subject to change in future versions. There are not too much tests yet, so it is possible that you can find out some trivial bugs :(] 

[note These features are based on the [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4088.pdf [* n4088 - Task Region R3]] C++1y proposal from P. Halpern, A. Robison, A. Laksberg, H. Sutter, et al.  The text that follows has been adapted from this paper to show the differences.]

The major difference respect to the standard proposal is that we are able to use a common executor for several task regions.

[note 
Up to now, Boost.Thread doesn't implement the parallel algorithms as defined in [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4105.pdf [* n4105 - Information technology – Programming languages, their environments and system software interfaces – Technical Specification for C++ Extensions for Parallelism]].
]

[////////////////////]
[section Introduction]


This module introduces a C++11/c++14 library function template `task_region` and a library class `task_region_handle`
with member functions `run` and `wait` that together enable developers to write expressive and portable fork-join
parallel code.

The working draft for the Parallelism TS [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4105.pdf [*N4105]] augments the STL algorithms with the inclusion of parallel execution policies. Programmers use these as a basis to write additional high-level algorithms that can be implemented in terms of the provided parallel algorithms. However, the scope of n4105 does not include lower-level mechanisms to express arbitrary fork-join parallelism

The `task_region`, `run` and the `wait` functions provided by this library are based on the `task_group` concept that is a part of the common subset of the PPL and the TBB libraries.

[endsect] [/ Introduction]


[/////////////////////////]
[section:tutorial Tutorial]

Consider an example of a parallel traversal of a tree, where a user-provided function compute is applied to each node of the tree, returning the sum of the results:

  template<typename Func>
  int traverse(node *n, Func&& compute)
  {
    int left = 0, right = 0;
    task_region([&](task_region_handle& tr) {
      if (n->left)
        tr.run([&] { left = traverse(n->left, compute); });
      if (n->right)
        tr.run([&] { right = traverse(n->right, compute); });
    });
    return compute(n) + left + right;
  }

The example above demonstrates the use of two of the functions proposed in this paper, `task_region` and
`task_region_handle::run`.
The `task_region` function delineates a region in a program code potentially containing invocations of tasks
spawned by the `run` member function of the `task_region_handle` class.

The run function spawns a task, a unit of work that is allowed to execute in parallel with respect to the caller.
Any parallel tasks spawned by `run` within the `task_region` are joined back to a single thread of execution at
the end of the `task_region`.

`run` takes a user-provided function object `f` and starts it asynchronously - i.e. it may return before the
execution of `f` completes. The implementation's scheduler may choose to run `f` immediately or delay running
`f` until compute resources become available.

A `task_region_handle` can be constructed only by `task_region` because it has no public constructors.
Thus, `run` can be invoked (directly or indirectly) only from a user-provided function passed to `task_region`:

  void g();
  void f(task_region_handle& tr)
  {
    tr.run(g); // OK, invoked from within task_region in h
  }
  void h()
  {
    task_region(f);
  }

  int main()
  {
    task_region_handle tr; // Error: no public constructor
    tr.run(g); // No way to call run outside of a task_region
    return 0;
  }

[endsect] [/ Tutorial]

[////////////////]
[section:examples Examples]

[section:fib Parallel Fibonacci]


This is surely the worst implementation of the Fibonacci function. Anyway, here it is, as it is simple and shows the fork-join structure clearly. `Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)`, so the task decomposition is trivial.

  int fib_task_region(int n)
  {
    using boost::experimental::parallel::task_region;
    using boost::experimental::parallel::task_region_handle;
  
    if (n == 0) return 0;
    if (n == 1) return 1;
  
    int n1;
    int n2;
  
    task_region([&](task_region_handle& trh)
        {
          trh.run([&]
              {
                n1 = fib_task_region(n - 1);
              });
  
          n2 = fib_task_region(n - 2);
        });
  
    return n1 + n2;
  }

  int main()
  {
    for (int i = 0; i<10; ++i) {
      std::cout << fib_task_region(i) << " ";
    }
    std::cout << std::endl;
  }

[endsect] [/ Fib]
[section:fibex Parallel Fibonacci - Specific executor]

The previous example make use of an implementation defined way to spawn the tasks. Often the user wants to master how the task must be spawned. There is an overload of `task_region` that accept an additional `Executor` parameter and a function that takes as parameter a `task_region_handle_gen<Executor>`. `task_region_handle_gen<Executor>` run uses this executor to spawn the tasks. 

  template <class Ex>
  int fib_task_region_gen( Ex& ex, int n)
  {
    using boost::experimental::parallel::task_region;
    using boost::experimental::parallel::task_region_handle_gen;
  
    if (n == 0) return 0;
    if (n == 1) return 1;
  
    int n1;
    int n2;
  
    task_region(ex, [&](task_region_handle_gen<Ex>& trh) // (2)
        {
          trh.run([&]
              {
                n1 = fib_task_region(n - 1);
              });
  
          n2 = fib_task_region(n - 2);
        });
  
    return n1 + n2;
  }

  int main()
  {
    boost::basic_thread_pool tp; // (1)
    for (int i = 0; i<10; ++i) {
      std::cout << fib_task_region_gen(tp,i) << " ";
    }
    std::cout << std::endl;
    return 0;
  }


The specific executor is declared in line (1) and it is used in line (2).

[endsect] [/ Fib ex]




[section:quick_sort Parallel Accumulate]


[endsect] [/ Accumulate]

[section:quick_sort Parallel Quick Sort]



[endsect] [/ QuickSort]
[endsect] [/ Examples]


[////////////////////////]
[section:rationale Design Rationale]


[endsect] [/ Design Rationale]
[endsect] [/ Fork-Join]

[/////////////////////]
[section:ref Reference -- EXPERIMENTAL]

[/////////////////////////]
[section:v1 Parallel V1]

[section:exception_list Header `<experimental/exception_list.hpp>`]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v1
  {
  
    class exception_list;

  } // v1
  } // parallel
  } // experimental
  } // boost


[/////////////////////////]
[section:exception_list Class `exception_list`]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v1
  {
  
    class exception_list: public std::exception
    {
    public:
      typedef 'implementation defined' const_iterator;
  
      ~exception_list() noexcept {}
  
      void add(exception_ptr const& e);
      size_t size() const noexcept;
      const_iterator begin() const noexcept;
      const_iterator end() const noexcept;
      const char* what() const noexcept;
  
    };

  } // v1
  } // parallel
  } // experimental
  } // boost


[endsect] [/ exception_list]

[endsect] [/ exception_list.hpp]

[endsect] [/ Parallel V1]

[////////////////////////////////////////////////////////////////////]
[section:v2 Parallel V2]
[////////////////////////////////////////////////////////////////////]
[section:concepts Concepts]
[////////////////////////////////////////////////////////////////////]
[section:regionCallable Concept `Region_Callable`]

[endsect] [/ Region_Callable]
[////////////////////////////////////////////////////////////////////]
[section:taskCallable Concept `Task_Callable`]


[endsect] [/ Task_Callable]
[////////////////////////////////////////////////////////////////////]
[endsect] [/ Concepts]
[////////////////////////////////////////////////////////////////////]
[section:task_region Header `<experimental/task_region.hpp>`]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v2
  {

    class task_canceled_exception;
  
    template <class Executor>
    class task_region_handle_gen;
  
    using default_executor = 'implementation defined';

    class task_region_handle;
  
    template <typename Executor, typename F>
      void task_region_final(Executor& ex, F&& f); 
    template <typename F>
      void task_region_final(F&& f);

    template <typename Executor, typename F>
      void task_region(Executor& ex, F&& f);
    template <typename F>
      void task_region(F&& f);

  } // v2
  } // parallel
  } // experimental
  } // boost


[////////////////////////////////////////////////////////////////////]
[section:task_canceled_exception Class `task_canceled_exception `]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v2
  {
  
    class task_canceled_exception: public std::exception
    {
    public:
      task_canceled_exception() noexcept;
      task_canceled_exception(const task_canceled_exception&) noexcept;
      task_canceled_exception& operator=(const task_canceled_exception&) noexcept;
      virtual const char* what() const noexcept;
    };

  } // v2
  } // parallel
  } // experimental
  } // boost

[endsect] [/ task_canceled_exception]
[////////////////////////////////////////////////////////////////////]
[section:task_region_handle_gen Template Class `task_region_handle_gen<>`]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v2
  {
  
    template <class Executor>
    class task_region_handle_gen
    {
    protected:
      task_region_handle_gen(Executor& ex);
   
      ~task_region_handle_gen(); 
  
    public:
      task_region_handle_gen(const task_region_handle_gen&) = delete;
      task_region_handle_gen& operator=(const task_region_handle_gen&) = delete;
      task_region_handle_gen* operator&() const = delete;
  
      template<typename F>
      void run(F&& f);
  
      void wait();
    };

  } // v2
  } // parallel
  } // experimental
  } // boost

[endsect] [/ task_region_handle_gen]
[////////////////////////////////////////////////////////////////////]
[section:default_executor Class `default_executor `]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v2
  {
  
    using default_executor = 'implementation defined';
 
  } // v2
  } // parallel
  } // experimental
  } // boost

[endsect] [/ default_executor]
[////////////////////////////////////////////////////////////////////]
[section:task_region_handle Class `task_region_handle `]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v2
  {
  
    class task_region_handle :
      public task_region_handle_gen<default_executor>
    {
    protected:
      task_region_handle();
      task_region_handle(const task_region_handle&) = delete;
      task_region_handle& operator=(const task_region_handle&) = delete;
      task_region_handle* operator&() const = delete;
  
    };
  
  } // v2
  } // parallel
  } // experimental
  } // boost

[endsect] [/ task_region_handle]
[////////////////////////////////////////////////////////////////////]
[section:task_region_final Template Function `task_region_final `]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v2
  { 
  
    template <typename Executor, typename F>
      void task_region_final(Executor& ex, F&& f); 
    template <typename F>
      void task_region_final(F&& f);

  } // v2
  } // parallel
  } // experimental
  } // boost

[endsect] [/ task_region_final]
[////////////////////////////////////////////////////////////////////]
[section:task_region Template Function `task_region `]

  namespace boost
  {
  namespace experimental
  {
  namespace parallel
  {
  inline namespace v2
  { 
  
    template <typename Executor, typename F>
      void task_region(Executor& ex, F&& f);
    template <typename F>
      void task_region(F&& f);

  } // v2
  } // parallel
  } // experimental
  } // boost

[endsect] [/ task_region]




[endsect] [/ task_region.hpp]
[endsect] [/ Parallel V2]
[endsect] [/ Reference]

[endsect] [/ Parallel]