Advertisement
Guest User

Loop Heuristics

a guest
Mar 29th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. double thresh = 256; // 2^8
  7. int vectorSize =  8;
  8. int lo = 0;
  9. int hi =  100000; // 10^5 for now
  10. double vectorCost = 1.5;
  11. double spawnCost = 10;
  12. double serialCost = 1;
  13.  
  14. double DAC_Cost(double n){
  15.  
  16.   double tmp= n / thresh;
  17.   int h =(int) (ceil(log2(tmp))); // Height of DAC tree
  18.   int total_leaves = 1 << h;
  19.   int k = (int)(n / total_leaves);   // Size of smallest leaf
  20.   int y = n - k * total_leaves;
  21.   int x = total_leaves - y;
  22.   int total_spawns = total_leaves - 1;
  23.   int total_vectors_instr =  (k/vectorSize)*x + ((k+1)/vectorSize)*y;
  24.   int total_serial_instr = (k % vectorSize)* x + ((k+1)%vectorSize)*y;
  25.    
  26.   double costEstimate = total_spawns * spawnCost + total_vectors_instr * vectorCost + total_serial_instr * serialCost;
  27.  
  28.   printf("x : %d  y: %d height:%d  small leaf size:%d n: %lf \n", x, y,h,k,n);
  29.   return costEstimate;
  30. }
  31.  
  32.  
  33. double LIE_Cost(int n){
  34.    
  35.     int a[35];
  36.     int msb = 0;
  37.     int cur_n = n;
  38.     while( cur_n > 0){
  39.         a[msb] = cur_n%2;
  40.         cur_n = cur_n /2;
  41.         msb++;
  42.     }
  43.     msb--;
  44.     int total_leaves = 1;  // for last leaf
  45.     int total_vectors_instr = 0;
  46.     int total_serial_instr = 0;
  47.    
  48.     cur_n = n;
  49.     int i = msb;
  50.     while(cur_n >= thresh){
  51.         if (a[i] == 1){
  52.             int rootSize = 1 << i;
  53.             cur_n = cur_n - rootSize;
  54.             int leafSize = thresh;
  55.             int leafNo = rootSize/thresh;
  56.             total_leaves += leafNo;
  57.             total_vectors_instr += ((thresh/vectorSize)*leafNo);
  58.            
  59.         }
  60.         i--;
  61.     }
  62.    
  63.    
  64.     total_vectors_instr  += (cur_n/vectorSize);
  65.     total_serial_instr += (cur_n%vectorSize);
  66.     int total_spawns = total_leaves - 1;
  67.     double costEstimate = total_spawns * spawnCost + total_vectors_instr * vectorCost + total_serial_instr * serialCost;
  68.    
  69.     return costEstimate;
  70.    
  71. }
  72.  
  73. int main() {
  74.    
  75.     double cst1 = DAC_Cost(1100*1.0);
  76.     double cst2 = LIE_Cost(1100);
  77.     cout<< cst1 << " " <<cst2;
  78.    
  79.    
  80.    
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement