You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@joshua.apache.org by mj...@apache.org on 2016/05/03 05:07:40 UTC
[3/6] incubator-joshua git commit: Reduce concurrency on Vocab by
using opt stamped rw locks Drops concurrency in Vocab class from 7min14s to
0m0s during training run of 50K
Reduce concurrency on Vocab by using opt stamped rw locks
Drops concurrency in Vocab class from 7min14s to 0m0s during training run of 50K
Project: http://git-wip-us.apache.org/repos/asf/incubator-joshua/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-joshua/commit/cb700140
Tree: http://git-wip-us.apache.org/repos/asf/incubator-joshua/tree/cb700140
Diff: http://git-wip-us.apache.org/repos/asf/incubator-joshua/diff/cb700140
Branch: refs/heads/master
Commit: cb7001406da2f601dac51669d56648342c881b45
Parents: 39f59a8
Author: Kellen Sunderland <ke...@amazon.com>
Authored: Wed Jan 27 11:55:13 2016 +0100
Committer: Kellen Sunderland <ke...@amazon.com>
Committed: Mon May 2 12:27:39 2016 -0700
----------------------------------------------------------------------
src/joshua/corpus/Vocabulary.java | 188 +++++++++++++++++++--------------
1 file changed, 106 insertions(+), 82 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/cb700140/src/joshua/corpus/Vocabulary.java
----------------------------------------------------------------------
diff --git a/src/joshua/corpus/Vocabulary.java b/src/joshua/corpus/Vocabulary.java
index 1792219..ee59507 100644
--- a/src/joshua/corpus/Vocabulary.java
+++ b/src/joshua/corpus/Vocabulary.java
@@ -1,21 +1,3 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
package joshua.corpus;
import static joshua.util.FormatUtils.isNonterminal;
@@ -32,6 +14,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.locks.StampedLock;
import joshua.decoder.Decoder;
import joshua.decoder.ff.lm.NGramLanguageModel;
@@ -40,33 +23,32 @@ import joshua.util.FormatUtils;
/**
* Static singular vocabulary class.
* Supports (de-)serialization into a vocabulary file.
- *
+ *
* @author Juri Ganitkevitch
*/
public class Vocabulary {
- private final static ArrayList<NGramLanguageModel> LMs = new ArrayList<NGramLanguageModel>();
+ private final static ArrayList<NGramLanguageModel> LMs = new ArrayList<>();
private static List<String> idToString;
private static Map<String, Integer> stringToId;
-
+ private static final StampedLock lock = new StampedLock();
private static volatile List<Integer> nonTerminalIndices;
- private static final Integer lock = new Integer(0);
-
static final int UNKNOWN_ID = 0;
static final String UNKNOWN_WORD = "<unk>";
public static final String START_SYM = "<s>";
public static final String STOP_SYM = "</s>";
-
+
static {
clear();
}
public static boolean registerLanguageModel(NGramLanguageModel lm) {
- synchronized (lock) {
+ long lock_stamp = lock.writeLock();
+ try {
// Store the language model.
LMs.add(lm);
// Notify it of all the existing words.
@@ -74,39 +56,40 @@ public class Vocabulary {
for (int i = idToString.size() - 1; i > 0; i--)
collision = collision || lm.registerWord(idToString.get(i), i);
return collision;
+ } finally {
+ lock.unlockWrite(lock_stamp);
}
}
/**
* Reads a vocabulary from file. This deletes any additions to the vocabulary made prior to
* reading the file.
- *
+ *
* @param file_name
* @return Returns true if vocabulary was read without mismatches or collisions.
* @throws IOException
*/
public static boolean read(final File vocab_file) throws IOException {
- synchronized (lock) {
- DataInputStream vocab_stream =
- new DataInputStream(new BufferedInputStream(new FileInputStream(vocab_file)));
- int size = vocab_stream.readInt();
- Decoder.LOG(1, String.format("Read %d entries from the vocabulary", size));
- clear();
- for (int i = 0; i < size; i++) {
- int id = vocab_stream.readInt();
- String token = vocab_stream.readUTF();
- if (id != Math.abs(id(token))) {
- vocab_stream.close();
- return false;
- }
+ DataInputStream vocab_stream =
+ new DataInputStream(new BufferedInputStream(new FileInputStream(vocab_file)));
+ int size = vocab_stream.readInt();
+ Decoder.LOG(1, String.format("Read %d entries from the vocabulary", size));
+ clear();
+ for (int i = 0; i < size; i++) {
+ int id = vocab_stream.readInt();
+ String token = vocab_stream.readUTF();
+ if (id != Math.abs(id(token))) {
+ vocab_stream.close();
+ return false;
}
- vocab_stream.close();
- return (size + 1 == idToString.size());
}
+ vocab_stream.close();
+ return (size + 1 == idToString.size());
}
public static void write(String file_name) throws IOException {
- synchronized (lock) {
+ long lock_stamp =lock.readLock();
+ try {
File vocab_file = new File(file_name);
DataOutputStream vocab_stream =
new DataOutputStream(new BufferedOutputStream(new FileOutputStream(vocab_file)));
@@ -118,44 +101,81 @@ public class Vocabulary {
}
vocab_stream.close();
}
+ finally{
+ lock.unlockRead(lock_stamp);
+ }
}
/**
* Get the id of the token if it already exists, new id is created otherwise.
- *
- * TODO: currently locks for every call.
- * Separate constant (frozen) ids from changing (e.g. OOV) ids.
- * Constant ids could be immutable -> no locking.
- * Alternatively: could we use ConcurrentHashMap to not have to lock if actually contains it and only lock for modifications?
+ *
+ * TODO: currently locks for every call. Separate constant (frozen) ids from
+ * changing (e.g. OOV) ids. Constant ids could be immutable -> no locking.
+ * Alternatively: could we use ConcurrentHashMap to not have to lock if
+ * actually contains it and only lock for modifications?
*/
public static int id(String token) {
- synchronized (lock) {
+ // First attempt an optimistic read
+ long attempt_read_lock = lock.tryOptimisticRead();
+ if (stringToId.containsKey(token)) {
+ int resultId = stringToId.get(token);
+ if (lock.validate(attempt_read_lock)) {
+ return resultId;
+ }
+ }
+else {
+ if (nonTerminalIndices != null && nt(token)) {
+ throw new IllegalArgumentException(
+ "After the nonterminal indices have been set by calling getNonterminalIndices you can't call id on new nonterminals anymore.");
+ }
+ }
+
+ // The optimistic read failed, try a read with a stamped read lock
+ long read_lock_stamp = lock.readLock();
+ try {
if (stringToId.containsKey(token)) {
return stringToId.get(token);
} else {
if (nonTerminalIndices != null && nt(token)) {
- throw new IllegalArgumentException("After the nonterminal indices have been set by calling getNonterminalIndices you can't call id on new nonterminals anymore.");
+ throw new IllegalArgumentException(
+ "After the nonterminal indices have been set by calling getNonterminalIndices you can't call id on new nonterminals anymore.");
}
- int id = idToString.size() * (nt(token) ? -1 : 1);
-
- // register this (token,id) mapping with each language
- // model, so that they can map it to their own private
- // vocabularies
- for (NGramLanguageModel lm : LMs)
- lm.registerWord(token, Math.abs(id));
+ }
+ } finally {
+ lock.unlockRead(read_lock_stamp);
+ }
- idToString.add(token);
- stringToId.put(token, id);
- return id;
+ // Looks like the id we want is not there, let's get a write lock and add it
+ long write_lock_stamp = lock.writeLock();
+ try {
+ if (stringToId.containsKey(token)) {
+ return stringToId.get(token);
}
+ int id = idToString.size() * (nt(token) ? -1 : 1);
+
+ // register this (token,id) mapping with each language
+ // model, so that they can map it to their own private
+ // vocabularies
+ for (NGramLanguageModel lm : LMs)
+ lm.registerWord(token, Math.abs(id));
+
+ idToString.add(token);
+ stringToId.put(token, id);
+ return id;
+ } finally {
+ lock.unlockWrite(write_lock_stamp);
}
}
public static boolean hasId(int id) {
- synchronized (lock) {
+ long lock_stamp = lock.readLock();
+ try {
id = Math.abs(id);
return (id < idToString.size());
}
+ finally{
+ lock.unlockRead(lock_stamp);
+ }
}
public static int[] addAll(String sentence) {
@@ -170,10 +190,14 @@ public class Vocabulary {
}
public static String word(int id) {
- synchronized (lock) {
+ long lock_stamp = lock.readLock();
+ try {
id = Math.abs(id);
return idToString.get(id);
}
+ finally{
+ lock.unlockRead(lock_stamp);
+ }
}
public static String getWords(int[] ids) {
@@ -190,19 +214,15 @@ public class Vocabulary {
sb.append(word(id)).append(" ");
return sb.deleteCharAt(sb.length() - 1).toString();
}
-
+
/**
* This method returns a list of all (positive) indices
* corresponding to Nonterminals in the Vocabulary.
*/
- public static List<Integer> getNonterminalIndices()
+ public static synchronized List<Integer> getNonterminalIndices()
{
if (nonTerminalIndices == null) {
- synchronized (lock) {
- if (nonTerminalIndices == null) {
- nonTerminalIndices = findNonTerminalIndices();
- }
- }
+ nonTerminalIndices = findNonTerminalIndices();
}
return nonTerminalIndices;
}
@@ -211,7 +231,7 @@ public class Vocabulary {
* Iterates over the Vocabulary and finds all non terminal indices.
*/
private static List<Integer> findNonTerminalIndices() {
- List<Integer> nonTerminalIndices = new ArrayList<Integer>();
+ List<Integer> nonTerminalIndices = new ArrayList<>();
for(int i = 0; i < idToString.size(); i++) {
final String word = idToString.get(i);
if(isNonterminal(word)){
@@ -230,8 +250,8 @@ public class Vocabulary {
}
/**
- * Returns true if the Vocabulary ID represents a nonterminal.
- *
+ * Returns true if the Vocabulary ID represents a nonterminal.
+ *
* @param id
* @return
*/
@@ -244,33 +264,37 @@ public class Vocabulary {
}
public static int size() {
- synchronized (lock) {
+ long lock_stamp = lock.readLock();
+ try {
return idToString.size();
+ } finally {
+ lock.unlockRead(lock_stamp);
}
}
- public static int getTargetNonterminalIndex(int id) {
+ public static synchronized int getTargetNonterminalIndex(int id) {
return FormatUtils.getNonterminalIndex(word(id));
}
/**
- * Clears the vocabulary and initializes it with an unknown word.
- * Registered language models are left unchanged.
+ * Clears the vocabulary and initializes it with an unknown word. Registered
+ * language models are left unchanged.
*/
public static void clear() {
- synchronized (lock) {
- nonTerminalIndices = null;
-
+ long lock_stamp = lock.writeLock();
+ try {
idToString = new ArrayList<String>();
- stringToId = new HashMap<String, Integer>();
-
+ stringToId = new HashMap<String, Integer>();
+
idToString.add(UNKNOWN_ID, UNKNOWN_WORD);
stringToId.put(UNKNOWN_WORD, UNKNOWN_ID);
+ } finally {
+ lock.unlockWrite(lock_stamp);
}
}
-
+
public static void unregisterLanguageModels() {
LMs.clear();
}
-
+
}