Question

i use aparapi for parallelize and i wante to convert this java code:

public static void main(String[] args) {
        float res = 0;
        for (int i = 2; i < 5; i++) {
            for (int j = 3; j < 5; j++) {
                res += i * j;
            }
        }

        System.out.println(res);

    }

to its equivalent in aparapi:

Kernel kernel = new Kernel() {
    @Override
    public void run() {
        int i = getGlobalId();
        ...
    }
};
kernel.execute();
kernel.dispose();
Was it helpful?

Solution

There are a few issues here.

First your code is not data parallel. You have a 'race' condition on 'res' so this code cannot be computed on the GPU.

Secondly the range of execution is way too small. You are trying to execute 6 threads (x [2,3,4] * y [ 3,4]). This will not really gain any benefit from the GPU.

To answer the question regarding how you might implement over the 2 dim grid above.

Range range = Range.create2D(3, 2) ; // A two dimension grid 3x2
Kernel kernel = new Kernel() {
    @Override
    public void run() {
        int x = getGlobalId(0)+2; // x starts at 2 
        int y = getGlobalId(1)+3; // y starts at 3
        ...
    }
};
kernel.execute(range);
kernel.dispose();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top