我的系统上有一个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;
}
}
缺少什么?