Apache Lucene open-source search software
Parallel processing (#132)
balmukundblrf1d54f7c3519
diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/Constants.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/Constants.javaindex 53a1a25fc0b..86534eaf99c 100644--- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/Constants.java+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/Constants.java@@ -26,4 +26,6 @@ public class Constants {public static Boolean[] BOOLEANS = new Boolean[] {Boolean.FALSE, Boolean.TRUE};public static final int DEFAULT_MAXIMUM_DOCUMENTS = Integer.MAX_VALUE;++ public static final String PARALLEL_TASK_THREAD_NAME_PREFIX = "ParallelTaskThread";}diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/ReutersContentSource.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/ReutersContentSource.javaindex 76c229ff908..8e040b7f134 100644--- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/ReutersContentSource.java+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/ReutersContentSource.java@@ -28,6 +28,7 @@ import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Locale;+import org.apache.lucene.benchmark.Constants;import org.apache.lucene.benchmark.byTask.utils.Config;/**@@ -50,8 +51,8 @@ public class ReutersContentSource extends ContentSource {private ThreadLocal<DateFormatInfo> dateFormat = new ThreadLocal<>();private Path dataDir = null;private ArrayList<Path> inputFiles = new ArrayList<>();- private int nextFile = 0;- private int iteration = 0;+ private int[] docCountArr;+ private volatile boolean docCountArrCreated;@Overridepublic void setConfig(Config config) {@@ -100,21 +101,35 @@ public class ReutersContentSource extends ContentSource {@Overridepublic DocData getNextDocData(DocData docData) throws NoMoreDataException, IOException {- Path f = null;- String name = null;- synchronized (this) {- if (nextFile >= inputFiles.size()) {- // exhausted files, start a new round, unless forever set to false.- if (!forever) {- throw new NoMoreDataException();- }- nextFile = 0;- iteration++;- }- f = inputFiles.get(nextFile++);- name = f.toRealPath() + "_" + iteration;+ if (docCountArrCreated == false) {+ docCountArrInit();}+ int threadIndexSize = Thread.currentThread().getName().length();+ int parallelTaskThreadSize = Constants.PARALLEL_TASK_THREAD_NAME_PREFIX.length();++ // Extract ThreadIndex from unique ThreadName which is set with '"ParallelTaskThread-"+index',+ // in TaskSequence.java's doParallelTasks()+ int threadIndex =+ Integer.parseInt(+ Thread.currentThread()+ .getName()+ .substring(parallelTaskThreadSize + 1, threadIndexSize));++ assert (threadIndex >= 0 && threadIndex < docCountArr.length)+ : "Please check threadIndex or docCountArr length";+ int stride = threadIndex + docCountArr[threadIndex] * docCountArr.length;+ int inFileSize = inputFiles.size();++ // Modulo Operator covers all three possible senarios i.e. 1. If inputFiles.size() < Num Of+ // Threads 2.inputFiles.size() == Num Of Threads 3.inputFiles.size() > Num Of Threads+ int fileIndex = stride % inFileSize;+ int iteration = stride / inFileSize;+ docCountArr[threadIndex]++;++ Path f = inputFiles.get(fileIndex);+ String name = f.toRealPath() + "_" + iteration;+try (BufferedReader reader = Files.newBufferedReader(f, StandardCharsets.UTF_8)) {// First line is the date, 3rd is the title, rest is bodyString dateStr = reader.readLine();@@ -143,7 +158,12 @@ public class ReutersContentSource extends ContentSource {@Overridepublic synchronized void resetInputs() throws IOException {super.resetInputs();- nextFile = 0;- iteration = 0;+ }++ private synchronized void docCountArrInit() {+ if (docCountArrCreated == false) {+ docCountArr = new int[getConfig().getNumThreads()];+ docCountArrCreated = true;+ }}}diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.javaindex f74aaab11f0..bb049d490ff 100644--- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.java+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.java@@ -20,6 +20,7 @@ import java.text.NumberFormat;import java.util.ArrayList;import java.util.List;import java.util.Locale;+import org.apache.lucene.benchmark.Constants;import org.apache.lucene.benchmark.byTask.PerfRunData;import org.apache.lucene.benchmark.byTask.feeds.NoMoreDataException;import org.apache.lucene.benchmark.byTask.stats.TaskStats;@@ -340,12 +341,23 @@ public class TaskSequence extends PerfTask {initTasksArray();ParallelTask t[] = runningParallelTasks = new ParallelTask[repetitions * tasks.size()];+ // Get number of parallel threads from algo file and set it to use in ReuersContentSource.java's+ // docCountArrInit()+ this.getRunData().getConfig().setNumThreads(t.length);// prepare threadsint index = 0;for (int k = 0; k < repetitions; k++) {for (int i = 0; i < tasksArray.length; i++) {final PerfTask task = tasksArray[i].clone();- t[index++] = new ParallelTask(task);+ t[index] = new ParallelTask(task);+ // Setting unique ThreadName with index value which is used in ReuersContentSource.java's+ // getNextDocData().Please make changes+ // in ReuersContentSource.java's getNextDocData() for+ // Integer.parseInt(Thread.currentThread().getName().substring(parallelTaskThreadSize + 1,+ // threadIndexSize))+ // before making any modifications here+ t[index].setName(Constants.PARALLEL_TASK_THREAD_NAME_PREFIX + "-" + index);+ index++;}}// run threadsdiff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Config.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Config.javaindex 74709156c9a..5eafb553fcf 100644--- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Config.java+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Config.java@@ -54,6 +54,7 @@ public class Config {private HashMap<String, Object> valByRound = new HashMap<>();private HashMap<String, String> colForValByRound = new HashMap<>();private String algorithmText;+ private int numThreads = 1;/*** Read both algorithm and config properties.@@ -113,6 +114,14 @@ public class Config {}}+ public void setNumThreads(int numThreads) {+ this.numThreads = numThreads;+ }++ public int getNumThreads() {+ return numThreads;+ }+@SuppressWarnings({"unchecked", "rawtypes"})private void printProps() {System.out.println("------------> config properties:");