Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include "tbb/tick_count.h"
  4. #include "tbb/task.h"
  5. #include "tbb/task_scheduler_init.h"
  6. #include <list>
  7.  
  8. using namespace std;
  9. using namespace tbb;
  10.  
  11.  
  12. // Matrix dimenstion
  13. #define M       2
  14.  
  15.  
  16. unsigned int r[M][M];
  17.  
  18. unsigned int rr[M][M];
  19.  
  20. unsigned int rrr[M][M];
  21.  
  22.  
  23.  
  24.  
  25. unsigned int in[M][M] = {
  26.     { 0, 1 },
  27.     { 2, 3 },
  28. };
  29.  
  30. /*unsigned matrixIn = {
  31. { 1, 2, 3, 4, 5, 6, 7, 8 },
  32. { 1, 2, 3, 4, 5, 6, 7, 8 },
  33. { 1, 2, 3, 4, 5, 6, 7, 8 },
  34. { 1, 2, 3, 4, 5, 6, 7, 8 },
  35. { 1, 2, 3, 4, 5, 6, 7, 8 },
  36. { 1, 2, 3, 4, 5, 6, 7, 8 },
  37. { 1, 2, 3, 4, 5, 6, 7, 8 },
  38. { 1, 2, 3, 4, 5, 6, 7, 8 }
  39. }; */
  40.  
  41.  
  42. unsigned int c[M][M] = {
  43.     { 4, 5 },
  44.     { 6, 7 },
  45. };
  46.  
  47. /*unsigned matrixC = {
  48. { 1, 2, 3, 4, 5, 6, 7, 8 },
  49. { 1, 2, 3, 4, 5, 6, 7, 8 },
  50. { 1, 2, 3, 4, 5, 6, 7, 8 },
  51. { 1, 2, 3, 4, 5, 6, 7, 8 },
  52. { 1, 2, 3, 4, 5, 6, 7, 8 },
  53. { 1, 2, 3, 4, 5, 6, 7, 8 },
  54. { 1, 2, 3, 4, 5, 6, 7, 8 },
  55. { 1, 2, 3, 4, 5, 6, 7, 8 }
  56. }; */
  57.  
  58. unsigned int alpha[M][M] = {
  59.     { 8, 9 },
  60.     { 10, 11 },
  61. };
  62.  
  63. /*unsigned matrixAlpha = {
  64. { 1, 2, 3, 4, 5, 6, 7, 8 },
  65. { 1, 2, 3, 4, 5, 6, 7, 8 },
  66. { 1, 2, 3, 4, 5, 6, 7, 8 },
  67. { 1, 2, 3, 4, 5, 6, 7, 8 },
  68. { 1, 2, 3, 4, 5, 6, 7, 8 },
  69. { 1, 2, 3, 4, 5, 6, 7, 8 },
  70. { 1, 2, 3, 4, 5, 6, 7, 8 },
  71. { 1, 2, 3, 4, 5, 6, 7, 8 }
  72. }; */
  73.  
  74.  
  75. class RRR : public tbb::task {
  76. public:
  77.     int sum;
  78.     int i;
  79.  
  80.     RRR(int i) : i(i) {};
  81.     list<empty_task*> successors;
  82.     task* execute() {
  83.         __TBB_ASSERT(ref_count() == 0, NULL);
  84.         sum = 0;
  85.  
  86.         for (int k = 0; k < M; k++) {
  87.             sum += rr[i][k] * alpha[i][k];
  88.             rrr[i][k] = sum;
  89.         }
  90.         cout << "task rrr executing...\n";
  91.         for (auto i = successors.begin(); i != successors.end(); i++) {
  92.             if (empty_task * t = *i) {
  93.                 if (t->decrement_ref_count() == 0) {
  94.                     spawn(*t);
  95.                 }
  96.             }
  97.         }
  98.         return NULL;
  99.     }
  100. };
  101.  
  102.  
  103. class RR :  public tbb::task {
  104. public:
  105.     int sum;
  106.     int i, j;
  107.     list<RRR*> successors;
  108.     RR(int i, int j) : i(i), j(j) {};
  109.  
  110.  
  111.     task* execute() {
  112.         __TBB_ASSERT(ref_count() == 0, NULL);
  113.         sum = 0;
  114.         for (int k = 0; k < M; k++) {
  115.             sum += in[i][k] * c[k][j];
  116.         }
  117.         rr[i][j] = sum;
  118.         cout << "task rr executing...\n";
  119.         list<RRR*>::const_iterator it;
  120.         for (it = successors.begin(); it != successors.end(); it++) {
  121.             if (RRR * t = *it) {
  122.                 if (t->decrement_ref_count() == 0) {
  123.                     spawn(*t);
  124.                 }
  125.             }
  126.         }
  127.         return NULL;
  128.     }
  129. };
  130.  
  131.  
  132. class R : public tbb::task {
  133. public:
  134.     int i, j;
  135.     int sum;
  136.  
  137.     R(int i, int j) : i(i), j(j) {};
  138.     list<RR*> successors;
  139.  
  140.     task* execute() {
  141.         __TBB_ASSERT(ref_count() == 0, NULL);
  142.         sum = 0;
  143.         for (int k = 0; k < M; k++) {
  144.             sum += in[i][k] * c[k][j];
  145.         }
  146.         r[i][j] = sum;
  147.         cout << "task r executing...\n";
  148.         list<RR*>::const_iterator it;
  149.         for (it = successors.begin(); it != successors.end(); it++) {
  150.  
  151.             if (RR * t = *it) {
  152.                 if (t->decrement_ref_count() == 0) {
  153.                     spawn(*t);
  154.                 }
  155.             }
  156.  
  157.         }
  158.         return NULL;
  159.     }
  160. };
  161.  
  162. void BuildAndEvaluateDAG() {
  163.     /*
  164.     empty_task* e = new(task::allocate_root()) empty_task();
  165.     e->set_ref_count(M + 1);
  166.     task_list list;
  167.     R* ra[M][M];
  168.     RR* rra[M][M];
  169.     for (int i = 0; i < M; i++) {
  170.         RRR* rrrt = new RRR(i);
  171.         rrrt->successors.push_back(e);
  172.         rrrt->set_ref_count(M);
  173.  
  174.         for (int j = 0; j < M; j++) {
  175.             RR* rrt = new(task::allocate_root()) RR(i, j);
  176.             rrt->successors.push_back(rrrt);
  177.             rrt->set_ref_count(M);
  178.             R* rt = new(task::allocate_root()) R(i, j);
  179.             rt->successors.push_back(rrt);
  180.             ra[i][j] = rt;
  181.             rra[i][j] = rrt;
  182.         }
  183.     }
  184.     for (int i = 0; i < M; i++) {
  185.         for (int j = 0; j < M; j++) {
  186.             list.push_back(*ra[i][j]);
  187.         }
  188.     }
  189.  
  190.     e->spawn_and_wait_for_all(list);
  191.     e->destroy(*e);
  192.     */
  193.     empty_task* e = new(task::allocate_root()) empty_task;
  194.     e->set_ref_count(M + 1);
  195.     R* r[M][M];
  196.     RR* rr[M][M];
  197.     RRR* rrr[M];
  198.  
  199.     for (int i = 0; i < M; i++) {
  200.         rrr[i] = new(task::allocate_root()) RRR(i);
  201.         rrr[i]->set_ref_count(M);
  202.         rrr[i]->successors.push_back(e);
  203.     }
  204.    
  205.  
  206.     for (int i = 0; i < M; i++) {
  207.         for (int j = 0; j < M; j++) {
  208.             rr[i][j] = new(task::allocate_root()) RR(i, j);
  209.             rr[i][j]->set_ref_count(M);
  210.             rr[i][j]->successors.push_back(rrr[i]);
  211.         }
  212.     }
  213.  
  214.     for (int i = 0; i < M; i++) {
  215.         for (int j = 0; j < M; j++) {
  216.             r[i][j] = new(task::allocate_root()) R(i, j);
  217.             for (int k = 0; k < M; k++) {
  218.                 r[i][j]->successors.push_back(rr[i][k]);
  219.             }
  220.         }
  221.     }
  222.  
  223.    
  224.  
  225.    
  226.  
  227.     task_list list;
  228.     for (int i = 0; i < M; i++) {
  229.         for (int j = 0; j < M; j++) {
  230.             list.push_back(*r[i][j]);
  231.         }
  232.     }
  233.  
  234.     e->spawn_and_wait_for_all(list);
  235.  
  236.     return;
  237.  
  238. }
  239.  
  240. void paralell() {
  241.     BuildAndEvaluateDAG();
  242.     return;
  243. }
  244.  
  245.  
  246. int main(int argc, char* argv[]) {
  247.  
  248.     unsigned int r[M][M];
  249.     unsigned int rr[M][M];
  250.     unsigned int rrr[M][M];
  251.  
  252.     unsigned int i, j, k;
  253.  
  254.     unsigned int sum;
  255.  
  256.     cout << endl << "DCT serial version" << endl;
  257.  
  258.     // measure DCT execution time
  259.     tick_count startTime = tick_count::now();
  260.  
  261.  
  262.     // r = in * c
  263.     for (i = 0; i<M; i++) {
  264.         for (j = 0; j<M; j++) {
  265.             sum = 0;
  266.  
  267.             for (k = 0; k<M; k++) {
  268.                 sum += in[i][k] * c[k][j];
  269.             }
  270.             r[i][j] = sum;
  271.         }
  272.     }
  273.  
  274.     // rr = r * c'
  275.     for (i = 0; i<M; i++) {
  276.         for (j = 0; j<M; j++) {
  277.             sum = 0;
  278.  
  279.             for (k = 0; k<M; k++) {
  280.                 sum += r[i][k] * c[j][k];
  281.             }
  282.             rr[i][j] = sum;
  283.         }
  284.     }
  285.  
  286.     // rrr = rr .* alpha
  287.     for (i = 0; i<M; i++) {
  288.         for (j = 0; j<M; j++) {
  289.             rrr[i][j] = rr[i][j] * alpha[i][j];;
  290.         }
  291.     }
  292.  
  293.  
  294.     // print matrix
  295.     cout << endl;
  296.     for (int i = 0; i < M; ++i)
  297.     {
  298.         for (int j = 0; j < M; ++j)
  299.         {
  300.             cout << rrr[i][j] << " ";
  301.         }
  302.         cout << endl;
  303.     }
  304.  
  305.     // measure DCT execution time
  306.     tick_count endTime = tick_count::now();
  307.  
  308.     cout << endl << "Execution time [" << M << "][" << M << "]: " << (endTime - startTime).seconds() * 1000 << "ms." << endl << endl;
  309.  
  310.  
  311.     tick_count startTimeP = tick_count::now();
  312.  
  313.     paralell();
  314.  
  315.     // print matrix
  316.     cout << endl;
  317.     cout << "printing matrix parallel" << endl;
  318.     for (int i = 0; i < M; ++i)
  319.     {
  320.         for (int j = 0; j < M; ++j)
  321.         {
  322.             cout << rrr[i][j] << " ";
  323.         }
  324.         cout << endl;
  325.     }
  326.  
  327.     // measure DCT execution time
  328.     tick_count endTimeP = tick_count::now();
  329.  
  330.     cout << endl << "Execution time [" << M << "][" << M << "]: " << (endTimeP - startTimeP).seconds() * 1000 << "ms." << endl << endl;
  331.  
  332.  
  333.     getchar();
  334.     return 0;
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement