From 80e97f75752c84a4e926d13158e0c3b87809112c Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Tue, 14 Nov 2017 19:38:23 -0600 Subject: [PATCH 1/8] Again branched out from Master, and made use of `schedule_index` --- loopy/target/c/__init__.py | 40 +++++++++++++++++++------------------- test/test_loopy.py | 24 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index e54ac0f69..fc1783955 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -342,26 +342,26 @@ class CASTBuilder(ASTBuilderBase): result = [] from loopy.kernel.data import temp_var_scope - - for tv in sorted( - six.itervalues(kernel.temporary_variables), - key=lambda tv: tv.name): - - if tv.scope == temp_var_scope.GLOBAL and tv.initializer is not None: - assert tv.read_only - - decl_info, = tv.decl_info(self.target, - index_dtype=kernel.index_dtype) - decl = self.wrap_global_constant( - self.get_temporary_decl( - codegen_state, schedule_index, tv, - decl_info)) - - if tv.initializer is not None: - decl = Initializer(decl, generate_array_literal( - codegen_state, tv, tv.initializer)) - - result.append(decl) + if schedule_index == 0: + for tv in sorted( + six.itervalues(kernel.temporary_variables), + key=lambda tv: tv.name): + + if tv.scope == temp_var_scope.GLOBAL and tv.initializer is not None: + assert tv.read_only + + decl_info, = tv.decl_info(self.target, + index_dtype=kernel.index_dtype) + decl = self.wrap_global_constant( + self.get_temporary_decl( + codegen_state, schedule_index, tv, + decl_info)) + + if tv.initializer is not None: + decl = Initializer(decl, generate_array_literal( + codegen_state, tv, tv.initializer)) + + result.append(decl) fbody = FunctionBody(function_decl, function_body) if not result: diff --git a/test/test_loopy.py b/test/test_loopy.py index d0398f216..cf21b6e34 100644 --- a/test/test_loopy.py +++ b/test/test_loopy.py @@ -52,6 +52,30 @@ __all__ = [ ] +def test_globals_decl_once_with_multi_subprogram(ctx_factory): + ctx = ctx_factory() + queue = cl.CommandQueue(ctx) + a = np.random.randn(16) + cnst = np.random.randn(16) + knl = lp.make_kernel( + "{[i, ii]: 0<=i, ii Date: Tue, 14 Nov 2017 23:55:02 -0600 Subject: [PATCH 2/8] Previously ignored the check that the first scheduled item may not be a CallKernel --- loopy/target/c/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index fc1783955..98c0be82b 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -342,7 +342,14 @@ class CASTBuilder(ASTBuilderBase): result = [] from loopy.kernel.data import temp_var_scope - if schedule_index == 0: + # determining if the global temps are to be written + from loopy.schedule import CallKernel + globals_to_be_written = True + for i in range(schedule_index): + if isinstance(kernel.schedule[i], CallKernel): + globals_to_be_written = False + break + if globals_to_be_written: for tv in sorted( six.itervalues(kernel.temporary_variables), key=lambda tv: tv.name): -- GitLab From a8631d4e9d51e555832189e07b04a736722f8638 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Wed, 15 Nov 2017 02:01:50 -0600 Subject: [PATCH 3/8] Changed the variable name, added comments for explaining the changes. --- loopy/target/c/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index 98c0be82b..5b972cdc6 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -342,14 +342,16 @@ class CASTBuilder(ASTBuilderBase): result = [] from loopy.kernel.data import temp_var_scope - # determining if the global temps are to be written from loopy.schedule import CallKernel - globals_to_be_written = True + # We only need to write declarations for global variables with + # the first device program. `is_first_dev_prog` determines + # whether this is the first device program in the schedule. + is_first_dev_prog = True for i in range(schedule_index): if isinstance(kernel.schedule[i], CallKernel): - globals_to_be_written = False + is_first_dev_prog = False break - if globals_to_be_written: + if is_first_dev_prog: for tv in sorted( six.itervalues(kernel.temporary_variables), key=lambda tv: tv.name): -- GitLab From e41e8367dcb88c0b76cbfabfdca1e363908eef83 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 16 Nov 2017 00:28:27 -0600 Subject: [PATCH 4/8] Tries a fix for local temps being declared in each sub-program --- loopy/target/c/__init__.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index 5b972cdc6..d8a4ff8c8 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -428,6 +428,19 @@ class CASTBuilder(ASTBuilderBase): base_storage_to_align_bytes = {} from cgen import ArrayOf, Initializer, AlignedAttribute, Value, Line + # Getting the temporary variables that are needed for the current + # sub-program. + from loopy.schedule import CallKernel, RunInstruction + sub_prog_temps = frozenset() + + for i in range(schedule_index+1, codegen_state.schedule_index_end): + if isinstance(kernel.schedule[i], CallKernel): + break + if isinstance(kernel.schedule[i], RunInstruction): + insn = kernel.id_to_insn[kernel.schedule[i].insn_id] + sub_prog_temps = sub_prog_temps |\ + insn.read_dependency_names() |\ + insn.write_dependency_names() # noqa for tv in sorted( six.itervalues(kernel.temporary_variables), @@ -437,7 +450,7 @@ class CASTBuilder(ASTBuilderBase): if not tv.base_storage: for idi in decl_info: # global temp vars are mapped to arguments or global declarations - if tv.scope != temp_var_scope.GLOBAL: + if tv.scope != temp_var_scope.GLOBAL and tv.name in sub_prog_temps: decl = self.wrap_temporary_decl( self.get_temporary_decl( codegen_state, schedule_index, tv, idi), -- GitLab From 5111fa4f3d2184ea50f763ee12d63e58077d08f2 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 16 Nov 2017 00:33:31 -0600 Subject: [PATCH 5/8] some pep8 fixes --- loopy/target/c/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index d8a4ff8c8..ecb60dc18 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -440,7 +440,7 @@ class CASTBuilder(ASTBuilderBase): insn = kernel.id_to_insn[kernel.schedule[i].insn_id] sub_prog_temps = sub_prog_temps |\ insn.read_dependency_names() |\ - insn.write_dependency_names() # noqa + insn.write_dependency_names() for tv in sorted( six.itervalues(kernel.temporary_variables), @@ -450,7 +450,8 @@ class CASTBuilder(ASTBuilderBase): if not tv.base_storage: for idi in decl_info: # global temp vars are mapped to arguments or global declarations - if tv.scope != temp_var_scope.GLOBAL and tv.name in sub_prog_temps: + if tv.scope != temp_var_scope.GLOBAL and\ + tv.name in sub_prog_temps: decl = self.wrap_temporary_decl( self.get_temporary_decl( codegen_state, schedule_index, tv, idi), -- GitLab From 847810ea825f33b962070d0a1b5c4ee23817d56d Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 16 Nov 2017 00:45:18 -0600 Subject: [PATCH 6/8] removed the line continuations --- loopy/target/c/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index ecb60dc18..fbb78dcb2 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -438,9 +438,10 @@ class CASTBuilder(ASTBuilderBase): break if isinstance(kernel.schedule[i], RunInstruction): insn = kernel.id_to_insn[kernel.schedule[i].insn_id] - sub_prog_temps = sub_prog_temps |\ - insn.read_dependency_names() |\ - insn.write_dependency_names() + sub_prog_temps = ( + sub_prog_temps | + insn.read_dependency_names() | + insn.write_dependency_names()) for tv in sorted( six.itervalues(kernel.temporary_variables), @@ -450,8 +451,8 @@ class CASTBuilder(ASTBuilderBase): if not tv.base_storage: for idi in decl_info: # global temp vars are mapped to arguments or global declarations - if tv.scope != temp_var_scope.GLOBAL and\ - tv.name in sub_prog_temps: + if tv.scope != temp_var_scope.GLOBAL and ( + tv.name in sub_prog_temps): decl = self.wrap_temporary_decl( self.get_temporary_decl( codegen_state, schedule_index, tv, idi), -- GitLab From 5ba6d9d52c417874d2565f657b47eddc42c6045f Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 16 Nov 2017 15:02:36 -0600 Subject: [PATCH 7/8] Using already provided functions in `loopy/schedule/tools.py` --- loopy/target/c/__init__.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index fbb78dcb2..832c224f3 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -429,19 +429,14 @@ class CASTBuilder(ASTBuilderBase): from cgen import ArrayOf, Initializer, AlignedAttribute, Value, Line # Getting the temporary variables that are needed for the current - # sub-program. - from loopy.schedule import CallKernel, RunInstruction - sub_prog_temps = frozenset() - - for i in range(schedule_index+1, codegen_state.schedule_index_end): - if isinstance(kernel.schedule[i], CallKernel): - break - if isinstance(kernel.schedule[i], RunInstruction): - insn = kernel.id_to_insn[kernel.schedule[i].insn_id] - sub_prog_temps = ( - sub_prog_temps | - insn.read_dependency_names() | - insn.write_dependency_names()) + # sub-kernel. + from loopy.schedule.tools import ( + temporaries_read_in_subkernel, + temporaries_written_in_subkernel) + subkernel = kernel.schedule[schedule_index].kernel_name + sub_knl_temps = ( + temporaries_read_in_subkernel(kernel, subkernel) | + temporaries_written_in_subkernel(kernel, subkernel)) for tv in sorted( six.itervalues(kernel.temporary_variables), @@ -452,7 +447,7 @@ class CASTBuilder(ASTBuilderBase): for idi in decl_info: # global temp vars are mapped to arguments or global declarations if tv.scope != temp_var_scope.GLOBAL and ( - tv.name in sub_prog_temps): + tv.name in sub_knl_temps): decl = self.wrap_temporary_decl( self.get_temporary_decl( codegen_state, schedule_index, tv, idi), -- GitLab From 28ec28215fe6871617c11a0d69fb0e4c13d43b64 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 16 Nov 2017 15:06:55 -0600 Subject: [PATCH 8/8] Added `np.random.seed(...)` --- test/test_loopy.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_loopy.py b/test/test_loopy.py index cf21b6e34..af50599dc 100644 --- a/test/test_loopy.py +++ b/test/test_loopy.py @@ -55,6 +55,7 @@ __all__ = [ def test_globals_decl_once_with_multi_subprogram(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) + np.random.seed(17) a = np.random.randn(16) cnst = np.random.randn(16) knl = lp.make_kernel( -- GitLab