Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <omp.h>
- #include <iostream>
- #include <sstream>
- #define NUMBER 1000000000
- static double init;
- static int limit = 32;
- static std::string color[4] = {"white","cyan","yellow","green"};
- inline double get_time(){
- return omp_get_wtime() - init;
- }
- inline std::string name(int num, int cpu){
- std::stringstream ss;
- ss << "\"" << num << " (" << cpu << ")\"";
- return ss.str();
- }
- int fib(int num){
- int p = omp_get_thread_num();
- #pragma omp critical
- std::cout << name(num, p) << " [fillcolor=" << color[omp_get_thread_num()] << "]" << std::endl;
- int a = 0, b = 0;
- //left child
- if (num*2 < limit)
- {
- #pragma omp task shared(a,p,num)
- {
- double t = get_time();
- #pragma omp critical
- std::cout << name(num, p) << " -> " << name(num*2, omp_get_thread_num()) << " [label=" << t << "]" << std::endl;
- a = fib(num*2);
- }
- }
- //right child
- if (num*2 + 1 < limit){
- #pragma omp task shared(b,p,num)
- {
- double t = get_time();
- #pragma omp critical
- std::cout << name(num, p) << " -> " << name(num*2+1, omp_get_thread_num()) << " [label=" << t << "]" << std::endl;
- b = fib(num*2+1);
- }
- }
- // waste some time
- int c = 0;
- for(int i=0; i<NUMBER * num; i++)
- c += i/num;
- #pragma omp taskwait
- return a+b+c;
- }
- int main(){
- init = omp_get_wtime();
- int a;
- std::cout << "digraph {\nnode [shape=circle, style=\"filled\"]" << std::endl;
- #pragma omp parallel shared(a)
- #pragma omp single
- a = fib(1);
- std::cout << "}" << std::endl;
- std::cerr << a << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement