package be.ac.vub.ir.multithreading; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.swing.JFileChooser; import flanagan.math.Fmath; import be.ac.vub.ir.util.Chrono; import be.ac.vub.ir.util.UserPreferences; public class FileReading { public static void main(String[] args) throws MalformedURLException { testLatencyHidingForJob(4); testLatencyHidingForFileFromURL(); testLatencyHidingForFileFromSystem(); } //======================== LATENCY HIDING TESTS ===========================// // run on 1 processor (set affinity of eclipse process in task manager) public static void testLatencyHidingForJob(final int NBR_JOBS){ final long ITERATIONS = 30000000L; final Thread[] t = new Thread[NBR_JOBS]; Chrono totalT = new Chrono(); for(int j = 0;j speedup = "+Fmath.truncate((float)jobT.elapsedtime()*NBR_JOBS/totalT.elapsedtime(), 3)); System.out.println(); } public static void testLatencyHidingForFileFromURL() throws MalformedURLException{ final long NBR = 25000000; // set this number so that the time of the job is similar to that of the file reading Chrono seq = new Chrono(); URL url = new URL("http://parallel.vub.ac.be/research/causalModels/causalLearningWithKde.jar"); Chrono reading = new Chrono(); int count = countLinesInURL(url) ; reading.stop(); System.out.println("URL "+url+" contains "+count+" lines in "+reading); Chrono calc = new Chrono(); int seed = seedCalculation(NBR) ; calc.stop(); seq.stop(); System.out.println("The "+NBR+"th number is "+seed+" in "+calc); System.out.println("Total time = "+Chrono.toString(reading.elapsedtime() + calc.elapsedtime())); // the same file, but a copy, otherwise the first one is taken from the cache! URL url2 = new URL("http://parallel.vub.ac.be/research/causalModels/causalLearningWithKde_copy.jar"); Chrono par = new Chrono(); Thread t = new Thread(){ public void run(){ // some calculations... int seed = seedCalculation(NBR) ; System.out.println("The "+NBR+"th number is "+seed); }}; t.start(); count = countLinesInURL(url2) ; System.out.println("URL "+url2+" contains "+count+" lines"); try { t.join(); } catch (InterruptedException e) { } par.stop(); System.out.println("Seq "+seq+", par "+par); System.out.println("Parallel runtime "+par); System.out.println(" => speedup = "+Fmath.truncate((float)seq.elapsedtime()/par.elapsedtime(), 3) ); System.out.println(); } public static void testLatencyHidingForFileFromSystem() throws MalformedURLException{ final long NBR = 25000000; // set this number so that the time of the job is similar to that of the file reading File file = openFile(); Chrono seq = new Chrono(); Chrono reading = new Chrono(); int count = countLinesInFile(file); reading.stop(); System.out.println("File "+file+" contains "+count+" lines in "+reading); Chrono calc = new Chrono(); int seed = seedCalculation(NBR) ; calc.stop(); seq.stop(); System.out.println("The "+NBR+"th number is "+seed+" in "+calc); System.out.println("Total time = "+Chrono.toString(reading.elapsedtime() + calc.elapsedtime())); Chrono par = new Chrono(); Thread t = new Thread(){ public void run(){ // some calculations... int seed = seedCalculation(NBR) ; System.out.println("The "+NBR+"th number is "+seed); }}; t.start(); count = countLinesInFile(file); System.out.println("File "+file+" contains "+count+" lines"); try { t.join(); } catch (InterruptedException e) { } par.stop(); System.out.println("Seq "+seq+", par "+par); System.out.println("Parallel runtime "+par); System.out.println(" => speedup = "+Fmath.truncate((float)seq.elapsedtime()/par.elapsedtime(), 3) ); System.out.println(); } //======================== FILE AND URL OPERATIONS ===========================// public static int countLinesInFile(File file){ BufferedReader reader = null; try{ reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { System.err.println("File "+file+" not found. Aborting..."); return -1; } int count = 0; try { String line = reader.readLine(); while (line != null){ count++; line = reader.readLine(); } } catch (IOException e) { System.err.println("Error reading line "+count+" of file "+file+". Aborting..."); } return count; } public static int countLinesInURL(URL url){ BufferedReader reader = null; try{ URLConnection urlConn = url.openConnection(); InputStream inputStream = urlConn.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); } catch (IOException e) { System.err.println("URL "+url+" not found. Aborting..."); return -1; } int count = 0; try { String line = reader.readLine(); while (line != null){ count++; line = reader.readLine(); } } catch (IOException e) { System.err.println("Error reading line "+count+" of URL "+url+". Aborting..."); } return count; } public static int seedCalculation(long nbr){ int seed = (int)(Math.random()*1013); for(long i=0;i