0030959: OSD_Parallel_TBB: number of execution threads is strictly limited by the...
[occt.git] / src / OSD / OSD_Parallel_TBB.cxx
1 // Created on: 2014-08-19
2 // Created by: Alexander Zaikin
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Version of parallel executor used when TBB is available
18 #ifdef HAVE_TBB
19
20 #include <OSD_Parallel.hxx>
21 #include <OSD_ThreadPool.hxx>
22 #include <Standard_ProgramError.hxx>
23
24 #include <tbb/parallel_for.h>
25 #include <tbb/parallel_for_each.h>
26 #include <tbb/blocked_range.h>
27 #include <tbb/task_scheduler_init.h>
28
29 //=======================================================================
30 //function : forEachExternal
31 //purpose  : 
32 //=======================================================================
33
34 void OSD_Parallel::forEachExternal (UniversalIterator& theBegin,
35                                     UniversalIterator& theEnd,
36                                     const FunctorInterface& theFunctor,
37                                     Standard_Integer theNbItems)
38 {
39   try
40   {
41     const Handle(OSD_ThreadPool)& aThreadPool = OSD_ThreadPool::DefaultPool();
42     const Standard_Integer aNbThreads = theNbItems > 0 ?
43       aThreadPool->NbDefaultThreadsToLaunch() : -1;
44
45     tbb::task_scheduler_init aScheduler (aNbThreads);
46     tbb::parallel_for_each (theBegin, theEnd, theFunctor);
47   }
48   catch (tbb::captured_exception& anException)
49   {
50     throw Standard_ProgramError (anException.what());
51   }
52 }
53
54 #endif /* HAVE_TBB */