Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#define TBB_PREVIEW_ISOLATED_TASK_GROUP 1
19#include "tbb/task_group.h"
20#include "tbb/task_arena.h"
21#include "ROOT/RTaskArena.hxx"
22#include "ROpaqueTaskArena.hxx"
23#endif
24
25#include <type_traits>
26#include <stdexcept>
27
28/**
29\class ROOT::Experimental::TTaskGroup
30\brief A class to manage the asynchronous execution of work items.
31
32A TTaskGroup represents concurrent execution of a group of tasks.
33Tasks may be dynamically added to the group as it is executing.
34Nesting TTaskGroup instances may result in a runtime overhead.
35*/
36
37namespace ROOT {
38
39namespace Internal {
40
41#ifdef R__USE_IMT
42tbb::isolated_task_group *CastToTG(void *p)
43{
44 return (tbb::isolated_task_group *)p;
45}
46#endif
47
48} // namespace Internal
49
50namespace Experimental {
51
52using namespace ROOT::Internal;
53
54// in the constructor and destructor the casts are present in order to be able
55// to be independent from the runtime used.
56// This leaves the door open for other TTaskGroup implementations.
57
59{
60#ifdef R__USE_IMT
62 throw std::runtime_error("Implicit parallelism not enabled. Cannot instantiate a TTaskGroup.");
63 }
65 fTaskContainer = ((void *)new tbb::isolated_task_group());
66#endif
67}
68
70{
71 *this = std::move(other);
72}
73
75{
76 fTaskArenaW = other.fTaskArenaW;
77 fTaskContainer = other.fTaskContainer;
78 other.fTaskContainer = nullptr;
79 return *this;
80}
81
83{
84#ifdef R__USE_IMT
85 if (!fTaskContainer)
86 return;
87 Wait();
89#endif
90}
91
92/////////////////////////////////////////////////////////////////////////////
93/// Run operation in the internal task arena to implement work isolation, i.e.
94/// prevent stealing of work items spawned by ancestors.
95void TTaskGroup::ExecuteInIsolation(const std::function<void(void)> &operation)
96{
97#ifdef R__USE_IMT
98 fTaskArenaW->Access().execute([&] { operation(); });
99#else
100 operation();
101#endif
102}
103
104/////////////////////////////////////////////////////////////////////////////
105/// Cancel all submitted tasks immediately.
107{
108#ifdef R__USE_IMT
109 ExecuteInIsolation([&] { CastToTG(fTaskContainer)->cancel(); });
110#endif
111}
112
113/////////////////////////////////////////////////////////////////////////////
114/// Add to the group an item of work which will be ran asynchronously.
115/// Adding many small items of work to the TTaskGroup is not efficient,
116/// unless they run for long enough. If the work to be done is little, look
117/// try to express nested parallelism or resort to other constructs such as
118/// the TThreadExecutor.
119/// Trying to add a work item to the group while it is in waiting state
120/// makes the method block.
121void TTaskGroup::Run(const std::function<void(void)> &closure)
122{
123#ifdef R__USE_IMT
125#else
126 closure();
127#endif
128}
129
130/////////////////////////////////////////////////////////////////////////////
131/// Wait until all submitted items of work are completed. This method
132/// is blocking.
134{
135#ifdef R__USE_IMT
136 ExecuteInIsolation([&] { CastToTG(fTaskContainer)->wait(); });
137#endif
138}
139} // namespace Experimental
140} // namespace ROOT
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
A class to manage the asynchronous execution of work items.
std::shared_ptr< ROOT::Internal::RTaskArenaWrapper > fTaskArenaW
void ExecuteInIsolation(const std::function< void(void)> &operation)
Run operation in the internal task arena to implement work isolation, i.e.
void Run(const std::function< void(void)> &closure)
Add to the group an item of work which will be ran asynchronously.
void Wait()
Wait until all submitted items of work are completed.
void Cancel()
Cancel all submitted tasks immediately.
TTaskGroup & operator=(TTaskGroup &&other)
std::shared_ptr< ROOT::Internal::RTaskArenaWrapper > GetGlobalTaskArena(unsigned maxConcurrency=0)
Factory function returning a shared pointer to the instance of the global RTaskArenaWrapper.
tbb::isolated_task_group * CastToTG(void *p)
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:669