Aparapi:不调用GPU

时间:2018-11-02 11:48:21

标签: java gpu cpu aparapi

我的系统上有一个GPU:

enter image description here

使用CPU运行时:

package com.example.gpuexample;

import java.util.concurrent.ThreadLocalRandom;

import com.aparapi.Kernel;
import com.aparapi.Range;

public class Main {

    public static void main(String[] args) {
        final float inA[] = getFloatArray(100000000); // get a float array of data from somewhere
        final float inB[] = getFloatArray(100000000); // get a float array of data from somewhere
        assert (inA.length == inB.length);

        for(int i = 0; i < 30; i++) {
            System.out.println(i + "--");
            arrayTestCpu(inA, inB);
        }
    }

    public static void arrayTestCpu(final float inA[], final float inB[]) {

        final float[] result = new float[inA.length];

        for (int i = 0; i < result.length; i++) {
            result[i] = inA[i] * inB[i] * inA[i] * inB[i] * inA[i] * inB[i];
        }
    }

    public static float[] getFloatArray(int len) {
        float[] arr = new float[len];

        for (int i = 0; i < len; i++) {
            arr[i] = ThreadLocalRandom.current().nextFloat();
        }

        return arr;
    }
}

CPU被触发:

enter image description here

使用GPU运行时,看不到GPU被触发:

package com.example.gpuexample;

import java.util.concurrent.ThreadLocalRandom;

import com.aparapi.Kernel;
import com.aparapi.Range;

public class Main {

    public static void main(String[] args) {
        final float inA[] = getFloatArray(100000000); // get a float array of data from somewhere
        final float inB[] = getFloatArray(100000000); // get a float array of data from somewhere
        assert (inA.length == inB.length);

        for(int i = 0; i < 30; i++) {
            System.out.println(i + "--");
            arrayTestGpu(inA, inB);
        }
    }

    public static void arrayTestGpu(final float inA[], final float inB[]) {
        final float[] result = new float[inA.length];

        Kernel kernel = new Kernel() {
            @Override
            public void run() {
                int i = getGlobalId();
                result[i] = inA[i] * inB[i] * inA[i] * inB[i] * inA[i] * inB[i];
            }
        };

        Range range = Range.create(result.length);
        kernel.execute(range);

        kernel.dispose();
    }

    public static float[] getFloatArray(int len) {
        float[] arr = new float[len];

        for (int i = 0; i < len; i++) {
            arr[i] = ThreadLocalRandom.current().nextFloat();
        }

        return arr;
    }
}

enter image description here

缺少什么?

0 个答案:

没有答案