Advertisement
Guest User

Different Loop Costs

a guest
Mar 29th, 2017
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. double thresh = 256; // The grainSize
  7. int vectorSize =  8; // Size of vector register. Power of two
  8. int lo = 0;
  9. int hi =  100000; // 10^5 for now
  10. // The relative costs of vector instr, spawns, serial loop instrs.
  11. double vectorCost = 2;
  12. double spawnCost = 10;
  13. double serialCost = 1;
  14.  
  15. double DAC_Cost(double n){
  16.  
  17.   double tmp= n / thresh;
  18.   int h =(int) (ceil(log2(tmp))); // Height of DAC tree
  19.   int total_leaves = 1 << h;
  20.   int k = (int)(n / total_leaves);   // Size of smallest leaf
  21.   int y = n - k * total_leaves;
  22.   int x = total_leaves - y;
  23.   int total_spawns = total_leaves - 1;
  24.   int total_vectors_instr =  (k/vectorSize)*x + ((k+1)/vectorSize)*y;
  25.   int total_serial_instr = (k % vectorSize)* x + ((k+1)%vectorSize)*y;
  26.    
  27.   double costEstimate = total_spawns * spawnCost + total_vectors_instr * vectorCost + total_serial_instr * serialCost;
  28.  
  29.   //printf("x : %d  y: %d height:%d  small leaf size:%d n: %lf \n", x, y,h,k,n);
  30.   return costEstimate;
  31. }
  32.  
  33.  
  34. double TWO_Cost(int n){
  35.    
  36.     int a[35];
  37.     int msb = 0;
  38.     int cur_n = n;
  39.     while( cur_n > 0){
  40.         a[msb] = cur_n%2;
  41.         cur_n = cur_n /2;
  42.         msb++;
  43.     }
  44.     msb--;
  45.     int total_leaves = 1;  // for last leaf
  46.     int total_vectors_instr = 0;
  47.     int total_serial_instr = 0;
  48.    
  49.     cur_n = n;
  50.     int i = msb;
  51.     while(cur_n >= thresh){
  52.         if (a[i] == 1){
  53.             int rootSize = 1 << i;
  54.             cur_n = cur_n - rootSize;
  55.             int leafSize = thresh;
  56.             int leafNo = rootSize/thresh;
  57.             total_leaves += leafNo;
  58.             total_vectors_instr += ((thresh/vectorSize)*leafNo);
  59.            
  60.         }
  61.         i--;
  62.     }
  63.    
  64.    
  65.     total_vectors_instr  += (cur_n/vectorSize);
  66.     total_serial_instr += (cur_n%vectorSize);
  67.     int total_spawns = total_leaves - 1;
  68.     double costEstimate = total_spawns * spawnCost + total_vectors_instr * vectorCost + total_serial_instr * serialCost;
  69.    
  70.     return costEstimate;
  71.    
  72. }
  73.  
  74. void doHeuristics(){
  75.  
  76.  int count = 0;
  77.  for(int i = lo; i <= hi; i++){
  78.    
  79.     double dacCost = DAC_Cost(i * 1.0);
  80.     double twoCost = TWO_Cost(i);
  81.  
  82.     if (twoCost > dacCost){
  83.       cout << "N =" << i << "  DAC: "<< dacCost <<"  TWO:  "<<twoCost <<endl;
  84.       count++;
  85.     }
  86.  }
  87.  
  88.  
  89.   double perc = (count * 1.0 ) / ((hi - lo) *1.0);
  90.   printf ("Percentage Fail : %lf Range: lo : %d hi: %d\n", perc, lo, hi);
  91.  
  92.   return;
  93. }
  94. int main() {
  95.    
  96.     doHeuristics();
  97.    
  98.    
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement