Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loopy unable to generate the device code for a kernel with conditional branching #156

Open
nipunayf opened this issue Mar 19, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@nipunayf
Copy link
Contributor

Description

When attempting to convert and generate the device code for a C kernel with a if condition, it generates the following error:

/home/hp/mambaforge/envs/libnomp/bin/python /home/hp/projects/nomp/libnomp/python/loopy_api.py 
Traceback (most recent call last):
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/loopy/tools.py", line 933, in wrapper
    result = transform_cache[cache_key]
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/pytools/persistent_dict.py", line 583, in __getitem__
    return self.fetch(key, _stacklevel=1)
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/pytools/persistent_dict.py", line 702, in fetch
    raise NoSuchEntryError(key)
pytools.persistent_dict.NoSuchEntryError: ('preprocess_program', 'preprocess_program', (TranslationUnit(func_id_to_in_knl_callable_mappers=[], entrypoints=frozenset({'foo'}), callables_table=pmap({'foo': CallableKernel(arg_id_to_descr=None, arg_id_to_dtype=None, name='foo', subkernel=LoopKernel(domains=[BasicSet("[N] -> { [i] : 0 <= i <= N }")], instructions=[Assignment(conflicts_with_groups=frozenset(), assignee=Subscript(Variable('a'), Variable('i')), tags=frozenset(), depends_on=frozenset(), no_sync_with=frozenset(), within_inames=frozenset({'i'}), groups=frozenset(), id='_nomp_insn', within_inames_is_final=False, expression=Product((Subscript(Variable('a'), Variable('i')), Sum((Variable('i'), 1)))), predicates=frozenset({Comparison(Subscript(Variable('a'), Variable('i')), '>', 0)}), temp_var_type=Optional(), atomicity=(), depends_on_is_final=True, priority=0)], args=[<a: ArrayArg, type: np:dtype('float64'), shape: unknown in/out aspace: global>, <N: ValueArg, type: np:dtype('int32')>], assumptions=BasicSet("[N] -> {  :  }"), temporary_variables={}, inames={'i': Iname(name='i', tags=frozenset())}, substitutions={}, options=Options(disable_global_barriers=False, enforce_variable_access_ordered=True, return_dict=False, trace_assignments=False, insert_gbarriers=False, check_dep_resolution=True, no_numpy=False, annotate_inames=False, cl_exec_manage_array_events=True, edit_code=False, write_code=False, allow_fp_reordering=True, enforce_array_accesses_within_bounds=True, trace_assignment_values=False, skip_arg_checks=False, allow_terminal_colors=True, build_options=[], write_wrapper=False), target=<loopy.target.opencl.OpenCLTarget object at 0x7fc1fcd3b670>, tags=frozenset(), state=<KernelState.INITIAL: 0>, name='foo', preambles=(), preamble_generators=(), symbol_manglers=(), linearization=None, iname_slab_increments=immutables.Map({}), loop_priority=frozenset(), applied_iname_rewrites=(), index_dtype=np:dtype('int32'), silenced_warnings=[], overridden_get_grid_sizes_for_insn_ids=None))}), target=<loopy.target.opencl.OpenCLTarget object at 0x7fc1fcd3b670>),), {})

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hp/projects/nomp/libnomp/python/loopy_api.py", line 624, in <module>
    print(lp.generate_code_v2(lp_knl).device_code())
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/loopy/codegen/__init__.py", line 597, in generate_code_v2
    program = preprocess_program(program)
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/loopy/tools.py", line 949, in wrapper
    result = func(*args, **kwargs)
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/loopy/preprocess.py", line 843, in preprocess_program
    new_subkernel = _preprocess_single_kernel(
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/loopy/preprocess.py", line 761, in _preprocess_single_kernel
    check_for_writes_to_predicates(kernel)
  File "/home/hp/mambaforge/envs/libnomp/lib/python3.10/site-packages/loopy/preprocess.py", line 67, in check_for_writes_to_predicates
    raise LoopyError("In instruction '%s': may not write to "
loopy.diagnostic.LoopyError: In instruction '_nomp_insn': may not write to variable(s) 'a' involved in the instruction's predicates

Process finished with exit code 1

Reproduce Steps

  1. Use the loopy_api.py to convert the following C kernel.
void foo(double *a, int N) {
  for (int i = 0; i < N + 1; i++)
    if (a[i] > 0)
      a[i] = a[i] * (i + 1);
}
  1. Attempt to generate the device code with,
print(lp.generate_code_v2(lp_knl).device_code())
@nipunayf nipunayf added the bug Something isn't working label Mar 19, 2023
@thilinarmtb thilinarmtb self-assigned this Mar 20, 2023
@thilinarmtb
Copy link
Contributor

Thanks for reporting this !

@nipunayf
Copy link
Contributor Author

Additional note to this issue,

The issue appears when we access an array element in the condition, in this case a[i]. If we rewrite the kernel as below, the issue will be resolved.

void foo(double *a, int N) {
  for (int i = 0; i < N + 1; i++)
    int temp = a[i];
    if (temp > 0)
      a[i] = a[i] * (i + 1);
}

@thilinarmtb
Copy link
Contributor

Additional note to this issue,

The issue appears when we access an array element in the condition, in this case a[i]. If we rewrite the kernel as below, the issue will be resolved.

void foo(double *a, int N) {
  for (int i = 0; i < N + 1; i++)
    int temp = a[i];
    if (temp > 0)
      a[i] = a[i] * (i + 1);
}

Yes, we need to make sure that if condition is a dependency for the code in then and else branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants