Advertisement
canezzy

cinDag

Apr 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include "tbb/task_scheduler_init.h"
  5. #include "tbb/tick_count.h"
  6. #include "tbb/task.h"
  7. #include "math.h"
  8.  
  9. using namespace tbb;
  10. using namespace std;
  11.  
  12. int side;
  13.  
  14. #define WORKLOAD 100000000
  15.  
  16. void do_work() {
  17.     for(int i = 0; i<WORKLOAD; i++) {
  18.         ;
  19.     }
  20. }
  21.  
  22. class Dijagonala : public task {
  23. public:
  24.     float dijagonala;
  25.     task * execute() {
  26.         dijagonala = side * sqrt(2.);
  27.         do_work();
  28.         return NULL;
  29.     }
  30. };
  31.  
  32. class Povrsina : public task {
  33. public:
  34.     int povrsina;
  35.     task * execute() {
  36.         povrsina = side * side;
  37.         do_work();
  38.         return NULL;
  39.     }
  40. };
  41.  
  42. class Obim : public task {
  43. public:
  44.     int obim;
  45.     task * execute() {
  46.         obim = 4 * side;
  47.         do_work();
  48.         return NULL;
  49.     }
  50. };
  51.  
  52. void serial() {
  53.  
  54.     //ToDo: Replace with actual tasks  
  55.     Dijagonala *dijagonalaZadatak = new(tbb::task::allocate_root()) Dijagonala();
  56.     dijagonalaZadatak->execute();
  57.     Povrsina *povrsinaZadatak = new(tbb::task::allocate_root()) Povrsina();
  58.     povrsinaZadatak->execute();
  59.     Obim *obimZadatak = new(tbb::task::allocate_root()) Obim();
  60.     obimZadatak->execute();
  61.  
  62.     cout << "Dijagonala serijske: " << dijagonalaZadatak->dijagonala << endl;
  63.     cout << "Povrsina serijske:" << povrsinaZadatak->povrsina << endl;
  64.     cout << "Obim serijske: " << obimZadatak->obim << endl;
  65. }
  66.  
  67. void BuildAndEvaluateDAG() {
  68.     empty_task* parent = new(task::allocate_root()) empty_task;
  69.     parent->set_ref_count(4);
  70.  
  71.     Obim* obimZadatak = new(parent->allocate_child()) Obim();
  72.     Povrsina* povrsinaZadatak = new(parent->allocate_child()) Povrsina();
  73.     Dijagonala* dijagonalaZadatak = new(parent->allocate_child()) Dijagonala();
  74.  
  75.     __TBB_ASSERT(parent->ref_count() == 4, NULL);
  76.     parent->spawn(*obimZadatak);
  77.     parent->spawn(*povrsinaZadatak);
  78.     parent->spawn_and_wait_for_all(*dijagonalaZadatak);
  79.  
  80.     __TBB_ASSERT(parent->ref_count() == 0, NULL);
  81.     parent->destroy(*parent);
  82.  
  83.     cout << "Dijagonala paralelne: " << dijagonalaZadatak->dijagonala << endl;
  84.     cout << "Povrsina paralelne:" << povrsinaZadatak->povrsina << endl;
  85.     cout << "Obim paralelne: " << obimZadatak->obim << endl;
  86. }
  87.  
  88. void parallel() {
  89.  
  90.     BuildAndEvaluateDAG();
  91. }
  92.  
  93. int main(int argc, char* argv[]) {
  94.  
  95.     if(argc != 2) {
  96.         cout << "Input arguments are wrong!" << endl;
  97.         return 1;
  98.     }
  99.  
  100.     side = atoi(argv[1]);
  101.     tick_count startTime = tick_count::now();
  102.     serial();
  103.     tick_count stopTime = tick_count::now();
  104.     cout << "serial finished. Took: " <<
  105.         (stopTime - startTime).seconds() * 1000 << "ms." << endl << endl;
  106.  
  107.     startTime = tick_count::now();
  108.     parallel();
  109.     stopTime = tick_count::now();
  110.     cout << "parallel finished. Took: " <<
  111.         (stopTime - startTime).seconds() * 1000 << "ms." << endl;
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement