Advertisement
canezzy

notSoSimpleDag

Apr 25th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <list>
  5. #include <string>
  6. #include "tbb/task_scheduler_init.h"
  7. #include "tbb/tick_count.h"
  8. #include "tbb/task.h"
  9.  
  10. using namespace tbb;
  11. using namespace std;
  12.  
  13. #define WORKLOAD 100000000
  14.  
  15. void do_work() {
  16.     for( int i=0; i<WORKLOAD; i++ ){
  17.         ;
  18.     }
  19. }
  20.  
  21. class Dummy: public tbb::task {
  22.     list<Dummy*> succ;
  23.     string message;
  24. public:
  25.     Dummy(string msg) {
  26.         message = msg;
  27.     }
  28.     void addSuccessor(Dummy* suc) {
  29.         succ.push_back(suc);
  30.     }
  31.     tbb::task* execute( ) {
  32.         __TBB_ASSERT(ref_count() == 0, "DUVAJ GA");
  33.         cout << message << " Hello from execute()!\n";
  34.         do_work();
  35.         for (Dummy* dummy : succ)
  36.         {
  37.             dummy->decrement_ref_count();
  38.             if (dummy->ref_count() == 0) {
  39.                 spawn(*dummy);
  40.             }
  41.         }
  42.        
  43.  
  44.         return NULL;
  45.     }
  46. };
  47.  
  48. void serial() {
  49.     //ToDo: Replace with actual tasks  
  50.     Dummy *t1 = new( tbb::task::allocate_root( ) ) Dummy("task A");
  51.     t1->execute();
  52.     t1->destroy(*t1);
  53.     Dummy *t2 = new( tbb::task::allocate_root( ) ) Dummy("task B");
  54.     t2->execute();
  55.     t2->destroy(*t2);
  56.     Dummy *t3 = new( tbb::task::allocate_root( ) ) Dummy("task C");
  57.     t3->execute();
  58.     t3->destroy(*t3);
  59.     Dummy *t4 = new(tbb::task::allocate_root()) Dummy("task D");
  60.     t4->execute();
  61.     t4->destroy(*t4);
  62.     Dummy *t5 = new(tbb::task::allocate_root()) Dummy("task F");
  63.     t5->execute();
  64.     t5->destroy(*t5);
  65.     return;
  66. }
  67.  
  68. void BuildAndEvaluateDAG() {   
  69.     Dummy* parent = new(task::allocate_root()) Dummy("TASK E");
  70.     parent->set_ref_count(3);
  71.     Dummy* f = new(task::allocate_root()) Dummy("TASK F");
  72.     Dummy* d = new(task::allocate_root()) Dummy("TASK D");
  73.     Dummy* c = new(task::allocate_root()) Dummy("TASK C");
  74.     Dummy* b = new(task::allocate_root()) Dummy("TASK B");
  75.     Dummy* a = new(task::allocate_root()) Dummy("TASK A");
  76.     f->set_ref_count(0);
  77.     d->set_ref_count(0);
  78.     c->set_ref_count(0);
  79.  
  80.     b->set_ref_count(2);
  81.     a->set_ref_count(2);
  82.     a->addSuccessor(parent);
  83.     b->addSuccessor(parent);
  84.     f->addSuccessor(b);
  85.     d->addSuccessor(a);
  86.     c->addSuccessor(a);
  87.     c->addSuccessor(b);
  88.     parent->spawn(*d);
  89.     parent->spawn(*c);
  90.     parent->spawn_and_wait_for_all(*f);
  91.  
  92.     parent->execute();
  93.     parent->destroy(*parent);
  94.  
  95.     return;
  96. }
  97.  
  98. void parallel() {
  99.     BuildAndEvaluateDAG();
  100.     return;
  101. }
  102.  
  103. int main( int argc, char* argv[] ) {
  104.  
  105.     tick_count startTime = tick_count::now();
  106.     serial();
  107.     tick_count stopTime = tick_count::now();
  108.     cout << "serial finished. Took: " <<
  109.         (stopTime - startTime).seconds()*1000 << "ms." << endl;
  110.  
  111.     startTime = tick_count::now();
  112.     parallel();
  113.     stopTime = tick_count::now();
  114.     cout << "parallel finished. Took: " <<
  115.         (stopTime - startTime).seconds()*1000 << "ms." << endl;
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement