Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include "tbb/task_scheduler_init.h"
- #include "tbb/tick_count.h"
- #include "tbb/task.h"
- #include "math.h"
- using namespace tbb;
- using namespace std;
- int side;
- #define WORKLOAD 100000000
- void do_work() {
- for(int i = 0; i<WORKLOAD; i++) {
- ;
- }
- }
- class Dijagonala : public task {
- public:
- float dijagonala;
- task * execute() {
- dijagonala = side * sqrt(2.);
- do_work();
- return NULL;
- }
- };
- class Povrsina : public task {
- public:
- int povrsina;
- task * execute() {
- povrsina = side * side;
- do_work();
- return NULL;
- }
- };
- class Obim : public task {
- public:
- int obim;
- task * execute() {
- obim = 4 * side;
- do_work();
- return NULL;
- }
- };
- void serial() {
- //ToDo: Replace with actual tasks
- Dijagonala *dijagonalaZadatak = new(tbb::task::allocate_root()) Dijagonala();
- dijagonalaZadatak->execute();
- Povrsina *povrsinaZadatak = new(tbb::task::allocate_root()) Povrsina();
- povrsinaZadatak->execute();
- Obim *obimZadatak = new(tbb::task::allocate_root()) Obim();
- obimZadatak->execute();
- cout << "Dijagonala serijske: " << dijagonalaZadatak->dijagonala << endl;
- cout << "Povrsina serijske:" << povrsinaZadatak->povrsina << endl;
- cout << "Obim serijske: " << obimZadatak->obim << endl;
- }
- void BuildAndEvaluateDAG() {
- empty_task* parent = new(task::allocate_root()) empty_task;
- parent->set_ref_count(4);
- Obim* obimZadatak = new(parent->allocate_child()) Obim();
- Povrsina* povrsinaZadatak = new(parent->allocate_child()) Povrsina();
- Dijagonala* dijagonalaZadatak = new(parent->allocate_child()) Dijagonala();
- __TBB_ASSERT(parent->ref_count() == 4, NULL);
- parent->spawn(*obimZadatak);
- parent->spawn(*povrsinaZadatak);
- parent->spawn_and_wait_for_all(*dijagonalaZadatak);
- __TBB_ASSERT(parent->ref_count() == 0, NULL);
- parent->destroy(*parent);
- cout << "Dijagonala paralelne: " << dijagonalaZadatak->dijagonala << endl;
- cout << "Povrsina paralelne:" << povrsinaZadatak->povrsina << endl;
- cout << "Obim paralelne: " << obimZadatak->obim << endl;
- }
- void parallel() {
- BuildAndEvaluateDAG();
- }
- int main(int argc, char* argv[]) {
- if(argc != 2) {
- cout << "Input arguments are wrong!" << endl;
- return 1;
- }
- side = atoi(argv[1]);
- tick_count startTime = tick_count::now();
- serial();
- tick_count stopTime = tick_count::now();
- cout << "serial finished. Took: " <<
- (stopTime - startTime).seconds() * 1000 << "ms." << endl << endl;
- startTime = tick_count::now();
- parallel();
- stopTime = tick_count::now();
- cout << "parallel finished. Took: " <<
- (stopTime - startTime).seconds() * 1000 << "ms." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement