Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/processing/ExternalRuns.java

Go to the documentation of this file.
00001 
00020 package m2m.backend.processing;
00021 
00022 import java.io.BufferedReader;
00023 import java.io.BufferedWriter;
00024 import java.io.File;
00025 import java.io.FileWriter;
00026 import java.io.IOException;
00027 import java.io.InputStreamReader;
00028 import java.io.OutputStreamWriter;
00029 
00030 //import org.eclipse.ui.console.MessageConsoleStream;
00031 import m2m.backend.buildingblocks.BuildingBlock;
00032 import m2m.backend.buildingblocks.BuildingBlock.NumType;
00033 import m2m.backend.processing.Errors;
00034 import m2m.backend.processing.Errors.ErrorNum;
00035 import m2m.backend.processing.SystemVerilogCreator;
00036 import m2m.backend.project.M2MProject;
00037 import m2m.backend.project.SimulationResult;
00038 import m2m.backend.structure.StructTreatment;
00039 import m2m.backend.utils.FileUtils;
00040 import m2m.backend.verifpga.ISETCLGenerator;
00041 import m2m.backend.verifpga.M2MClient;
00042 import m2m.backend.project.ExternalToolsProperties;
00043 
00048 public class ExternalRuns {
00049 
00050         private static ExternalRuns instance=null;
00051         
00052         public static ExternalRuns getInstance() {
00053                 if (instance==null)
00054                         instance = new ExternalRuns();
00055                 return instance;
00056         }
00057         
00058         
00059         private ExternalRuns() {
00060         }
00061         
00062         
00063         static public boolean runAll(M2MProject project) {
00064 
00065                 project.checkFolders();
00066                 project.monitoring.subTask("Generating VHDL...");
00067                 if (!project.generateVHDL()) {
00068                         m2m.backend.processing.Errors.setLastErrorMessage("VHDL files could not be created. Please check the Math2mat library location with the menu Configure...External tools.");
00069                         return false;
00070                 }
00071                 project.monitoring.worked(20);
00072                         project.monitoring.subTask("Generating Octave...");
00073                 if (project.getStructTreatment().getMonitor()) {
00074                         if (!project.reGenerateOctave())
00075                                 return false;
00076                                         project.monitoring.subTask("Running Octave...");
00077                         if (!runOctave(project, true))
00078                                 return false;
00079                 }
00080                 else {
00081                                         project.monitoring.subTask("Running Octave...");
00082                         if (!runOctave(project,false))
00083                                 return false;
00084                 }
00085                 project.monitoring.worked(80);
00086                 if (!runQuesta(project))
00087                         return false;
00088                 
00089                 return true;
00090         }
00091         
00092         
00093         static public boolean runOctave(M2MProject project, boolean codeRegen) {
00094                 final String projectPath = project.getProperties().getProjectPath();
00095                 final ExternalToolsProperties externalToolsProperties = ExternalToolsProperties.getReference();
00096                 
00097 
00098                 BufferedReader brOctave;
00099                 BufferedReader brErrOctave;
00100                 
00101                 String line = "";
00102                 
00103                 Process process = null;
00104                 
00105                 //copy the necessary m2m files into the m2m source directory
00106                 FileUtils.copyFile(M2MProject.getLibPath()+"/m2m/m2m_doall.m", project.getOctavePath()+"/m2m_doall.m");
00107                 FileUtils.copyFile(M2MProject.getLibPath()+"/m2m/m2m_generatesamples.m", project.getOctavePath()+"/m2m_generatesamples.m");
00108                 if (project.getProperties().getOptimisationProperties().getOptimisationDataType()==BuildingBlock.NumType.FLOAT32) {
00109                         FileUtils.copyFile(M2MProject.getLibPath()+"/m2m/m2m_readvarfromfile.m", project.getOctavePath()+"/m2m_readvarfromfile.m");
00110                         FileUtils.copyFile(M2MProject.getLibPath()+"/m2m/m2m_writevartofile.m", project.getOctavePath()+"/m2m_writevartofile.m");
00111                 }
00112                 else if (project.getProperties().getOptimisationProperties().getOptimisationDataType()==BuildingBlock.NumType.FLOAT64) {
00113                         FileUtils.copyFile(M2MProject.getLibPath()+"/m2m/m2m_readvarfromfile_float64.m", project.getOctavePath()+"/m2m_readvarfromfile.m");
00114                         FileUtils.copyFile(M2MProject.getLibPath()+"/m2m/m2m_writevartofile_float64.m", project.getOctavePath()+"/m2m_writevartofile.m");       
00115                 }
00116                 else {
00117                         System.out.println("Error with the type of data to be processed.");
00118                 }
00119                 StructTreatment struct = project.getStructTreatment();
00120                 OctaveScriptCreator octaveScriptCreator = new OctaveScriptCreator(struct.getTop(), project.getSimulationProperties(), codeRegen);
00121                 octaveScriptCreator.createOctaveScript(project, project.getSourceFile());
00122                 
00123                 project.checkFolders();
00124                 
00125                 /* Execute the "octave" command */
00126                 try
00127                 {
00128                         System.out.println("Starting Octave...\n");
00129                         if (externalToolsProperties.getOctavePath() != null)
00130                                 process = Runtime.getRuntime().exec(externalToolsProperties.getOctavePath()+" -q");
00131                         else
00132 //                              if (System.getProperty("os.name").contains("Windows"))
00133 //                                      process = Runtime.getRuntime().exec("octave.exe");
00134 //                              else if (System.getProperty("os.name").contains("Linux"))
00135                                         process = Runtime.getRuntime().exec("octave -q");
00136                                         
00137                                 ProcessWatcher.getInstance().addProcess(process);
00138                                 //create streams to communicate with Octave application
00139                                 BufferedWriter bwOctave;
00140                                 bwOctave  = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
00141                                 brOctave = new BufferedReader(new InputStreamReader(process.getInputStream()));
00142                                 brErrOctave = new BufferedReader(new InputStreamReader(process.getErrorStream()));
00143                         
00144                                 try {
00145                                         bwOctave.write("cd "+"\""+project.getOctavePath()+"\""+"\n");
00146                                         bwOctave.flush();
00147 
00148                                         File m2mScript = new File(project.getOctavePath()+"/m2m_doall.m");
00149                                         if (!m2mScript.exists()) {
00150                                                 System.err.println("Octave script error: cannot find \"m2m_doall.m\" script in "+projectPath);
00151                                                 return false;
00152                                         }
00153                                         
00154                                         bwOctave.write("m2m_doall\n");
00155                                         bwOctave.flush();
00156                                         
00157                                         bwOctave.write("quit\n");
00158                                         bwOctave.flush();
00159                                         
00160                                         bwOctave.close();
00161                                 } catch (IOException e) {
00162                                         e.printStackTrace();
00163                                 }
00164                                 if (brOctave == null || brErrOctave == null)
00165                                         return false;
00166 
00167                                 while (true) {
00168                                         try {
00169                                                 if ((line = brOctave.readLine()) != null)
00170                                                         System.out.println(line);
00171                                                 else
00172                                                         break;
00173                                         } catch (IOException e1) {
00174                                                 // TODO Auto-generated catch block
00175                                                 e1.printStackTrace();
00176                                         }
00177                                 }
00178                                 while (true) {
00179                                         try {
00180                                                 if ((line = brErrOctave.readLine()) != null)
00181                                                         System.err.println(line);
00182                                                 else {
00183                                                         System.out.println("Octave generation finished");
00184                                                         project.setLastMonitoring(project.getStructTreatment().getMonitor());
00185                                                         break;
00186                                                 }
00187                                         } catch (IOException e1) {
00188                                                 e1.printStackTrace();
00189                                         }
00190                                 }
00191                                 
00192                 } catch (IOException ioe) {
00193                         System.err.println("Error launching Octave\nOctave path is not well configured, configure it correctly before continue");
00194                         Errors.setLastError(ErrorNum.OCTAVENOTFOUND);
00195                         return false;
00196                 }
00197 
00198                 ProcessWatcher.getInstance().removeProcess(process);
00199                 
00200                 return true;
00201         }
00202         
00203         static public boolean runDot(String dotFile,String pngFile) {
00204 
00205                 Process dotProcess = null;
00206 
00207                 try {
00208                         String cmdArray[]= new String[5];
00209                         cmdArray[0]=ExternalToolsProperties.getReference().getDotPath();
00210                         cmdArray[1]="-Tpng";
00211                         cmdArray[2]=dotFile;
00212                         cmdArray[3]="-o";
00213                         cmdArray[4]=pngFile;
00214                         dotProcess = new ProcessBuilder(cmdArray[0],cmdArray[1],cmdArray[2],cmdArray[3],cmdArray[4]).start();
00215 
00216                 } catch (IOException e1) {
00217                         System.out.println("Error lauching dot");
00218                         Errors.setLastError(Errors.ErrorNum.DOTNOTFOUND);
00219                         return false;
00220                 }
00221 
00222                 try {
00223                         dotProcess.waitFor();
00224                 }
00225                 catch (InterruptedException e) {
00226                         System.out.println("dot interrupted\n");
00227                         Errors.setLastErrorMessage("Dot process interrupted");
00228                         return false;
00229                 }
00230                 if (dotProcess.exitValue()!=0) {
00231                         Errors.setLastErrorMessage("Error with Dot execution. exit code is "+dotProcess.exitValue());
00232                         return false;
00233                 }
00234                 return true;
00235         }
00236         
00237         
00238         
00239         static private int nbSimErrors;
00240         
00241         static public int getNbSimErrors() {
00242                 return nbSimErrors;
00243         }
00244         static public boolean runQuesta(M2MProject project/*,MessageConsoleStream warStream*/) {
00245 
00246                 nbSimErrors=0;
00247                 
00248                 SimulationResult simResult=new SimulationResult();
00249                 
00250                 final String projectPath = project.getProperties().getProjectPath();
00251                 final String reportPath = project.getReportPath()+"/";
00252                 
00253 
00254                 final SystemVerilogCreator SVCreator;
00255                 BufferedReader brQuesta;
00256                 BufferedReader brErrQuesta;
00257 
00258                 SVCreator = new SystemVerilogCreator(project.getStructTreatment().getTop());
00259                 
00260                 String line = "";
00261                 
00262                 Process process = null;
00263                 FileWriter fwNote = null;
00264                 FileWriter fwError = null;
00265                 FileWriter fwFailure = null;
00266                 FileWriter fwWarning = null;
00267                 
00268 
00269                 //create the report folder, and the files in it
00270                 File reportFolder = new File(reportPath);
00271                 if ((reportFolder.exists() && reportFolder.isDirectory())) {
00272                         FileUtils.deleteDirContent(reportFolder,null);
00273                 }
00274                 
00275                 project.checkFolders();
00276                 
00277                 //create the "note" file
00278                 File noteFile = new File(reportPath+"Note_Report.txt");
00279                 //create the "error" file
00280                 File errorFile = new File(reportPath+"Error_Report.txt");
00281                 //create the "failure" file
00282                 File failureFile = new File(reportPath+"Failure_Report.txt");
00283                 //create the "warning" file
00284                 File warningFile = new File(reportPath+"Warning_Report.txt");
00285                 try {
00286                         fwNote = new FileWriter(noteFile);
00287                         fwError = new FileWriter(errorFile);
00288                         fwFailure = new FileWriter(failureFile);
00289                         fwWarning = new FileWriter(warningFile);
00290                 } catch (IOException e2) {
00291                         e2.printStackTrace();
00292                 }
00293 
00294                 
00295                 SVCreator.createSystemVerilog(project);
00296                 
00297                 File simulation = new File(project.getSimPath()+"/simulation.do");
00298                 if (!simulation.exists()) {
00299                         System.err.println("File not Found\nFile \"simulation.do\" not found in "+projectPath);
00300                         return false;
00301                 }
00302 
00303                 try {
00304                         //get questa path
00305                         ExternalToolsProperties externalToolsProperties = ExternalToolsProperties.getReference();
00306                         String vsimPath = externalToolsProperties.getVsimPath();
00307                         ProcessBuilder pb=new ProcessBuilder(vsimPath,"-c","-l",project.getSimPath()+"/transcript.txt");
00308                         pb.directory(new File(project.getSimPath()));
00309                         process = pb.start();
00310                 } catch (IOException ioe) {
00311                         System.err.println("Error launching vsim\nvsim path is not well configured, configure it correctly before continue");
00312                         Errors.setLastError(ErrorNum.QUESTANOTFOUND);
00313                         return false;
00314                 }
00315                 
00316 
00317                 ProcessWatcher.getInstance().addProcess(process);
00318 
00319                 //create streams to communicate with Questa application
00320                 BufferedWriter bwQuesta  = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
00321                 brQuesta = new BufferedReader(new InputStreamReader(process.getInputStream()));
00322                 brErrQuesta = new BufferedReader(new InputStreamReader(process.getErrorStream()));
00323                 
00324                 try {
00325                         bwQuesta.write("do "+"\""+project.getSimPath()+"/simulation.do"+"\"");
00326                         bwQuesta.flush();
00327                         
00328                         bwQuesta.close();
00329                 } catch (IOException e) {
00330                         e.printStackTrace();
00331                 }
00332                 
00333                 if (brQuesta == null || brErrQuesta == null)
00334                         return false;
00335                 
00336                 String prefix;
00337                 if (ExternalToolsProperties.getReference().getSimulationFiles()==ExternalToolsProperties.SIMULATIONFILES_UVM)
00338                         prefix="UVM_";
00339                 else if (ExternalToolsProperties.getReference().getSimulationFiles()==ExternalToolsProperties.SIMULATIONFILES_M2MOVM)
00340                         prefix="M2M_";
00341                 else
00342                         prefix="OVM_";
00343 //              prefix="";
00344                 simResult.setPrefix(prefix);
00345 
00346                 project.monitoring.subTask("Simulating...");
00347                 while (true) {
00348                         try {
00349                                 if ((line = brQuesta.readLine()) != null) {
00350                                         //System.out.println(line);
00351                                         if (line.contains(prefix+"WARNING") || line.contains("Warning")) {
00352                                                 System.out.println(line);
00353                                                 if (line.contains(prefix+"WARNING"))
00354                                                         fwWarning.write(line + "\r\n");
00355                                         }
00356                                         else if (line.contains(prefix+"FATAL")) {
00357                                                 nbSimErrors++;
00358                                                 System.err.println(line);
00359                                                 fwFailure.write(line + "\r\n");
00360                                         }
00361                                         else if (line.contains(prefix+"ERROR")) {
00362                                                 nbSimErrors++;
00363                                                 System.err.println(line);
00364                                                 fwError.write(line + "\r\n");
00365                                         }
00366                                         else if (line.contains(prefix+"INFO")) {
00367                                                 fwNote.write(line + "\r\n");
00368                                         }
00369                                         else if (line.contains("M2MVar(")) {
00370                                                 String variable;
00371                                                 String value;
00372                                                 int pos1=line.indexOf("(");
00373                                                 int pos2=line.indexOf(")");
00374                                                 variable=line.substring(pos1+1,pos2);
00375                                                 value=line.substring(pos2+2);
00376                                                 int valueInt;
00377                                                 try {
00378                                                         valueInt=Integer.parseInt(value);
00379                                                 }
00380                                                 catch (NumberFormatException e) {
00381                                                         valueInt=0;
00382                                                 }
00383                                                 if (variable.equals("Latency")) {
00384                                                         simResult.setLatency(valueInt);
00385                                                 }
00386                                                 else if (variable.equals("TotalTime")) {
00387                                                         simResult.setTotalTime(valueInt);
00388                                                 }
00389                                                 else if (variable.equals("NbInputs")) {
00390                                                         simResult.setNbInputs(valueInt);
00391                                                 }
00392                                                 else if (variable.equals("MaxBitError")) {
00393                                                         simResult.setMaxBitError(valueInt);
00394                                                 }
00395                                                 else if (variable.equals("Progress")) {
00396                                                         project.monitoring.worked(valueInt);
00397                                                 }
00398                                         }
00399                                         else {
00400                                                 System.out.println(line);
00401                                         }
00402                                 }
00403                                 else
00404                                         break;
00405                         } catch (IOException e1) {
00406                                 e1.printStackTrace();
00407                         }
00408                 }
00409 
00410                 while (true) {
00411                         try {
00412                                 if ((line = brErrQuesta.readLine()) != null)
00413                                 {}//    System.err.println(line);
00414                                 else {
00415                                         System.out.println("Questa verification finished");
00416                                         break;
00417                                 }
00418                         } catch (IOException e1) {
00419                                 e1.printStackTrace();
00420                         }
00421                 }
00422                 
00423                 try {
00424                         fwNote.close();
00425                         fwError.close();
00426                         fwFailure.close();
00427                         fwWarning.close();
00428                 } catch (IOException e) {
00429                         e.printStackTrace();
00430                 }
00431                 try {
00432                         process.waitFor();
00433                 }
00434                 catch (InterruptedException e) {
00435                 }
00436                 
00437                 System.out.println("Questa return value: "+process.exitValue()+"\n");
00438                 if (process.exitValue()==4) {
00439                         Errors.setLastError(ErrorNum.QUESTALICENSE);
00440                         return false;
00441                 }
00442                 
00443                 ProcessWatcher.getInstance().removeProcess(process);
00444                 simResult.setNbErrors(nbSimErrors-2);
00445                 project.setSimulationResult(simResult);
00446                 System.out.println("-- Simulation results ---------------------------------------");
00447                 if (simResult.getNbErrors()==0)
00448                         System.out.println("Number of errors : "+simResult.getNbErrors());
00449                 else
00450                         System.err.println("Number of errors : "+simResult.getNbErrors());
00451                 System.out.println("Number of wrong bits (from Lowest) : "+simResult.getMaxBitError());
00452                 if (simResult.getMaxBitError()!=0) {
00453                         if (project.getProperties().getOptimisationProperties().getOptimisationDataType()==NumType.FLOAT32) {
00454                                 System.out.print("    ");
00455                         
00456                                 for(int i=0;i<32+4;i++)
00457                                         System.out.print("-");
00458                                 System.out.print("\n");
00459                                 System.out.print("    ");
00460                                 System.out.print("|");
00461                                 for(int i=0;i<32;i++) {
00462                                         if (i<32-simResult.getMaxBitError())
00463                                                 System.out.print("v");
00464                                         else
00465                                                 System.err.print("x");
00466                                         if (i==0)
00467                                                 System.out.print("|");
00468                                         if (i==8)
00469                                                 System.out.print("|");
00470                                 }
00471                                 System.out.print("|\n");
00472                                 System.out.print("    ");
00473                                 for(int i=0;i<32+4;i++)
00474                                         System.out.print("-");
00475                                 System.out.print("\n\n");
00476                         }
00477                         else {
00478                                 System.out.print("    ");
00479                         
00480                                 for(int i=0;i<64+4;i++)
00481                                         System.out.print("-");
00482                                 System.out.print("\n");
00483                                 System.out.print("    ");
00484                                 System.out.print("|");
00485                                 for(int i=0;i<64;i++) {
00486                                         if (i<64-simResult.getMaxBitError())
00487                                                 System.out.print("v");
00488                                         else
00489                                                 System.err.print("x");
00490                                         if (i==0)
00491                                                 System.out.print("|");
00492                                         if (i==11)
00493                                                 System.out.print("|");
00494                                 }
00495                                 System.out.print("|\n");
00496                                 System.out.print("    ");
00497                                 for(int i=0;i<64+4;i++)
00498                                         System.out.print("-");
00499                                 System.out.print("\n\n");
00500                         }
00501                 }
00502                 System.out.println("Latency          : "+simResult.getLatency()+ " clock cycles");
00503                 System.out.println("Total time for "+simResult.getNbInputs()+" inputs : "+simResult.getTotalTime() + " clock cycles");
00504                 System.out.println("-- End of simulation ----------------------------------------");
00505                 return true;
00506         }
00507         
00508         static public boolean runXilinxUbidule(M2MProject project) {
00509                 ISETCLGenerator iseTclGen = new  ISETCLGenerator();
00510                 if (! iseTclGen.generateTCLISE(project))
00511                         return false;
00512                 if (!M2MClient.generateXSVF(project))
00513                         return false;
00514                 return true;
00515         }
00516         
00517         static public boolean runAllUbidule(M2MProject project) {
00518                 if (!runXilinxUbidule(project))
00519                         return false;
00520 
00521                 return runUbidule(project);
00522         }
00523         
00524         static public boolean runUbidule(M2MProject project) {
00525 
00526                 // Get the number of input of the function
00527                 int nbrInput = project.getStructTreatment().getInput().size();
00528                         
00529                 // Get the number of output of the function
00530                 int nbrOutput = project.getStructTreatment().getOutput().size();
00531                         
00532                 // Get the number of sample of the M2M project
00533                 int nbrSamples = project.getSimulationProperties().getNumberOfSamples();
00534                         
00535                 M2MClient client=new M2MClient();
00536                 return client.testOnUbidule(nbrInput, nbrOutput, nbrSamples, project);
00537         }
00538 
00539 }
 All Classes Namespaces Files Functions Variables Enumerations