0

我的系统上有一个 GPU:

在此处输入图像描述

使用 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被触发:

在此处输入图像描述

使用 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;
    }
}

在此处输入图像描述

什么不见​​了?

4

0 回答 0