Пример #1
0
/* Option 2: Pipeline */
void pipeline() {
  set[1].delay = 0;
  int dep_delay, overlap;
  int i;
  for(i = 2; i <= n; i++) {
    // Check for a raw dep
    dep_delay = 0; // Assume instr is independent
    if((set[i-2].dest == set[i].src1) ||
       (set[i-2].dest == set[i].src2)) {
      if(overlap == 0) {
	dep_delay = 1;
	overlap = 1;
      }
      else {
	dep_delay = 0;
	overlap = 0;
      }
    }
    else { // Not dependent
      overlap = 0;
    }
    if((set[i-1].dest == set[i].src1) ||
       (set[i-1].dest == set[i].src2)) {
      dep_delay = 2;
      overlap = 1;
    }
    set[i].delay = set[i-1].delay + dep_delay + 1;
  } // End for loop
  printf("Total number of cycles: %d\n", set[n].delay + 6);
  printChart();
}
Пример #2
0
// Main body of the program, for computing strategy
void compute_strategy ()
{
  //File name to print chart to 
  const char *filename = "../output/Blackjack strategy chart.tex"; 
  //Indicates whether to show the win percentage for each hand in the chart
  const int SHOW_WIN_PCT = TRUE;
  //Indicates whether to exclude doubles and splits 
  const int MAKE_SIMPLE_CHART = FALSE;
  //Ratio of the player's bet that he wins by getting blackjack 
  const double BLACKJACK_PAYS = 3./2.; 
  
  Strategy **chart = NULL; 
  int i; 
  
  //chart is a NUM_HANDS by NUM_CARDS+1 matrix, with entry i,j being hands[i] 
  //and the card with face value j. 
  chart = (Strategy **) malloc(NUM_HANDS * sizeof(Strategy *)); 
  for (i = 0; i < NUM_HANDS; i++)
    chart[i] = (Strategy *) malloc((NUM_CARDS+1) * sizeof(Strategy)); 
  if (chart == NULL) throwMemErr("chart", "main"); 
  
  //Make vector of possible hands 
  makeHands(); 
  //Make matrix of dealer's probabilities of ending up with a given total given
  //each given up card 
  dealersProbabilities = makeDealersProbabilities(); 
  
  //Compute optimal strategy for each combination of player's hand and 
  //dealer's up card 
  calculateStrategyChart (chart, MAKE_SIMPLE_CHART); 
  
  //Print chart to Latex   
  printChart (chart, filename, SHOW_WIN_PCT, MAKE_SIMPLE_CHART); 
  printf("Program complete.\n"); 
  
  //Compute player's expected value 
  if (!(MAKE_SIMPLE_CHART))
    computeExpectedValue (chart, BLACKJACK_PAYS); 
  
  freematrix(dealersProbabilities, NUM_CARDS+1); 
  for (i = 0; i < NUM_HANDS; i++)
    free(chart[i]); 
  free(chart); 
}
Пример #3
0
/* Option 3: Superscalar */
void superscalar() {
  int pipe_delay = 1;
  int dep_delay = 0;
  int overlap1, overlap2, overlap3, overlap4;
  overlap1 = overlap2 = overlap3 = overlap4 = 0;
  set[1].delay = 0;
  int i;
  for(i = 2; i <= n; i++) {
    dep_delay = 0;
    pipe_delay = 1 - pipe_delay;
    if(i >= 5) {
      if((set[i-5].dest == set[i].src1) ||
	 (set[i-5].dest == set[i].src2)) {
	pipe_delay = 1;
      }
    } // End 5

    if(i >= 4) {
      if((set[i-4].dest == set[i].src1) || 
	 (set[i-4].dest == set[i].src2)) {
	if((overlap1 == 0) && (overlap2 == 0) &&
	   (overlap3 == 0) && (overlap4 == 0)) {
	  overlap4 = 1;
	  if(pipe_delay == 0) dep_delay = 1;
	  else dep_delay = 1;
	  pipe_delay = 1;
	}
	else {
	  overlap4 = 0;
	}
      }
      else { 
	overlap4 = 0;
      }
    } // End 4  

    if(i >= 3) {
      if((set[i-3].dest == set[i].src1) || 
	 (set[i-3].dest == set[i].src2)) {
	if((overlap1 == 0) && (overlap2 == 0) &&
	   (overlap3 == 0)) {
	  overlap3 = 1;
	  if(pipe_delay == 0) dep_delay = 1;
	  else dep_delay = 1;
	  pipe_delay = 1;
	}
	else {
	  overlap3 = 0;
	}
      }
      else { 
	overlap3 = 0;
      }
    } // End 3

    if(i >= 2) {
      if((set[i-2].dest == set[i].src1) || 
	 (set[i-2].dest == set[i].src2)) {
	if((overlap1 == 0) && (overlap2 == 0)) {
	  overlap2 = 1;
	  if(pipe_delay == 0) dep_delay = 1;
	  else dep_delay = 2;
	  pipe_delay = 1;
	}
	else {
	  overlap2 = 0;
	}
      }
      else { 
	overlap2 = 0;
      }
    } // End 2

    if(i >= 1) {
      if((set[i-1].dest == set[i].src1) || 
	 (set[i-1].dest == set[i].src2)) {
	if((overlap1 == 0)) {
	  overlap1 = 1;
	  dep_delay = 2;
	  pipe_delay = 1;
	}
	else {
	  overlap1 = 0;
	}
      }
      else { 
	overlap1 = 0;
      }
    } // End 1

    set[i].delay = set[i-1].delay + dep_delay + pipe_delay;
  } // End for loop
  printf("Total number of cycles: %d\n", set[n].delay + 6);
  printChart();
}
Пример #4
0
int main(int argc,char *argv[])
{

	int m,i;

	init(argc,argv);

	while(remaining_parts) {
		int r;
		prioritizeJobs();
	
		for(r=0;r<remaining_parts;r++) {
			if(runq[r].starttime) continue;
			if( (m=getBestMachine(r))== -1) continue;

			int partid=runq[r].partid;
			
			machines[m].partid=partid;
			machines[m].partsrno=runq[r].partsrno;
			machines[m].partidx=runq[r].partidx;			
			machines[m].jid=runq[r].jid;
			machines[m].rtime=runq[r].rtime;
			machines[m].workingfor=runq[r].priority_incby;
			if(runq[r].priority_incby!=-1)
				machines[runq[r].priority_incby].workedby=m;
			
			if(runq[r].partidx+1 < parts[partid].totjobs) {
				runq[r].partidx++;
				runq[r].starttime=runq[r].rtime;
				runq[r].jid=parts[partid].jobs[runq[r].partidx];
				runq[r].rtime=jobs[runq[r].jid].rtime;
			} else {
				for(r;r<remaining_parts-1;r++)
					runq[r]=runq[r+1];
				remaining_parts--;
			}

			
			prioritizeJobs();
			r=-1;
		}

		for(m=0;m<total_machines;m++) {
			if(machines[m].partid != -1) /* if machine is working */ {
				chart[m][tsec].partid=machines[m].partid;
				chart[m][tsec].partsrno=machines[m].partsrno;
				chart[m][tsec].jid=machines[m].jid;
				chart[m][tsec].secondfill=0;

				machines[m].rtime--;
				for(r=0;r<remaining_parts;r++) {
					if(runq[r].partid==machines[m].partid && 
						runq[r].partsrno==machines[m].partsrno) {
						runq[r].starttime--;
						break;
					}
				}

				if(machines[m].rtime==0) {
					for(r=0;r<remaining_parts;r++) {
						if(runq[r].partid==machines[m].partid && 
							runq[r].partsrno==machines[m].partsrno)
							if(runq[r].starttime) {
								runq[r].starttime = 0;
							}
					}
					machines[m].partid=machines[m].partsrno=machines[m].partidx=-1;
					machines[m].jid=-1;
					if(machines[m].workingfor!=-1)
						machines[machines[m].workingfor].workedby=-1;
					machines[m].workingfor=-1;
				}
			}
			else
				chart[m][tsec].partid=-1;

		}

		if(++tsec == alloc_time) {
			alloc_time*=2;
			for(m=0;m<total_machines;m++)
				chart[m]=(struct schedule_chart *) realloc(chart[m],sizeof(struct schedule_chart) * alloc_time);
		}
	}

	while(1) {
		i=0;
		for(m=0;m<total_machines;m++) {
			chart[m][tsec].partid=machines[m].partid;
			chart[m][tsec].partsrno=machines[m].partsrno;
			chart[m][tsec].jid=machines[m].jid;
			chart[m][tsec].secondfill=0;
			if(machines[m].partid != -1) {
				i=1;
				--machines[m].rtime;
				if(machines[m].rtime == 0)
					machines[m].partid=-1;
			}
		}
		if(!i)  break;
		if(++tsec == alloc_time) {
			alloc_time*=2;
			for(m=0;m<total_machines;m++)
				chart[m]=(struct schedule_chart *) realloc(chart[m],sizeof(struct schedule_chart) * alloc_time);
		}
	}

	printChart();
   printf ("Number of empty slots : %d\n", getTotalEmptySlots());
	fillEmptySlots();

   printf ("Predicted number of empty slots after manufacturing additional parts : %d\n", getTotalEmptySlots());

   printf ("Following parts can be manufactured additionally\n");
   printf ("Part\tCount\n");
   for (i = 0; i < total_parts; i++) {
      if (parts[i].count_added) {
         printf ("%s\t%d\n", parts[i].pname, parts[i].count_added);
      }  
   }  
}