Logo ROOT   6.14/05
Reference Guide
TTaskGroup.cxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Author: Danilo Piparo August 2017
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "RConfigure.h"
13 
14 #include "ROOT/TTaskGroup.hxx"
15 
16 #ifdef R__USE_IMT
17 #include "TROOT.h"
18 #include "tbb/task_group.h"
19 #endif
20 
21 #include <type_traits>
22 
23 /**
24 \class ROOT::Experimental::TTaskGroup
25 \ingroup Parallelism
26 \brief A class to manage the asynchronous execution of work items.
27 
28 A TTaskGroup represents concurrent execution of a group of tasks. Tasks may be dynamically added to the group as it is
29 executing.
30 */
31 
32 namespace ROOT {
33 
34 namespace Experimental {
35 
36 // in the constructor and destructor the casts are present in order to be able
37 // to be independent from the runtime used.
38 // This leaves the door open for other TTaskGroup implementations.
39 
41 {
42 #ifdef R__USE_IMT
44  throw std::runtime_error("Implicit parallelism not enabled. Cannot instantiate a TTaskGroup.");
45  }
46  fTaskContainer = ((TaskContainerPtr_t *)new tbb::task_group());
47 #endif
48 }
49 
51 {
52  *this = std::move(other);
53 }
54 
56 {
57  fTaskContainer = other.fTaskContainer;
58  other.fTaskContainer = nullptr;
59  fCanRun.store(other.fCanRun);
60  return *this;
61 }
62 
64 {
65 #ifdef R__USE_IMT
66  if (!fTaskContainer)
67  return;
68  Wait();
69  delete ((tbb::task_group *)fTaskContainer);
70 #endif
71 }
72 
73 /////////////////////////////////////////////////////////////////////////////
74 /// Cancel all submitted tasks immediately.
76 {
77 #ifdef R__USE_IMT
78  fCanRun = false;
79  ((tbb::task_group *)fTaskContainer)->cancel();
80  fCanRun = true;
81 #endif
82 }
83 
84 /////////////////////////////////////////////////////////////////////////////
85 /// Add to the group an item of work which will be ran asynchronously.
86 /// Adding many small items of work to the TTaskGroup is not efficient,
87 /// unless they run for long enough. If the work to be done is little, look
88 /// try to express nested parallelism or resort to other constructs such as
89 /// the TThreadExecutor.
90 /// Trying to add a work item to the group while it is in waiting state
91 /// makes the method block.
92 void TTaskGroup::Run(const std::function<void(void)> &closure)
93 {
94 #ifdef R__USE_IMT
95  while (!fCanRun)
96  /* empty */;
97 
98  ((tbb::task_group *)fTaskContainer)->run(closure);
99 #else
100  closure();
101 #endif
102 }
103 
104 /////////////////////////////////////////////////////////////////////////////
105 /// Wait until all submitted items of work are completed. This method
106 /// is blocking.
108 {
109 #ifdef R__USE_IMT
110  fCanRun = false;
111  ((tbb::task_group *)fTaskContainer)->wait();
112  fCanRun = true;
113 #endif
114 }
115 }
116 }
TTaskGroup & operator=(TTaskGroup &&other)
Definition: TTaskGroup.cxx:55
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void Wait()
Wait until all submitted items of work are completed.
Definition: TTaskGroup.cxx:107
std::atomic< bool > fCanRun
Definition: TTaskGroup.hxx:33
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:146
A class to manage the asynchronous execution of work items.
Definition: TTaskGroup.hxx:21
void Run(const std::function< void(void)> &closure)
Add to the group an item of work which will be ran asynchronously.
Definition: TTaskGroup.cxx:92
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition: TROOT.cxx:607
void Cancel()
Cancel all submitted tasks immediately.
Definition: TTaskGroup.cxx:75
TaskContainerPtr_t fTaskContainer
Shield completely from implementation.
Definition: TTaskGroup.hxx:32