Advertisement
Guest User

Ispravkaa

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