如何在GPU上运行我的java程序?如何更改我的程序可以加速?

我编写了一个由几个类组成的程序,但计算速度太慢(程序用粗体表示),我希望让我的java程序在GPU上运行以加速计算,或者是否有其他方法可以加快运行速度,如何更改我的代码?程序的计算如下:

public class ComputeThreadPool { public static double[][] distance = new double[40][8]; public static HashMap simMap = new HashMap(); static class WorkThread implements Runnable { private Map testWordTFMap; private Map trainWordTFMap; private Map words; private String trainname; public WorkThread(Map map1, Map map2, Map words,String trainname) { this.testWordTFMap = map1; this.trainWordTFMap = map2; this.words = words; this.trainname=trainname; } @Override public void run() { System.out.println(Thread.currentThread().getName()+" Start. Command = "+command); double mul = 0, testAbs = 0, trainAbs = 0; WordsSimilarity computeS = new WordsSimilarity(); double wf = 0; Set<Map.Entry> testWordTFMapSet = testWordTFMap.entrySet(); for (Iterator<Map.Entry> it = testWordTFMapSet.iterator(); it.hasNext(); ) { Map.Entry me = it.next(); Set<Map.Entry> trainWordTFMapSet = trainWordTFMap.entrySet(); ***for (Iterator<Map.Entry> it2 = trainWordTFMapSet.iterator(); it2.hasNext(); ) { Map.Entry me2 = it2.next(); wf = computeS.similarity(me.getKey(), me2.getKey(), words); if (wf > 0.45) mul += wf * me.getValue() * me2.getValue(); } }*** for (Iterator<Map.Entry> it3 = testWordTFMapSet.iterator(); it3.hasNext(); ) { Map.Entry me3 = it3.next(); testAbs += me3.getValue() * me3.getValue(); } testAbs = Math.sqrt(testAbs); Set<Map.Entry> trainWordTFMapSet = trainWordTFMap.entrySet(); for (Iterator<Map.Entry> it4 = trainWordTFMapSet.iterator(); it4.hasNext(); ) { Map.Entry me4 = it4.next(); trainAbs += me4.getValue() * me4.getValue(); } trainAbs = Math.sqrt(trainAbs); simMap.put(trainname,mul / (testAbs * trainAbs)); System.out.println(Thread.currentThread().getName() + " Start. " ); processCommand(); System.out.println(Thread.currentThread().getName() + " End."); } private void processCommand() { try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } } public static HashMap main(Map testWordTFMap,Map<String, TreeMap> trainFileNameWordTFMap,Map words) { int num=0; ExecutorService executor = Executors.newFixedThreadPool(6); Set<Map.Entry<String,TreeMap>> trainFileNameWordTFMapSet = trainFileNameWordTFMap.entrySet(); for(Iterator<Map.Entry<String,TreeMap>> it = trainFileNameWordTFMapSet.iterator(); it.hasNext();){ Map.Entry<String, TreeMap> me = it.next(); num=num++; Runnable worker = new WorkThread(testWordTFMap,me.getValue(),words,me.getKey()); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println("Finished all threads"); return simMap; } 

}

wf的计算方法如下:

  public static double similarity(String word1, String word2,Map words) { double[] count1=words.get(word1); double[] count2=words.get(word2); double sum=0; double Abs1=0; double Abs2=0; if(count1 == null || count2 == null) { return 0; } for (int c = 0; c < count1.length; c++) { sum += count1[c] * count2[c]; Abs1 += count1[c] * count1[c]; Abs2 += count2[c] * count2[c]; } return sum / (Abs1 * Abs2); } 

您需要找到在GPU上运行的JVM的实现,或者以GPU为目标的运行时环境/ shell,您可以在其中运行标准JVM; 但除非JVM是为GPU构建的,否则您可能会或可能不会获得性能提升。

但是我想说,你应该能够首先在代码中找到优化。 比如使用增强的for循环。 除了计算单词相似性之外,似乎没有太多应该导致过多的运行时间。