RoshanGerard / aparapi

Automatically exported from code.google.com/p/aparapi
Other
0 stars 0 forks source link

Invoking Math.log(float) #8

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The Java Math library only has Math.log(double), which means that floats must 
handled with a cast to float.

For a simple kernel, this looks like:
public class LogKernel extends Kernel {
    private float[] data;
    private int offset;
    @Override public void run(){
       int i= getGlobalId();
       data[offset+i]=(float)Math.log(data[offset+i]);
    }
}

This works fine if the hardware supports double precision, otherwise it fails. 
Is there any way to rewrite the kernel to instruct Aparapi to use the single 
precision version of log when executed on a GPU ?

Original issue reported on code.google.com by kenneth@hexad.dk on 25 Oct 2011 at 9:19

GoogleCodeExporter commented 9 years ago
Thanks for finding this. 

Maybe we can add a distinct wrapper for float/double log (flog?, dlog?)

We already do this for other Math wrappers.

A bit embarassed we missed this.  I am out of office for a week or so, but will 
look at this again next week.

Original comment by frost.g...@gmail.com on 26 Oct 2011 at 1:48

GoogleCodeExporter commented 9 years ago
I was just about to work on this when I read the code again. 

Kernel code should not call Math.log(float || double) it should use the 
Kernel.log(float||double) method.  This method will then delegate to the 
correct Math.log(float||double) or OpenCL log(float||double).

So your example should use:
public class LogKernel extends Kernel {
    private float[] data;
    private int offset;
    @Override public void run(){
       int i= getGlobalId();
       data[offset+i]=log(data[offset+i]);
    }
}

And should not require double support.  

Original comment by frost.g...@gmail.com on 3 Nov 2011 at 2:35

GoogleCodeExporter commented 9 years ago
For the moment I am re-assigning this to WontFix. Until I hear back from the 
bug reporter. 

Original comment by frost.g...@gmail.com on 3 Nov 2011 at 2:37

GoogleCodeExporter commented 9 years ago
Ah, I had not found that function. (Actually I did, but I assumed it had 
something to do with logging).

I think that solution is fine, as it would be a impossible to determine if the 
compiler had auto-converted to float or the user wanted to use a double.

Original comment by kenneth@hexad.dk on 7 Nov 2011 at 11:44