Logo AND Algorithmique Numérique Distribuée

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