2

I've ran the "Finding prime numbers by executing code on GPU" from the following link https://www.logicbig.com/tutorials/misc/gpu-programming/aparapi/intro-with-example.html, but I fixed it, so it would work + changed to run for 500,000.

When I run this many times, the results are different. primeNumbers[499901], sometimes it is false, and sometimes it is true.

Code is:

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();
  }
}

compilation is: 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

and execution is: 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

Any ideas why it is inconsistent?

Results:

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               <---------------  ???????????

Further info: running on Windows 10, my GPU is AMD Radeon Vega 8 Graphics. I also tried to only proceed when the primeNumbers flag was set to true, and can confirm that this didn't work (sometimes never set to true). I suspect some instructions not executed on GPU.

the generated openCL is (using -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;
   }
}
Mark Amabile
  • 152
  • 6
  • 1
    I'm not familiar with aparapi, but is the computation finished when `kernel.execute(range)` returns, or do you need to wait? – tgdavies Oct 08 '21 at 04:50
  • 1
    I would expect it should block until the kernel.execute finished. this is my understanding. – Mark Amabile Oct 08 '21 at 04:54
  • Mark. I replied with some suggestions on the aparapi gitter site. For context here I can't reproduce the inconsistency. I always see 499903 as prime (as it should be). I have an older AMD card. I also took the Aparapi generated OpenCL and executed from C. Same result. Also saw the correct use using POCL and Intel's CPU based OpenCL implementations. You can use -Dcom.aparapi.enableShowGeneratedOpenCL=true To generate OpenCL I suspect this is an issue pulling the array of bools back from the device. I can show you how to hack Aparapi JNI to diagnose if you''d like – gfrost Oct 18 '21 at 18:03
  • Oh and yes Aparapi blocks until execution and all data transfers from the device are complete. So your assumption is correct. – gfrost Oct 18 '21 at 18:04
  • I am happy to hack Aparapi JNI to diagnose.. I am happy to help anyway I can. – Mark Amabile Oct 19 '21 at 03:27

0 Answers0