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