Skip to content

Commit

Permalink
a tbb example
Browse files Browse the repository at this point in the history
  • Loading branch information
Srinath Vadlamani committed Apr 1, 2015
1 parent 770b790 commit 15e0e19
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions codingTests/cplusplus/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def shellCommand(command,errorMessage):
TAU_MAKEFILE=/work/02463/srinathv/tau2/x86_64/lib/Makefile.tau-intelImpiCuda.aac-icpc-papi-mpi-cupti-pdt; \
export TAU_OPTIONS="-optLinkOnly -optVerbose"'
OMP_FLAG=' -DOMP -openmp '
TBB_FLAG=' -DUSE_TBB -tbb '



Expand Down Expand Up @@ -55,6 +56,8 @@ def main():

parser.add_argument('-o','--openmp',action="store_true",
help='Compile with OpenMP')
parser.add_argument('-tbb','--tbb',action="store_true",
help='Compile with Intel Thread building blocks.')
args = parser.parse_args()

if (args.debug):
Expand All @@ -73,6 +76,8 @@ def main():
IFDEF = IFDEF + '-DDEQ '
if (args.numelems):
IFDEF = IFDEF + '-DSET_N='+str(args.numelems)
if (args.tbb):
IFDEF = IFDEF + TBB_FLAG
logging.debug('IFDEF is ' + IFDEF)


Expand Down
4 changes: 4 additions & 0 deletions codingTests/cplusplus/testVec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#ifdef USE_TAU
#include <TAU.h>
#endif
#ifdef USE_TBB
#include "tbb/concurrent_vector.h"
#endif


using std::vector;

Expand Down
49 changes: 49 additions & 0 deletions codingTests/intelTbb/example1.cpp
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;
}

0 comments on commit 15e0e19

Please sign in to comment.