Advertisement
Guest User

Task_scheduling_test.cpp

a guest
Jul 12th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <omp.h>
  2. #include <iostream>
  3. #include <sstream>
  4.  
  5. #define NUMBER 1000000000
  6. static double init;
  7. static int limit = 32;
  8. static std::string color[4] = {"white","cyan","yellow","green"};
  9.  
  10. inline double get_time(){
  11.     return omp_get_wtime() - init;
  12. }
  13.  
  14. inline std::string name(int num, int cpu){
  15.     std::stringstream ss;
  16.     ss << "\"" << num << " (" << cpu << ")\"";
  17.     return ss.str();
  18. }
  19.  
  20. int fib(int num){
  21.  
  22.     int p = omp_get_thread_num();
  23.     #pragma omp critical
  24.     std::cout << name(num, p) << " [fillcolor=" << color[omp_get_thread_num()] << "]" << std::endl;
  25.  
  26.     int a = 0, b = 0;
  27.  
  28.     //left child
  29.     if (num*2 < limit)
  30.     {
  31.         #pragma omp task shared(a,p,num)
  32.         {
  33.             double t = get_time();
  34.             #pragma omp critical
  35.             std::cout << name(num, p) << " -> " << name(num*2, omp_get_thread_num()) << " [label=" << t << "]" << std::endl;
  36.             a = fib(num*2);
  37.         }
  38.     }
  39.     //right child
  40.     if (num*2 + 1 < limit){
  41.         #pragma omp task shared(b,p,num)
  42.         {
  43.             double t = get_time();
  44.             #pragma omp critical
  45.             std::cout << name(num, p) << " -> " << name(num*2+1, omp_get_thread_num()) << " [label=" << t << "]" << std::endl;
  46.             b = fib(num*2+1);
  47.         }
  48.     }
  49.  
  50.     // waste some time
  51.     int c = 0;
  52.     for(int i=0; i<NUMBER * num; i++)
  53.         c += i/num;
  54.  
  55.     #pragma omp taskwait
  56.  
  57.     return a+b+c;
  58. }
  59.  
  60. int main(){
  61.     init = omp_get_wtime();
  62.     int a;
  63.     std::cout << "digraph {\nnode [shape=circle, style=\"filled\"]" << std::endl;
  64.     #pragma omp parallel shared(a)
  65.     #pragma omp single
  66.     a = fib(1);
  67.     std::cout << "}" << std::endl;
  68.     std::cerr << a << std::endl;
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement