0

here, there are two functions in branchpredictor class namely predictor and trainer.

what i have pointed out is that predictor is working fine but trainer is not updating the variables of the above class.
is it that only first function can access the variables.

if not, can any one point out the possible reason.
it will be of great help.

branchpredictor is called at line 329 and
trainer is called at line 282 of main function.

if u don't want to look at the code then kindly clear my above doubt in bold.

import java.io.*;
//import java.util.*;

class branchpredictor
{
	
	private int selector=2,decimalpc1,decimalpc;
	
	private int[] bimpht=new int[(int)Math.pow(2,10)];    //initializes pattern history table
 	private int[] gpht=new int[(int)Math.pow(2,12)];
 	private char[] bhr=new char[6];
 	
 	public branchpredictor()
 	{
 	for(int l1=0;l1<10;l1++)
 	{
 		bimpht[l1]=2;
 	}
	
	

 	for(int l=0;l<4096;l++)
 			{
 				gpht[l]=2;
 			}
	
	
 	for(int m=0;m<6;m++)
 	{
 	 	bhr[m]='0';
 	}
	}
	
	public String predictor(String s4)
    {	
	System.out.println("predictor called");
	System.out.println("mem add="+s4);
	int ihex1;
 	int result1;
 	
 	

 	
 	char[] arr1=new char[s4.length()-2];
 	s4.getChars(2,s4.length(),arr1,0);

 	String s51=new String(arr1);
 	System.out.println(s51);
 	
 		String orgpc1;	
 	
		ihex1=Integer.parseInt(s51,16);
		String bin1=Integer.toBinaryString(ihex1);
		System.out.println(bin1);
		if(bin1.length()-10 > 0)
			orgpc1=bin1.substring(bin1.length()-10,bin1.length());
		else
			orgpc1=new String(bin1);
		decimalpc1= Integer.parseInt(orgpc1,2);
 	
 	
 
 		System.out.println(decimalpc1);
 	
 	
 	
    		if(bimpht[decimalpc1]>1)
     			result1=1;
    		else
     			result1=0;
	

 	
 	int ihex;
 	int result;
 	
 	
 	
 	
 		char[] arr=new char[s4.length()-2];
 		s4.getChars(2,s4.length(),arr,0);

 		String s5=new String(arr);
 		System.out.println(s5);
 		
 		
 			String orgpc;	
 	
			ihex=Integer.parseInt(s5,16);
			System.out.println(ihex);
			String bin=Integer.toBinaryString(ihex);
			System.out.println(bin);
	
			if(bin.length()-12 > 0)
				orgpc=bin.substring(bin.length()-12);
			else
				orgpc=new String(bin);
			System.out.println(orgpc);
	
			char[] org=new char[12];
			for(int r=0;r<12;r++)
			{
				org[r]='0';
			}
		
			if(orgpc.length()==12)
				orgpc.getChars(0,12,org,0);
			else if(orgpc.length()<12)
				orgpc.getChars(0,orgpc.length(),org,12-orgpc.length());
	
			char[] bhr1=new char[12];
			int n;
	
			for(int z=0;z<12;z++)
			{
			 System.out.println("org=");
			 System.out.println(org[z]);
			}
			
			
			
			for(n=0;n<6;n++)
	 			{bhr1[n]='0';}
			
			for(n=6;n<12;n++)
	 			{bhr1[n]=bhr[n-6];  }
	 
			for(int z=0;z<12;z++)
			{
			 System.out.println("bhr1=");
			 System.out.println(bhr1[z]); 
			}
			
			char[] xoredpc=new char[12];
	
			
	
			for(int p=0;p<12;p++)
			{
	 			int a=org[p];
	 			int b=bhr1[p];
	 			int check=a^b;
	 			
	 			if(check==1)
	 				xoredpc[p]='1';
	
	 			else
	 				xoredpc[p]='0';
	 			System.out.println("XORED="+xoredpc[p]);
			}
	
			String xored=new String(xoredpc);
			System.out.println("XORED="+xored);
	
			decimalpc= Integer.parseInt(xored,2);



 	
    		if(gpht[decimalpc]>1)
     			result=1;
    		else
     			result=0;
	
			int result2;
			if(selector>1)
				{result2=result;    System.out.println("prediction of gshare predictor="+result2);   System.out.println("selector="+selector);}
			else
				{result2=result1;    System.out.println("prediction of bimodal predictor"+result2);  System.out.println("selector="+selector);}
				
			String finresult=Integer.toString(result2) + Integer.toString(result1) + Integer.toString(result1);
			
			return finresult;
	}
	

	public void trainer(String s1,String s2)
	{
		System.out.println("trainer called");
		if((s1.charAt(1)==s2.charAt(0))&&(s1.charAt(2)!=s2.charAt(0)))	
			selector--;
		else if((s1.charAt(1)!=s2.charAt(0))&&(s1.charAt(2)==s2.charAt(0)))
			selector++;
		
		if((s2.equals(Integer.toString(0)))&&(bimpht[decimalpc1]>0))
	 		bimpht[decimalpc1]--;
		else if((s2.equals(Integer.toString(1)))&&(bimpht[decimalpc1]<3))
		 	bimpht[decimalpc1]++;
		 	
		if((s2.equals(Integer.toString(0)))&&(gpht[decimalpc]>0))
	 		gpht[decimalpc]--;
		else if((s2.equals(Integer.toString(1)))&&(gpht[decimalpc]<3))
		 	gpht[decimalpc]++;
		 	
		for(int y=0;y<5;y++)
			{
			 	bhr[y+1]=bhr[y];
			}
			bhr[0]=s2.charAt(0);
	}


}



public class dan{
public static void main(String[] args) throws Exception
{
	File f= new File("dyna2.txt");
	BufferedReader in = new BufferedReader(new FileReader(f));
	String[] line = new String[40000];

	line[0]  = in.readLine();
	int i=0;
	while(line[i]!=null)
	line[++i] = in.readLine();


	File f1= new File("stata1.txt");
	BufferedReader in1 = new BufferedReader(new FileReader(f1));
	String[] line1 = new String[40000];

	line1[0]  = in1.readLine();
	int j=0;
	while(line1[j]!=null)
	line1[++j] = in1.readLine();

	String[] mem_add=new String[40000];
	String[] isBranch=new String[40000];
	String[] dynamic_pc=new String[40000];
	for(int k=0;k<i;k++)
	{
 		String[] s4=line[k].split(" ");
 		dynamic_pc[k]=s4[0];
 		mem_add[k]=s4[1];
 		isBranch[k]=s4[2];
 	}
	
 	String[] static_pc=new String[40000];
 	String[] instruction_type=new String[40000];
 	for(int k=0;k<j;k++)
 	{
 		String[] ins=line1[k].split(" ");
 		static_pc[k]=ins[0];
 		instruction_type[k]=ins[1];
 	}


      
      
      int num=0,correct=0,total=0,flag=0;                              
      //int mem_next=-1;
      int ex_next=-1;
      int id_next=-1;
      int If_next=-1,exlast=0,idlast=0;
      //int inst_type=-1;
      //int l1miss=l2miss=0;
      int clock=0;//flagc=0;
      
      int []flagbr=new int[i];
      for(int mo=0;mo<i;mo++)
      {
       flagbr[mo]=0;
      }
      //index=-1;
      String st1=new String("-1");
      
      branchpredictor bp=new branchpredictor();
      while(ex_next!=i-1){
      					  
      	
      
                  
                   		if((id_next!=-1)&&(exlast==0))
                   		{
                       	
                       		
                       		if(flagbr[id_next]==1)
                       		{
                       			
                       			bp.trainer(st1,isBranch[id_next]);		//st1 is returned by predictor();
                       			
                       		}
                       		
                       		if(id_next==i-1)
                       			exlast=1;
                       		ex_next=id_next;
                       		
                       		
                       		
                       		}
                   
                  // Id(If_next)
				
                       		
                       		if((If_next!=-1)&&(idlast==0))
                       		{
                       	
                       		
                       		if(If_next==i-1)
                       			idlast=1;
                       		id_next=If_next;
                            }
                   
                  
                  // If()
                  if(num<i)
                     {
                         System.out.println("num="+num);
                        System.out.println("If stage ");
                         System.out.println(st1);
                         
                         if(flag==1)
                          {
                           	//System.out.println("flag=1");
                           	If_next=-1;
                           	flag=0;
                            num++;
                          }
                         
                        
                     
                         else if(isBranch[num].equals(Integer.toString(0)) || isBranch[num].equals(Integer.toString(1)))
                         {
                          	//System.out.println(num);
                          	total++;
                          	flagbr[num]=1;
                          	st1=bp.predictor(dynamic_pc[num]);             //result1 bim gshare
                         	if(isBranch[num].charAt(0)==st1.charAt(0))
                         		correct++;
                            
                          if(isBranch[num].charAt(0)!=st1.charAt(0))
                         {
                         	 	System.out.println("prediction wrong");
                         	 	flag=1;
                         	 	If_next=-1;
                         	  
                         	  //System.out.println("flagc="+flagc);
                          }
                          else
                          If_next=num++;
                     }
                         
                         else
                         If_next=num++;
                  //System.out.println("If_next="+If_next);
         	}
         
         
         
         
         
         clock++;
         }
         
         float bpacc=(float)correct/total;
         System.out.println(bpacc);
          float ipc=(float)i/clock;                                  
         System.out.println(ipc);







}
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by JamesCherrill
1

I can't make any sense of your undocumented code, but in general:
There is no restriction in Java anything like "only first function can access the variables."
Methods can share variables that are defined at class level, but not any variables that are declared within a method. If you declare a variable within a method that has the same name as a class variable then there is no compiler error, but it's very confusing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.