Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include "tbb/tick_count.h"
- #include "tbb/task.h"
- #include "tbb/task_scheduler_init.h"
- #include <list>
- using namespace std;
- using namespace tbb;
- // Matrix dimenstion
- #define M 2
- unsigned int r[M][M];
- unsigned int rr[M][M];
- unsigned int rrr[M][M];
- unsigned int in[M][M] = {
- { 0, 1 },
- { 2, 3 },
- };
- /*unsigned matrixIn = {
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 }
- }; */
- unsigned int c[M][M] = {
- { 4, 5 },
- { 6, 7 },
- };
- /*unsigned matrixC = {
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 }
- }; */
- unsigned int alpha[M][M] = {
- { 8, 9 },
- { 10, 11 },
- };
- /*unsigned matrixAlpha = {
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 },
- { 1, 2, 3, 4, 5, 6, 7, 8 }
- }; */
- class RRR : public tbb::task {
- public:
- int sum;
- int i;
- RRR(int i) : i(i) {};
- list<empty_task*> successors;
- task* execute() {
- __TBB_ASSERT(ref_count() == 0, NULL);
- sum = 0;
- for (int k = 0; k < M; k++) {
- sum += rr[i][k] * alpha[i][k];
- rrr[i][k] = sum;
- }
- cout << "task rrr executing...\n";
- for (auto i = successors.begin(); i != successors.end(); i++) {
- if (empty_task * t = *i) {
- if (t->decrement_ref_count() == 0) {
- spawn(*t);
- }
- }
- }
- return NULL;
- }
- };
- class RR : public tbb::task {
- public:
- int sum;
- int i, j;
- list<RRR*> successors;
- RR(int i, int j) : i(i), j(j) {};
- task* execute() {
- __TBB_ASSERT(ref_count() == 0, NULL);
- sum = 0;
- for (int k = 0; k < M; k++) {
- sum += in[i][k] * c[k][j];
- }
- rr[i][j] = sum;
- cout << "task rr executing...\n";
- list<RRR*>::const_iterator it;
- for (it = successors.begin(); it != successors.end(); it++) {
- if (RRR * t = *it) {
- if (t->decrement_ref_count() == 0) {
- spawn(*t);
- }
- }
- }
- return NULL;
- }
- };
- class R : public tbb::task {
- public:
- int i, j;
- int sum;
- R(int i, int j) : i(i), j(j) {};
- list<RR*> successors;
- task* execute() {
- __TBB_ASSERT(ref_count() == 0, NULL);
- sum = 0;
- for (int k = 0; k < M; k++) {
- sum += in[i][k] * c[k][j];
- }
- r[i][j] = sum;
- cout << "task r executing...\n";
- list<RR*>::const_iterator it;
- for (it = successors.begin(); it != successors.end(); it++) {
- if (RR * t = *it) {
- if (t->decrement_ref_count() == 0) {
- spawn(*t);
- }
- }
- }
- return NULL;
- }
- };
- void BuildAndEvaluateDAG() {
- /*
- empty_task* e = new(task::allocate_root()) empty_task();
- e->set_ref_count(M + 1);
- task_list list;
- R* ra[M][M];
- RR* rra[M][M];
- for (int i = 0; i < M; i++) {
- RRR* rrrt = new RRR(i);
- rrrt->successors.push_back(e);
- rrrt->set_ref_count(M);
- for (int j = 0; j < M; j++) {
- RR* rrt = new(task::allocate_root()) RR(i, j);
- rrt->successors.push_back(rrrt);
- rrt->set_ref_count(M);
- R* rt = new(task::allocate_root()) R(i, j);
- rt->successors.push_back(rrt);
- ra[i][j] = rt;
- rra[i][j] = rrt;
- }
- }
- for (int i = 0; i < M; i++) {
- for (int j = 0; j < M; j++) {
- list.push_back(*ra[i][j]);
- }
- }
- e->spawn_and_wait_for_all(list);
- e->destroy(*e);
- */
- empty_task* e = new(task::allocate_root()) empty_task;
- e->set_ref_count(M + 1);
- R* r[M][M];
- RR* rr[M][M];
- RRR* rrr[M];
- for (int i = 0; i < M; i++) {
- rrr[i] = new(task::allocate_root()) RRR(i);
- rrr[i]->set_ref_count(M);
- rrr[i]->successors.push_back(e);
- }
- for (int i = 0; i < M; i++) {
- for (int j = 0; j < M; j++) {
- rr[i][j] = new(task::allocate_root()) RR(i, j);
- rr[i][j]->set_ref_count(M);
- rr[i][j]->successors.push_back(rrr[i]);
- }
- }
- for (int i = 0; i < M; i++) {
- for (int j = 0; j < M; j++) {
- r[i][j] = new(task::allocate_root()) R(i, j);
- for (int k = 0; k < M; k++) {
- r[i][j]->successors.push_back(rr[i][k]);
- }
- }
- }
- task_list list;
- for (int i = 0; i < M; i++) {
- for (int j = 0; j < M; j++) {
- list.push_back(*r[i][j]);
- }
- }
- e->spawn_and_wait_for_all(list);
- return;
- }
- void paralell() {
- BuildAndEvaluateDAG();
- return;
- }
- int main(int argc, char* argv[]) {
- unsigned int r[M][M];
- unsigned int rr[M][M];
- unsigned int rrr[M][M];
- unsigned int i, j, k;
- unsigned int sum;
- cout << endl << "DCT serial version" << endl;
- // measure DCT execution time
- tick_count startTime = tick_count::now();
- // r = in * c
- for (i = 0; i<M; i++) {
- for (j = 0; j<M; j++) {
- sum = 0;
- for (k = 0; k<M; k++) {
- sum += in[i][k] * c[k][j];
- }
- r[i][j] = sum;
- }
- }
- // rr = r * c'
- for (i = 0; i<M; i++) {
- for (j = 0; j<M; j++) {
- sum = 0;
- for (k = 0; k<M; k++) {
- sum += r[i][k] * c[j][k];
- }
- rr[i][j] = sum;
- }
- }
- // rrr = rr .* alpha
- for (i = 0; i<M; i++) {
- for (j = 0; j<M; j++) {
- rrr[i][j] = rr[i][j] * alpha[i][j];;
- }
- }
- // print matrix
- cout << endl;
- for (int i = 0; i < M; ++i)
- {
- for (int j = 0; j < M; ++j)
- {
- cout << rrr[i][j] << " ";
- }
- cout << endl;
- }
- // measure DCT execution time
- tick_count endTime = tick_count::now();
- cout << endl << "Execution time [" << M << "][" << M << "]: " << (endTime - startTime).seconds() * 1000 << "ms." << endl << endl;
- tick_count startTimeP = tick_count::now();
- paralell();
- // print matrix
- cout << endl;
- cout << "printing matrix parallel" << endl;
- for (int i = 0; i < M; ++i)
- {
- for (int j = 0; j < M; ++j)
- {
- cout << rrr[i][j] << " ";
- }
- cout << endl;
- }
- // measure DCT execution time
- tick_count endTimeP = tick_count::now();
- cout << endl << "Execution time [" << M << "][" << M << "]: " << (endTimeP - startTimeP).seconds() * 1000 << "ms." << endl << endl;
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement