Changeset 1723


Ignore:
Timestamp:
09/04/14 17:08:05 (10 years ago)
Author:
rkrimmel
Message:

More parallelism

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java

    r1721 r1723  
    2323import java.io.Serializable; 
    2424import java.util.ArrayList; 
    25 import java.util.Collection; 
    2625import java.util.Collections; 
    2726import java.util.Comparator; 
     
    3332import java.util.Map; 
    3433import java.util.Set; 
    35 import java.util.concurrent.Callable; 
    3634import java.util.concurrent.ExecutorService; 
    3735import java.util.concurrent.Executors; 
     
    656654                 
    657655                // Generate pairwise alignments 
    658                 int numberSeqSize = appData.getNumberSequences().size(); 
    659                 Console.traceln(Level.INFO, "generating pairwise alignments from " + numberSeqSize + " sessions"); 
    660                 int count = 0; 
    661                  
    662                 //Checking if i have an already calculated file result of this action in the working directory  
    663                 File aligned = new File("aligned" + iteration + ".dat"); 
    664                 if(!(aligned.exists() && !aligned.isDirectory())) { 
    665                         appData.matchseqs = new LinkedList<Match>(); 
    666                         for (int i = 0; i < appData.getNumberSequences().size(); i++) { 
    667                                 NumberSequence ns1 = appData.getNumberSequences().get(i); 
    668                                 count++; 
    669                                 printProgressPercentage(count,numberSeqSize); 
    670                                 for (int j = 0; j < appData.getNumberSequences().size(); j++) { 
    671                                         NumberSequence ns2 = appData.getNumberSequences().get(j); 
    672                                         if (i != j) { 
    673                                                 //Console.traceln(Level.FINEST,"Aligning sequence " + i + " with sequence " + j); 
    674                                                 AlignmentAlgorithm aa = AlignmentAlgorithmFactory.create(); 
    675                                                 aa.align(ns1, ns2, appData.getSubmat(),9); 
    676                                                 appData.getMatchseqs().addAll(aa.getMatches()); 
    677                                         } 
    678                                 } 
    679                         } 
    680                         GlobalDataContainer.getInstance().addData("aligned" + iteration, appData); 
    681                         saveAppData("aligned" + iteration); 
    682                 } 
    683                 else { 
    684                         Console.traceln(Level.INFO,"loading matches from file"); 
    685                         appData = loadAppData("aligned"+iteration); 
    686                 } 
    687  
    688  
    689                 Console.traceln(Level.INFO, "searching for patterns occuring most with " + nThreads + " threads"); 
    690                  
    691                 //Prepare parallel search of matchseqs 
    692                 ExecutorService executor = Executors.newFixedThreadPool(nThreads); 
    693                 int matchSeqSize=appData.getMatchseqs().size(); 
    694                 int interval = Math.round(matchSeqSize/nThreads); 
    695                 int rest = matchSeqSize % nThreads; 
    696                  
    697                 for(int i =0;i<matchSeqSize-interval;i+=interval) { 
    698                         int offset =0; 
    699                         if(rest!=0) { 
    700                                 offset=1; 
    701                                 rest--; 
    702                         } 
    703                         int from = i; 
    704                         int to = i+interval+offset; 
    705                         System.out.println("Creating thread with matches from " + from + " to " + to); 
    706                         // search each match in every other sequence 
    707                         ParallelMatchOcurrencesFinder finder = new ParallelMatchOcurrencesFinder(appData,from,to); 
    708                         executor.execute(finder); 
    709                 } 
    710                 executor.shutdown(); 
    711                 try { 
    712                         executor.awaitTermination(2, TimeUnit.HOURS); 
    713                 } catch (InterruptedException e) { 
    714                         // TODO Auto-generated catch block 
    715                         e.printStackTrace(); 
    716                 } 
    717                  
    718                  
    719                 Console.traceln(Level.INFO, "sorting results"); 
     656                appData.setMatchseqs(generatePairwiseAlignments(appData)); 
     657 
     658                //Searching each match in all other sessions, counting its occurences 
     659                searchMatchesInAllSessions(appData); 
     660                 
    720661                // Sort results to get the most occurring results 
     662                Console.traceln(Level.INFO, "sorting " + appData.getMatchseqs().size() + " results"); 
    721663                Comparator<Match> comparator = new Comparator<Match>() { 
    722664                        public int compare(Match m1, Match m2) { 
     
    725667                        } 
    726668                }; 
     669                 
    727670                Collections.sort(appData.getMatchseqs(), comparator); 
    728671                appData.getStopWatch().stop("detecting tasks"); 
    729672 
     673                // Replace matches in the sessions 
     674                Console.traceln(Level.INFO, "Replacing matches in sessions"); 
    730675                appData.getStopWatch().start("replacing tasks"); 
    731676                HashMap<Integer, List<MatchOccurence>> replacedOccurences = new HashMap<Integer, List<MatchOccurence>>(); 
    732                 // Replace matches in the sessions 
    733677                for (int i = 0; i < appData.getMatchseqs().size(); i++) { 
    734678                 
     
    744688                                        MatchOccurence oc = it.next(); 
    745689                                         
    746                                         /* 
    747                                         if (iteration > 0) { 
    748                                           
    749                                         System.out.println("Trying to replace sequence: "); 
    750                                         appData.getMatchseqs().get(i).getFirstSequence().printSequence(); 
    751                                         appData.getMatchseqs().get(i).getSecondSequence().printSequence(); 
    752                                         System.out.println(" in session number: " 
    753                                                         + (oc.getSequenceId() + 1) + " at position " 
    754                                                         + (oc.getStartindex()) + "-" + oc.getEndindex()); 
    755                                         System.out.println(); 
    756  
    757                                         System.out.println("Printing session: "); 
    758                                         for (int j = 0; j < appData.getSessions() 
    759                                                         .get(oc.getSequenceId()).size(); j++) { 
    760                                                 System.out.println(j 
    761                                                                 + ": " 
    762                                                                 + appData.getSessions().get(oc.getSequenceId()) 
    763                                                                                 .get(j)); 
    764                                         } 
    765                                         }*/ 
    766690                                         
    767691                                        // Check if nothing has been replaced in the sequence we 
     
    808732                                                } 
    809733                                        } 
     734                                        System.out.println("Replacing"); 
    810735                                        ISequenceInstance sequenceInstances = RuleUtils 
    811736                                                        .createNewSubSequenceInRange(appData.getSessions() 
     
    827752         
    828753         
     754         
     755 
     756        private void searchMatchesInAllSessions(RuleApplicationData appData) { 
     757                Console.traceln(Level.INFO, "searching for patterns occuring most with " + nThreads + " threads"); 
     758                //Prepare parallel search of matchseqs 
     759                ExecutorService executor = Executors.newFixedThreadPool(nThreads); 
     760                int matchSeqSize=appData.getMatchseqs().size(); 
     761                int interval = Math.round(matchSeqSize/nThreads); 
     762                int rest = matchSeqSize % nThreads; 
     763                 
     764                for(int i =0;i<matchSeqSize-interval;i+=interval) { 
     765                        int offset =0; 
     766                        if(rest!=0) { 
     767                                offset=1; 
     768                                rest--; 
     769                        } 
     770                        int from = i; 
     771                        int to = i+interval+offset; 
     772                        System.out.println("Creating thread with matches from " + from + " to " + to); 
     773                        // search each match in every other sequence 
     774                        ParallelMatchOcurrencesFinder finder = new ParallelMatchOcurrencesFinder(appData,from,to); 
     775                        executor.execute(finder); 
     776                } 
     777                executor.shutdown(); 
     778                try { 
     779                        executor.awaitTermination(2, TimeUnit.HOURS); 
     780                } catch (InterruptedException e) { 
     781                        // TODO Auto-generated catch block 
     782                        e.printStackTrace(); 
     783                } 
     784                 
     785        } 
     786 
    829787        //Print out the progress 
    830788         private static void printProgressPercentage(int count, int size) { 
    831                  
    832789                if(size>100) { 
    833790                        if((count%(size/100)==0)) {      
    834                                 Console.traceln(Level.INFO,("Thread" + Thread.currentThread().getName() + ": " + Math.round((float) count/size*100))+ "%"); 
     791                                //Console.traceln(Level.INFO,("Thread" + Thread.currentThread().getName() + ": " + Math.round((float) count/size*100))+ "%"); 
     792                                System.out.println("Thread" + Thread.currentThread().getName() + ": " + Math.round((float) count/size*100)+ "%"); 
    835793                        } 
    836794                } 
    837795                else { 
    838                         Console.traceln(Level.INFO,("Thread" + Thread.currentThread().getName() + ": " +Math.round((float) count/size*100))+ "%"); 
     796                        //Console.traceln(Level.INFO,("Thread" + Thread.currentThread().getName() + ": " +Math.round((float) count/size*100))+ "%"); 
     797                        System.out.println("Thread" + Thread.currentThread().getName() + ": " +Math.round((float) count/size*100)+ "%"); 
     798                         
    839799                } 
    840800        } 
     
    857817                int count = 0; 
    858818                int size=to-from; 
    859                 Console.traceln(Level.INFO, "Pattern count is " + size); 
    860                 for (int i=from; i<to;i++) { { 
     819                 
     820                for (int i=from; i<to;i++) {  
    861821                        Match pattern = appData.getMatchseqs().get(i); 
    862822                        count++; 
     
    880840                                                                + pattern.size(), j)); 
    881841                                        } 
    882  
    883842                                } 
    884843                        } 
    885844                }   
     845        } 
     846        } 
     847         
     848         
     849        private class ParallelPairwiseAligner implements Runnable { 
     850        private final RuleApplicationData appData; 
     851        private final int from; 
     852        private final int to; 
     853            ParallelPairwiseAligner(RuleApplicationData appData, int from, int to) { 
     854            this.appData = appData; 
     855            this.from = from; 
     856            this.to = to; 
     857        } 
     858         
     859        @Override 
     860        public void run() { 
     861                int count = 0; 
     862                int size=to-from; 
     863                 
     864                for (int i=from; i<to;i++) { 
     865                        NumberSequence ns1 = appData.getNumberSequences().get(i); 
     866                                count++; 
     867                                printProgressPercentage(count,size); 
     868                                for (int j = 0; j < appData.getNumberSequences().size(); j++) { 
     869                                        NumberSequence ns2 = appData.getNumberSequences().get(j); 
     870                                        if (i != j) { 
     871                                                AlignmentAlgorithm aa = AlignmentAlgorithmFactory.create(); 
     872                                                aa.align(ns1, ns2, appData.getSubmat(),9); 
     873                                                synchronized(appData.getMatchseqs()) { 
     874                                                        appData.getMatchseqs().addAll(aa.getMatches()); 
     875                                                } 
     876                                        } 
     877                                } 
     878                }   
    886879        } 
    887         } 
    888         } 
    889          
    890          
    891  
     880        } 
     881 
     882        private LinkedList<Match> generatePairwiseAlignments(RuleApplicationData appData) { 
     883                int numberSeqSize = appData.getNumberSequences().size(); 
     884                LinkedList<Match> result; 
     885                //Checking if i have an already calculated file result of this action in the working directory  
     886                File aligned = new File("aligned" + iteration + ".dat"); 
     887                if(!(aligned.exists() && !aligned.isDirectory())) { 
     888                        Console.traceln(Level.INFO, "generating pairwise alignments from " + numberSeqSize + " sessions"); 
     889                        result = new LinkedList<Match>(); 
     890                         
     891                        ExecutorService executor = Executors.newFixedThreadPool(nThreads); 
     892                        int interval = Math.round(numberSeqSize/nThreads); 
     893                        int rest = numberSeqSize % nThreads; 
     894                         
     895                        for (int i = 0; i < numberSeqSize-interval; i+=interval) { 
     896                                int offset = 0; 
     897                                if(rest!=0) { 
     898                                        offset=1; 
     899                                        rest--; 
     900                                } 
     901                                int from = i; 
     902                                int to = i+interval+offset; 
     903                                System.out.println("Creating thread with matches from " + from + " to " + to); 
     904                                ParallelPairwiseAligner aligner = new ParallelPairwiseAligner(appData,from,to); 
     905                                executor.execute(aligner); 
     906                        } 
     907                        executor.shutdown(); 
     908                        try { 
     909                                executor.awaitTermination(10, TimeUnit.MINUTES); 
     910                        } catch (InterruptedException e) { 
     911                                // TODO Auto-generated catch block 
     912                                e.printStackTrace(); 
     913                        } 
     914         
     915                        GlobalDataContainer.getInstance().addData("aligned" + iteration, appData); 
     916                        saveAppData("aligned" + iteration); 
     917                        return result; 
     918                } 
     919                else { 
     920                        Console.traceln(Level.INFO,"loading matches from file"); 
     921                        appData = loadAppData("aligned"+iteration); 
     922                        return appData.getMatchseqs(); 
     923                } 
     924        } 
     925         
    892926 
    893927        /** 
     
    947981                        newTasks = new LinkedList<ITask>(); 
    948982                         
    949                         //this.detectedAndReplacedTasks = true; 
     983                        this.detectedAndReplacedTasks = true; 
    950984                } 
    951985 
Note: See TracChangeset for help on using the changeset viewer.