Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not request status if not requested by caller.
[simgrid.git] / src / bindings / rubyDag / rb_SD_task.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 //#include "ruby_simdag.h"
8 #include "rb_SD_task.h"
9
10 // Free Method
11 static void SD_task_free(SD_task_t tk)
12 {
13 //   SD_task_destroy(tk);
14 }
15
16 static void rb_SD_task_destroy(VALUE class, VALUE task)
17 {
18   SD_task_t tk;
19   Data_Get_Struct(task, SD_task_t, tk);
20   SD_task_destroy(tk);
21 }
22
23 // New Method
24 static VALUE rb_SD_task_new(VALUE class, VALUE name, VALUE amount)
25 {
26   //data Set to NULL
27   SD_task_t task =
28       SD_task_create(RSTRING(name)->ptr, NULL, NUM2DBL(amount));
29   // Wrap m_task_t to a Ruby Value
30   return Data_Wrap_Struct(class, 0, SD_task_free, task);
31
32 }
33
34 //Get Name
35 static VALUE rb_SD_task_name(VALUE class, VALUE task)
36 {
37   // Wrap Ruby Value to m_task_t struct
38   SD_task_t tk;
39   Data_Get_Struct(task, SD_task_t, tk);
40   return rb_str_new2(SD_task_get_name(tk));
41
42 }
43
44 // Schedule Task
45 static void rb_SD_task_schedule(VALUE class, VALUE task,
46                                 VALUE workstation_nb,
47                                 VALUE workstation_list,
48                                 VALUE computation_amount,
49                                 VALUE communication_amount, VALUE rate)
50 {
51   // Wrap Ruby Value to m_task_t struct
52   int i, wrk_nb, comp_nb, comm_nb;
53   double *comp_amount, *comm_amount;
54   double rt;
55   VALUE *ptr_wrk, *ptr_comp, *ptr_comm;
56   SD_task_t tk;
57   Data_Get_Struct(task, SD_task_t, tk);
58   wrk_nb = NUM2INT(workstation_nb);
59   comp_nb = RARRAY(computation_amount)->len;
60   comm_nb = RARRAY(communication_amount)->len;
61   rt = NUM2DBL(rate);
62   SD_workstation_t *wrk_list;
63
64   ptr_wrk = RARRAY(workstation_list)->ptr;
65   ptr_comp = RARRAY(computation_amount)->ptr;
66   ptr_comm = RARRAY(communication_amount)->ptr;
67
68   wrk_list = xbt_new0(SD_workstation_t, wrk_nb);
69   comp_amount = xbt_new0(double, wrk_nb);
70   comm_amount = xbt_new0(double, wrk_nb);
71
72   // wrk_nb == comp_nb == comm_nb ???
73   for (i = 0; i < wrk_nb; i++) {
74     Data_Get_Struct(ptr_wrk[i], SD_workstation_t, wrk_list[i]);
75     comp_amount[i] = NUM2DBL(ptr_comp[i]);
76     comm_amount[i] = NUM2DBL(ptr_comm[i]);
77   }
78   /*for (i=0;i<comp_nb;i++)
79      comp_amount[i] = NUM2DBL(ptr_comp[i]);
80
81      for (i=0;i<comm_nb;i++)
82      comm_amount[i] = NUM2DBL(ptr_comm[i]); */
83
84   SD_task_schedule(tk, wrk_nb, wrk_list, comp_amount, comm_amount, rt);
85
86   xbt_free(wrk_list);
87   xbt_free(comp_amount);
88   xbt_free(comm_amount);
89
90
91 }
92
93 //unschedule
94 static void rb_SD_task_unschedule(VALUE class, VALUE task)
95 {
96   SD_task_t tk;
97   Data_Get_Struct(task, SD_task_t, tk);
98   SD_task_unschedule(tk);
99
100 }
101
102 // task dependency add
103 static void rb_SD_task_add_dependency(VALUE Class, VALUE task_src,
104                                       VALUE task_dst)
105 {
106   SD_task_t tk_src, tk_dst;
107   Data_Get_Struct(task_src, SD_task_t, tk_src);
108   Data_Get_Struct(task_dst, SD_task_t, tk_dst);
109   SD_task_dependency_add(NULL, NULL, tk_src, tk_dst);
110
111 }
112
113 //task execution time
114 static VALUE rb_SD_task_execution_time(VALUE class, VALUE task,
115                                        VALUE workstation_nb,
116                                        VALUE workstation_list,
117                                        VALUE computation_amount,
118                                        VALUE communication_amount,
119                                        VALUE rate)
120 {
121
122   int i, wrk_nb;
123   double *comp_amount, *comm_amount;
124   double rt;
125   VALUE *ptr_wrk, *ptr_comp, *ptr_comm;
126   SD_task_t tk;
127   Data_Get_Struct(task, SD_task_t, tk);
128   wrk_nb = NUM2INT(workstation_nb);
129   rt = NUM2DBL(rate);
130   SD_workstation_t *wrk_list;
131
132   ptr_wrk = RARRAY(workstation_list)->ptr;
133   ptr_comp = RARRAY(computation_amount)->ptr;
134   ptr_comm = RARRAY(communication_amount)->ptr;
135
136   wrk_list = xbt_new0(SD_workstation_t, wrk_nb);
137   comp_amount = xbt_new0(double, wrk_nb);
138   comm_amount = xbt_new0(double, wrk_nb);
139
140   for (i = 0; i < wrk_nb; i++) {
141     Data_Get_Struct(ptr_wrk[i], SD_workstation_t, wrk_list[i]);
142     comp_amount[i] = NUM2DBL(ptr_comp[i]);
143     comm_amount[i] = NUM2DBL(ptr_comm[i]);
144   }
145
146   return
147       rb_float_new(SD_task_get_execution_time
148                    (tk, wrk_nb, wrk_list, comp_amount, comm_amount, rt));
149
150 }
151
152 //task start time
153 static VALUE rb_SD_task_start_time(VALUE class, VALUE task)
154 {
155
156   SD_task_t tk;
157   Data_Get_Struct(task, SD_task_t, tk);
158   double time = SD_task_get_start_time(tk);
159   return rb_float_new(time);
160
161 }
162
163 //task fininsh time
164 static VALUE rb_SD_task_finish_time(VALUE class, VALUE task)
165 {
166   SD_task_t tk;
167   Data_Get_Struct(task, SD_task_t, tk);
168   double time = SD_task_get_finish_time(tk);
169   return rb_float_new(time);
170
171 }
172
173 // Simulate : return a SD_task
174 static VALUE rb_SD_simulate(VALUE class, VALUE how_long)
175 {
176   int i;
177   SD_task_t task;
178   double hl = NUM2DBL(how_long);
179   xbt_dynar_t simulated_tasks = SD_simulate(hl);
180
181   VALUE rb_tasks = rb_ary_new();
182   xbt_dynar_foreach(simulated_tasks, i, task) {
183     VALUE tk = Qnil;
184     tk = Data_Wrap_Struct(class, 0, SD_task_free, task);
185     rb_ary_push(rb_tasks, tk);
186   }
187
188   return rb_tasks;
189
190 }