From: coldpeace Date: Thu, 18 Mar 2010 16:23:40 +0000 (+0000) Subject: simple example ruby using simdag (first tests) X-Git-Tag: SVN~469 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/23436dcdc124bde8deb55df96988e7939a39f145?ds=sidebyside simple example ruby using simdag (first tests) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7270 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/bindings/rubyDag/Makefile b/src/bindings/rubyDag/Makefile new file mode 100644 index 0000000000..48d3c0d7b5 --- /dev/null +++ b/src/bindings/rubyDag/Makefile @@ -0,0 +1,157 @@ + +SHELL = /bin/sh + +#### Start of system configuration section. #### + +srcdir = . +topdir = /usr/lib/ruby/1.8/i486-linux +hdrdir = $(topdir) +VPATH = $(srcdir):$(topdir):$(hdrdir) +exec_prefix = $(prefix) +prefix = $(DESTDIR)/usr +sharedstatedir = $(prefix)/com +mandir = $(prefix)/share/man +psdir = $(docdir) +oldincludedir = $(DESTDIR)/usr/include +localedir = $(datarootdir)/locale +bindir = $(exec_prefix)/bin +libexecdir = $(prefix)/lib/ruby1.8 +sitedir = $(DESTDIR)/usr/local/lib/site_ruby +htmldir = $(docdir) +vendorarchdir = $(vendorlibdir)/$(sitearch) +includedir = $(prefix)/include +infodir = $(prefix)/share/info +vendorlibdir = $(vendordir)/$(ruby_version) +sysconfdir = $(DESTDIR)/etc +libdir = $(exec_prefix)/lib +sbindir = $(exec_prefix)/sbin +rubylibdir = $(libdir)/ruby/$(ruby_version) +docdir = $(datarootdir)/doc/$(PACKAGE) +dvidir = $(docdir) +vendordir = $(libdir)/ruby/vendor_ruby +datarootdir = $(prefix)/share +pdfdir = $(docdir) +archdir = $(rubylibdir)/$(arch) +sitearchdir = $(sitelibdir)/$(sitearch) +datadir = $(datarootdir) +localstatedir = $(DESTDIR)/var +sitelibdir = $(sitedir)/$(ruby_version) + +CC = gcc +LIBRUBY = $(LIBRUBY_SO) +LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a +LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME) -I . ../../.libs/libsimgrid.a -I /usr/include/lua5.1/ -ldl -llua5.1 +LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static + +RUBY_EXTCONF_H = +CFLAGS = -fPIC -fno-strict-aliasing -g -g -O2 -fPIC $(cflags) +INCFLAGS = -I. -I$(topdir) -I$(hdrdir) -I$(srcdir) +DEFS = -D_FILE_OFFSET_BITS=64 +CPPFLAGS = $(DEFS) $(cppflags) +CXXFLAGS = $(CFLAGS) +ldflags = -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic +dldflags = +archflag = +DLDFLAGS = $(ldflags) $(dldflags) $(archflag) +LDSHARED = $(CC) -shared +AR = ar +EXEEXT = + +RUBY_INSTALL_NAME = ruby1.8 +RUBY_SO_NAME = ruby1.8 +arch = i486-linux +sitearch = i486-linux +ruby_version = 1.8 +ruby = /usr/bin/ruby1.8 +RUBY = $(ruby) +RM = rm -f +MAKEDIRS = mkdir -p +INSTALL = /usr/bin/install -c +INSTALL_PROG = $(INSTALL) -m 0755 +INSTALL_DATA = $(INSTALL) -m 644 +COPY = cp + +#### End of system configuration section. #### + +preload = + +libpath = . $(libdir) +LIBPATH = -L. -L$(libdir) +DEFFILE = + +CLEANFILES = mkmf.log +DISTCLEANFILES = + +extout = +extout_prefix = +target_prefix = +LOCAL_LIBS = +LIBS = $(LIBRUBYARG_SHARED) -lpthread -lrt -ldl -lcrypt -lm -lc +SRCS = rb_SD_task.c rb_SD_workstation.c rb_simdag.c +OBJS = rb_SD_task.o rb_SD_workstation.o rb_simdag.o +TARGET = dag +DLLIB = $(TARGET).so +EXTSTATIC = +STATIC_LIB = + +BINDIR = $(bindir) +RUBYCOMMONDIR = $(sitedir)$(target_prefix) +RUBYLIBDIR = $(sitelibdir)$(target_prefix) +RUBYARCHDIR = $(sitearchdir)$(target_prefix) + +TARGET_SO = $(DLLIB) +CLEANLIBS = $(TARGET).so $(TARGET).il? $(TARGET).tds $(TARGET).map +CLEANOBJS = *.o *.a *.s[ol] *.pdb *.exp *.bak + +all: $(DLLIB) +static: $(STATIC_LIB) + +clean: + @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) + +distclean: clean + @-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log + @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES) + +realclean: distclean +install: install-so install-rb + +install-so: $(RUBYARCHDIR) +install-so: $(RUBYARCHDIR)/$(DLLIB) +$(RUBYARCHDIR)/$(DLLIB): $(DLLIB) + $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR) +install-rb: pre-install-rb install-rb-default +install-rb-default: pre-install-rb-default +pre-install-rb: Makefile +pre-install-rb-default: Makefile +$(RUBYARCHDIR): + $(MAKEDIRS) $@ + +site-install: site-install-so site-install-rb +site-install-so: install-so +site-install-rb: install-rb + +.SUFFIXES: .c .m .cc .cxx .cpp .C .o + +.cc.o: + $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $< + +.cxx.o: + $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $< + +.cpp.o: + $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $< + +.C.o: + $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $< + +.c.o: + $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) -c $< + +$(DLLIB): $(OBJS) Makefile + @-$(RM) $@ + $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS) + + + +$(OBJS): ruby.h defines.h diff --git a/src/bindings/rubyDag/example.rb b/src/bindings/rubyDag/example.rb new file mode 100644 index 0000000000..c3993a0842 --- /dev/null +++ b/src/bindings/rubyDag/example.rb @@ -0,0 +1,70 @@ +require 'simdag' +include DAG + +# Still using the old method to run : +#>> make ( to generate dag.so) +#>> ruby example.rb ( to run ) +# FIXME if using valgrind , you will get Some 'Invalid Read of Size" When Calling rb_task_schedule ' + + +class Example + + def initialize() + DAG.init(ARGV) + + if (ARGV.length == 1) + DAG.init(ARGV[0]) + else + DAG.createEnvironment("platform.xml") + end + + taskA = DAG::Task.new("Tache A",10) + taskB = DAG::Task.new("Tache B",40) + taskC = DAG::Task.new("Tache C",30) + taskD = DAG::Task.new("Tache D",60) + +# Adding dependency + DAG::Task::addDependency(taskB,taskA) + DAG::Task::addDependency(taskC,taskA) + DAG::Task::addDependency(taskD,taskB) + DAG::Task::addDependency(taskD,taskC) + +# Execution Params + + workstation_number = 2 + workstation_list = DAG::Workstation.getList() + w1 = workstation_list[0] + w2 = workstation_list[1] + + p "workstation 1 : "+ w1.name + p "workstation 2 : "+ w2.name + + computation_amount = Array.new() + communication_amount = Array.new() + + computation_amount << 2000000 << 1000000 + communication_amount << 200000 << 300000 + + rate = -1 + +# Scheduling + + DAG::Task.schedule(taskA,workstation_number,workstation_list,computation_amount,communication_amount,rate) + DAG::Task.schedule(taskB,workstation_number,workstation_list,computation_amount,communication_amount,rate) + DAG::Task.schedule(taskC,workstation_number,workstation_list,computation_amount,communication_amount,rate) + DAG::Task.schedule(taskD,workstation_number,workstation_list,computation_amount,communication_amount,rate) + + changed_tasks = Array.new() + changed_tasks = DAG::Task.simulate(-1.0) + + for i in 0..changed_tasks.size-1 + puts "Task "+ changed_tasks[i].name + " Started at " + changed_tasks[i].startTime.to_s + " has been done at "+ changed_tasks[i].finishTime.to_s + + end + + puts "Everything's Done ... Bye!!" + + end +end + +expl = Example.new \ No newline at end of file diff --git a/src/bindings/rubyDag/extconfig.rb b/src/bindings/rubyDag/extconfig.rb new file mode 100644 index 0000000000..f557573b9d --- /dev/null +++ b/src/bindings/rubyDag/extconfig.rb @@ -0,0 +1,4 @@ +require 'mkmf' +extension_name ='dag' +dir_config(extension_name) +create_makefile(extension_name) diff --git a/src/bindings/rubyDag/platform.xml b/src/bindings/rubyDag/platform.xml new file mode 100644 index 0000000000..3fa9afd94b --- /dev/null +++ b/src/bindings/rubyDag/platform.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/bindings/rubyDag/platform_.xml b/src/bindings/rubyDag/platform_.xml new file mode 100644 index 0000000000..3fa9afd94b --- /dev/null +++ b/src/bindings/rubyDag/platform_.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/bindings/rubyDag/rb_SD_task.c b/src/bindings/rubyDag/rb_SD_task.c new file mode 100644 index 0000000000..b2dadc4709 --- /dev/null +++ b/src/bindings/rubyDag/rb_SD_task.c @@ -0,0 +1,172 @@ +#include "rb_SD_task.h" + +// Free Method +static void SD_task_free(SD_task_t tk) { +// SD_task_destroy(tk); +} + +static void rb_SD_task_destroy(VALUE class,VALUE task) +{ + SD_task_t tk ; + Data_Get_Struct(task, SD_task_t, tk); + SD_task_destroy(tk); +} +// New Method +static VALUE rb_SD_task_new(VALUE class, VALUE name,VALUE amount) +{ + //data Set to NULL + SD_task_t task = SD_task_create(RSTRING(name)->ptr,NULL,NUM2DBL(amount)); + // Wrap m_task_t to a Ruby Value + return Data_Wrap_Struct(class, 0, SD_task_free, task); + +} + +//Get Name +static VALUE rb_SD_task_name(VALUE class,VALUE task) +{ + // Wrap Ruby Value to m_task_t struct + SD_task_t tk; + Data_Get_Struct(task, SD_task_t, tk); + return rb_str_new2(SD_task_get_name(tk)); + +} + +// Schedule Task +static void rb_SD_task_schedule(VALUE class,VALUE task,VALUE workstation_nb,VALUE workstation_list,VALUE computation_amount,VALUE communication_amount,VALUE rate) +{ + // Wrap Ruby Value to m_task_t struct + int i,wrk_nb,comp_nb,comm_nb; + double *comp_amount,*comm_amount; + double rt; + VALUE *ptr_wrk,*ptr_comp,*ptr_comm; + SD_task_t tk; + Data_Get_Struct(task, SD_task_t, tk); + wrk_nb = NUM2INT(workstation_nb); + comp_nb = RARRAY(computation_amount)->len; + comm_nb = RARRAY(communication_amount)->len; + rt = NUM2DBL(rate); + SD_workstation_t *wrk_list; + + ptr_wrk = RARRAY(workstation_list)->ptr; + ptr_comp = RARRAY(computation_amount)->ptr; + ptr_comm = RARRAY(communication_amount)->ptr; + + wrk_list = xbt_new0(SD_workstation_t,wrk_nb); + comp_amount = xbt_new0(double,wrk_nb);//malloc(sizeof(double)*wrk_nb); //xbt_new0(double,wrk_nb); + comm_amount = xbt_new0(double,wrk_nb);//malloc(sizeof(double)*wrk_nb); //xbt_new0(double,wrk_nb); + + // wrk_nb == comp_nb == comm_nb ??? + for (i=0;iptr; + ptr_comp = RARRAY(computation_amount)->ptr; + ptr_comm = RARRAY(communication_amount)->ptr; + + wrk_list = xbt_new0(SD_workstation_t,wrk_nb); + comp_amount = xbt_new0(double,wrk_nb); + comm_amount = xbt_new0(double,wrk_nb); + + for (i=0;i +#include +#include "xbt/sysdep.h" + + +// Free Method +static void SD_task_free(SD_task_t tk); // needed by ruby while wrapping + +//destroy +static void rb_SD_task_destory(VALUE Class,VALUE task); + +// New Method +static VALUE rb_SD_task_new(VALUE Class, VALUE name,VALUE amount); //data set to NULL + +//Get Name +static VALUE rb_SD_task_name(VALUE Class,VALUE task); + +// Schedule Task +static void rb_SD_task_schedule(VALUE Class,VALUE task,VALUE workstation_nb,VALUE workstation_list,VALUE comp_amount,VALUE comm_amount,VALUE rate); + +// unschedule Task +static void rb_SD_task_unschedule(VALUE Class,VALUE task); + +// task dependency Add ( name & data set to NULL) +static void rb_SD_task_add_dependency(VALUE Class,VALUE task_src,VALUE task_dst); + +// task execution time +static VALUE rb_SD_task_execution_time(VALUE Class,VALUE task,VALUE workstation_nb,VALUE workstation_list,VALUE comp_amount,VALUE comm_amount,VALUE rate); + +// task start time +static VALUE rb_SD_task_start_time(VALUE Class,VALUE task); + +// task finish time +static VALUE rb_SD_task_finish_time(VALUE Class,VALUE task); + +// simulation +static VALUE rb_SD_simulate(VALUE Class,VALUE how_long); + +#endif \ No newline at end of file diff --git a/src/bindings/rubyDag/rb_SD_workstation.c b/src/bindings/rubyDag/rb_SD_workstation.c new file mode 100644 index 0000000000..2be055d568 --- /dev/null +++ b/src/bindings/rubyDag/rb_SD_workstation.c @@ -0,0 +1,38 @@ +#include "rb_SD_workstation.h" + +static void SD_workstation_free(SD_workstation_t wrk) +{ + //NOTHING TO DO +} +// Workstation list +static VALUE rb_SD_workstation_list(VALUE class) +{ + + int i,nb; + nb = SD_workstation_get_number(); + VALUE workstation_list = rb_ary_new2(nb); + for (i=0;i +#include + +// free +static void SD_workstation_free(SD_workstation_t); + +// Workstation List +static VALUE rb_SD_workstation_list(VALUE Class); + +// Workstation number +static VALUE rb_SD_workstation_number(VALUE Class); + +// Workstation name +static VALUE rb_SD_workstation_name(VALUE Class,VALUE workstation); + +#endif \ No newline at end of file diff --git a/src/bindings/rubyDag/rb_simdag.c b/src/bindings/rubyDag/rb_simdag.c new file mode 100644 index 0000000000..55aff59c3e --- /dev/null +++ b/src/bindings/rubyDag/rb_simdag.c @@ -0,0 +1,126 @@ +/* + * $Id$ + * + * Copyright 2010 Simgrid Team + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + */ + +#include "rb_SD_task.c" +#include "rb_SD_workstation.c" +#include +#define MY_DEBUG + +// XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby); + +// MSG Module +VALUE rb_dag; +// MSG Classes +VALUE rb_task; +VALUE rb_workstation; + +//Init Msg_Init From Ruby +static void sd_init(VALUE Class,VALUE args) +{ + char **argv=NULL; + const char *tmp; + int argc,type,i; + VALUE *ptr; + type = TYPE(args); + if ( type != T_ARRAY ) + { + rb_raise(rb_eRuntimeError,"Bad Argument to SD_init"); + return; + } + ptr = RARRAY(args)->ptr; + argc= RARRAY(args)->len; + argc++; + argv= xbt_new0(char*,argc); + argv[0] = strdup("ruby"); + for (i=0;iptr; + argv[i+1] = strdup(tmp); + } + SD_init(&argc,argv); + free(argv); +// INFO0("SD Init...Done"); + printf("SD Init...Done\n"); + return; + +} + +//Init Msg_Run From Ruby +static void sd_reinit(VALUE class) +{ + SD_application_reinit(); +} + +//deploy Application +static void sd_createEnvironment(VALUE class,VALUE plateformFile ) +{ + int type = TYPE(plateformFile); + if ( type != T_STRING ) + rb_raise(rb_eRuntimeError,"Bad Argument's Type"); + const char * platform = RSTRING(plateformFile)->ptr; + SD_create_environment(platform); + printf("Create Environment...Done\n"); + + return; +} + + +// INFO +static void sd_info(VALUE class,VALUE msg) +{ + const char *s = RSTRING(msg)->ptr; +// INFO1("%s",s); + printf("%s\n",s); +} + +// Get Clock +static VALUE sd_get_clock(VALUE class) +{ + return rb_float_new(SD_get_clock()); +} + +void Init_dag() +{ + // Modules + rb_dag = rb_define_module("DAG"); + + // Classes + //Associated Environment Methods! + rb_define_method(rb_dag,"init",sd_init,1); + rb_define_method(rb_dag,"reinit",sd_reinit,0); + rb_define_method(rb_dag,"createEnvironment",sd_createEnvironment,1); + rb_define_method(rb_dag,"info",sd_info,1); + rb_define_method(rb_dag,"getClock",sd_get_clock,0); + + //Classes + rb_task = rb_define_class_under(rb_dag,"SdTask",rb_cObject); + rb_workstation = rb_define_class_under(rb_dag,"SdWorkstation",rb_cObject); +// TODO Link Class + + //Task Methods + rb_define_module_function(rb_task,"new",rb_SD_task_new,2); +// rb_define_module_function(rb_task,"free",rb_SD_task_destroy,1); + rb_define_module_function(rb_task,"name",rb_SD_task_name,1); + rb_define_module_function(rb_task,"schedule",rb_SD_task_schedule,6); + rb_define_module_function(rb_task,"unschedule",rb_SD_task_unschedule,1); + rb_define_module_function(rb_task,"addDependency",rb_SD_task_add_dependency,2); + rb_define_module_function(rb_task,"executionTime",rb_SD_task_execution_time,6); + rb_define_module_function(rb_task,"startTime",rb_SD_task_start_time,1); + rb_define_module_function(rb_task,"finishTime",rb_SD_task_finish_time,1); + rb_define_module_function(rb_task,"simulate",rb_SD_simulate,1); + + //Worstation Methods + rb_define_module_function(rb_workstation,"getList",rb_SD_workstation_list,0); + rb_define_module_function(rb_workstation,"getNumber",rb_SD_workstation_number,0); + rb_define_module_function(rb_workstation,"name",rb_SD_workstation_name,1); +} diff --git a/src/bindings/rubyDag/simdag.rb b/src/bindings/rubyDag/simdag.rb new file mode 100644 index 0000000000..a4bcccac07 --- /dev/null +++ b/src/bindings/rubyDag/simdag.rb @@ -0,0 +1,39 @@ +require 'dag' + +#=========================== +# Task Class +# ========================== +class DAG::Task < DAG::SdTask + + def initialize(*args) + super(args) + end + + def name() + super(self) + end + + def startTime() + super(self) + end + + def finishTime() + super(self) + end + +end + +#============================= +# WorkStation Class +#============================= +class DAG::Workstation < DAG::SdWorkstation + + def initialize() + super() + end + + def name() + super(self) + end + +end \ No newline at end of file