Reference documentation for deal.II version GIT 4e92747666 2022-12-02 08:40:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
work_stream.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2022 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_work_stream_h
17 # define dealii_work_stream_h
18 
19 
20 # include <deal.II/base/config.h>
21 
25 # include <deal.II/base/parallel.h>
29 
30 # ifdef DEAL_II_WITH_TBB
32 # ifdef DEAL_II_TBB_WITH_ONEAPI
33 # include <tbb/parallel_pipeline.h>
34 # else
35 # include <tbb/pipeline.h>
36 # endif
38 # endif
39 
40 # include <functional>
41 # include <iterator>
42 # include <memory>
43 # include <utility>
44 # include <vector>
45 
47 
48 
49 
162 namespace WorkStream
163 {
168  namespace internal
169  {
170 # ifdef DEAL_II_WITH_TBB
185  namespace tbb_no_coloring
186  {
190  template <typename Iterator, typename ScratchData, typename CopyData>
192  {
193  public:
200  struct ItemType
201  {
208  {
209  std::unique_ptr<ScratchData> scratch_data;
211 
216  : currently_in_use(false)
217  {}
218 
219  ScratchDataObject(ScratchData *p, const bool in_use)
220  : scratch_data(p)
221  , currently_in_use(in_use)
222  {}
223 
224  // Provide a copy constructor that actually doesn't copy the
225  // internal state. This makes handling ScratchAndCopyDataObjects
226  // easier to handle with STL containers.
228  : currently_in_use(false)
229  {}
230 
231  ScratchDataObject(ScratchDataObject &&o) noexcept = default;
232  };
233 
234 
239  using ScratchDataList = std::list<ScratchDataObject>;
240 
245  std::vector<Iterator> iterators;
246 
252  std::vector<CopyData> copy_datas;
253 
259  unsigned int n_iterators;
260 
293 
298  const ScratchData *sample_scratch_data;
299 
305 
306 
312  : n_iterators(0)
313  , scratch_data(nullptr)
314  , sample_scratch_data(nullptr)
315  , currently_in_use(false)
316  {}
317  };
318 
319 
326  const Iterator & end,
327  const unsigned int buffer_size,
328  const unsigned int chunk_size,
329  const ScratchData &sample_scratch_data,
330  const CopyData & sample_copy_data)
332  , item_buffer(buffer_size)
335  {
336  // initialize the elements of the ring buffer
337  for (auto &item : item_buffer)
338  {
339  Assert(item.n_iterators == 0, ExcInternalError());
340 
341  item.iterators.resize(chunk_size,
342  remaining_iterator_range.second);
343  item.scratch_data = &thread_local_scratch;
344  item.sample_scratch_data = &sample_scratch_data;
345  item.copy_datas.resize(chunk_size, sample_copy_data);
346  item.currently_in_use = false;
347  }
348  }
349 
350 
354  ItemType *
356  {
357  // find first unused item. we know that there must be one
358  // because we have set the maximal number of tokens in flight
359  // and have set the ring buffer to have exactly this size. so
360  // if this function is called, we know that less than the
361  // maximal number of items in currently in flight
362  //
363  // note that we need not lock access to this array since
364  // the current stage is run sequentially and we can therefore
365  // enter the following block only once at any given time.
366  // thus, there can be no race condition between checking that
367  // a flag is false and setting it to true. (there may be
368  // another thread where we release items and set 'false'
369  // flags to 'true', but that too does not produce any
370  // problems)
371  ItemType *current_item = nullptr;
372  for (unsigned int i = 0; i < item_buffer.size(); ++i)
373  if (item_buffer[i].currently_in_use == false)
374  {
375  item_buffer[i].currently_in_use = true;
376  current_item = &item_buffer[i];
377  break;
378  }
379  Assert(current_item != nullptr,
380  ExcMessage("This can't be. There must be a free item!"));
381 
382  // initialize the next item. it may
383  // consist of at most chunk_size
384  // elements
385  current_item->n_iterators = 0;
386  while ((remaining_iterator_range.first !=
387  remaining_iterator_range.second) &&
388  (current_item->n_iterators < chunk_size))
389  {
390  current_item->iterators[current_item->n_iterators] =
392 
393  ++remaining_iterator_range.first;
394  ++current_item->n_iterators;
395  }
396 
397  if (current_item->n_iterators == 0)
398  // there were no items
399  // left. terminate the pipeline
400  return nullptr;
401  else
402  return current_item;
403  }
404 
405  private:
410  std::pair<Iterator, Iterator> remaining_iterator_range;
411 
415  std::vector<ItemType> item_buffer;
416 
449 
455  const ScratchData &sample_scratch_data;
456 
463  const unsigned int chunk_size;
464  };
465 
466 
467 
468  template <typename Worker,
469  typename Copier,
470  typename Iterator,
471  typename ScratchData,
472  typename CopyData>
473  void
474  run(const Iterator & begin,
475  const typename identity<Iterator>::type &end,
476  Worker worker,
477  Copier copier,
478  const ScratchData & sample_scratch_data,
479  const CopyData & sample_copy_data,
480  const unsigned int queue_length,
481  const unsigned int chunk_size)
482  {
483  using ItemType = typename IteratorRangeToItemStream<Iterator,
484  ScratchData,
485  CopyData>::ItemType;
486 
487  // Create the three stages of the pipeline:
488 
489  //
490  // ----- Stage 1 -----
491  //
492  // The first stage is the one that provides us with chunks of data
493  // to work on (the stream of "items"). This stage runs sequentially.
495  iterator_range_to_item_stream(begin,
496  end,
497  queue_length,
498  chunk_size,
499  sample_scratch_data,
500  sample_copy_data);
501  auto tbb_item_stream_filter = tbb::make_filter<void, ItemType *>(
502 # ifdef DEAL_II_TBB_WITH_ONEAPI
503  tbb::filter_mode::serial_in_order,
504 # else
506 # endif
507  [&](tbb::flow_control &fc) -> ItemType * {
508  if (const auto item = iterator_range_to_item_stream.get_item())
509  return item;
510  else
511  {
512  fc.stop();
513  return nullptr;
514  }
515  });
516 
517  //
518  // ----- Stage 2 -----
519  //
520  // The second stage is the one that does the actual work. This is the
521  // stage that runs in parallel
522  auto tbb_worker_filter = tbb::make_filter<ItemType *, ItemType *>(
523 # ifdef DEAL_II_TBB_WITH_ONEAPI
524  tbb::filter_mode::parallel,
525 # else
526  tbb::filter::parallel,
527 # endif
528  [worker =
529  std::function<void(const Iterator &, ScratchData &, CopyData &)>(
530  worker),
531  copier_exists =
532  static_cast<bool>(std::function<void(const CopyData &)>(copier))](
533  ItemType *current_item) {
534  // we need to find an unused scratch data object in the list that
535  // corresponds to the current thread and then mark it as used. if
536  // we can't find one, create one
537  //
538  // as discussed in the discussion of the documentation of the
539  // IteratorRangeToItemStream::scratch_data variable, there is no
540  // need to synchronize access to this variable using a mutex
541  // as long as we have no yield-point in between. this means that
542  // we can't take an iterator into the list now and expect it to
543  // still be valid after calling the worker, but we at least do
544  // not have to lock the following section
545  ScratchData *scratch_data = nullptr;
546  {
547  // see if there is an unused object. if so, grab it and mark
548  // it as used
549  for (auto &p : current_item->scratch_data->get())
550  if (p.currently_in_use == false)
551  {
552  scratch_data = p.scratch_data.get();
553  p.currently_in_use = true;
554 
555  break;
556  }
557 
558  // if no object was found, create one and mark it as used
559  if (scratch_data == nullptr)
560  {
561  scratch_data =
562  new ScratchData(*current_item->sample_scratch_data);
563  current_item->scratch_data->get().emplace_back(scratch_data,
564  true);
565  }
566  }
567 
568  // then call the worker function on each element of the chunk we
569  // were given. since these worker functions are called on separate
570  // threads, nothing good can happen if they throw an exception and
571  // we are best off catching it and showing an error message
572  for (unsigned int i = 0; i < current_item->n_iterators; ++i)
573  {
574  try
575  {
576  if (worker)
577  worker(current_item->iterators[i],
578  *scratch_data,
579  current_item->copy_datas[i]);
580  }
581  catch (const std::exception &exc)
582  {
584  }
585  catch (...)
586  {
588  }
589  }
590 
591  // finally mark the scratch object as unused again. as above, there
592  // is no need to lock anything here since the object we work on
593  // is thread-local
594  for (auto &p : current_item->scratch_data->get())
595  if (p.scratch_data.get() == scratch_data)
596  {
597  Assert(p.currently_in_use == true, ExcInternalError());
598  p.currently_in_use = false;
599 
600  break;
601  }
602 
603  // if there is no copier, mark current item as usable again
604  if (copier_exists == false)
605  current_item->currently_in_use = false;
606 
607 
608  // Then return the original pointer
609  // to the now modified object. The copier will work on it next.
610  return current_item;
611  });
612 
613 
614  //
615  // ----- Stage 3 -----
616  //
617  // The last stage is the one that copies data from the CopyData objects
618  // to the final destination. This stage runs sequentially again.
619  auto tbb_copier_filter = tbb::make_filter<ItemType *, void>(
620 # ifdef DEAL_II_TBB_WITH_ONEAPI
621  tbb::filter_mode::serial_in_order,
622 # else
624 # endif
625  [copier = std::function<void(const CopyData &)>(copier)](
626  ItemType *current_item) {
627  if (copier)
628  {
629  // Initiate copying data. For the same reasons as in the worker
630  // class above, catch exceptions rather than letting them
631  // propagate into unknown territories:
632  for (unsigned int i = 0; i < current_item->n_iterators; ++i)
633  {
634  try
635  {
636  copier(current_item->copy_datas[i]);
637  }
638  catch (const std::exception &exc)
639  {
641  }
642  catch (...)
643  {
645  }
646  }
647  }
648  // mark current item as usable again
649  current_item->currently_in_use = false;
650  });
651 
652 
653  //
654  // ----- The pipeline -----
655  //
656  // Now create a pipeline from these stages and execute it:
657  tbb::parallel_pipeline(queue_length,
658  tbb_item_stream_filter & tbb_worker_filter &
659  tbb_copier_filter);
660  }
661 
662  } // namespace tbb_no_coloring
663 # endif // DEAL_II_WITH_TBB
664 
665 
672  namespace sequential
673  {
677  template <typename Worker,
678  typename Copier,
679  typename Iterator,
680  typename ScratchData,
681  typename CopyData>
682  void
683  run(const Iterator & begin,
684  const typename identity<Iterator>::type &end,
685  Worker worker,
686  Copier copier,
687  const ScratchData & sample_scratch_data,
688  const CopyData & sample_copy_data)
689  {
690  // need to copy the sample since it is marked const
691  ScratchData scratch_data = sample_scratch_data;
692  CopyData copy_data = sample_copy_data; // NOLINT
693 
694  // Optimization: Check if the functions are not the zero function. To
695  // check zero-ness, create a C++ function out of it:
696  const bool have_worker =
697  (static_cast<const std::function<
698  void(const Iterator &, ScratchData &, CopyData &)> &>(worker)) !=
699  nullptr;
700  const bool have_copier =
701  (static_cast<const std::function<void(const CopyData &)> &>(
702  copier)) != nullptr;
703 
704  // Finally loop over all items and perform the necessary work:
705  for (Iterator i = begin; i != end; ++i)
706  {
707  if (have_worker)
708  worker(i, scratch_data, copy_data);
709  if (have_copier)
710  copier(copy_data);
711  }
712  }
713 
714 
715 
719  template <typename Worker,
720  typename Copier,
721  typename Iterator,
722  typename ScratchData,
723  typename CopyData>
724  void
725  run(const std::vector<std::vector<Iterator>> &colored_iterators,
726  Worker worker,
727  Copier copier,
728  const ScratchData & sample_scratch_data,
729  const CopyData & sample_copy_data)
730  {
731  // need to copy the sample since it is marked const
732  ScratchData scratch_data = sample_scratch_data;
733  CopyData copy_data = sample_copy_data; // NOLINT
734 
735  // Optimization: Check if the functions are not the zero function. To
736  // check zero-ness, create a C++ function out of it:
737  const bool have_worker =
738  (static_cast<const std::function<
739  void(const Iterator &, ScratchData &, CopyData &)> &>(worker)) !=
740  nullptr;
741  const bool have_copier =
742  (static_cast<const std::function<void(const CopyData &)> &>(
743  copier)) != nullptr;
744 
745  // Finally loop over all items and perform the necessary work:
746  for (unsigned int color = 0; color < colored_iterators.size(); ++color)
747  if (colored_iterators[color].size() > 0)
748  for (auto &it : colored_iterators[color])
749  {
750  if (have_worker)
751  worker(it, scratch_data, copy_data);
752  if (have_copier)
753  copier(copy_data);
754  }
755  }
756 
757  } // namespace sequential
758 
759 
760 
761 # ifdef DEAL_II_WITH_TBB
769  namespace tbb_colored
770  {
776  template <typename Iterator, typename ScratchData, typename CopyData>
778  {
779  std::unique_ptr<ScratchData> scratch_data;
780  std::unique_ptr<CopyData> copy_data;
782 
787  : currently_in_use(false)
788  {}
789 
790  ScratchAndCopyDataObjects(std::unique_ptr<ScratchData> &&p,
791  std::unique_ptr<CopyData> && q,
792  const bool in_use)
793  : scratch_data(std::move(p))
794  , copy_data(std::move(q))
795  , currently_in_use(in_use)
796  {}
797 
798  // Provide a copy constructor that actually doesn't copy the
799  // internal state. This makes handling ScratchAndCopyDataObjects
800  // easier to handle with STL containers.
802  : currently_in_use(false)
803  {}
804  };
805 
806 
807 
813  template <typename Iterator, typename ScratchData, typename CopyData>
815  {
816  public:
821  const std::function<void(const Iterator &, ScratchData &, CopyData &)>
822  & worker,
823  const std::function<void(const CopyData &)> &copier,
824  const ScratchData & sample_scratch_data,
825  const CopyData & sample_copy_data)
826  : worker(worker)
827  , copier(copier)
830  {}
831 
832 
837  void
838  operator()(const tbb::blocked_range<
839  typename std::vector<Iterator>::const_iterator> &range)
840  {
841  // we need to find an unused scratch and corresponding copy
842  // data object in the list that corresponds to the current
843  // thread and then mark it as used. If we can't find one,
844  // create one as discussed in the discussion of the documentation
845  // of the IteratorRangeToItemStream::scratch_data variable,
846  // there is no need to synchronize access to this variable
847  // using a mutex as long as we have no yield-point in between.
848  // This means that we can't take an iterator into the list
849  // now and expect it to still be valid after calling the worker,
850  // but we at least do not have to lock the following section.
851  ScratchData *scratch_data = nullptr;
852  CopyData * copy_data = nullptr;
853  {
854  ScratchAndCopyDataList &scratch_and_copy_data_list = data.get();
855 
856  // see if there is an unused object. if so, grab it and mark
857  // it as used
858  for (typename ScratchAndCopyDataList::iterator p =
859  scratch_and_copy_data_list.begin();
860  p != scratch_and_copy_data_list.end();
861  ++p)
862  if (p->currently_in_use == false)
863  {
864  scratch_data = p->scratch_data.get();
865  copy_data = p->copy_data.get();
866  p->currently_in_use = true;
867  break;
868  }
869 
870  // if no element in the list was found, create one and mark it as
871  // used
872  if (scratch_data == nullptr)
873  {
874  Assert(copy_data == nullptr, ExcInternalError());
875 
876  scratch_and_copy_data_list.emplace_back(
877  std::make_unique<ScratchData>(sample_scratch_data),
878  std::make_unique<CopyData>(sample_copy_data),
879  true);
880  scratch_data =
881  scratch_and_copy_data_list.back().scratch_data.get();
882  copy_data = scratch_and_copy_data_list.back().copy_data.get();
883  }
884  }
885 
886  // then call the worker and copier functions on each
887  // element of the chunk we were given.
888  for (typename std::vector<Iterator>::const_iterator p = range.begin();
889  p != range.end();
890  ++p)
891  {
892  try
893  {
894  if (worker)
895  worker(*p, *scratch_data, *copy_data);
896  if (copier)
897  copier(*copy_data);
898  }
899  catch (const std::exception &exc)
900  {
902  }
903  catch (...)
904  {
906  }
907  }
908 
909  // finally mark the scratch object as unused again. as above, there
910  // is no need to lock anything here since the object we work on
911  // is thread-local
912  {
913  ScratchAndCopyDataList &scratch_and_copy_data_list = data.get();
914 
915  for (typename ScratchAndCopyDataList::iterator p =
916  scratch_and_copy_data_list.begin();
917  p != scratch_and_copy_data_list.end();
918  ++p)
919  if (p->scratch_data.get() == scratch_data)
920  {
921  Assert(p->currently_in_use == true, ExcInternalError());
922  p->currently_in_use = false;
923  }
924  }
925  }
926 
927  private:
928  using ScratchAndCopyDataObjects = typename tbb_colored::
929  ScratchAndCopyDataObjects<Iterator, ScratchData, CopyData>;
930 
935  using ScratchAndCopyDataList = std::list<ScratchAndCopyDataObjects>;
936 
938 
943  const std::function<void(const Iterator &, ScratchData &, CopyData &)>
945 
950  const std::function<void(const CopyData &)> copier;
951 
955  const ScratchData &sample_scratch_data;
956  const CopyData & sample_copy_data;
957  };
958 
962  template <typename Worker,
963  typename Copier,
964  typename Iterator,
965  typename ScratchData,
966  typename CopyData>
967  void
968  run(const std::vector<std::vector<Iterator>> &colored_iterators,
969  Worker worker,
970  Copier copier,
971  const ScratchData & sample_scratch_data,
972  const CopyData & sample_copy_data,
973  const unsigned int chunk_size)
974  {
975  // loop over the various colors of what we're given
976  for (unsigned int color = 0; color < colored_iterators.size(); ++color)
977  if (colored_iterators[color].size() > 0)
978  {
979  using WorkerAndCopier = internal::tbb_colored::
980  WorkerAndCopier<Iterator, ScratchData, CopyData>;
981 
982  WorkerAndCopier worker_and_copier(worker,
983  copier,
984  sample_scratch_data,
985  sample_copy_data);
986 
988  colored_iterators[color].begin(),
989  colored_iterators[color].end(),
990  [&worker_and_copier](
991  const tbb::blocked_range<
992  typename std::vector<Iterator>::const_iterator> &range) {
993  worker_and_copier(range);
994  },
995  chunk_size);
996  }
997  }
998 
999  } // namespace tbb_colored
1000 # endif // DEAL_II_WITH_TBB
1001 
1002 
1003  } // namespace internal
1004 
1005 
1006 
1054  template <typename Worker,
1055  typename Copier,
1056  typename Iterator,
1057  typename ScratchData,
1058  typename CopyData>
1059  void
1060  run(const std::vector<std::vector<Iterator>> &colored_iterators,
1061  Worker worker,
1062  Copier copier,
1063  const ScratchData & sample_scratch_data,
1064  const CopyData & sample_copy_data,
1065  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1066  const unsigned int chunk_size = 8);
1067 
1068 
1118  template <typename Worker,
1119  typename Copier,
1120  typename Iterator,
1121  typename ScratchData,
1122  typename CopyData>
1123  void
1124  run(const Iterator & begin,
1125  const typename identity<Iterator>::type &end,
1126  Worker worker,
1127  Copier copier,
1128  const ScratchData & sample_scratch_data,
1129  const CopyData & sample_copy_data,
1130  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1131  const unsigned int chunk_size = 8)
1132  {
1133  Assert(queue_length > 0,
1134  ExcMessage("The queue length must be at least one, and preferably "
1135  "larger than the number of processors on this system."));
1136  (void)queue_length; // removes -Wunused-parameter warning in optimized mode
1137  Assert(chunk_size > 0, ExcMessage("The chunk_size must be at least one."));
1138  (void)chunk_size; // removes -Wunused-parameter warning in optimized mode
1139 
1140  // If no work then skip. (only use operator!= for iterators since we may
1141  // not have an equality comparison operator)
1142  if (!(begin != end))
1143  return;
1144 
1145  if (MultithreadInfo::n_threads() > 1)
1146  {
1147 # ifdef DEAL_II_WITH_TBB
1148  if (static_cast<const std::function<void(const CopyData &)> &>(copier))
1149  {
1150  // If we have a copier, run the algorithm:
1152  end,
1153  worker,
1154  copier,
1155  sample_scratch_data,
1156  sample_copy_data,
1157  queue_length,
1158  chunk_size);
1159  }
1160  else
1161  {
1162  // There is no copier function. in this case, we have an
1163  // embarrassingly parallel problem where we can
1164  // essentially apply parallel_for. because parallel_for
1165  // requires subdividing the range for which operator- is
1166  // necessary between iterators, it is often inefficient to
1167  // apply it directly to cell ranges and similar iterator
1168  // types for which operator- is expensive or, in fact,
1169  // nonexistent. rather, in that case, we simply copy the
1170  // iterators into a large array and use operator- on
1171  // iterators to this array of iterators.
1172  //
1173  // instead of duplicating code, this is essentially the
1174  // same situation we have in the colored implementation below, so we
1175  // just defer to that place
1176  std::vector<std::vector<Iterator>> all_iterators(1);
1177  for (Iterator p = begin; p != end; ++p)
1178  all_iterators[0].push_back(p);
1179 
1180  run(all_iterators,
1181  worker,
1182  copier,
1183  sample_scratch_data,
1184  sample_copy_data,
1185  queue_length,
1186  chunk_size);
1187  }
1188 
1189  // exit this function to not run the sequential version below:
1190  return;
1191 # endif
1192  }
1193 
1194  // no TBB installed or we are requested to run sequentially:
1196  begin, end, worker, copier, sample_scratch_data, sample_copy_data);
1197  }
1198 
1199 
1200 
1208  template <typename Worker,
1209  typename Copier,
1210  typename IteratorRangeType,
1211  typename ScratchData,
1212  typename CopyData,
1213  typename = std::enable_if_t<has_begin_and_end<IteratorRangeType>>>
1214  void
1215  run(IteratorRangeType iterator_range,
1216  Worker worker,
1217  Copier copier,
1218  const ScratchData &sample_scratch_data,
1219  const CopyData & sample_copy_data,
1220  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1221  const unsigned int chunk_size = 8)
1222  {
1223  // Call the function above
1224  run(iterator_range.begin(),
1225  iterator_range.end(),
1226  worker,
1227  copier,
1228  sample_scratch_data,
1229  sample_copy_data,
1230  queue_length,
1231  chunk_size);
1232  }
1233 
1234 
1235 
1239  template <typename Worker,
1240  typename Copier,
1241  typename Iterator,
1242  typename ScratchData,
1243  typename CopyData>
1244  void
1245  run(const IteratorRange<Iterator> &iterator_range,
1246  Worker worker,
1247  Copier copier,
1248  const ScratchData & sample_scratch_data,
1249  const CopyData & sample_copy_data,
1250  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1251  const unsigned int chunk_size = 8)
1252  {
1253  // Call the function above
1254  run(iterator_range.begin(),
1255  iterator_range.end(),
1256  worker,
1257  copier,
1258  sample_scratch_data,
1259  sample_copy_data,
1260  queue_length,
1261  chunk_size);
1262  }
1263 
1264 
1265 
1266  template <typename Worker,
1267  typename Copier,
1268  typename Iterator,
1269  typename ScratchData,
1270  typename CopyData>
1271  void
1272  run(const std::vector<std::vector<Iterator>> &colored_iterators,
1273  Worker worker,
1274  Copier copier,
1275  const ScratchData & sample_scratch_data,
1276  const CopyData & sample_copy_data,
1277  const unsigned int queue_length,
1278  const unsigned int chunk_size)
1279  {
1280  Assert(queue_length > 0,
1281  ExcMessage("The queue length must be at least one, and preferably "
1282  "larger than the number of processors on this system."));
1283  (void)queue_length; // removes -Wunused-parameter warning in optimized mode
1284  Assert(chunk_size > 0, ExcMessage("The chunk_size must be at least one."));
1285  (void)chunk_size; // removes -Wunused-parameter warning in optimized mode
1286 
1287 
1288  if (MultithreadInfo::n_threads() > 1)
1289  {
1290 # ifdef DEAL_II_WITH_TBB
1291  internal::tbb_colored::run(colored_iterators,
1292  worker,
1293  copier,
1294  sample_scratch_data,
1295  sample_copy_data,
1296  chunk_size);
1297 
1298  // exit this function to not run the sequential version below:
1299  return;
1300 # endif
1301  }
1302 
1303  // run all colors sequentially:
1304  {
1305  internal::sequential::run(colored_iterators,
1306  worker,
1307  copier,
1308  sample_scratch_data,
1309  sample_copy_data);
1310  }
1311  }
1312 
1313 
1314 
1356  template <typename MainClass,
1357  typename Iterator,
1358  typename ScratchData,
1359  typename CopyData>
1360  void
1361  run(const Iterator & begin,
1362  const typename identity<Iterator>::type &end,
1363  MainClass & main_object,
1364  void (MainClass::*worker)(const Iterator &, ScratchData &, CopyData &),
1365  void (MainClass::*copier)(const CopyData &),
1366  const ScratchData &sample_scratch_data,
1367  const CopyData & sample_copy_data,
1368  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1369  const unsigned int chunk_size = 8)
1370  {
1371  // forward to the other function
1372  run(
1373  begin,
1374  end,
1375  [&main_object, worker](const Iterator &iterator,
1376  ScratchData & scratch_data,
1377  CopyData & copy_data) {
1378  (main_object.*worker)(iterator, scratch_data, copy_data);
1379  },
1380  [&main_object, copier](const CopyData &copy_data) {
1381  (main_object.*copier)(copy_data);
1382  },
1383  sample_scratch_data,
1384  sample_copy_data,
1385  queue_length,
1386  chunk_size);
1387  }
1388 
1389 
1390  template <typename MainClass,
1391  typename Iterator,
1392  typename ScratchData,
1393  typename CopyData>
1394  void
1397  MainClass &main_object,
1398  void (MainClass::*worker)(const Iterator &, ScratchData &, CopyData &),
1399  void (MainClass::*copier)(const CopyData &),
1400  const ScratchData &sample_scratch_data,
1401  const CopyData & sample_copy_data,
1402  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1403  const unsigned int chunk_size = 8)
1404  {
1405  // forward to the other function
1406  run(
1407  begin,
1408  end,
1409  [&main_object, worker](const Iterator &iterator,
1410  ScratchData & scratch_data,
1411  CopyData & copy_data) {
1412  (main_object.*worker)(iterator, scratch_data, copy_data);
1413  },
1414  [&main_object, copier](const CopyData &copy_data) {
1415  (main_object.*copier)(copy_data);
1416  },
1417  sample_scratch_data,
1418  sample_copy_data,
1419  queue_length,
1420  chunk_size);
1421  }
1422 
1423 
1424 
1432  template <typename MainClass,
1433  typename IteratorRangeType,
1434  typename ScratchData,
1435  typename CopyData,
1436  typename = std::enable_if_t<has_begin_and_end<IteratorRangeType>>>
1437  void
1438  run(IteratorRangeType iterator_range,
1439  MainClass & main_object,
1440  void (MainClass::*worker)(
1442  ScratchData &,
1443  CopyData &),
1444  void (MainClass::*copier)(const CopyData &),
1445  const ScratchData &sample_scratch_data,
1446  const CopyData & sample_copy_data,
1447  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1448  const unsigned int chunk_size = 8)
1449  {
1450  // Call the function above
1451  run(std::begin(iterator_range),
1452  std::end(iterator_range),
1453  main_object,
1454  worker,
1455  copier,
1456  sample_scratch_data,
1457  sample_copy_data,
1458  queue_length,
1459  chunk_size);
1460  }
1461 
1462 
1463 
1467  template <typename MainClass,
1468  typename Iterator,
1469  typename ScratchData,
1470  typename CopyData>
1471  void
1473  MainClass & main_object,
1474  void (MainClass::*worker)(const Iterator &, ScratchData &, CopyData &),
1475  void (MainClass::*copier)(const CopyData &),
1476  const ScratchData &sample_scratch_data,
1477  const CopyData & sample_copy_data,
1478  const unsigned int queue_length = 2 * MultithreadInfo::n_threads(),
1479  const unsigned int chunk_size = 8)
1480  {
1481  // Call the function above
1482  run(std::begin(iterator_range),
1483  std::end(iterator_range),
1484  main_object,
1485  worker,
1486  copier,
1487  sample_scratch_data,
1488  sample_copy_data,
1489  queue_length,
1490  chunk_size);
1491  }
1492 
1493 } // namespace WorkStream
1494 
1495 
1496 
1498 
1499 
1500 
1501 //---------------------------- work_stream.h ---------------------------
1502 // end of #ifndef dealii_work_stream_h
1503 #endif
1504 //---------------------------- work_stream.h ---------------------------
IteratorOverIterators end() const
IteratorOverIterators begin()
static unsigned int n_threads()
std::list< ScratchAndCopyDataObjects > ScratchAndCopyDataList
Definition: work_stream.h:935
WorkerAndCopier(const std::function< void(const Iterator &, ScratchData &, CopyData &)> &worker, const std::function< void(const CopyData &)> &copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data)
Definition: work_stream.h:820
Threads::ThreadLocalStorage< ScratchAndCopyDataList > data
Definition: work_stream.h:937
void operator()(const tbb::blocked_range< typename std::vector< Iterator >::const_iterator > &range)
Definition: work_stream.h:838
const std::function< void(const Iterator &, ScratchData &, CopyData &)> worker
Definition: work_stream.h:944
typename tbb_colored::ScratchAndCopyDataObjects< Iterator, ScratchData, CopyData > ScratchAndCopyDataObjects
Definition: work_stream.h:929
const std::function< void(const CopyData &)> copier
Definition: work_stream.h:950
IteratorRangeToItemStream(const Iterator &begin, const Iterator &end, const unsigned int buffer_size, const unsigned int chunk_size, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data)
Definition: work_stream.h:325
Threads::ThreadLocalStorage< typename ItemType::ScratchDataList > thread_local_scratch
Definition: work_stream.h:448
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:458
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
Definition: config.h:472
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:459
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
Definition: config.h:512
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
Definition: exceptions.h:1501
static ::ExceptionBase & ExcMessage(std::string arg1)
constexpr int chunk_size
Definition: cuda_size.h:35
void handle_std_exception(const std::exception &exc)
VectorType::value_type * begin(VectorType &V)
VectorType::value_type * end(VectorType &V)
std::vector< unsigned int > serial(const std::vector< unsigned int > &targets, const std::function< RequestType(const unsigned int)> &create_request, const std::function< AnswerType(const unsigned int, const RequestType &)> &answer_request, const std::function< void(const unsigned int, const AnswerType &)> &process_answer, const MPI_Comm &comm)
void run(const Iterator &begin, const typename identity< Iterator >::type &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data)
Definition: work_stream.h:683
void run(const std::vector< std::vector< Iterator >> &colored_iterators, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int chunk_size)
Definition: work_stream.h:968
void run(const Iterator &begin, const typename identity< Iterator >::type &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
Definition: work_stream.h:474
void run(const std::vector< std::vector< Iterator >> &colored_iterators, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length=2 *MultithreadInfo::n_threads(), const unsigned int chunk_size=8)
Definition: work_stream.h:1272
void parallel_for(Iterator x_begin, Iterator x_end, const Functor &functor, const unsigned int grainsize)
Definition: parallel.h:80
ScratchAndCopyDataObjects(const ScratchAndCopyDataObjects &)
Definition: work_stream.h:801
ScratchAndCopyDataObjects(std::unique_ptr< ScratchData > &&p, std::unique_ptr< CopyData > &&q, const bool in_use)
Definition: work_stream.h:790
Threads::ThreadLocalStorage< ScratchDataList > * scratch_data
Definition: work_stream.h:292