Question

I'm using Aparapi to do number crunching inside my Java program on the GPU. From what I understand, Aparapi plays nice with float arrays.

I want to compute Pi to the Nth decimal using Aparapi. I'm thinking of using the Leibniz method, but I'm not sure how I would deal with representing and storing the long decimals in float or integer form.

Would an array of integers work, with the size of the array being the N number of decimals wanted?

int[] digits = new int[N];

If I were to use this with the Leibniz method, I would need to calculate an array of N integers for M terms I find (Liebniz says pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 ....), and then add those together and multiply the resulting number by 4. But this would mean I'd need to allocate M many integers for EACH term I computed, which would add up and really take a toll on memory.

tl;dr: How can I calculate Pi using loops of float operations so I can do it with Aparapi?

Thanks a bunch!

Was it helpful?

Solution

You need a reduction to partially add the m elements in a @Local annotated array(used for local memory in aparapi). Lets say there are N total elements so you need N/m partial sums that needs faster bandwidth(so in local memory).

Also you should use localBarrier(); to synch the cores in compute units or work units in local work groups. Then send the data to main memory.

Very short example:

You need 1+2+3+4+5+6+..... and you have 3 cores per compute unit of gpu.

1+2 is done in core number 1

3+4 is done in core number 2

5+6 is done in core number 3

then you add cores' outputs in local memory are which is shared by all neighbour cores of that compute unit.

At last you get 3,7,11 in an array which are summed to 21 in local memory.

Up all these datas of all compute units to main memory such as 21,57,.... then you can add them all simply in cpu .

Of course there will be (+/-)(1/(2*n+1)) instead of 1,2,3,4,5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top