Logo AND Algorithmique Numérique Distribuée

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