Logo AND Algorithmique Numérique Distribuée

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