Parallelising a loop

46 views
Skip to first unread message

Emil Vatai

unread,
Oct 15, 2023, 9:39:35 PM10/15/23
to isl Development
Dear Sven,

I want to parallelize loops: let's say I have a schedule (tree), and after some analysis I determine which band nodes can be parallelized, and when generating code I want to have `#pragma omp parallel for` before the generated for loops.

The way I was trying to do this (which seems to be incorrect) was 
1. to mark the band nodes in the schedule tree (with a mark node) [no problem here, for now I do this by manually adding a mark node into a yaml file which contains the schedule]
2. then (I was hoping) to use `isl_ast_print_options_set_print_for()` to generate a custom loop which either has the omp pragma or doesn't depending whether the loop is marked or not.

This doesn't work because the mark is in the schedule tree while the printing (and set_print_for() function) is in the AST. I also considered using `isl_ast_build_set_{before|after}_each_{for|mark}` to make the connection between the schedule tree and AST using `isl_ast_node_set_annotation()`. However I failed: in the each_mark() function I could only get a copy of the child (i.e. the for loop) and setting the annotation did not appear in the original for `isl_ast_node`, and for the each_for() function I couldn't access the parent of the `isl_ast_node` (AFAIK there is nothing similar to `is_ast_node_parent()`).

So my question is: if I have multiple loops in a program, and want to parallelize some of the loops by modifying the yaml file containing the schedule of the program (e.g. adding mark nodes), how do I do this, i.e. how do I generate different for loops for the parallelized and non-parallelized loops (e.g. add omp paragma vs not add omp pragma)? (I'm mostly relying on the pet_loopback.c example which you were kind enough to provide earlier).

Best,
Emil

Sven Verdoolaege

unread,
Oct 16, 2023, 4:52:13 PM10/16/23
to Emil Vatai, isl Development
On Sun, Oct 15, 2023 at 06:39:35PM -0700, Emil Vatai wrote:
> Dear Sven,
>
> I want to parallelize loops: let's say I have a schedule (tree), and after
> some analysis I determine which band nodes can be parallelized, and when
> generating code I want to have `#pragma omp parallel for` before the
> generated for loops.
>
> The way I was trying to do this (which seems to be incorrect) was
> 1. to mark the band nodes in the schedule tree (with a mark node) [no
> problem here, for now I do this by manually adding a mark node into a yaml
> file which contains the schedule]
> 2. then (I was hoping) to use `isl_ast_print_options_set_print_for()` to
> generate a custom loop which either has the omp pragma or doesn't depending
> whether the loop is marked or not.

Sounds reasonable.
PPCG does something similar, although it performs the analysis
during the AST generation, so it's not directly applicable
for your needs.

> This doesn't work because the mark is in the schedule tree while the
> printing (and set_print_for() function) is in the AST. I also considered
> using `isl_ast_build_set_{before|after}_each_{for|mark}` to make the
> connection between the schedule tree and AST using
> `isl_ast_node_set_annotation()`. However I failed: in the each_mark()
> function I could only get a copy of the child (i.e. the for loop) and
> setting the annotation did not appear in the original for `isl_ast_node`,
> and for the each_for() function I couldn't access the parent of the
> `isl_ast_node` (AFAIK there is nothing similar to `is_ast_node_parent()`).

What node did you return from the isl_ast_build_set_after_each_mark?
The annotated for-node or something else?
Is the problem that you want to preserve the mark-node even
after adding the annotation to the for-node?

skimo

Emil Vatai

unread,
Oct 17, 2023, 9:13:56 PM10/17/23
to sven.ver...@gmail.com, isl Development


Dear Skimo,

Thanks!

What node did you return from the isl_ast_build_set_after_each_mark?
The annotated for-node or something else?

This was the problem.

While I did try returning the annotated for-node, I made a silly mistake.
I had
```
  isl_id *annotation = isl_id_alloc(ctx, "foo-bar", NULL);
  isl_ast_node_set_annotation(forloop, annotation);
```
instead of
```
  isl_id *annotation = isl_id_alloc(ctx, "foo-bar", NULL);
  forloop = isl_ast_node_set_annotation(forloop, annotation);
```
which obviously didn't work...


--
Emil Vatai
Reply all
Reply to author
Forward
0 new messages