From aaa77ff04d1e1ec4c9cddfa215936018e1301614 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 10 May 2018 16:22:39 -0400 Subject: [PATCH 1/3] include math.h when C-math functions are mangled to avoid annoying compiler warnings & test --- loopy/target/c/__init__.py | 60 +++++++++++++++++++++----------------- test/test_c_execution.py | 18 ++++++++++++ 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index 8e69793e8..0803aae1e 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -75,29 +75,6 @@ class DTypeRegistryWrapper(object): # }}} -# {{{ preamble generator - -def _preamble_generator(preamble_info): - c_funcs = set(func.c_name for func in preamble_info.seen_functions) - if "int_floor_div" in c_funcs: - yield ("05_int_floor_div", """ - #define int_floor_div(a,b) \ - (( (a) - \ - ( ( (a)<0 ) != ( (b)<0 )) \ - *( (b) + ( (b)<0 ) - ( (b)>=0 ) )) \ - / (b) ) - """) - - if "int_floor_div_pos_b" in c_funcs: - yield ("05_int_floor_div_pos_b", """ - #define int_floor_div_pos_b(a,b) ( \ - ( (a) - ( ((a)<0) ? ((b)-1) : 0 ) ) / (b) \ - ) - """) - -# }}} - - # {{{ cgen overrides from cgen import Declarator @@ -356,6 +333,13 @@ def c_symbol_mangler(kernel, name): # {{{ function mangler +c_math_unitary_functions = ["fabs", "acos", "asin", "atan", "cos", "cosh", "sin", + "sinh", "tanh", "exp", "log", "log10", "sqrt", "ceil", + "floor"] +c_math_binary_functions = ["fmax", "fmin"] +c_math_functions = set(c_math_unitary_functions + c_math_binary_functions) + + def c_math_mangler(target, name, arg_dtypes, modify_name=True): # Function mangler for math functions defined in C standard # Convert abs, min, max to fabs, fmin, fmax. @@ -369,8 +353,7 @@ def c_math_mangler(target, name, arg_dtypes, modify_name=True): name = "f" + name # unitary functions - if (name in ["fabs", "acos", "asin", "atan", "cos", "cosh", "sin", "sinh", - "tanh", "exp", "log", "log10", "sqrt", "ceil", "floor"] + if (name in c_math_unitary_functions and len(arg_dtypes) == 1 and arg_dtypes[0].numpy_dtype.kind == "f"): @@ -392,7 +375,7 @@ def c_math_mangler(target, name, arg_dtypes, modify_name=True): arg_dtypes=arg_dtypes) # binary functions - if (name in ["fmax", "fmin"] + if (name in c_math_binary_functions and len(arg_dtypes) == 2): dtype = np.find_common_type( @@ -424,6 +407,31 @@ def c_math_mangler(target, name, arg_dtypes, modify_name=True): # }}} +# {{{ preamble generator + +def _preamble_generator(preamble_info): + c_funcs = set(func.c_name for func in preamble_info.seen_functions) + if "int_floor_div" in c_funcs: + yield ("05_int_floor_div", """ + #define int_floor_div(a,b) \ + (( (a) - \ + ( ( (a)<0 ) != ( (b)<0 )) \ + *( (b) + ( (b)<0 ) - ( (b)>=0 ) )) \ + / (b) ) + """) + + if "int_floor_div_pos_b" in c_funcs: + yield ("05_int_floor_div_pos_b", """ + #define int_floor_div_pos_b(a,b) ( \ + ( (a) - ( ((a)<0) ? ((b)-1) : 0 ) ) / (b) \ + ) + """) + if len(c_funcs & c_math_functions): + yield ('00_cmath', "#include ") + +# }}} + + class CASTBuilder(ASTBuilderBase): # {{{ library diff --git a/test/test_c_execution.py b/test/test_c_execution.py index c355893e4..5d2d6d28c 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -343,6 +343,24 @@ def test_missing_compilers(): __test(eval_tester, ExecutableCTarget, compiler=ccomp) +def test_include_c_math_header(): + from loopy.target.c import ExecutableCTarget + n = 10 + + knl = lp.make_kernel('{[i]: 0 <= i < n}', + """ + a[i] = fabs(b[i]) + """, + [lp.GlobalArg('a', shape=(n,), dtype=np.int32), + lp.GlobalArg('b', shape=(n,), dtype=np.int32),], + target=ExecutableCTarget()) + + knl = lp.fix_parameters(knl, n=n) + assert ('#include ') in lp.generate_code_v2(knl).device_code() + assert np.allclose(knl(a=np.zeros(10, dtype=np.int32), + b=-np.arange(10, dtype=np.int32))[1], np.arange(10)) + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) -- GitLab From 3ffb2a62d719d0927aa566a1a675002ab107bf7b Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 10 May 2018 16:39:18 -0400 Subject: [PATCH 2/3] only include math.h for C-targets that have it (currently, C and CUDA) --- loopy/target/c/__init__.py | 7 ++++++- loopy/target/ispc.py | 4 ++++ loopy/target/opencl.py | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index 0803aae1e..444d7338f 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -282,6 +282,10 @@ class CTarget(TargetBase): def get_kernel_executor(self, knl, *args, **kwargs): raise NotImplementedError() + @property + def has_math_header(self): + return True + # }}} @@ -426,7 +430,8 @@ def _preamble_generator(preamble_info): ( (a) - ( ((a)<0) ? ((b)-1) : 0 ) ) / (b) \ ) """) - if len(c_funcs & c_math_functions): + if len(c_funcs & c_math_functions) and ( + preamble_info.kernel.target.has_math_header): yield ('00_cmath', "#include ") # }}} diff --git a/loopy/target/ispc.py b/loopy/target/ispc.py index 8e07eb692..ee0ff30c0 100644 --- a/loopy/target/ispc.py +++ b/loopy/target/ispc.py @@ -197,6 +197,10 @@ class ISPCTarget(CTarget): include_bool=True) return result + @property + def has_math_header(self): + return False + # }}} diff --git a/loopy/target/opencl.py b/loopy/target/opencl.py index 31e0569b9..75327fca8 100644 --- a/loopy/target/opencl.py +++ b/loopy/target/opencl.py @@ -356,6 +356,10 @@ class OpenCLTarget(CTarget): vec.types[base.numpy_dtype, count], target=self) + @property + def has_math_header(self): + return False + # }}} # }}} -- GitLab From a9fcfcc675ca650ae847254efa62db0cee21c490 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 10 May 2018 17:12:30 -0400 Subject: [PATCH 3/3] flake fix --- test/test_c_execution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_c_execution.py b/test/test_c_execution.py index 5d2d6d28c..5ba91ec25 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -352,7 +352,7 @@ def test_include_c_math_header(): a[i] = fabs(b[i]) """, [lp.GlobalArg('a', shape=(n,), dtype=np.int32), - lp.GlobalArg('b', shape=(n,), dtype=np.int32),], + lp.GlobalArg('b', shape=(n,), dtype=np.int32)], target=ExecutableCTarget()) knl = lp.fix_parameters(knl, n=n) -- GitLab