📄 threadedacousticscorer.java
字号:
/* * Copyright 1999-2002 Carnegie Mellon University. * Portions Copyright 2002 Sun Microsystems, Inc. * Portions Copyright 2002 Mitsubishi Electric Research Laboratories. * All Rights Reserved. Use is subject to license terms. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. * */package edu.cmu.sphinx.decoder.scorer;import java.io.IOException;import java.util.List;import java.util.ListIterator;import java.util.logging.Logger;import edu.cmu.sphinx.frontend.Data;import edu.cmu.sphinx.frontend.DataEndSignal;import edu.cmu.sphinx.frontend.DataProcessingException;import edu.cmu.sphinx.frontend.DataStartSignal;import edu.cmu.sphinx.frontend.FrontEnd;import edu.cmu.sphinx.frontend.Signal;import edu.cmu.sphinx.util.props.PropertyException;import edu.cmu.sphinx.util.props.PropertySheet;import edu.cmu.sphinx.util.props.PropertyType;import edu.cmu.sphinx.util.props.Registry;/** * An acoustic scorer that breaks the scoring up into a configurable number of * separate threads. * * All scores are maintained in LogMath log base */public class ThreadedAcousticScorer implements AcousticScorer { /** * Property the defines the frontend to retrieve features from for scoring * */ public final static String PROP_FRONTEND = "frontend"; /** * A SphinxProperty name that controls the number of threads that are used * to score hmm states. If the isCpuRelative property is false, then is is * the exact number of threads that are used to score hmm states. if the * isCpuRelative property is true, then this value is combined with the * number of available proessors on the system. If you want to have one * thread per CPU available to score states, set the NUM_THREADS property * to 0 and the isCpuRelative to true. If you want exactly one thread to * process scores set NUM_THREADS to 1 and isCpuRelative to false. The * default value is 1 */ public final static String PROP_NUM_THREADS = "numThreads"; /** * The default value for PROP_NUM_THREADS. */ public final static int PROP_NUM_THREADS_DEFAULT = 1; /** * A sphinx property name that controls whether the number of available * CPUs on the system is used when determining the number of threads to use * for scoring. If true, the NUM_THREADS property is combined with the * available number of CPUS to deterimine the number of threads. Note that * the number of threads is contrained to be never lower than zero. Also, * if the number of threads is one, the states are scored on the calling * thread, no separate threads are started. The default value is false. */ public final static String PROP_IS_CPU_RELATIVE = "isCpuRelative"; /** * The default value for PROP_IS_CPU_RELATIVE. */ public final static boolean PROP_IS_CPU_RELATIVE_DEFAULT = false; /** * A Sphinx Property name that controls the minimum number of scoreables * sent to a thread. This is used to prevent over threading of the scoring * that could happen if the number of threads is high compared to the size * of the activelist. The default is 50 */ public final static String PROP_MIN_SCOREABLES_PER_THREAD = "minScoreablesPerThread"; /** * The default value for PROP_MIN_SCOREABLES_PER_THREAD. */ public final static int PROP_MIN_SCOREABLES_PER_THREAD_DEFAULT = 50; /** * A SphinxProperty specifying whether the scoreables should keep a * reference to the scored features. */ public final static String PROP_SCOREABLES_KEEP_FEATURE = "scoreablesKeepFeature"; /** * The default value for PROP_SCOREABLES_KEEP_FEATURE. */ public final static boolean PROP_SCOREABLES_KEEP_FEATURE_DEFAULT = false; /** * A sphinx property that controls the amount of acoustic gain. */ public final static String PROP_ACOUSTIC_GAIN = "acousticGain"; /** * The default value for the PROP_ACOUSTIC_LOOKAHEAD_FRAMES property. */ public final static float PROP_ACOUSTIC_GAIN_DEFAULT = 1F; // ---------------------------- // Configuration data // ---------------------------- private String name; private FrontEnd frontEnd; // where features come from private int numThreads; // number of threads in use private int minScoreablesPerThread; // min scoreables sent to a thread private boolean keepData; // scoreables keep feature or not private Logger logger; private float acousticGain; // acoustic gain private Mailbox mailbox; // sync between caller and threads private Semaphore semaphore; // join after call private Data currentData; // current feature being processed /* * (non-Javadoc) * * @see edu.cmu.sphinx.decoder.scorer.AcousticScorer#allocate() */ public void allocate() throws IOException { logger.info("# of scoring threads: " + numThreads); // if we only have one thread, then we'll score the // states in the calling thread and we won't need any // of the synchronization objects or threads if (numThreads > 1) { mailbox = new Mailbox(); semaphore = new Semaphore(); for (int i = 0; i < (numThreads - 1); i++) { Thread t = new ScoringThread(); t.start(); } } } /* * (non-Javadoc) * * @see edu.cmu.sphinx.decoder.scorer.AcousticScorer#deallocate() */ public void deallocate() { } /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#register(java.lang.String, * edu.cmu.sphinx.util.props.Registry) */ public void register(String name, Registry registry) throws PropertyException { this.name = name; registry.register(PROP_FRONTEND, PropertyType.COMPONENT); registry.register(PROP_IS_CPU_RELATIVE, PropertyType.BOOLEAN); registry.register(PROP_NUM_THREADS, PropertyType.INT); registry.register(PROP_MIN_SCOREABLES_PER_THREAD, PropertyType.INT); registry.register(PROP_SCOREABLES_KEEP_FEATURE, PropertyType.BOOLEAN); registry.register(PROP_ACOUSTIC_GAIN, PropertyType.FLOAT); } /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#newProperties(edu.cmu.sphinx.util.props.PropertySheet) */ public void newProperties(PropertySheet ps) throws PropertyException { logger = ps.getLogger(); this.frontEnd = (FrontEnd) ps.getComponent(PROP_FRONTEND, FrontEnd.class); boolean cpuRelative = ps.getBoolean(PROP_IS_CPU_RELATIVE, PROP_IS_CPU_RELATIVE_DEFAULT); numThreads = ps.getInt(PROP_NUM_THREADS, PROP_NUM_THREADS_DEFAULT); minScoreablesPerThread = ps.getInt(PROP_MIN_SCOREABLES_PER_THREAD, PROP_MIN_SCOREABLES_PER_THREAD_DEFAULT); keepData = ps.getBoolean(PROP_SCOREABLES_KEEP_FEATURE, PROP_SCOREABLES_KEEP_FEATURE_DEFAULT); acousticGain = ps.getFloat(PROP_ACOUSTIC_GAIN, PROP_ACOUSTIC_GAIN_DEFAULT); if (cpuRelative) { numThreads += Runtime.getRuntime().availableProcessors(); } if (numThreads < 1) { numThreads = 1; } } /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#getName() */ public String getName() { return name; } /** * Initializes the scorer */ public void startRecognition() { } /** * Scores the given set of states * * @param scoreableList * a list containing scoreable objects to be scored * * @return the best scorign scoreable, or null if there are no more * features to score */ public Scoreable calculateScores(List scoreableList) { Scoreable best = null; try { Data data = frontEnd.getData(); if (data == null) { return best; } if (data instanceof DataStartSignal) { data = frontEnd.getData(); if (data == null) { return best; } } if (data instanceof DataEndSignal) { return best; } if (data instanceof Signal) { throw new Error("Can't score non-content feature"); } currentData = data; if (numThreads > 1) { int nThreads = numThreads; int scoreablesPerThread = (scoreableList.size() + (numThreads - 1)) / numThreads; if (scoreablesPerThread < minScoreablesPerThread) { scoreablesPerThread = minScoreablesPerThread; nThreads = (scoreableList.size() + (scoreablesPerThread - 1)) / scoreablesPerThread; } semaphore.reset(nThreads); for (int i = 0; i < nThreads; i++) { ScoreableJob job = new ScoreableJob(scoreableList, i * scoreablesPerThread, scoreablesPerThread); if (i < (nThreads - 1)) { mailbox.post(job);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -