Logo AND Algorithmique Numérique Distribuée

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