Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d3f75d924990cca1eac59797a74d141e67068119
[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);//malloc(sizeof(double)*wrk_nb); //xbt_new0(double,wrk_nb);
56   comm_amount = xbt_new0(double,wrk_nb);//malloc(sizeof(double)*wrk_nb); //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   
72   SD_task_schedule(tk,wrk_nb,wrk_list,comp_amount,comm_amount,rt);
73   
74   xbt_free(wrk_list);
75   // FIXME, used later if wanna calculate start & fininsh time for a task
76   free(comp_amount);
77   free(comm_amount);
78   
79
80 }
81
82 //unschedule
83 static void rb_SD_task_unschedule(VALUE class,VALUE task)
84 {
85   SD_task_t tk;
86   Data_Get_Struct(task,SD_task_t,tk);
87   SD_task_unschedule (tk);
88   
89 }
90
91 // task dependency add
92 static void rb_SD_task_add_dependency(VALUE Class,VALUE task_src,VALUE task_dst)
93 {
94   SD_task_t tk_src,tk_dst;
95   Data_Get_Struct(task_src, SD_task_t ,tk_src);
96   Data_Get_Struct(task_dst, SD_task_t ,tk_dst);
97   SD_task_dependency_add(NULL,NULL,tk_src,tk_dst);
98   
99 }
100
101 //task execution time
102 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)
103 {
104   
105   int i,wrk_nb;
106   double *comp_amount,*comm_amount;
107   double rt;
108   VALUE *ptr_wrk,*ptr_comp,*ptr_comm;
109   SD_task_t tk;
110   Data_Get_Struct(task, SD_task_t, tk);
111   wrk_nb = NUM2INT(workstation_nb);
112   rt = NUM2DBL(rate);
113   SD_workstation_t *wrk_list;
114   
115   ptr_wrk = RARRAY(workstation_list)->ptr;
116   ptr_comp = RARRAY(computation_amount)->ptr;
117   ptr_comm = RARRAY(communication_amount)->ptr;
118   
119   wrk_list = xbt_new0(SD_workstation_t,wrk_nb);
120   comp_amount = xbt_new0(double,wrk_nb);
121   comm_amount = xbt_new0(double,wrk_nb);
122   
123   for (i=0;i<wrk_nb;i++)
124   {
125     Data_Get_Struct(ptr_wrk[i],SD_workstation_t,wrk_list[i]); 
126     comp_amount[i] = NUM2DBL(ptr_comp[i]);
127     comm_amount[i] = NUM2DBL(ptr_comm[i]);
128   }
129   
130   return rb_float_new(SD_task_get_execution_time (tk,wrk_nb,wrk_list,comp_amount,comm_amount,rt));
131   
132 }
133
134 //task start time
135 static VALUE rb_SD_task_start_time(VALUE class,VALUE task)
136 {
137
138   SD_task_t tk;
139   Data_Get_Struct(task,SD_task_t,tk);
140   double time=SD_task_get_start_time(tk);
141   return rb_float_new(time);
142   
143 }
144
145 //task fininsh time
146 static VALUE rb_SD_task_finish_time(VALUE class,VALUE task)
147 {
148   SD_task_t tk;
149   Data_Get_Struct(task,SD_task_t,tk);
150   double time=SD_task_get_finish_time(tk);
151   return rb_float_new(time);
152   
153 }
154
155 // Simulate : return a SD_task
156 static VALUE rb_SD_simulate(VALUE class,VALUE how_long)
157 {
158   int i;
159   double hl = NUM2DBL(how_long);
160   SD_task_t * tasks = SD_simulate(hl); 
161   VALUE rb_tasks = rb_ary_new();
162   for (i = 0; tasks[i] != NULL; i++)
163     {
164       VALUE tk = Qnil;
165       tk = Data_Wrap_Struct(class, 0, SD_task_free, tasks[i]);
166       rb_ary_push(rb_tasks,tk);
167       
168     }
169   return  rb_tasks;
170  
171 }