Ejemplo n.º 1
0
int execute_branch(struct decoded_ins *branch){ //return 1 for success, 0 failure
	clean_array();
	int i;
	for(i=0;i<cpre_issue->count;i++){
		ins2array(cpre_issue->q[i],i);
	}
	ins2array(branch,cpre_issue->count);

	if(ck_hazard(cpre_issue->count)){//if passed, execute it!
		int opcode;
		opcode = get_opcode(branch->binary);
		switch (opcode){
			case 18:{ //BEQ
				if(reg[branch->rt] == reg[branch->rs]){
					pc = pc +4 +branch->imd * 4;
					executed_ins = branch;
					waiting_ins =NULL;//afte exec, this should be NULL again
					return 1;;
				}
				else{
					pc+=4;
					executed_ins = branch;
					waiting_ins =NULL;
					return 1;;
				}
			}
			case 19:{ //BLTZ
				if(reg[branch->rs] < 0){
					pc = pc +4 +branch->imd * 4;
					executed_ins = branch;
					waiting_ins =NULL;
					return 1;;
				}
				else{
					pc+=4;
					executed_ins = branch;
					waiting_ins =NULL;
					return 1;;
				}
			}
			case 20:{ //BGTZ
				if(reg[branch->rs] > 0){
					pc = pc +4 +branch->imd * 4;
					executed_ins = branch;
					waiting_ins =NULL;
					return 1;;
				}
				else{
					pc+=4;
					executed_ins = branch;
					waiting_ins =NULL;
					return 1;;
				}
			}					
		}//end of switch
	}
	return 0;
}
Ejemplo n.º 2
0
int issue(void){
	//printf("in issue\n");
	struct decoded_ins *cand_ls=NULL;
	struct decoded_ins *cand_else=NULL;
	int issue_count=0;
	int i;
	clean_array();
	/*in the loop is ckecking and making candidates period*/

	for(i=0;i<4;i++){
		//printf("round: %d\n",i );
		struct decoded_ins *cking_ins;
		cking_ins = cpre_issue->q[i];
		
		//check breaking conditon for exiting examining for loop
		if(cking_ins ==NULL) //end of cpre_issue queue
			break;
		if(cand_ls!= NULL && cand_else!= NULL)//full cand
			break;


		ins2array(cking_ins,i);

		if(ck_hazard(i) && ck_ls_case(i)){ //both passed
			//printf("all pass\n");
			if(get_opcode(cking_ins->binary)== 22 || get_opcode(cking_ins->binary)==23){
				if(cand_ls == NULL){
					cand_ls=cking_ins;
					update_working_array(i);
				}
				else
					continue;
			}
			else{
				if(cand_else == NULL){
					cand_else=cking_ins;
					update_working_array(i);
				}
				else
					continue;
			}
		}
	}//end of for loop

	if(cand_ls!=NULL && cpre_alu1->count <2){
		enq_prealu(cand_ls,pre_alu1);
		deq_preissue(cand_ls);
		issue_count++;
	}
	if(cand_else!=NULL && cpre_alu2->count <2){
		enq_prealu(cand_else,pre_alu2 );
		deq_preissue(cand_else);
		issue_count++;
	}
	return issue_count;
}
Ejemplo n.º 3
0
void* recv_thread (void *p)
{
	char buf[1024];
	int cursize = 0;
	while (1)
	{
		cursize = recv(tcp_link, buf, sizeof(buf), 0);
		if (cursize <= 0)
		{
			g_print("recv_thread\n");
			return;
		}
		g_print("%s\n", buf);
		deal_message (buf);
		clean_array (buf, cursize, '\0');
	}
	
}
void reverse_step (int start_grid[][24], int stop_grid[][24], int vote_case[], double probability_case[]) {
	int i, j, n, idx;
	//int count_ambiguity[4] = {0, 0, 0, 0};
	FLIP_STACK fs = {.size = 0};
	for (i = 2; i < 22; ++i) {
		for (j = 2; j < 22; ++j) {	
			idx = get_pattern_idx(stop_grid, i, j);
			start_grid[i][j] = vote_case[idx];
			
			//if ((0.15 < probability_case[idx]) && (probability_case[idx] < 0.85)) ++count_ambiguity[0];
			//if ((0.3 < probability_case[idx]) && (probability_case[idx] < 0.7)) ++count_ambiguity[1];
			//if ((0.4 < probability_case[idx]) && (probability_case[idx] < 0.6)) ++count_ambiguity[2];
			//if ((0.45 < probability_case[idx]) && (probability_case[idx] < 0.55)) ++count_ambiguity[3];
			
			// put all the points with median probability into a stack
			if (fabs(0.5-probability_case[idx]) < 0.125) push_flip(&fs, i, j, fabs(0.5-probability_case[idx]));
		}
	}
	
	//printf("%d,%d,%d,%d,%d,\n", count_1s_grid(stop_grid),
	//       count_ambiguity[0], count_ambiguity[1], count_ambiguity[2], count_ambiguity[3]);
	//printf("%d %d %f\n", imax, jmax, probability_max);
	
	quicksort_flips(fs.items, fs.size); // sort the stack
	
	FLIP_POINT ijp;
	double score_start, score_flip;
	for (n = fs.size; n > 0; --n) {
		score_start = difference_forward(start_grid, stop_grid);
		ijp = pop_flip(&fs);
		flip_point(ijp.i, ijp.j, start_grid);
		score_flip = difference_forward(start_grid, stop_grid);
		if (score_start <= score_flip) flip_point(ijp.i, ijp.j, start_grid);
	}
}

////////////////////////////////////////////////////////////////////////

int count_case_0[5][2097152], count_case_1[5][2097152]; // 2097152 = pow(2, 21)
int vote_case[5][2097152];
double probability_case[5][2097152];

int main () {
	
	// utility parameters
	int n, i;
	
	clean_array((int*)count_case_0, 2097152*5);
	clean_array((int*)count_case_1, 2097152*5);
	
	// read the train.csv data
	FILE *train;
	train = fopen ("train.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line

	int id, delta, initial_grid[24][24], start_grid[24][24], stop_grid[24][24];
	clean_array((int*)initial_grid, 24*24);
	clean_array((int*)start_grid, 24*24);
	clean_array((int*)stop_grid, 24*24);
	
	// read all the train set and make statistical table for them
	for (n = 0; n < 50000; ++n) {
		read_train(train, &id, &delta, (int*)start_grid);
		for (i = 0; i < delta; ++i) {
			conway_step(start_grid, stop_grid);
			vote_step(start_grid, stop_grid, count_case_0[i], count_case_1[i]);
			copy_grid(start_grid, stop_grid);
		}
	}
	
	// vote for statistical table
	for (i = 0; i < 5; ++i) {
		for (n = 0; n < 2097152; ++n) {
			if (count_case_0[i][n] < count_case_1[i][n]) vote_case[i][n] = 1;
			else vote_case[i][n] = 0;
			
			if (count_case_0[i][n]+count_case_1[i][n] == 0) probability_case[i][n] = 0;
			// for the patterns happen really less frequently, we assume the original center is definitely 0.
			else probability_case[i][n] = (double)count_case_1[i][n] / (double)(count_case_0[i][n]+count_case_1[i][n]);
		}
	}
	
	//------------------------------------------------------------------
	
	// check prediction accuracy
	///*
	train = fopen ("train.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line
	
	FILE *pred;
	pred = fopen ("pred.csv", "w");
	fprintf(pred, "id,");
	for (n = 1; n < 401; ++n) fprintf(pred, "start.%d,", n);
	for (n = 1; n < 400; ++n) fprintf(pred, "diff.%d,", n);
	fprintf(pred, "diff.400\n");
	
	double difference_all = 0;
	for (n = 0; n < 50000; ++n) {
		read_train (train, &id, &delta, (int*)initial_grid);
		copy_grid(start_grid, initial_grid);

		for (i = 0; i < delta; ++i) {
		//for (i = 0; i < 1; ++i) {
			conway_step(start_grid, stop_grid);
			copy_grid(start_grid, stop_grid);
		}
		
		for (i = delta-1; i >= 0; --i) {
		//for (i = 0; i >= 0; --i) {
			reverse_step (start_grid, stop_grid, vote_case[i], probability_case[i]);
			copy_grid(stop_grid, start_grid);
		}
		write_pred(pred, id, initial_grid, start_grid);
		difference_all += difference_grids(initial_grid, start_grid);
	}
	printf("training set score: %f\n", difference_all/50000);
	fclose(pred);
	//*/

	fclose(train);
	
	//------------------------------------------------------------------
	
	// make submission file
	/*
	FILE *test;
	test = fopen ("test.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line
	
	FILE *submission;
	submission = fopen ("submission.csv", "w");
	fprintf(submission, "id,");
	for (n = 1; n < 400; ++n) fprintf(submission, "start.%d,", n);
	fprintf(submission, "start.400\n");
	
	for (n = 0; n < 50000; ++n) {
		read_test(test, &id, &delta, stop_grid);
		
		for (i = delta-1; i >= 0; --i) {
			reverse_step (start_grid, stop_grid, vote_case[i], probability_case[i]);
			copy_grid(stop_grid, start_grid);
		}
		write_submission(submission, id, start_grid);
	}
	
	fclose(test);
	fclose(submission);
	*/
	
	return 0;
	
	// goes the reverse check (see everything after vote_case in reverse_step).
	// however, the imporvement seems really tiny.
	// for the cutoff of points which need to be checked reversely,
	// I use all the predicted data (50000+50000) and got the following table.
	// threshold | score
	//     0     | 0.128226
	//     0.05  | 0.127253
	//     0.1   | 0.126986
	//     0.12  | 0.126851
	//     0.125 | 0.126803  <--
	//     0.127 | 0.126826
	//     0.13  | 0.126824
	//     0.135 | 0.126889
	//     0.14  | 0.126914
	//     0.15  | 0.127000
	
	// so I use 0.127 (should use 0.125) to predict the test set.
	// predicted set score: 0.12957 (-0.0003)
	// the improvement is even smaller than I thought, I think it should be around 0.0014.
	// :-(
	
	// probably the imporvement of the training set, is because we use the training set
	// itself to calculate the scores, which gives bias.
	
	//---------------------
	// then use 0.125, and set the probably to be 0.5 if the pattern never appear.
	// predicted set score: 0.13034 (+0.0004)  -- even go worse~~
	
	//---------------------
	// if ruling out the unfrequent patterns, it doesn't seem help.
	//     0.125 | 0.127049 (rule out < 2)
	//     0.125 | 0.127095 (rule out < 4)
}
double difference_forward (int start_grid[][24], int stop_grid[][24]) {
	int forward_grid[24][24];
	clean_array((int*)forward_grid, 24*24);
	conway_step(start_grid, forward_grid);
	return difference_grids(forward_grid, stop_grid);
}
void reverse_step (int start_grid[][24], int stop_grid[][24], int vote_case[]) {
	int i, j, n, idx;
	FLIP_STACK fs = {.size = 0};
	for (i = 2; i < 22; ++i) {
		for (j = 2; j < 22; ++j) {	
			idx = get_pattern_idx(stop_grid, i, j);
			start_grid[i][j] = vote_case[idx]%2;
			if (vote_case[idx] >= 2) push_flip(&fs, i, j);		
		}
	}
	
	FLIP_POINT ijp;
	double score_start, score_flip;
	for (n = fs.size; n > 0; --n) {
		score_start = difference_forward(start_grid, stop_grid);
		ijp = pop_flip(&fs);
		flip_point(ijp.i, ijp.j, start_grid);
		score_flip = difference_forward(start_grid, stop_grid);
		if (score_start <= score_flip) flip_point(ijp.i, ijp.j, start_grid);
	}
}

////////////////////////////////////////////////////////////////////////

unsigned int count_case_0[5][2097152], count_case_1[5][2097152]; // 2097152 = pow(2, 21)
int vote_case[5][2097152];
//double probability_case[5][2097152];

int main () {
	
	// utility parameters
	int n, i;
	
	clean_array((int*)count_case_0, 2097152*5);
	clean_array((int*)count_case_1, 2097152*5);
	
	// read the train.csv data
	FILE *train;
	train = fopen ("train.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line

	int id, delta, initial_grid[24][24], start_grid[24][24], stop_grid[24][24];
	clean_array((int*)initial_grid, 24*24);
	clean_array((int*)start_grid, 24*24);
	clean_array((int*)stop_grid, 24*24);
	
	// read all the train set and make statistical table for them
	for (n = 0; n < 50000; ++n) {
		read_train(train, &id, &delta, (int*)start_grid);
		for (i = 0; i < delta; ++i) {
			conway_step(start_grid, stop_grid);
			vote_step(start_grid, stop_grid, count_case_0[i], count_case_1[i]);
			copy_grid(start_grid, stop_grid);
		}
	}
	
	// vote for statistical table
	for (i = 0; i < 5; ++i) {
		for (n = 0; n < 2097152; ++n) {
			vote_case[i][n] = vote_pattern(n, count_case_0[i][n], count_case_1[i][n], i);
			
			//if (count_case_0[i][n]+count_case_1[i][n] == 0) probability_case[i][n] = 0;
			//else probability_case[i][n] = (double)count_case_1[i][n] / (double)(count_case_0[i][n]+count_case_1[i][n]);
		}
	}
	
	//------------------------------------------------------------------
	
	// check prediction accuracy
	/*
	train = fopen ("train.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line
	
	FILE *pred;
	pred = fopen ("pred.csv", "w");
	fprintf(pred, "id,");
	for (n = 1; n < 401; ++n) fprintf(pred, "start.%d,", n);
	for (n = 1; n < 400; ++n) fprintf(pred, "diff.%d,", n);
	fprintf(pred, "diff.400\n");
	
	double difference_all = 0;
	for (n = 0; n < 50000; ++n) {
		read_train (train, &id, &delta, (int*)initial_grid);
		copy_grid(start_grid, initial_grid);

		for (i = 0; i < delta; ++i) {
			conway_step(start_grid, stop_grid);
			copy_grid(start_grid, stop_grid);
		}
		
		for (i = delta-1; i >= 0; --i) {
			reverse_step (start_grid, stop_grid, vote_case[i]);
			copy_grid(stop_grid, start_grid);
		}
		write_pred(pred, id, initial_grid, start_grid);
		difference_all += difference_grids(initial_grid, start_grid);
	}
	printf("training set score: %f\n", difference_all/50000);
	fclose(pred);
	*/

	fclose(train);
	
	//------------------------------------------------------------------
	
	// make submission file
	///*
	FILE *test;
	test = fopen ("test.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line
	
	FILE *submission;
	submission = fopen ("submission.csv", "w");
	fprintf(submission, "id,");
	for (n = 1; n < 400; ++n) fprintf(submission, "start.%d,", n);
	fprintf(submission, "start.400\n");
	
	for (n = 0; n < 50000; ++n) {
		read_test(test, &id, &delta, stop_grid);
		
		for (i = delta-1; i >= 0; --i) {
			reverse_step (start_grid, stop_grid, vote_case[i]);
			copy_grid(stop_grid, start_grid);
		}
		write_submission(submission, id, start_grid);
	}
	
	fclose(test);
	fclose(submission);
	//*/
	
	return 0;
	// training set score: 0.124948 (-0.00054)
	// real score: 0.12685 (-0.00002)
	// actual improvement is really tiny compare to the training set one.
}
int main () {
	
	// utility parameters
	int n, i;
	
	clean_array((int*)count_case_0, 2097152*5);
	clean_array((int*)count_case_1, 2097152*5);
	
	// read the train.csv data
	FILE *train;
	train = fopen ("train.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line

	int id, delta, initial_grid[24][24], start_grid[24][24], stop_grid[24][24];
	clean_array((int*)initial_grid, 24*24);
	clean_array((int*)start_grid, 24*24);
	clean_array((int*)stop_grid, 24*24);
	
	// read all the train set and make statistical table for them
	for (n = 0; n < 50000; ++n) {
		read_train(train, &id, &delta, (int*)start_grid);
		for (i = 0; i < delta; ++i) {
			conway_step(start_grid, stop_grid);
			vote_step(start_grid, stop_grid, count_case_0[i], count_case_1[i]);
			copy_grid(start_grid, stop_grid);
		}
	}
	
	// vote for statistical table
	for (i = 0; i < 5; ++i) {
		for (n = 0; n < 2097152; ++n) 
			vote_case[i][n] = vote_pattern(n, count_case_0[i][n], count_case_1[i][n], i);
	}
	
	//------------------------------------------------------------------
	
	// check prediction accuracy
	///*
	train = fopen ("train.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line
	
	FILE *pred;
	pred = fopen ("pred.csv", "w");
	fprintf(pred, "id,");
	for (n = 1; n < 401; ++n) fprintf(pred, "start.%d,", n);
	for (n = 1; n < 400; ++n) fprintf(pred, "diff.%d,", n);
	fprintf(pred, "diff.400\n");
	
	double difference_all = 0;
	for (n = 0; n < 50000; ++n) {
		read_train (train, &id, &delta, (int*)initial_grid);
		copy_grid(start_grid, initial_grid);

		for (i = 0; i < delta; ++i) {
			conway_step(start_grid, stop_grid);
			copy_grid(start_grid, stop_grid);
		}
		
		for (i = delta-1; i >= 0; --i) {
			reverse_step (start_grid, stop_grid, vote_case[i]);
			copy_grid(stop_grid, start_grid);
		}
		write_pred(pred, id, initial_grid, start_grid);
		difference_all += difference_grid(initial_grid, start_grid);
	}
	printf("training set score: %f\n", difference_all/50000);
	fclose(pred);
	//*/

	fclose(train);
	
	//------------------------------------------------------------------
	
	// make submission file
	///*
	FILE *test;
	test = fopen ("test.csv", "r");
	while (fgetc(train) != '\n') ;  // skip the head line
	
	FILE *submission;
	submission = fopen ("submission.csv", "w");
	fprintf(submission, "id,");
	for (n = 1; n < 400; ++n) fprintf(submission, "start.%d,", n);
	fprintf(submission, "start.400\n");
	
	for (n = 0; n < 50000; ++n) {
		read_test(test, &id, &delta, stop_grid);
		
		for (i = delta-1; i >= 0; --i) {
			reverse_step (start_grid, stop_grid, vote_case[i]);
			copy_grid(stop_grid, start_grid);
		}
		write_submission(submission, id, start_grid);
	}
	
	fclose(test);
	fclose(submission);
	//*/
	
	return 0;
	// training set score: 0.12549
	// real score: 0.12687 	

	// I also tried set up vote_case table cutoff based on different delta number.
	// it may helps (the entire table parameter need a long time to be fixed),
	// but a naive setup of the final step (5->6) to 0.5 makes everything goes worse.
}