update pet to version 0.11.4
[ppcg.git] / schedule.c
blob10a63a9c3a674f1082ed501b15cf68bf3881c5c3
1 /*
2 * Copyright 2010-2011 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <string.h>
15 #include <isl/set.h>
16 #include <isl/map.h>
17 #include <isl/constraint.h>
19 #include "grouping.h"
20 #include "schedule.h"
22 /* Add parameters with identifiers "ids" to "set".
24 static __isl_give isl_set *add_params(__isl_take isl_set *set,
25 __isl_keep isl_id_list *ids)
27 int i, n;
28 unsigned nparam;
30 n = isl_id_list_n_id(ids);
32 nparam = isl_set_dim(set, isl_dim_param);
33 set = isl_set_add_dims(set, isl_dim_param, n);
35 for (i = 0; i < n; ++i) {
36 isl_id *id;
38 id = isl_id_list_get_id(ids, i);
39 set = isl_set_set_dim_id(set, isl_dim_param, nparam + i, id);
42 return set;
45 /* Equate the dimensions of "set" starting at "first" to
46 * freshly created parameters with identifiers "ids".
47 * The number of equated dimensions is equal to the number of elements in "ids".
49 static __isl_give isl_set *parametrize(__isl_take isl_set *set,
50 int first, __isl_keep isl_id_list *ids)
52 int i, n;
53 unsigned nparam;
55 nparam = isl_set_dim(set, isl_dim_param);
57 set = add_params(set, ids);
59 n = isl_id_list_n_id(ids);
60 for (i = 0; i < n; ++i)
61 set = isl_set_equate(set, isl_dim_param, nparam + i,
62 isl_dim_set, first + i);
64 return set;
67 /* Given a parameter space "space", create a set of dimension "len"
68 * of which the dimensions starting at "first" are equated to
69 * freshly created parameters with identifiers "ids".
71 __isl_give isl_set *parametrization(__isl_take isl_space *space,
72 int len, int first, __isl_keep isl_id_list *ids)
74 isl_set *set;
76 space = isl_space_set_from_params(space);
77 space = isl_space_add_dims(space, isl_dim_set, len);
78 set = isl_set_universe(space);
80 return parametrize(set, first, ids);
83 /* Load and return a schedule from a file called "filename".
85 static __isl_give isl_schedule *load_schedule(isl_ctx *ctx,
86 const char *filename)
88 FILE *file;
89 isl_schedule *schedule;
91 file = fopen(filename, "r");
92 if (!file) {
93 fprintf(stderr, "Unable to open '%s' for reading\n", filename);
94 return NULL;
96 schedule = isl_schedule_read_from_file(ctx, file);
97 fclose(file);
99 return schedule;
102 /* Save the schedule "schedule" to a file called "filename".
103 * The schedule is printed in block style.
105 static void save_schedule(__isl_keep isl_schedule *schedule,
106 const char *filename)
108 FILE *file;
109 isl_ctx *ctx;
110 isl_printer *p;
112 if (!schedule)
113 return;
115 file = fopen(filename, "w");
116 if (!file) {
117 fprintf(stderr, "Unable to open '%s' for writing\n", filename);
118 return;
120 ctx = isl_schedule_get_ctx(schedule);
121 p = isl_printer_to_file(ctx, file);
122 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
123 p = isl_printer_print_schedule(p, schedule);
124 isl_printer_free(p);
125 fclose(file);
128 /* Compute a schedule on the domain of "sc" that respects the schedule
129 * constraints in "sc", without trying to combine groups of statements.
131 __isl_give isl_schedule *ppcg_compute_non_grouping_schedule(
132 __isl_take isl_schedule_constraints *sc, struct ppcg_options *options)
134 if (options->debug->dump_schedule_constraints)
135 isl_schedule_constraints_dump(sc);
136 return isl_schedule_constraints_compute_schedule(sc);
139 /* Compute a schedule on the domain of "sc" that respects the schedule
140 * constraints in "sc".
142 * "schedule" is a known correct schedule that is used to combine
143 * groups of statements if options->group_chains is set.
145 __isl_give isl_schedule *ppcg_compute_schedule(
146 __isl_take isl_schedule_constraints *sc,
147 __isl_keep isl_schedule *schedule, struct ppcg_options *options)
149 if (options->group_chains)
150 return ppcg_compute_grouping_schedule(sc, schedule, options);
151 return ppcg_compute_non_grouping_schedule(sc, options);
154 /* Obtain a schedule, either by reading it form a file
155 * or by computing it using "compute".
156 * Also take care of saving the computed schedule and/or
157 * dumping the obtained schedule if requested by the user.
159 __isl_give isl_schedule *ppcg_get_schedule(isl_ctx *ctx,
160 struct ppcg_options *options,
161 __isl_give isl_schedule *(*compute)(void *user), void *user)
163 isl_schedule *schedule;
165 if (options->load_schedule_file) {
166 schedule = load_schedule(ctx, options->load_schedule_file);
167 } else {
168 schedule = compute(user);
169 if (options->save_schedule_file)
170 save_schedule(schedule, options->save_schedule_file);
172 if (options->debug->dump_schedule)
173 isl_schedule_dump(schedule);
175 return schedule;
178 /* Mark all dimensions in the band node "node" to be of "type".
180 __isl_give isl_schedule_node *ppcg_set_schedule_node_type(
181 __isl_take isl_schedule_node *node, enum isl_ast_loop_type type)
183 int i, n;
185 n = isl_schedule_node_band_n_member(node);
186 for (i = 0; i < n; ++i)
187 node = isl_schedule_node_band_member_set_ast_loop_type(node, i,
188 type);
190 return node;