-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Srinath Vadlamani
committed
Apr 1, 2015
1 parent
770b790
commit 15e0e19
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "tbb/blocked_range.h" | ||
#include "tbb/parallel_for.h" | ||
#include "tbb/task_scheduler_init.h" | ||
#include <iostream> | ||
#include <vector> | ||
|
||
struct mytask { | ||
mytask(size_t n) | ||
:_n(n) | ||
{} | ||
void operator()() { | ||
for (int i=0;i<1000000;++i) {} // Deliberately run slow | ||
std::cerr << "[" << _n << "]"; | ||
} | ||
size_t _n; | ||
}; | ||
|
||
struct executor | ||
{ | ||
executor(std::vector<mytask>& t) | ||
:_tasks(t) | ||
{} | ||
executor(executor& e,tbb::split) | ||
:_tasks(e._tasks) | ||
{} | ||
|
||
void operator()(const tbb::blocked_range<size_t>& r) const { | ||
for (size_t i=r.begin();i!=r.end();++i) | ||
_tasks[i](); | ||
} | ||
|
||
std::vector<mytask>& _tasks; | ||
}; | ||
|
||
int main(int,char**) { | ||
|
||
tbb::task_scheduler_init init; // Automatic number of threads | ||
// tbb::task_scheduler_init init(2); // Explicit number of threads | ||
|
||
std::vector<mytask> tasks; | ||
for (int i=0;i<1000;++i) | ||
tasks.push_back(mytask(i)); | ||
|
||
executor exec(tasks); | ||
tbb::parallel_for(tbb::blocked_range<size_t>(0,tasks.size()),exec); | ||
std::cerr << std::endl; | ||
|
||
return 0; | ||
} |