devito icon indicating copy to clipboard operation
devito copied to clipboard

Absolute value of an integer produces C warning

Open navjotk opened this issue 1 year ago • 4 comments

Here is an MFE:

import numpy as np
from sympy import sign
from devito import Function, Grid, Eq, Operator

gridR = Grid(shape=(2,))

index = Function(name="index", grid=gridR, dtype=np.int32)
R = Function(name="R", grid=gridR)
a = gridR.dimensions

eqs = [Eq(index[a], abs(sign(R[a])))]


op = Operator(eqs)
op.apply()

produces the following warnings:

...snipped.c:36:25: warning: using floating point absolute value function 'fabs' when argument is of integer type [-Wabsolute-value]
    index[x + 1] = fmin(fabs((((B[x + 1]) > 0) - ((B[x + 1]) < 0))), fabs((((R[x + 1]) > 0) - ((R[x + 1]) < 0))));
                        ^
...snipped.c:36:25: note: use function 'abs' instead
    index[x + 1] = fmin(fabs((((B[x + 1]) > 0) - ((B[x + 1]) < 0))), fabs((((R[x + 1]) > 0) - ((R[x + 1]) < 0))));
                        ^~~~
                        abs
...snipped.c:36:70: warning: using floating point absolute value function 'fabs' when argument is of integer type [-Wabsolute-value]
    index[x + 1] = fmin(fabs((((B[x + 1]) > 0) - ((B[x + 1]) < 0))), fabs((((R[x + 1]) > 0) - ((R[x + 1]) < 0))));
                                                                     ^
...snipped.c:36:70: note: use function 'abs' instead
    index[x + 1] = fmin(fabs((((B[x + 1]) > 0) - ((B[x + 1]) < 0))), fabs((((R[x + 1]) > 0) - ((R[x + 1]) < 0))));
                                                                     ^~~~
                                                                     abs
2 warnings generated.

I think the correct behaviour would be to generate abs instead of fabs here.

Happy to file a PR fixing it if people agree this is incorrect/unwanted behaviour.

Can @FabioLuporini or @mloubout or @georgebisbas point me to where to look in order to change the mapping?

navjotk avatar Jul 13 '22 15:07 navjotk

Well the tricky part is to infer the type of the input of abs which is not always trivial and some compiler do not allow abs(float) and will throw a warning for the other case

mloubout avatar Jul 13 '22 15:07 mloubout

Yeah we probably don't want to do abs(float) as that would just generate the reverse warning.

Can we infer types in some cases though? For example, in this case, I think it is the sign() function causing things to be int and not float? Is there some place we could hold this information in order to use it at code generation time?

navjotk avatar Jul 13 '22 15:07 navjotk

Would it be useful to create a custom FABS as we did with MIN/MAX bounds? https://github.com/devitocodes/devito/pull/1673/files

georgebisbas avatar Jul 13 '22 20:07 georgebisbas

Can we infer types in some cases though?

In some case yeah. I guess we could always do abs "if we can infer in" and still default to fabs if not

mloubout avatar Jul 13 '22 20:07 mloubout