From 4cc6cfa3ac7d0a4ed90c594e9c013f18997a3f20 Mon Sep 17 00:00:00 2001 From: "trevor.cohn" Date: Tue, 6 Jul 2010 16:23:11 +0000 Subject: Thread pooling git-svn-id: https://ws10smt.googlecode.com/svn/trunk@151 ec762483-ff6d-05da-a07a-a48fb63a330f --- .../prjava/src/phrase/PhraseCluster.java | 165 ++++++++++++++++----- .../prjava/src/phrase/PhraseCorpus.java | 1 - .../prjava/src/phrase/PhraseObjective.java | 8 +- 3 files changed, 129 insertions(+), 45 deletions(-) (limited to 'gi') diff --git a/gi/posterior-regularisation/prjava/src/phrase/PhraseCluster.java b/gi/posterior-regularisation/prjava/src/phrase/PhraseCluster.java index 8b1e0a8c..cd28c12e 100644 --- a/gi/posterior-regularisation/prjava/src/phrase/PhraseCluster.java +++ b/gi/posterior-regularisation/prjava/src/phrase/PhraseCluster.java @@ -1,53 +1,65 @@ package phrase; import io.FileUtil; - import java.io.PrintStream; import java.util.Arrays; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; public class PhraseCluster { - - /**@brief number of clusters*/ + public int K; + public double scale; private int n_phrase; private int n_words; public PhraseCorpus c; + private ExecutorService pool; /**@brief * emit[tag][position][word] */ private double emit[][][]; private double pi[][]; + - public static int ITER=20; - public static String postFilename="../pdata/posterior.out"; - public static String phraseStatFilename="../pdata/phrase_stat.out"; - private static int NUM_TAG=3; public static void main(String[] args) { - - PhraseCorpus c=new PhraseCorpus(PhraseCorpus.DATA_FILENAME); - - PhraseCluster cluster=new PhraseCluster(NUM_TAG,c); - PhraseObjective.ps=FileUtil.openOutFile(phraseStatFilename); - for(int i=0;i 0) + pool = Executors.newFixedThreadPool(threads); emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words]; pi=new double[n_phrase][K]; @@ -61,28 +73,15 @@ public class PhraseCluster { for(double []j:pi){ arr.F.randomise(j); } - - pi[0]=new double[]{ - 0.3,0.5,0.2 - }; - - double temp[][]=new double[][]{ - {0.11,0.16,0.19,0.11,0.1}, - {0.10,0.15,0.18,0.1,0.11}, - {0.09,0.07,0.12,0.14,0.13} - }; - - for(int tag=0;tag<3;tag++){ - for(int word=0;word<4;word++){ - for(int pos=0;pos<4;pos++){ - emit[tag][pos][word]=temp[tag][word]; - } - } - } - + } + + public void finish() + { + if (pool != null) + pool.shutdown(); } - public void EM(){ + public double EM(){ double [][][]exp_emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words]; double [][]exp_pi=new double[n_phrase][K]; @@ -125,9 +124,14 @@ public class PhraseCluster { } pi=exp_pi; + + return loglikelihood; } - public void PREM(){ + public double PREM(){ + if (pool != null) + return PREMParallel(); + double [][][]exp_emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words]; double [][]exp_pi=new double[n_phrase][K]; @@ -171,6 +175,89 @@ public class PhraseCluster { } pi=exp_pi; + + return primal; + } + + public double PREMParallel(){ + assert(pool != null); + final LinkedBlockingQueue expectations + = new LinkedBlockingQueue(); + + double [][][]exp_emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words]; + double [][]exp_pi=new double[n_phrase][K]; + + double loglikelihood=0; + double primal=0; + //E + for(int phrase=0;phrase