Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让java程序在GPU上运行;我如何改变我的计划可以加速?;_Java_Gpu - Fatal编程技术网

如何让java程序在GPU上运行;我如何改变我的计划可以加速?;

如何让java程序在GPU上运行;我如何改变我的计划可以加速?;,java,gpu,Java,Gpu,我写了一个由几个类组成的程序,但是计算速度太慢(粗体),我希望让我的java程序在GPU上运行以加快计算速度,或者有其他方法来加快运行速度,我如何更改代码 程序计算如下: public class ComputeThreadPool { public static double[][] distance = new double[40][8]; public static HashMap<String,Double> simMap = new HashMap<Strin

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

public class ComputeThreadPool {
  public static double[][] distance = new double[40][8];
  public static HashMap<String,Double> simMap = new HashMap<String,Double>();
static class WorkThread implements Runnable {
    private Map<String, Double> testWordTFMap;
    private Map<String, Double> trainWordTFMap;
    private Map<String, double[]> words;
    private String trainname;

    public WorkThread(Map<String, Double> map1, Map<String, Double> map2, Map<String, double[]> 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<String, Double>> testWordTFMapSet = testWordTFMap.entrySet();
        for (Iterator<Map.Entry<String, Double>> it = testWordTFMapSet.iterator(); it.hasNext(); ) {
            Map.Entry<String, Double> me = it.next();
            Set<Map.Entry<String, Double>> trainWordTFMapSet = trainWordTFMap.entrySet();
            ***for (Iterator<Map.Entry<String, Double>> it2 = trainWordTFMapSet.iterator(); it2.hasNext(); ) {
                Map.Entry<String, Double> 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<String, Double>> it3 = testWordTFMapSet.iterator(); it3.hasNext(); ) {
            Map.Entry<String, Double> me3 = it3.next();
            testAbs += me3.getValue() * me3.getValue();
        }
        testAbs = Math.sqrt(testAbs);
        Set<Map.Entry<String, Double>> trainWordTFMapSet = trainWordTFMap.entrySet();
        for (Iterator<Map.Entry<String, Double>> it4 = trainWordTFMapSet.iterator(); it4.hasNext(); ) {
            Map.Entry<String, Double> 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<String,Double> main(Map<String, Double> testWordTFMap,Map<String, TreeMap<String, Double>> trainFileNameWordTFMap,Map<String, double[]> words) {
    int num=0;
    ExecutorService executor = Executors.newFixedThreadPool(6);
    Set<Map.Entry<String,TreeMap<String,Double>>> trainFileNameWordTFMapSet = trainFileNameWordTFMap.entrySet();
    for(Iterator<Map.Entry<String,TreeMap<String,Double>>> it = trainFileNameWordTFMapSet.iterator(); it.hasNext();){
        Map.Entry<String, TreeMap<String,Double>> 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;
}
    public static double similarity(String word1, String word2,Map<String, double[]> 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);

}                                                                                           
公共类ComputeThreadPool{
公共静态双精度[][]距离=新双精度[40][8];
public static HashMap simMap=new HashMap();
静态类WorkThread实现可运行{
私有映射testWordTFMap;
私有地图;
私有地图词;
私有字符串trainname;
公共工作线程(映射映射1、映射2、映射字、字符串trainname){
this.testWordTFMap=map1;
this.trainWordTFMap=map2;
这个单词=单词;
this.trainname=trainname;
}
@凌驾
公开募捐{
System.out.println(Thread.currentThread().getName()+“Start.Command=“+Command”);
双mul=0,testAbs=0,trainAbs=0;
WordsSimilarity computeS=新的WordsSimilarity();
双wf=0;
Set testWordTFMapSet=testWordTFMap.entrySet();
for(Iterator it=testWordTFMapSet.Iterator();it.hasNext();){
Map.Entry me=it.next();
Set trainWordTFMapSet=trainWordTFMap.entrySet();
***for(迭代器it2=trainWordTFMapSet.Iterator();it2.hasNext();){
Map.Entry me2=it2.next();
wf=computeS.similarity(me.getKey(),me2.getKey(),words);
如果(wf>0.45)
mul+=wf*me.getValue()*me2.getValue();
}
}***
for(迭代器it3=testWordTFMapSet.Iterator();it3.hasNext();){
Map.Entry me3=it3.next();
testAbs+=me3.getValue()*me3.getValue();
}
testAbs=Math.sqrt(testAbs);
Set trainWordTFMapSet=trainWordTFMap.entrySet();
for(迭代器it4=trainWordTFMapSet.Iterator();it4.hasNext();){
Map.Entry me4=it4.next();
列车ABS+=me4.getValue()*me4.getValue();
}
列车ABS=数学sqrt(列车ABS);
simMap.put(列车名称,mul/(列车试验*列车试验));
System.out.println(Thread.currentThread().getName()+“Start”);
processCommand();
System.out.println(Thread.currentThread().getName()+“End”);
}
私有void processCommand(){
试一试{
睡眠(5);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
公共静态HashMap main(映射testWordTFMap、映射trainFileNameWordTFMap、映射字){
int num=0;
ExecutorService executor=Executors.newFixedThreadPool(6);
Set-trainFileNameWordTFMapSet=trainFileNameWordTFMap.entrySet();
for(Iterator it=trainFileNameWordTFMapSet.Iterator();it.hasNext();){
Map.Entry me=it.next();
num=num++;
Runnable worker=new WorkThread(testWordTFMap,me.getValue(),words,me.getKey());
执行人,执行人(工人);
}
executor.shutdown();
而(!executor.isTerminated()){
}
System.out.println(“完成所有线程”);
返回simMap;
}
}

wf的计算如下:

public class ComputeThreadPool {
  public static double[][] distance = new double[40][8];
  public static HashMap<String,Double> simMap = new HashMap<String,Double>();
static class WorkThread implements Runnable {
    private Map<String, Double> testWordTFMap;
    private Map<String, Double> trainWordTFMap;
    private Map<String, double[]> words;
    private String trainname;

    public WorkThread(Map<String, Double> map1, Map<String, Double> map2, Map<String, double[]> 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<String, Double>> testWordTFMapSet = testWordTFMap.entrySet();
        for (Iterator<Map.Entry<String, Double>> it = testWordTFMapSet.iterator(); it.hasNext(); ) {
            Map.Entry<String, Double> me = it.next();
            Set<Map.Entry<String, Double>> trainWordTFMapSet = trainWordTFMap.entrySet();
            ***for (Iterator<Map.Entry<String, Double>> it2 = trainWordTFMapSet.iterator(); it2.hasNext(); ) {
                Map.Entry<String, Double> 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<String, Double>> it3 = testWordTFMapSet.iterator(); it3.hasNext(); ) {
            Map.Entry<String, Double> me3 = it3.next();
            testAbs += me3.getValue() * me3.getValue();
        }
        testAbs = Math.sqrt(testAbs);
        Set<Map.Entry<String, Double>> trainWordTFMapSet = trainWordTFMap.entrySet();
        for (Iterator<Map.Entry<String, Double>> it4 = trainWordTFMapSet.iterator(); it4.hasNext(); ) {
            Map.Entry<String, Double> 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<String,Double> main(Map<String, Double> testWordTFMap,Map<String, TreeMap<String, Double>> trainFileNameWordTFMap,Map<String, double[]> words) {
    int num=0;
    ExecutorService executor = Executors.newFixedThreadPool(6);
    Set<Map.Entry<String,TreeMap<String,Double>>> trainFileNameWordTFMapSet = trainFileNameWordTFMap.entrySet();
    for(Iterator<Map.Entry<String,TreeMap<String,Double>>> it = trainFileNameWordTFMapSet.iterator(); it.hasNext();){
        Map.Entry<String, TreeMap<String,Double>> 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;
}
    public static double similarity(String word1, String word2,Map<String, double[]> 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);

}                                                                                           
公共静态双重相似性(字符串word1、字符串word2、映射词){
double[]count1=words.get(word1);
double[]count2=words.get(word2);
双和=0;
双Abs1=0;
双Abs2=0;
如果(count1==null | | count2==null){
返回0;
}
for(int c=0;c
您需要找到一个在GPU上运行的JVM实现,或者一个针对GPU的运行时环境/shell,您可以在其中运行标准JVM;但是,除非JVM是为GPU构建的,否则您可能会或不会获得性能提升


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

什么是“太慢”?您是否进行过分析以确定哪些方法花费的时间最多?如果没有分析数据,我们无法告诉您要优化什么。GPU不理解Java字节码。更好的问题是如何从Java程序调用GPU上的逻辑。您不应该注意到GPU的处理能力来自于大型向量操作。即使Java可以在GPU上运行,Map之类的数据结构也不受支持。我想,你能做的就是通过JavaCL库@PeterLawrey运行OpenCL。Gimby链接到的答案中列出了一些方法。然而,对于给定的代码片段,有几十种方法可以提高性能,尽管这里缺少(可能)最重要的类
WordsSimilarity