From 3dbcf0024ec8195a5a0d150218a13fefdad796be Mon Sep 17 00:00:00 2001 From: Marmaduke Woodman Date: Sat, 10 Feb 2018 20:34:48 +0100 Subject: [PATCH 01/10] move ccomp out of kwarg default --- loopy/target/c/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index 177daa029..8e69793e8 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -314,11 +314,11 @@ class ExecutableCTarget(CTarget): """ An executable CTarget that uses (by default) JIT compilation of C-code """ - from loopy.target.c.c_execution import CCompiler - def __init__(self, compiler=CCompiler(), fortran_abi=False): + def __init__(self, compiler=None, fortran_abi=False): super(ExecutableCTarget, self).__init__(fortran_abi=fortran_abi) - self.compiler = compiler + from loopy.target.c.c_execution import CCompiler + self.compiler = compiler or CCompiler() def get_kernel_executor(self, knl, *args, **kwargs): from loopy.target.c.c_execution import CKernelExecutor -- GitLab From f0c5ab341b615f9891b0aabb4717f3d236b3c856 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 15:28:53 -0500 Subject: [PATCH 02/10] fix missing values on default CCompiler case --- loopy/target/c/c_execution.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/loopy/target/c/c_execution.py b/loopy/target/c/c_execution.py index 2c8ab16f2..ef4504077 100644 --- a/loopy/target/c/c_execution.py +++ b/loopy/target/c/c_execution.py @@ -227,9 +227,15 @@ class CCompiler(object): logger = logging.getLogger(__name__) logger.warn('Default toolchain guessed from python config ' 'not found, replacing with default GCCToolchain.') - self.toolchain = GCCToolchain() + self.toolchain = GCCToolchain( + cc=cc, + cflags=cflags, + ldflags=ldflags, + libraries=libraries, + library_dirs=library_dirs, + defines=defines, + source_suffix=source_suffix) - self.source_suffix = source_suffix if toolchain is None: # copy in all differing values diff = {'cc': cc, -- GitLab From 39b42b1a479f68488950faecae6ca7b58f9092f3 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 15:49:41 -0500 Subject: [PATCH 03/10] initialize default GCC dict w/ args --- loopy/target/c/c_execution.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/loopy/target/c/c_execution.py b/loopy/target/c/c_execution.py index ef4504077..2980eaa10 100644 --- a/loopy/target/c/c_execution.py +++ b/loopy/target/c/c_execution.py @@ -227,14 +227,16 @@ class CCompiler(object): logger = logging.getLogger(__name__) logger.warn('Default toolchain guessed from python config ' 'not found, replacing with default GCCToolchain.') + # this is ugly, but I'm not sure there's a clean way to copy the + # default args self.toolchain = GCCToolchain( - cc=cc, - cflags=cflags, - ldflags=ldflags, - libraries=libraries, - library_dirs=library_dirs, - defines=defines, - source_suffix=source_suffix) + cc='gcc', + cflags='-std=c99 -O3 -fPIC'.split(), + ldflags='-shared'.split(), + libraries=[], + library_dirs=[], + defines=[], + source_suffix='c') if toolchain is None: # copy in all differing values -- GitLab From 42c45f4cdb8bc26198ba2083c0ee64defc299acf Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 15:50:10 -0500 Subject: [PATCH 04/10] add test for missing compilers --- test/test_c_execution.py | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/test_c_execution.py b/test/test_c_execution.py index d1b3c95ca..b05b44a8e 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -291,6 +291,49 @@ def test_c_execution_with_global_temporaries(): assert np.allclose(knl(a=np.zeros(10, dtype=np.int32))[1], np.arange(10)) +def test_missing_compilers(): + from loopy.target.c import ExecutableCTarget, CTarget + from loopy.target.c.c_execution import CCompiler + from codepy.toolchain import GCCToolchain + + def __test(evalfunc, target, **targetargs): + n = 10 + + knl = lp.make_kernel('{[i]: 0 <= i < n}', + """ + a[i] = b[i] + """, + [lp.GlobalArg('a', shape=(n,), dtype=np.int32), + lp.GlobalArg('b', shape=(n,), dtype=np.int32)], + target=target(**targetargs)) + + knl = lp.fix_parameters(knl, n=n) + return evalfunc(knl) + + assert __test(lambda knl: lp.generate_code_v2(knl)[0], CTarget) + + from pytools.prefork import ExecError + + def eval_tester(knl): + return np.allclose(knl(a=np.zeros(10, dtype=np.int32), + b=np.arange(10, dtype=np.int32))[1], np.arange(10)) + import os + path_store = os.environ["PATH"] + try: + # test with path wiped out such that we can't find gcc + with pytest.raises(ExecError): + os.environ["PATH"] = '' + __test(eval_tester, ExecutableCTarget) + finally: + # make sure we restore the path regardless for future testing + os.environ["PATH"] = path_store + + # next test that some made up compiler defaults to gcc + ccomp = CCompiler(cc='foo') + assert isinstance(ccomp.toolchain, GCCToolchain) + assert ccomp.cc = 'gcc' + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) -- GitLab From f4570447fc7a4332a63b75bb643760e22b44e6c4 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 15:52:54 -0500 Subject: [PATCH 05/10] fix dumb typo --- 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 b05b44a8e..95f1c499b 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -331,7 +331,7 @@ def test_missing_compilers(): # next test that some made up compiler defaults to gcc ccomp = CCompiler(cc='foo') assert isinstance(ccomp.toolchain, GCCToolchain) - assert ccomp.cc = 'gcc' + assert ccomp.cc == 'gcc' if __name__ == "__main__": -- GitLab From b54abbeabd82eafa0c45e5ec91c9e30df647bfd8 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 16:06:08 -0500 Subject: [PATCH 06/10] properly guard if statment --- loopy/target/c/c_execution.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/loopy/target/c/c_execution.py b/loopy/target/c/c_execution.py index 2980eaa10..3634cc71a 100644 --- a/loopy/target/c/c_execution.py +++ b/loopy/target/c/c_execution.py @@ -249,9 +249,8 @@ class CCompiler(object): 'defines': defines} # filter empty and those equal to toolchain defaults diff = dict((k, v) for k, v in six.iteritems(diff) - if v and - not hasattr(self.toolchain, k) or - getattr(self.toolchain, k) != v) + if v and (not hasattr(self.toolchain, k) or + getattr(self.toolchain, k) != v)) self.toolchain = self.toolchain.copy(**diff) self.tempdir = tempfile.mkdtemp(prefix="tmp_loopy") self.source_suffix = source_suffix -- GitLab From 60f59aca1aa3d5960c19e7392a742d2711440974 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 16:07:21 -0500 Subject: [PATCH 07/10] 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 95f1c499b..46a98307a 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -310,7 +310,7 @@ def test_missing_compilers(): knl = lp.fix_parameters(knl, n=n) return evalfunc(knl) - assert __test(lambda knl: lp.generate_code_v2(knl)[0], CTarget) + assert __test(lambda knl: lp.generate_code_v2(knl).host_code(), CTarget) from pytools.prefork import ExecError -- GitLab From 79bc51de1b131f06a6245bbf67881bc6401cd7dc Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 16:23:37 -0500 Subject: [PATCH 08/10] fix code we're looking at --- 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 46a98307a..d779c020b 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -310,7 +310,7 @@ def test_missing_compilers(): knl = lp.fix_parameters(knl, n=n) return evalfunc(knl) - assert __test(lambda knl: lp.generate_code_v2(knl).host_code(), CTarget) + assert __test(lambda knl: lp.generate_code_v2(knl).device_code(), CTarget) from pytools.prefork import ExecError -- GitLab From afc8c14018fd714fbe3db123602ba7301703f008 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 16:23:47 -0500 Subject: [PATCH 09/10] fix tests --- test/test_c_execution.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/test_c_execution.py b/test/test_c_execution.py index d779c020b..460af0c4b 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -328,10 +328,15 @@ def test_missing_compilers(): # make sure we restore the path regardless for future testing os.environ["PATH"] = path_store - # next test that some made up compiler defaults to gcc + # next test that some made up compiler can be specified ccomp = CCompiler(cc='foo') assert isinstance(ccomp.toolchain, GCCToolchain) - assert ccomp.cc == 'gcc' + assert ccomp.toolchain.cc == 'foo' + + # and that said made up compiler errors out + + with pytest.raises(ExecError): + __test(eval_tester, ExecutableCTarget, compiler=ccomp) if __name__ == "__main__": -- GitLab From b9bd8437d3a31e061e7aee7f5df13abf57fa35dc Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 10 Feb 2018 16:41:21 -0500 Subject: [PATCH 10/10] fix caching test -- need to use a copy of the kernel such that we share the same tempdir --- test/test_c_execution.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/test_c_execution.py b/test/test_c_execution.py index 460af0c4b..20725d691 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -257,9 +257,10 @@ def test_c_caching(): # setup test logger to check logs tl = TestingLogger() tl.start_capture() - # remake kernel to clear cache - knl = __get_knl() - assert np.allclose(knl(b=np.arange(10))[1], np.arange(10)) + # copy kernel such that we share the same executor cache + knl = knl.copy() + # but use different args, so we can't cache the result + assert np.allclose(knl(b=np.arange(1, 11))[1], np.arange(1, 11)) # and get logs logs = tl.stop_capture() # check that we didn't recompile -- GitLab