0

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

1 Answers1

2

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();
gfrost
  • 948
  • 8
  • 5
  • Thank you very much gfrost, this is just an exmple to understand how it works, but can you explain easily why this code is not data parallel ?? thank you – user2184177 Mar 21 '13 at 14:48
  • Because your original code requires each inner loop to update the value of 'res'. So if all the loop bodies were implemented in parallel, they would all 'race' to add their value to 'res', res would not contain the result you wanted. You can put res in an array and use Aparapi's atomicInc(arr, index, value) to increment it, but this will slow down your execution. – gfrost Mar 21 '13 at 18:57