PIPS
cost_model.c
Go to the documentation of this file.
1 #ifdef HAVE_CONFIG_H
2  #include "pips_config.h"
3 #endif
4 
5 
6 #include <stdio.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include "boolean.h"
10 #include <stdbool.h>
11 
12 #include "genC.h"
13 #include "linear.h"
14 #include "ri.h"
15 #include "effects.h"
16 #include "database.h"
17 #include "misc.h"
18 #include "text.h"
19 #include "text-util.h"
20 #include "ri-util.h"
21 #include "effects-util.h"
22 #include "accel-util.h"
23 
24 #include "effects-generic.h"
25 #include "effects-simple.h"
26 
27 #include "pipsdbm.h"
28 #include "resources.h"
29 #include "control.h"
30 #include "c_syntax.h"
31 #include "conversion.h"
32 #include "properties.h"
33 #include "transformations.h"
34 
35 #include "effects-convex.h"
36 #include "genC.h"
37 
38 #include "complexity_ri.h"
39 #include "complexity.h"
40 #include <time.h>
41 #include "dg.h"
42 
43 /* Instantiation of the dependence graph: */
46 #include "graph.h"
47 #include "ricedg.h"
48 #include "chains.h"
49 #include "task_parallelization.h"
50 
51 
53  bool costly_p = false;
56  return true;
57  else{
58  switch(instruction_tag(inst)){
60  MAPL( stmt_ptr,
61  {
62  statement local_stmt = STATEMENT(CAR(stmt_ptr ));
63  costly_p = costly_p || costly_task(local_stmt);
64  },
65  instruction_block(inst));
66  return costly_p;
67  break;
68  }
69  case is_instruction_test :{
70  test t = instruction_test(inst);
71  return costly_task(test_true(t)) ||
73  break;
74  }
75  case is_instruction_loop :{
76  return true;
77  }
79  return true;
80  }
82  return true;
83  }
84  case is_instruction_call:{
85  return user_call_p(statement_call(st));
86  }
87  default:
88  return false;
89  }
90  }
91 }
92 
93 double polynomial_to_numerical (Ppolynome poly_amount)
94 {
95  double size = 0.f;
96  /* if polynomial is not a constant monomial, we use
97  * an heuristic to map it into a numerical constant
98  * take the higher degree monomial
99  * and decide upon its coefficient
100  */
101  if(!POLYNOME_UNDEFINED_P(poly_amount))
102  {
103  int max_degree = polynome_max_degree(poly_amount);
104  for(Ppolynome p = poly_amount; !POLYNOME_NUL_P(p); p = polynome_succ(p)) {
105  int curr_degree = (int)vect_sum(monome_term(polynome_monome(p)));
106  if(curr_degree == max_degree) {
107  size = monome_coeff(polynome_monome(p));
108  break;
109  }
110  }
111  }
112  return size;
113 }
114 
115 double size_of_regions(list l_data)
116 {
117  Ppolynome transfer_time = POLYNOME_NUL;
118  FOREACH(REGION,reg,l_data){
119  Ppolynome reg_footprint= region_enumerate(reg);
121  polynome_add(&transfer_time, reg_footprint);
122  }
123  return polynomial_to_numerical(transfer_time);
124 }
125 
127  list l_data = NIL;
130  l_data = RegionsMustUnion(regions_dup(l_read), regions_dup(l_write), r_w_combinable_p);
131  return l_data;
132 }
133 
134 static double task_time(statement s)
135 {
136  complexity stat_comp = load_statement_complexity(s);
137  Ppolynome poly = complexity_polynome(stat_comp);
138  return polynomial_to_numerical(poly);
139 }
140 
141 /*
142  Per-byte transfer time(tb)is the time to transmit one byte along a
143  data communication channel; the duration of this transmission
144  is defined by the communication channel bandwidth
145 */
146 
148 {
149  Ppolynome transfer_time = POLYNOME_NUL;
152  list l_communications = RegionsIntersection(regions_dup(l_write), regions_dup(l_read), w_r_combinable_p);
153  FOREACH(REGION,reg,l_communications){
154  Ppolynome reg_footprint = region_enumerate(reg);
155  //reg_footprint = polynome_mult(reg_footprint, expression_to_polynome(int_to_expression(SizeOfElements(variable_basic(type_variable(entity_type(region_entity(reg))))))));
156  //reg_footprint =
157  //polynome_mult(reg_footprint,expression_to_polynome(int_to_expression(2.5)));//\beta= 2.5 on Cmmcluster
158  polynome_add(&transfer_time, reg_footprint);
159  }
160  //polynome_add(&transfer_time, latency);// the latency \alpha = 15000 on Cmmcluster
161  //polynome_fprint(stderr,transfer_time,entity_local_name,default_is_inferior_var);
162  return polynomial_to_numerical(transfer_time);
163 }
164 
165 
166 /* First parameter is the top level (earliest start time) for each node
167 */
169 {
170  double max = 0, level;
171  list vertices = (graph_vertices(dg));
172  annotation *an,*anp;
174  FOREACH(VERTEX, pre, vertices) {
175  statement parent = vertex_to_statement(pre);
176  FOREACH(SUCCESSOR, su, (vertex_successors(pre))) {
177  vertex s = successor_vertex(su);
178  statement child = vertex_to_statement(s);
179  if(statement_equal_p(child, sv) && !statement_equal_p(child,parent) && gen_array_item(annotations, (int)statement_ordering(parent))){
180  double tl_p;
181  anp = gen_array_item(annotations, (int)statement_ordering(parent));
182  double edge_c = *(double *)/*(intptr_t)*/(gen_array_item(anp->edge_cost, statement_ordering(sv)));
183  if(anp->tlevel != -1)
184  tl_p = anp->tlevel;
185  else
186  tl_p = t_level(pre, dg, annotations);
187  level = tl_p + anp->task_time + edge_c ;
188  if(level > max)
189  max = level;
190  }
191  else {
192  if(statement_equal_p(child, sv) && !statement_equal_p(child,parent) && gen_array_item(annotations, (int)statement_ordering(parent))==NULL )
193  {
194  anp = gen_array_item(annotations, (int)statement_ordering(parent));
195  double edge_c = *(double *)(gen_array_item(anp->edge_cost,statement_ordering(sv)));
196  level = t_level(pre, dg, annotations) + anp->task_time + edge_c;
197  if(level > max)
198  max = level;
199  }
200  }
201  }
202  }
205  else
206  an = (annotation *)malloc(sizeof(annotation));
207  an->tlevel = max;
209  return max;
210 }
212 {
213  list vertices = graph_vertices(dg);
214  FOREACH(VERTEX, v, vertices){
215  t_level(v, dg, annotations);
216  }
217  return;
218 }
219 
220 /* Second parameter is the bottom level (latest start time) for each
221  node
222 */
224 {
225  double max, level;
226  list vertices = (graph_vertices(dg));
227  annotation *an, *anp;
228  FOREACH(VERTEX, v, vertices) {
229  max = 0;
231  FOREACH(VERTEX, pre, (graph_vertices(dg))) {
232  statement parent = vertex_to_statement(pre);
233  anp = gen_array_item(annotations, (int)statement_ordering(parent));
234  FOREACH(SUCCESSOR, su, (vertex_successors(pre))) {
235  vertex s = successor_vertex(su);
236  statement child = vertex_to_statement(s);
237  if(statement_equal_p(child, sv) && gen_array_item(annotations, (int)statement_ordering(parent)) )
238  {
239  double edge_c = *(double *)(gen_array_item(anp->edge_cost,statement_ordering(sv)));
240  level = anp->blevel + edge_c;
241  if(level > max)
242  max = level;
243  }
244  }
245  }
247  an->blevel = an->task_time + max;
249  }
250  return;
251 }
252 
254 {
255  size_t i;
256  for(i = 0; i< gen_array_nitems(annotations); i++){
257  if(gen_array_item(annotations, i) != NULL){
259  item->prio = item->tlevel + item->blevel;
260  gen_array_addto(annotations, i, item);
261  }
262  }
263  return;
264 }
265 
266 
268 {
269  list vertices = graph_vertices(dg);
270  int sigma = get_int_property("BDSC_SENSITIVITY");
271  float clock = 1, overhead = 0;
272  srand(time(NULL));
273  FOREACH(VERTEX, v, vertices){
275  annotation *item = (annotation *)malloc(sizeof(annotation));
276  float x = (float)((float)rand()/((float)RAND_MAX) * (float)sigma/100);
277  item->task_time = clock * task_time(stmt) * (1 + x ) + overhead;
279  FOREACH(SUCCESSOR, su, (vertex_successors(v))){//statement_to_vertex(stmt, dg)))) {
280  vertex s = successor_vertex(su);
281  statement child = vertex_to_statement(s);
282  double *ec = (double *)malloc(sizeof(double));
283  *ec = clock * edge_cost(stmt, child) * (1+((float)rand()/(float)RAND_MAX * sigma/100)) + overhead;
284  gen_array_addto(item->edge_cost, statement_ordering(child), ec);
285  }
286  item->scheduled = false;
287  item->order_sched = -1;
288  item->cluster = -1;
289  item->nbclusters = 0;
290  item->data = used_data(stmt);
292  }
293  return;
294 }
295 
297 {
298  FILE *finstrumented;
299  double cost;
300  int ordering, ordering2;
301  finstrumented = fopen(file_name,"r");
302  list vertices = graph_vertices(dg);
303  annotation *item;
304  FOREACH(VERTEX, v, vertices){
306  item = (annotation *)malloc(sizeof(annotation));
307  item->edge_cost = gen_array_make(0);
309  vertex s = successor_vertex(su);
310  statement child = vertex_to_statement(s);
311  double *ec = (double *)malloc(sizeof(double));
312  *ec = 0;
313  gen_array_addto(item->edge_cost, statement_ordering(child), ec);
314  }
316  }
317  while (!feof(finstrumented) && (fscanf(finstrumented,"%d->%d = %lf \n", &ordering,&ordering2, &cost)))
318  {
319  if(ordering2 == -1){
320  item = gen_array_item(annotations, ordering);
321  item->task_time = (double)cost;
322  if(item->edge_cost == NULL)
323  item->edge_cost = gen_array_make(0);
324  gen_array_addto(annotations, ordering, item);
325  }
326  else {
327  item = gen_array_item(annotations, ordering);
328  if(item->edge_cost == NULL)
329  item->edge_cost = gen_array_make(0);
330  double *ec = (double *)malloc(sizeof(double));
331  *ec = cost;
332  gen_array_addto(item->edge_cost, ordering2, ec);
333  gen_array_addto(annotations, ordering, item);
334  }
335 
336  }
337  fclose(finstrumented);
338  return;
339 }
340 
int get_int_property(const string)
gen_array_t annotations
Global variables.
Definition: SDG.c:62
void const char const char const int
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
size_t gen_array_size(const gen_array_t a)
Definition: array.c:137
gen_array_t gen_array_make(size_t size)
declarations...
Definition: array.c:40
void gen_array_addto(gen_array_t a, size_t i, void *what)
Definition: array.c:87
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
static graph dg
dg is the dependency graph ; FIXME : should not be static global ?
Definition: chains.c:124
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
Ppolynome complexity_polynome(complexity comp)
Because complexity is composed of two elements, we use this function to get the first element : polyn...
Definition: comp_math.c:506
complexity load_statement_complexity(statement)
void parse_instrumented_file(char *file_name, graph dg, gen_array_t annotations)
Definition: cost_model.c:296
void bottom_level(graph dg, gen_array_t annotations)
Second parameter is the bottom level (latest start time) for each node.
Definition: cost_model.c:223
void top_level(graph dg, gen_array_t annotations)
Definition: cost_model.c:211
void initialization(graph dg, gen_array_t annotations)
Definition: cost_model.c:267
static list used_data(statement(st))
Definition: cost_model.c:126
double polynomial_to_numerical(Ppolynome poly_amount)
Definition: cost_model.c:93
static double task_time(statement s)
Definition: cost_model.c:134
double t_level(vertex v, graph dg, gen_array_t annotations)
First parameter is the top level (earliest start time) for each node.
Definition: cost_model.c:168
dg_vertex_label vertex_label
Definition: cost_model.c:45
double size_of_regions(list l_data)
Definition: cost_model.c:115
double edge_cost(statement s1, statement s2)
Definition: cost_model.c:147
dg_arc_label arc_label
Instantiation of the dependence graph:
Definition: cost_model.c:44
bool costly_task(statement st)
cost_model.c
Definition: cost_model.c:52
void priorities(gen_array_t annotations)
Definition: cost_model.c:253
#define region_entity(reg)
#define REGION
list RegionsMustUnion(list l1, list l2, bool(*union_combinable_p)(effect, effect))
list RegionsMustUnion(list l1, list l2, union_combinable_p) input : two lists of regions output : a l...
list RegionsIntersection(list l1, list l2, bool(*intersection_combinable_p)(effect, effect))
list RegionsIntersection(list l1,l2, bool (*intersection_combinable_p)(effect, effect)) input : outpu...
#define max(a, b)
list regions_write_regions(list)
list regions_read_regions(list)
list regions_dup(list)
Ppolynome region_enumerate(effect)
bool w_r_combinable_p(effect, effect)
bool r_w_combinable_p(effect, effect)
list load_statement_local_regions(statement)
void * malloc(YYSIZE_T)
#define successor_vertex(x)
Definition: graph.h:118
#define vertex_successors(x)
Definition: graph.h:154
#define SUCCESSOR(x)
SUCCESSOR.
Definition: graph.h:86
#define graph_vertices(x)
Definition: graph.h:82
#define VERTEX(x)
VERTEX.
Definition: graph.h:122
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
size_t gen_length(const list l)
Definition: list.c:150
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
call statement_call(statement)
Get the call of a statement.
Definition: statement.c:1406
bool statement_contains_user_call_p(statement)
Definition: statement.c:3939
statement vertex_to_statement(vertex v)
Vertex_to_statement looks for the statement that is pointed to by vertex v.
Definition: util.c:45
static vertex statement_to_vertex(statement s, graph g)
Definition: impact_check.c:227
Value vect_sum(Pvecteur v)
Value vect_sum(Pvecteur v): somme des coefficients d'un vecteur (i.e.
Definition: reductions.c:261
Ppolynome polynome_mult(Ppolynome pp1, Ppolynome pp2)
Ppolynome polynome_mult(Ppolynome pp1, Ppolynome pp2) returns pp1 * pp2.
Definition: pnome-bin.c:287
void polynome_add(Ppolynome *ppp, Ppolynome pp2)
void polynome_add(Ppolynome* ppp, Ppolynome pp2) (*ppp) = (*ppp) + pp2.
Definition: pnome-bin.c:171
int polynome_max_degree(Ppolynome pp)
int polynome_max_degree(Ppolynome pp) returns the degree of polynomial pp Let's hope there aren't too...
Definition: pnome-reduc.c:113
static bool statement_equal_p(statement s1, statement s2)
Definition: unstructured.c:55
#define POLYNOME_NUL
#define POLYNOME_UNDEFINED_P(pp)
#define monome_term(pm)
#define polynome_monome(pp)
#define monome_coeff(pm)
Macros definitions.
#define POLYNOME_NUL_P(pp)
#define polynome_succ(pp)
#define is_instruction_block
soft block->sequence transition
#define instruction_block(i)
expression int_to_expression(_int i)
transform an int into an expression and generate the corresponding entity if necessary; it is not cle...
Definition: expression.c:1188
bool user_call_p(call c)
Test if a call is a user call.
Definition: expression.c:4361
Ppolynome expression_to_polynome(expression exp)
===========================================================================
Definition: expression.c:3650
_int SizeOfElements(basic)
This function returns the length in bytes of the Fortran or C type represented by a basic,...
Definition: size.c:297
#define statement_ordering(x)
Definition: ri.h:2454
#define test_false(x)
Definition: ri.h:2837
#define type_variable(x)
Definition: ri.h:2949
@ is_instruction_whileloop
Definition: ri.h:1472
@ is_instruction_test
Definition: ri.h:1470
@ is_instruction_call
Definition: ri.h:1474
@ is_instruction_forloop
Definition: ri.h:1477
@ is_instruction_loop
Definition: ri.h:1471
#define instruction_tag(x)
Definition: ri.h:1511
#define test_true(x)
Definition: ri.h:2835
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_test(x)
Definition: ri.h:1517
#define entity_type(x)
Definition: ri.h:2792
#define variable_basic(x)
Definition: ri.h:3120
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
#define level
s1
Definition: set.c:247
static char * x
Definition: split_file.c:159
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
Definition: statement.c:54
static string file_name