Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- using namespace std;
- double thresh = 256; // The grainSize
- int vectorSize = 8; // Size of vector register. Power of two
- int lo = 0;
- int hi = 100000; // 10^5 for now
- // The relative costs of vector instr, spawns, serial loop instrs.
- double vectorCost = 2;
- double spawnCost = 10;
- double serialCost = 1;
- double DAC_Cost(double n){
- double tmp= n / thresh;
- int h =(int) (ceil(log2(tmp))); // Height of DAC tree
- int total_leaves = 1 << h;
- int k = (int)(n / total_leaves); // Size of smallest leaf
- int y = n - k * total_leaves;
- int x = total_leaves - y;
- int total_spawns = total_leaves - 1;
- int total_vectors_instr = (k/vectorSize)*x + ((k+1)/vectorSize)*y;
- int total_serial_instr = (k % vectorSize)* x + ((k+1)%vectorSize)*y;
- double costEstimate = total_spawns * spawnCost + total_vectors_instr * vectorCost + total_serial_instr * serialCost;
- //printf("x : %d y: %d height:%d small leaf size:%d n: %lf \n", x, y,h,k,n);
- return costEstimate;
- }
- double TWO_Cost(int n){
- int a[35];
- int msb = 0;
- int cur_n = n;
- while( cur_n > 0){
- a[msb] = cur_n%2;
- cur_n = cur_n /2;
- msb++;
- }
- msb--;
- int total_leaves = 1; // for last leaf
- int total_vectors_instr = 0;
- int total_serial_instr = 0;
- cur_n = n;
- int i = msb;
- while(cur_n >= thresh){
- if (a[i] == 1){
- int rootSize = 1 << i;
- cur_n = cur_n - rootSize;
- int leafSize = thresh;
- int leafNo = rootSize/thresh;
- total_leaves += leafNo;
- total_vectors_instr += ((thresh/vectorSize)*leafNo);
- }
- i--;
- }
- total_vectors_instr += (cur_n/vectorSize);
- total_serial_instr += (cur_n%vectorSize);
- int total_spawns = total_leaves - 1;
- double costEstimate = total_spawns * spawnCost + total_vectors_instr * vectorCost + total_serial_instr * serialCost;
- return costEstimate;
- }
- void doHeuristics(){
- int count = 0;
- for(int i = lo; i <= hi; i++){
- double dacCost = DAC_Cost(i * 1.0);
- double twoCost = TWO_Cost(i);
- if (twoCost > dacCost){
- cout << "N =" << i << " DAC: "<< dacCost <<" TWO: "<<twoCost <<endl;
- count++;
- }
- }
- double perc = (count * 1.0 ) / ((hi - lo) *1.0);
- printf ("Percentage Fail : %lf Range: lo : %d hi: %d\n", perc, lo, hi);
- return;
- }
- int main() {
- doHeuristics();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement