1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 //#include "ruby_simdag.h"
8 #include "rb_SD_task.h"
11 static void SD_task_free(SD_task_t tk)
13 // SD_task_destroy(tk);
16 static void rb_SD_task_destroy(VALUE class, VALUE task)
19 Data_Get_Struct(task, SD_task_t, tk);
24 static VALUE rb_SD_task_new(VALUE class, VALUE name, VALUE amount)
28 SD_task_create(RSTRING(name)->ptr, NULL, NUM2DBL(amount));
29 // Wrap m_task_t to a Ruby Value
30 return Data_Wrap_Struct(class, 0, SD_task_free, task);
35 static VALUE rb_SD_task_name(VALUE class, VALUE task)
37 // Wrap Ruby Value to m_task_t struct
39 Data_Get_Struct(task, SD_task_t, tk);
40 return rb_str_new2(SD_task_get_name(tk));
45 static void rb_SD_task_schedule(VALUE class, VALUE task,
47 VALUE workstation_list,
48 VALUE computation_amount,
49 VALUE communication_amount, VALUE rate)
51 // Wrap Ruby Value to m_task_t struct
52 int i, wrk_nb, comp_nb, comm_nb;
53 double *comp_amount, *comm_amount;
55 VALUE *ptr_wrk, *ptr_comp, *ptr_comm;
57 Data_Get_Struct(task, SD_task_t, tk);
58 wrk_nb = NUM2INT(workstation_nb);
59 comp_nb = RARRAY(computation_amount)->len;
60 comm_nb = RARRAY(communication_amount)->len;
62 SD_workstation_t *wrk_list;
64 ptr_wrk = RARRAY(workstation_list)->ptr;
65 ptr_comp = RARRAY(computation_amount)->ptr;
66 ptr_comm = RARRAY(communication_amount)->ptr;
68 wrk_list = xbt_new0(SD_workstation_t, wrk_nb);
69 comp_amount = xbt_new0(double, wrk_nb);
70 comm_amount = xbt_new0(double, wrk_nb);
72 // wrk_nb == comp_nb == comm_nb ???
73 for (i = 0; i < wrk_nb; i++) {
74 Data_Get_Struct(ptr_wrk[i], SD_workstation_t, wrk_list[i]);
75 comp_amount[i] = NUM2DBL(ptr_comp[i]);
76 comm_amount[i] = NUM2DBL(ptr_comm[i]);
78 /*for (i=0;i<comp_nb;i++)
79 comp_amount[i] = NUM2DBL(ptr_comp[i]);
81 for (i=0;i<comm_nb;i++)
82 comm_amount[i] = NUM2DBL(ptr_comm[i]); */
84 SD_task_schedule(tk, wrk_nb, wrk_list, comp_amount, comm_amount, rt);
87 xbt_free(comp_amount);
88 xbt_free(comm_amount);
94 static void rb_SD_task_unschedule(VALUE class, VALUE task)
97 Data_Get_Struct(task, SD_task_t, tk);
98 SD_task_unschedule(tk);
102 // task dependency add
103 static void rb_SD_task_add_dependency(VALUE Class, VALUE task_src,
106 SD_task_t tk_src, tk_dst;
107 Data_Get_Struct(task_src, SD_task_t, tk_src);
108 Data_Get_Struct(task_dst, SD_task_t, tk_dst);
109 SD_task_dependency_add(NULL, NULL, tk_src, tk_dst);
113 //task execution time
114 static VALUE rb_SD_task_execution_time(VALUE class, VALUE task,
115 VALUE workstation_nb,
116 VALUE workstation_list,
117 VALUE computation_amount,
118 VALUE communication_amount,
123 double *comp_amount, *comm_amount;
125 VALUE *ptr_wrk, *ptr_comp, *ptr_comm;
127 Data_Get_Struct(task, SD_task_t, tk);
128 wrk_nb = NUM2INT(workstation_nb);
130 SD_workstation_t *wrk_list;
132 ptr_wrk = RARRAY(workstation_list)->ptr;
133 ptr_comp = RARRAY(computation_amount)->ptr;
134 ptr_comm = RARRAY(communication_amount)->ptr;
136 wrk_list = xbt_new0(SD_workstation_t, wrk_nb);
137 comp_amount = xbt_new0(double, wrk_nb);
138 comm_amount = xbt_new0(double, wrk_nb);
140 for (i = 0; i < wrk_nb; i++) {
141 Data_Get_Struct(ptr_wrk[i], SD_workstation_t, wrk_list[i]);
142 comp_amount[i] = NUM2DBL(ptr_comp[i]);
143 comm_amount[i] = NUM2DBL(ptr_comm[i]);
147 rb_float_new(SD_task_get_execution_time
148 (tk, wrk_nb, wrk_list, comp_amount, comm_amount, rt));
153 static VALUE rb_SD_task_start_time(VALUE class, VALUE task)
157 Data_Get_Struct(task, SD_task_t, tk);
158 double time = SD_task_get_start_time(tk);
159 return rb_float_new(time);
164 static VALUE rb_SD_task_finish_time(VALUE class, VALUE task)
167 Data_Get_Struct(task, SD_task_t, tk);
168 double time = SD_task_get_finish_time(tk);
169 return rb_float_new(time);
173 // Simulate : return a SD_task
174 static VALUE rb_SD_simulate(VALUE class, VALUE how_long)
178 double hl = NUM2DBL(how_long);
179 xbt_dynar_t simulated_tasks = SD_simulate(hl);
181 VALUE rb_tasks = rb_ary_new();
182 xbt_dynar_foreach(simulated_tasks, i, task) {
184 tk = Data_Wrap_Struct(class, 0, SD_task_free, task);
185 rb_ary_push(rb_tasks, tk);