1

我已经从以下链接https://www.logicbig.com/tutorials/misc/gpu-programming/aparapi/intro-with-example.html运行了“通过在 GPU 上执行代码来查找素数” ,但我已修复它,所以它可以工作 + 改为运行 500,000。

当我多次运行时,结果是不同的。primeNumbers[499901],有时是假的,有时是真的。

代码是:

import com.aparapi.Kernel;
import java.util.Arrays;
import java.util.stream.IntStream;
import com.aparapi.Range;

public class GpuExample {
  public static void main(String[] args) {
      final int size = 500000;
      final int[] a = IntStream.range(2, size + 2).toArray();
      final boolean[] primeNumbers = new boolean[size];

      Kernel kernel = new Kernel() {
          @Override
          public void run() {
              int gid = getGlobalId();
              int num = a[gid];
              boolean prime = true;
              for (int i = 2; i < num; i++) {
                  if (num % i == 0) {
                      prime = false;
                      //break is not supported
                  }
              }
              primeNumbers[gid] = prime;
          }
      };
      long startTime = System.currentTimeMillis();
      Range range = Range.create(size);
      kernel.execute(range);

      System.out.printf("time taken: %s ms%n", System.currentTimeMillis() - startTime);
      System.out.println("a[499901]="+a[499901]+" should be a prime number!");
      System.out.println("result primeNumbers[499901]="+primeNumbers[499901]);
      kernel.dispose();
  }
}

编译是: javac -g -classpath aparapi-3.0.0.jar;aparapi-jni-1.4.3.jar;bcel-6.5.0.jar;scala-library-2.13.6.jar;*; GpuExample.java

执行是: java -classpath aparapi-3.0.0.jar;aparapi-jni-1.4.3.jar;bcel-6.5.0.jar;scala-library-2.13.6.jar;*; GpuExample

任何想法为什么它不一致?

结果:

java -classpath aparapi-3.0.0.jar;aparapi-jni-1.4.3.jar;bcel-6.5.0.jar;scala-library-2.13.6.jar;*; GpuExample
Oct. 08, 2021 3:35:38 PM com.aparapi.internal.model.ClassModel$AttributePool <init>
WARNING: Found unexpected Attribute (name = NestHost)
time taken: 14461 ms
a[499901]=499903 should be a prime number!
result primeNumbers[499901]=true

java -classpath aparapi-3.0.0.jar;aparapi-jni-1.4.3.jar;bcel-6.5.0.jar;scala-library-2.13.6.jar;*; GpuExample
Oct. 08, 2021 3:35:54 PM com.aparapi.internal.model.ClassModel$AttributePool <init>
WARNING: Found unexpected Attribute (name = NestHost)
time taken: 13675 ms
a[499901]=499903 should be a prime number!
result primeNumbers[499901]=false               <---------------  ???????????

更多信息:在 Windows 10 上运行,我的 GPU 是 AMD Radeon Vega 8 Graphics。我还尝试仅在 primeNumbers 标志设置为 true 时继续,并且可以确认这不起作用(有时从未设置为 true)。我怀疑有些指令没有在 GPU 上执行。

生成的openCL是(使用-Dcom.aparapi.enableShowGeneratedOpenCL=true)

typedef struct This_s{
   __global int *val$a;
   __global char  *val$primeNumbers;
   int passid;
}This;
int get_pass_id(This *this){
   return this->passid;
}
__kernel void run(
   __global int *val$a, 
   __global char  *val$primeNumbers, 
   int passid
){
   This thisStruct;
   This* this=&thisStruct;
   this->val$a = val$a;
   this->val$primeNumbers = val$primeNumbers;
   this->passid = passid;
   {
      int gid = get_global_id(0);
      int num = this->val$a[gid];
      char prime = 1;
      for (int i = 2; i<num; i++){
         if ((num % i)==0){
            prime = 0;
         }
      }
      this->val$primeNumbers[gid]  = prime;
      return;
   }
}
4

0 回答 0