From: mquinson Date: Tue, 2 Mar 2010 00:29:22 +0000 (+0000) Subject: Massive cleanups in ruby. Not yet working (segfault on task reception) X-Git-Tag: SVN~582 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/b3d9680ac65d8bdd8e267aed69fc32a5abab12d7 Massive cleanups in ruby. Not yet working (segfault on task reception) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7156 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/Makefile.am b/src/Makefile.am index dca45a9ebd..df5709a9c9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -327,7 +327,13 @@ endif ### Declare the library content ### -lib_LTLIBRARIES= libsimgrid.la libgras.la libsmpi.la +if HAVE_RUBY + libruby=simgrid_ruby.la +else + libruby= +endif + +lib_LTLIBRARIES= libsimgrid.la libgras.la libsmpi.la $(libruby) gras_sources=$(XBT_SRC) $(GRAS_COMMON_SRC) $(GRAS_RL_SRC) $(AMOK_SRC) simgrid_sources=$(XBT_SRC) $(SURF_SRC) $(GTNETS_USED) \ @@ -347,6 +353,12 @@ libsmpi_la_LIBADD = libsimgrid.la libsmpi_la_SOURCES = $(SMPI_SRC) libsmpi_la_LDFLAGS = $(VERSION_INFO) @SMPI_DEP@ @LD_DYNAMIC_FLAGS@ -lm +if HAVE_RUBY +simgrid_ruby_la_LIBADD = libsimgrid.la +simgrid_ruby_la_SOURCES = bindings/ruby/simgrid_ruby.c +simgrid_ruby_la_LDFLAGS = $(VERSION_INFO) @LD_DYNAMIC_FLAGS@ -lm -module +endif + # Windows users (at least, MSVC ones) need a def file. if IS_WINDOWS libgras_la_LDFLAGS += -Wl,--output-def,libgras.def @@ -387,7 +399,8 @@ endif RUBY_SRC= simix/smx_context_ruby.c \ bindings/ruby/rb_msg_process.c \ bindings/ruby/rb_msg_host.c \ - bindings/ruby/rb_msg_task.c + bindings/ruby/rb_msg_task.c \ + bindings/ruby/rb_application_handler.c if HAVE_RUBY simgrid_sources += $(RUBY_SRC) else diff --git a/src/bindings/ruby/ApplicationHandler.rb b/src/bindings/ruby/ApplicationHandler.rb index b33578257a..953f738fff 100644 --- a/src/bindings/ruby/ApplicationHandler.rb +++ b/src/bindings/ruby/ApplicationHandler.rb @@ -1,84 +1,9 @@ # -# * $Id$ -# * -# * Copyright 2010 Martin Quinson, Mehdi Fekari -# * 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. -require 'ProcessFactory' -$DEBUG = false -class ApplicationHandler - - @processFactory -# Initialize - def initialize() - #Nothing todo - end - - # onStartDocument - def onStartDocument() - - @processFactory = ProcessFactory.new - if ($DEBUG) - puts "onStartDocument" - end - - end - -# onBeginProcess - def onBeginProcess(hostName,function) - - @processFactory.setProcessIdentity(hostName,function) - - if ($DEBUG) - puts "onBeginProcess" - end - - end - -# onProperty - def onProperty(id,value) - - @processFactory.setProperty(id,value) - - if ($DEBUG) - puts "onProperty" - end - - end - -# RegisterProcessArg - def onProcessArg(arg) - - @processFactory.registerProcessArg(arg) - - if ($DEBUG) - puts "onProcessArg" - end - - end - -# OnEndProcess - def onEndProcess() - - @processFactory.createProcess() - - if ($DEBUG) - puts "onEndProcess" - end - - end +# Copyright 2010. The 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. - # onEndDocument - def onEndDocument() -# Euh...Actually Nothin' to Do !! - - if($DEBUG) - puts "onEndDocument" - end - end - - # End Class - end \ No newline at end of file +require 'simgrid' +include MSG +$DEBUG = false diff --git a/src/bindings/ruby/Makefile b/src/bindings/ruby/Makefile deleted file mode 100644 index 90b9c7bc08..0000000000 --- a/src/bindings/ruby/Makefile +++ /dev/null @@ -1,157 +0,0 @@ - -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 -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) -I../../../include -I../../include -I../.. -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_application_handler.c rb_msg_task.c rb_msg_process.c rb_msg_host.c rb_msg.c -OBJS = rb_application_handler.o rb_msg_task.o rb_msg_process.o rb_msg_host.o rb_msg.o -TARGET = msg -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/ruby/Master.rb b/src/bindings/ruby/Master.rb deleted file mode 100644 index eafe15d406..0000000000 --- a/src/bindings/ruby/Master.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'msg' -require 'RubyProcess' - -include MSG - -class Master < RbProcess - def initialize2() - super() - end - - - #for Testing - def msg_main2(args) - info("Hello From Master") - end - - - # msg_main : that function that will be executed when Running Simulation - def msg_main(args) # args is an array Containin' arguments for function master - info("Hello From Master") - size = args.size - info ("Number of Args for Master = " + size.to_s) - for i in 0..size-1 - info(args[i]) - end - - raise "Master needs 3 arguments" if size < 3 - numberOfTask = Integer(args[0]) - taskComputeSize = Float(args[1]) - taskCommunicationSize = Float(args[2]) - slaveCount = Integer(args[3]) - - #Creating & Sending Task - for i in 0..numberOfTask-1 - - - task = RbTask.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize ); - s_alias = "slave>>" + (i%slaveCount).to_s - info("Master Sending "+ RbTask.name(task) + " to " + s_alias + " with Comput Size " + RbTask.compSize(task).to_s) - RbTask.send(task,s_alias) - info("Master Done Sending " +RbTask.name(task) + " to " + s_alias) -# sameTask = RbTask.receive(s_alias) -# puts "Master Receiving its Own Task" - end - - # Sending Finalize Tasks - info ("Master: All tasks have been dispatched. Let's tell everybody the computation is over.") - for i in 0..slaveCount-1 - s_alias = "slave " + i.to_s - info ("Master Sending Finalize to " + s_alias) - RbTask.send(RbTask.new("finalize",0,0),s_alias) - end - info("Master : Everything's Done") - end - -end \ No newline at end of file diff --git a/src/bindings/ruby/MasterSlave.rb b/src/bindings/ruby/MasterSlave.rb index 307f865559..9b1cbe64ea 100644 --- a/src/bindings/ruby/MasterSlave.rb +++ b/src/bindings/ruby/MasterSlave.rb @@ -1,20 +1,102 @@ -require 'msg' -require 'RubyProcess' -require 'Master' -require 'Slave' +require 'simgrid' +include MSG -# include MSG +################################################# +# Class Master +################################################# -raise "Bad Number Of Arguments" if ARGV.length != 2 +class Master < RbProcess + def initialize2() + super() + end + + + # msg_main : that function that will be executed when Running Simulation + def msg_main(args) # args is an array containing arguments for function master + info("Hello From Master") + size = args.size + info ("Number of Args for Master = " + size.to_s) + for i in 0..size-1 + info("args["+String(i)+"]="+args[i]) + end + + raise "Master needs 3 arguments" if size < 3 + numberOfTask = Integer(args[0]) + taskComputeSize = Float(args[1]) + taskCommunicationSize = Float(args[2]) + slaveCount = Integer(args[3]) + + #Creating & Sending Task + for i in 0..numberOfTask-1 + + + task = RbTask.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize ); + s_alias = "slave>>" + (i%slaveCount).to_s + info("Master Sending "+ RbTask.name(task) + " to " + s_alias + " with Comput Size " + RbTask.compSize(task).to_s) + RbTask.send(task,s_alias) + info("Master Done Sending " +RbTask.name(task) + " to " + s_alias) +# sameTask = RbTask.receive(s_alias) +# puts "Master Receiving its Own Task" + end + + # Sending Finalize Tasks + info ("Master: All tasks have been dispatched. Let's tell everybody the computation is over.") + for i in 0..slaveCount-1 + s_alias = "slave " + i.to_s + info ("Master Sending Finalize to " + s_alias) + RbTask.send(RbTask.new("finalize",0,0),s_alias) + end + info("Master : Everything's Done") + end +end + +################################################# +# Class Slave +################################################# +class Slave < RbProcess + + def initialize() + super() + end + + # msg_main : that function that will be executed when Running Simulation + def msg_main(args) + info("Hello From Slave") + s_mailbox = "slave>>" + args[0] + + while true + + info("Ready to Receive Task") + task = RbTask.receive(s_mailbox) + task_name = RbTask.name(task) + info ("Task Received : " + task.name) + if (task_name == "finalize") + info("Slave" + s_mailbox + "got finalize msg") + break + end + info("Slave " + s_mailbox + " ...Processing" + RbTask.name(task)) + RbTask.execute(task) + end + info("Slave " + s_mailbox + "I'm Done , See You !!") + end + + end + + +################################################# +# main chunck +################################################# + +if (ARGV.length == 2) + MSG.createEnvironment(ARGV[0]) + MSG.deployApplication(ARGV[1]) +else + MSG.createEnvironment("platform.xml") + MSG.deployApplication("deploy.xml") + Thread.list.each {|t| p t} +end -MSG.init(ARGV) -# Thread.list.each {|t| p t} -raise "Bad Number Of Arguments" if (ARGV.length < 2) -MSG.createEnvironment(ARGV[0]) -# Thread.list.each {|t| p t} -MSG.deployApplication(ARGV[1]) # Thread.list.each {|t| p t} MSG.run() -# Thread.list.each {|t| p t} MSG.getClock() # exit() diff --git a/src/bindings/ruby/Master_old.rb b/src/bindings/ruby/Master_old.rb deleted file mode 100644 index 95e38a8c43..0000000000 --- a/src/bindings/ruby/Master_old.rb +++ /dev/null @@ -1,58 +0,0 @@ -require 'msg' -require 'RubyProcess' - -include MSG - -class Master < RbProcess - def initialize() - super() - end - - def msg_main2(args) - info("Hello From Master") - end - - # msg_main : that function that will be executed when Running Simulation - def msg_main(args) # args is an array Containin' arguments for function master - info("Hello From Master") - size = args.size - puts "Number of Args for Master = " + size.to_s - for i in 0..size-1 - puts args[i] - end - - raise "Master needs 3 arguments" if size < 3 - channel = 0 - numberOfTask = Integer(args[0]) - taskComputeSize = Float(args[1]) - taskCommunicationSize = Float(args[2]) - slaveCount = args.size - 3 - - slaves = Array.new - todo = Array.new - - - #Creating & Sending Task - for i in 0..numberOfTask-1 - - todo[i] = RbTask.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize ); - - end - - for i in 3..args.size-1 - - slaves[i-3] = RbHost.getByName(args[i]) - end - - - for i in 0..numberOfTask-1 - p "Sending ( Put ) Task .." + i.to_s+ " to Slave " + RbHost.name(slaves[i%slaveCount]) - RbTask.put(todo[i],slaves[i%slaveCount]) - end - - # Sending Finalize Tasks - p "Sending Finalize Task Later !!" - puts "Master : Everything's Done" - end - -end \ No newline at end of file diff --git a/src/bindings/ruby/ProcessFactory.rb b/src/bindings/ruby/ProcessFactory.rb deleted file mode 100644 index d31365f86b..0000000000 --- a/src/bindings/ruby/ProcessFactory.rb +++ /dev/null @@ -1,71 +0,0 @@ -# -# * $Id$ -# * -# * Copyright 2010 Martin Quinson, Mehdi Fekari -# * 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. -require 'RubyProcess' -require 'RubyHost' -class ProcessFactory - -# Attributes - attr_accessor :args, :proprieties, :hostName, :function -# Initlialize - def initialize() - - @args = Array.new - @proprieties = Hash.new - @hostName = nil - @function = nil - - end - -# setProcessIdentity - def setProcessIdentity(hostName,function) - @hostName = hostName - @function = function - - if !args.empty? - args.clear - end - - if !proprieties.empty? - proprieties.clear - end - - end - -# RegisterProcess - def registerProcessArg(arg) - - @args.push(arg) - - end - -# CreateProcess - def createProcess() - - process = rubyNewInstance(@function) #process = rubyNewInstanceArgs(@function,@args) # - size = @args.size - for i in 0..size-1 - process.pargs.push(@args[i]) - end - process.name = @function - process.id = process.nextId() # This increment Automaticly The Static ProcessNextId for The Class RbProces - host = RbHost.getByName(@hostName) - processCreate(process,host) - process.proprieties = @proprieties - @proprieties = Hash.new - - end - -# SetProperty - def setProperty(id,value) - @proprieties[id] = value - end - -# End Class - end \ No newline at end of file diff --git a/src/bindings/ruby/RubyHost.rb b/src/bindings/ruby/RubyHost.rb deleted file mode 100644 index babdecb2af..0000000000 --- a/src/bindings/ruby/RubyHost.rb +++ /dev/null @@ -1,24 +0,0 @@ -# -# * $Id$ -# * -# * Copyright 2010 Martin Quinson, Mehdi Fekari -# * 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. - -require 'msg' -include MSG - -class RbHost < Host -# Attributes - attr_accessor :bind, :data - -# Initialize - def initialize() - @bind = 0 - @data = nil - end - -end \ No newline at end of file diff --git a/src/bindings/ruby/RubyProcess.rb b/src/bindings/ruby/RubyProcess.rb deleted file mode 100644 index 1272a0e3f6..0000000000 --- a/src/bindings/ruby/RubyProcess.rb +++ /dev/null @@ -1,217 +0,0 @@ -# -# * $Id$ -# * -# * Copyright 2010 Martin Quinson, Mehdi Fekari -# * 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. - -require 'msg' -require 'Semaphore' -include MSG -$DEBUG = true # This is a Global Variable Useful for Debugging - -class RbProcess < Thread - @@nextProcessId = 0 -# Attributes - attr_accessor :bind, :id, :proprieties, :name, - :pargs, :schedBegin, :schedEnd, :mutex, :cv - -# Initialize : USED in ApplicationHandler to Initialize it - def initialize(*args) - - argc = args.size -# Default initializer - if argc == 0 #>>> new() - super() { - @id = 0 - @bind = 0 - @name = "" - @pargs = Array.new() - init_var() - start() - if $DEBUG - puts "Init Default Initialzer...Nothing to do...Bye" - end - } - end - - # Init with 2 arguments **********************>>>(HostName,Name) Or (Host , Name) - if argc == 2 - super(){ - type = args[0].type() - if ( type.to_s == "String") - host = Host.getByName(args[0]) - end - if ( type.to_s == "MSG::Host") - host = args[0] - end - if $DEBUG - puts host - end - raise "Process Name Cannot Be Null" if args[1].empty? - @name = args[1] # First Arg - if $DEBUG - puts @name - end - @pargs = Array.new() # No Args[] Passed in Arguments - @@nextProcessId += 1 - @id = @@nextProcessId - init_var() - start() - createProcess(self,host) - if $DEBUG - puts "Initilize with 2 args" - end - } - - end - - # Init with 3 arguments **********************(hostName,Name,args[]) or # (Host,Name,args[]) - - if argc == 3 - super(){ - type = args[0].type() - if( type.to_s == "String") - host =Host.getByName(args[0]) - end - if ( type.to_s == "MSG::Host" ) - host = args[0] - end - if $DEBUG - puts host - end - - raise "Process Name Cannot Be Null" if args[0].empty? - @name = args[1] - type = args[2].type() - raise "Third Argument Should be an Array" if type != "Array" - @pargs = args[3] - @@nextProcessId +=1 - @id = @@nextProcessId - init_var() - createProcess(self,host) - - if $DEBUG - puts "Initilize with 3 args" - end - -# sleep #keep the thread running - } - end - end - - # Init_var Called By Initialize - def init_var() - @proprieties = Hash.new() - @mutex = Mutex.new - @cv = ConditionVariable.new - # Process Synchronization Tools - @schedBegin = Semaphore.new(0) - @schedEnd = Semaphore.new(0) - end - - #main - def msg_main(args) - # To Be Implemented within The Process... - # The Main Code of The Process to be Executed ... - end - - # Start : To keep the Process Alive and waitin' via semaphore - def start() - @schedBegin.acquire(@mutex,@cv) - #execute The Main Code of The Process ( Example Master ; Slave ...) - msg_main(@pargs) - processExit(self) #Exite the Native Process - @schedEnd.release(@mutex,@cv) - end - -# NetxId - def nextId () - @@nextProcessId +=1 - return @@nextProcessId - end - - if $DEBUG - #Process List - def processList() - Thread.list.each {|t| p t} - end - end - - #Get Own ID - def getID() - return @id - end - - # set Id - def setID(id) - @id = id - end - - #Get a Process ID - def processID(process) - return process.id - end - - #Get Own Name - def getName() - return @name - end - - #Get a Process Name - def processName(process) - return process.name - end - - #Get Bind - def getBind() - return @bind - end - - #Get Binds - def setBind(bind) - @bind = bind - end - - def unschedule() - - @schedEnd.release(@mutex,@cv) -# info("@schedEnd.release(@mutex,@cv)") - @schedBegin.acquire(@mutex,@cv) -# info("@schedBegin.acquire(@mutex,@cv)") - - end - - def schedule() - @schedBegin.release(@mutex,@cv) - @schedEnd.acquire(@mutex,@cv) - end - - #C Simualateur Process Equivalent Management - # After Binding Ruby Process to C Process - -# pause - def pause() - processSuspend(self) - end - -# restart - def restart() - processResume(self) - end - -# isSuspended - def isSuspended() - processIsSuspended(self) - end - -# getHost - def getHost() - processGetHost(self) - end - -# The Rest of Methods !!! To be Continued ... -end \ No newline at end of file diff --git a/src/bindings/ruby/RubySem.rb b/src/bindings/ruby/RubySem.rb deleted file mode 100644 index 2860167511..0000000000 --- a/src/bindings/ruby/RubySem.rb +++ /dev/null @@ -1,52 +0,0 @@ -class Sem - - - attr_accessor :permits, :mutex, :cv - - Thread.abort_on_exception = true - - - -# Initialize - def initialize ( permits ) - - @permits = permits - @mutex = Mutex.new - @cv = ConditionVariable.new - - end - - - -# Aquire - def acquire() - - if(!Thread.current.alive?) #Thread.interrupted in Java - raise "Exception : Thread Interrupted" - end - - mutex.synchronize { - - while @permits <= 0 - - @cv.wait(mutex) #or Thread.stop ???!! - - end - - @permits -=1 - } - - end - - - def release() - - mutex.synchronize { - - @permits +=1 - @cv.signal #Notify ??!! - - } - - -end \ No newline at end of file diff --git a/src/bindings/ruby/RubyTask.rb b/src/bindings/ruby/RubyTask.rb deleted file mode 100644 index 9afbf10ef6..0000000000 --- a/src/bindings/ruby/RubyTask.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# * $Id$ -# * -# * Copyright 2010 Martin Quinson, Mehdi Fekari -# * 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. -require 'msg' -include MSG - -class RbTask < Task - - -# Attributes - attr_accessor :bind - - -# Initialize - def initialize(name,comp_size,comm_size) -# @bind = 10 - super(name,comp_size,comm_size) - end - - -end \ No newline at end of file diff --git a/src/bindings/ruby/Semaphore.rb b/src/bindings/ruby/Semaphore.rb deleted file mode 100644 index e087294f98..0000000000 --- a/src/bindings/ruby/Semaphore.rb +++ /dev/null @@ -1,51 +0,0 @@ -# -# * $Id$ -# * -# * Copyright 2010 Martin Quinson, Mehdi Fekari -# * 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. -require 'msg' -require 'thread' -class Semaphore - - Thread.abort_on_exception = true - attr_accessor :permits - - - def initialize ( permits ) - - @permits = permits - - end - - - def acquire(mutex,cv) - - raise "Interrupted Thread " if (!Thread.current.alive?) - mutex.synchronize { - while @permits <= 0 - - cv.wait(mutex) - - end - - @permits = @permits - 1 - cv.signal - - } - - end - - def release(mutex,cv) - mutex.synchronize{ - - @permits += 1 - cv.signal - - } - end - -end \ No newline at end of file diff --git a/src/bindings/ruby/Slave.rb b/src/bindings/ruby/Slave.rb deleted file mode 100644 index e85da1f0ac..0000000000 --- a/src/bindings/ruby/Slave.rb +++ /dev/null @@ -1,42 +0,0 @@ -require 'msg' -require 'RubyProcess' -require 'RubyTask' -include MSG - -class Slave < RbProcess - - def initialize() - super() - end - - #for Testing - def msg_main2(args) - info("Hello From Slave") - end - - - - - - # msg_main : that function that will be executed when Running Simulation - def msg_main(args) - info("Hello From Slave") - s_mailbox = "slave>>" + args[0] - - while true - - info("Ready to Receive Task") - task = RbTask.receive(s_mailbox) - task_name = RbTask.name(task) - info ("Task Received : " + task_name) - if (task_name == "finalize") - info("Slave" + s_mailbox + "got finalize msg") - break - end - info("Slave " + s_mailbox + " ...Processing" + RbTask.name(task)) - RbTask.execute(task) - end - info("Slave " + s_mailbox + "I'm Done , See You !!") - end - - end diff --git a/src/bindings/ruby/Slave_old.rb b/src/bindings/ruby/Slave_old.rb deleted file mode 100644 index fc2e061211..0000000000 --- a/src/bindings/ruby/Slave_old.rb +++ /dev/null @@ -1,37 +0,0 @@ -require 'msg' -require 'RubyProcess' -require 'RubyTask' -include MSG - -class Slave < RbProcess - - def initialize() - super() - end - - def msg_main(args) - info("Hello From Slave") - end - - # msg_main : that function that will be executed when Running Simulation - def msg_main(args) - info("Hello From Slave") - while true - - task = RbTask.get(); #by default Port set to 0 - task_name = RbTask.name(task) - puts "my name ........" + task_name - if ( task_name == "finalize" ) - puts "Received finalize msg" - break - end - puts "Processing..." + RbTask.name(task) - RbTask.execute(task) - end - puts "I'm Done ... See You !!" - break - end - - - end - \ No newline at end of file diff --git a/src/bindings/ruby/client.rb b/src/bindings/ruby/client.rb deleted file mode 100644 index 96d729058c..0000000000 --- a/src/bindings/ruby/client.rb +++ /dev/null @@ -1,77 +0,0 @@ -require 'msg' -include MSG -require 'RubyProcess' -require 'Master' -require 'RubyHost' - - -array = Array.new() - -# puts array.empty? -array << "Peace!!" << "Hey" << "Euh..."<< "Hey2" << "Euh...2" - - -# process = rubyNewInstanceArgs("Master",array) - -# array.clear -# puts array.empty? - -mehdi = "Hey...this is my name" - -hash = Hash.new - -var = "name" - -hash[var] = mehdi - -# puts hash["name"] - -array.push(mehdi) - -# info("Hey...") -# puts array[0] - -array2 = array - -# puts array2[0] - -size = array2.size - -task = Task.new('marine',12,23) -puts "Created task :"+task.to_s+" ; name: "+Task.name(task) - -# Task.send(Task.new("finalize",0,0),"alias") -#task2 =RbTask.new('task2') -# task = RbTask.new() -# host = Host.new("Brad") - -#host2 = Host.new() -# task_2 = Task.new('task2',12,23) -# name1 = task_2.name() -# name2 = Task.name(task) -# size = Task.compSize(task) -name = Task.name(task) -# task.bind -number = 56 -process = rubyNewInstance("RbProcess");#RbProcess.new() -# process.run() -p process.class -# puts "Created process :"+process.to_s+" ; name: "+RbProcess.name(task) -# name = process.name -puts name -argue = ARGV[0] -puts argue -# puts process.alive? -# Thread.stop -# process2 = RbProcess.new() -# process2.name = "Hope" -# reader = RubyXML.new(); -# reader.parseApplication("application.xml") -# reader.printAll() -# name2 = Task.test() -# puts name -# process2 = RbProcess.new() -# puts (name) -# init(array) -# createEnvironment(name); -# Task.goodbye \ No newline at end of file diff --git a/src/bindings/ruby/deploy.xml b/src/bindings/ruby/deploy.xml index 590b69e2cc..a0c6ec9138 100644 --- a/src/bindings/ruby/deploy.xml +++ b/src/bindings/ruby/deploy.xml @@ -6,10 +6,10 @@ - + - - + + @@ -21,7 +21,4 @@ - - - diff --git a/src/bindings/ruby/extconfig.rb b/src/bindings/ruby/extconfig.rb deleted file mode 100644 index 026fe9061c..0000000000 --- a/src/bindings/ruby/extconfig.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'mkmf' -extension_name ='msg' -dir_config(extension_name) -create_makefile(extension_name) diff --git a/src/bindings/ruby/libsimgrid.so b/src/bindings/ruby/libsimgrid.so new file mode 120000 index 0000000000..1a1d3e3542 --- /dev/null +++ b/src/bindings/ruby/libsimgrid.so @@ -0,0 +1 @@ +../../.libs/libsimgrid.so \ No newline at end of file diff --git a/src/bindings/ruby/rb_application_handler.c b/src/bindings/ruby/rb_application_handler.c index 32be9a53aa..aac298c446 100644 --- a/src/bindings/ruby/rb_application_handler.c +++ b/src/bindings/ruby/rb_application_handler.c @@ -1,84 +1,63 @@ /* - * $Id$ + * Copyright 2010. The SimGrid Team. All right reserved. * - * Copyright 2010 Martin Quinson, Mehdi Fekari - * 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. + * 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_application_handler.h" +#include "bindings/ruby_bindings.h" #include "surf/surfxml_parse.h" -#include + +static VALUE application_handler_class; // The Current Instance of ApplicationHandler Class ,is it the same as Current Thread ??!! + // #define MY_DEBUG -static void r_init() -{ - +static void r_init() { ruby_init(); ruby_init_loadpath(); rb_require("ApplicationHandler.rb"); } -static void application_handler_on_start_document(void) -{ +void rb_application_handler_on_start_document(void) { r_init(); - //current One - current = rb_funcall3(rb_const_get(rb_cObject, rb_intern("ApplicationHandler")), rb_intern("new"), 0, 0); - rb_funcall(current,rb_intern("onStartDocument"),0); + application_handler_class = rb_funcall3(rb_const_get(rb_cObject, rb_intern("ApplicationHandler")), rb_intern("new"), 0, 0); + rb_funcall(application_handler_class,rb_intern("onStartDocument"),0); #ifdef MY_DEBUG printf ("application_handler_on_start_document ...Done\n" ); #endif } -static void application_handler_on_end_document(void) -{ - //r_init(); - rb_funcall(current,rb_intern("onEndDocument"),0); - +void rb_application_handler_on_end_document(void) { + rb_funcall(application_handler_class,rb_intern("onEndDocument"),0); } -static void application_handler_on_begin_process(void) -{ - //r_init(); +void rb_application_handler_on_begin_process(void) { VALUE hostName = rb_str_new2(A_surfxml_process_host); VALUE function = rb_str_new2(A_surfxml_process_function); #ifdef MY_DEBUG printf ("On_Begin_Process: %s : %s \n",RSTRING(hostName)->ptr,RSTRING(function)->ptr); #endif - rb_funcall(current,rb_intern("onBeginProcess"),2,hostName,function); + rb_funcall(application_handler_class,rb_intern("onBeginProcess"),2,hostName,function); } -static void application_handler_on_process_arg(void) -{ - //r_init(); +void rb_application_handler_on_process_arg(void) { VALUE arg = rb_str_new2(A_surfxml_argument_value); #ifdef MY_DEBUG printf ("On_Process_Args >> Sufxml argument value : %s\n",RSTRING(arg)->ptr); #endif - rb_funcall(current,rb_intern("onProcessArg"),1,arg); + rb_funcall(application_handler_class,rb_intern("onProcessArg"),1,arg); } -static void application_handler_on_property(void) -{ - - //r_init() +void rb_application_handler_on_property(void) { VALUE id = rb_str_new2(A_surfxml_prop_id); VALUE val = rb_str_new2(A_surfxml_prop_value); - rb_funcall(current,rb_intern("onProperty"),2,id,val); - + rb_funcall(application_handler_class,rb_intern("onProperty"),2,id,val); } -static void application_handler_on_end_process(void) -{ - - //r_init() - rb_funcall(current,rb_intern("onEndProcess"),0); - +void rb_application_handler_on_end_process(void) { + rb_funcall(application_handler_class,rb_intern("onEndProcess"),0); } diff --git a/src/bindings/ruby/rb_application_handler.h b/src/bindings/ruby/rb_application_handler.h deleted file mode 100644 index d030b0fc4c..0000000000 --- a/src/bindings/ruby/rb_application_handler.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _RB_APPLICATION_HANDLER_H_ -#define _RB_APPLICATION_HANDLER_H_ - -#include -#include "msg/msg.h" - - - -VALUE current; // The Current Instance of ApplicationHandler Class ,is it the same as Current Thread ??!! - -static void r_init(void); - -static void application_handler_on_start_document(void); - -static void application_handler_on_end_document(void); - -static void application_handler_on_begin_process(void); - -static void application_handler_on_process_arg(void); - -static void application_handler_on_property(void); - -static void application_handler_on_end_process(void); - - -#endif diff --git a/src/bindings/ruby/rb_msg.c b/src/bindings/ruby/rb_msg.c index 8638eaedc6..b71f55617b 100644 --- a/src/bindings/ruby/rb_msg.c +++ b/src/bindings/ruby/rb_msg.c @@ -1,12 +1,8 @@ /* - * $Id$ + * Copyright 2010. The SimGrid Team. All right reserved. * - * Copyright 2010 Martin Quinson, Mehdi Fekari - * 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. + * 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_msg.h" diff --git a/src/bindings/ruby/rb_msg.h b/src/bindings/ruby/rb_msg.h deleted file mode 100644 index d6a4ebcc95..0000000000 --- a/src/bindings/ruby/rb_msg.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef RB_MSG -#define RB_MSG -#include -#include "msg/msg.h" -#include - -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, - "Messages specific for this msg example"); - -// #include "msg/private.h" -// #include "simix/private.h" -// #include "simix/smx_context_ruby.h" - -// MSG Module -VALUE rb_msg; -// MSG Classes -VALUE rb_task; -VALUE rb_host; - -//init_Msg Called When The Ruby Interpreter loads this C extension -void Init_msg(); - -// Msg_Init From Ruby -static void msg_init(VALUE Class,VALUE args); - -// Msg_Run From Ruby -static void msg_run(VALUE Class); - -// Create Environment -static void msg_createEnvironment(VALUE Class,VALUE plateformFile); - -// deploy Application -static void msg_deployApplication(VALUE Class,VALUE deploymntFile); - -// Tools -static void msg_info(VALUE Class,VALUE msg); - -//get Clock -static void msg_get_clock(VALUE Class); - -//pajeOutput -static void msg_paje_output(VALUE Class,VALUE pajeFile); - -// Ruby Introspection : To instanciate a Ruby Class from its Name -static VALUE msg_new_ruby_instance(VALUE Class,VALUE className); - -// The Same ... This Time with Args -static VALUE msg_new_ruby_instance_with_args(VALUE Class,VALUE className,VALUE args); - -#endif \ No newline at end of file diff --git a/src/bindings/ruby/rb_msg_host.c b/src/bindings/ruby/rb_msg_host.c index 797ac10d96..4ac3a82957 100644 --- a/src/bindings/ruby/rb_msg_host.c +++ b/src/bindings/ruby/rb_msg_host.c @@ -18,7 +18,7 @@ VALUE rb_host_get_by_name(VALUE class, VALUE name) { const char * h_name = RSTRING(name)->ptr; m_host_t host = MSG_get_host_by_name(h_name); if(!host) - rb_raise(rb_eRuntimeError,"MSG_get_host_by_name() failed"); + rb_raise(rb_eRuntimeError,bprintf("No host called '%s' found",h_name)); return Data_Wrap_Struct(class,0,rb_host_free,host); } diff --git a/src/bindings/ruby/rb_msg_process.c b/src/bindings/ruby/rb_msg_process.c index b19e39e1f9..8ec8a812a9 100644 --- a/src/bindings/ruby/rb_msg_process.c +++ b/src/bindings/ruby/rb_msg_process.c @@ -15,7 +15,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ruby,bindings,"Ruby Bindings"); void initRuby(void) { ruby_init(); ruby_init_loadpath(); - rb_require("RubyProcess.rb"); +// KILLME rb_require("RubyProcess.rb"); } diff --git a/src/bindings/ruby/rb_msg_task.c b/src/bindings/ruby/rb_msg_task.c index 3faeeee9ce..8af9413fa7 100644 --- a/src/bindings/ruby/rb_msg_task.c +++ b/src/bindings/ruby/rb_msg_task.c @@ -8,6 +8,8 @@ #include "bindings/ruby_bindings.h" +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby); + // Free Method void rb_task_free(m_task_t tk) { MSG_task_destroy(tk); @@ -56,27 +58,23 @@ void rb_task_send(VALUE class,VALUE task,VALUE mailbox) { // Wrap Ruby Value to m_task_t struct m_task_t tk; Data_Get_Struct(task, s_m_task_t, tk); + xbt_backtrace_display_current(); int res = MSG_task_send(tk,RSTRING(mailbox)->ptr); if(res != MSG_OK) rb_raise(rb_eRuntimeError,"MSG_task_send failed"); } // Receiving Task (returns a Task) -VALUE rb_task_receive(VALUE class,VALUE mailbox) { +VALUE rb_task_receive(VALUE class, VALUE mailbox) { // Task m_task_t task = NULL; + INFO1("Receiving a task on mailbox %s",RSTRING(mailbox)->ptr); + xbt_backtrace_display_current(); MSG_task_receive(&task,RSTRING(mailbox)->ptr); + INFO2("XXXXXXXXReceived a task %p %s",task,task->name); return Data_Wrap_Struct(class, 0, rb_task_free, task); } -// Recieve Task 2 -// Not Appreciated -void rb_task_receive2(VALUE class,VALUE task,VALUE mailbox) { - m_task_t tk; - Data_Get_Struct(task, s_m_task_t, tk); - MSG_task_receive(&tk,RSTRING(mailbox)->ptr); -} - // It Return a Native Process ( m_process_t ) VALUE rb_task_sender(VALUE class,VALUE task) { m_task_t tk; diff --git a/src/bindings/ruby/simgrid.rb b/src/bindings/ruby/simgrid.rb new file mode 100644 index 0000000000..90b8793591 --- /dev/null +++ b/src/bindings/ruby/simgrid.rb @@ -0,0 +1,416 @@ +require 'simgrid_ruby' +include MSG +require 'thread' + +$DEBUG = true # This is a Global Variable Useful for Debugging + +########################################################################### +# Class Semaphore +########################################################################### +class Semaphore + Thread.abort_on_exception = true + attr_accessor :permits + + def initialize ( permits ) + @permits = permits + end + + + def acquire(mutex,cv) + raise "Interrupted Thread " if (!Thread.current.alive?) + mutex.synchronize { + while @permits <= 0 + cv.wait(mutex) + end + + @permits = @permits - 1 + cv.signal + } + end + + def release(mutex,cv) + mutex.synchronize{ + @permits += 1 + cv.signal + } + end +end + +######################################################################## +# Class RbProcess +######################################################################## +class RbProcess < Thread + @@nextProcessId = 0 +# Attributes + attr_accessor :bind, :id, :properties, :name, + :pargs, :schedBegin, :schedEnd, :mutex, :cv + +# Initialize : Used from ApplicationHandler to fill it in + def initialize(*args) + + argc = args.size + + if argc == 0 # Default initializer + super() { + @id = 0 + @bind = 0 + @name = "" + @pargs = Array.new() + init_var() + start() + if $DEBUG + puts "Init Default Initializer...Nothing to do...Bye" + end + } + + # 2 arguments: (HostName,Name) Or (Host , Name) + elsif argc == 2 + super(){ + type = args[0].type() + if ( type.to_s == "String") + host = Host.getByName(args[0]) + elsif ( type.to_s == "MSG::Host") + host = args[0] + else + raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host" + end + if $DEBUG + puts host + end + raise "Process name cannot be null" if args[1].empty? + @name = args[1] + if $DEBUG + puts @name + end + @pargs = Array.new() # No Args[] Passed in Arguments + @@nextProcessId += 1 + @id = @@nextProcessId + init_var() + start() + createProcess(self,host) + if $DEBUG + puts "Initilize with 2 args" + end + } + + # 3 arguments: (hostName,Name,args[]) or (Host,Name,args[]) + elsif argc == 3 + super(){ + type = args[0].type() + if ( type.to_s == "String") + host = Host.getByName(args[0]) + elsif ( type.to_s == "MSG::Host") + host = args[0] + else + raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host" + end + if $DEBUG + puts host + end + + raise "Process name cannot be null" if args[1].empty? + @name = args[1] + type = args[2].type() + raise "Third argument should be an Array" if type != "Array" + @pargs = args[3] + @@nextProcessId +=1 + @id = @@nextProcessId + init_var() + createProcess(self,host) + + if $DEBUG + puts "Initilize with 3 args" + end + +# sleep #keep the thread running + } + else + raise "Bad number of argument: Expecting either 1, 2 or 3, but got "+argc + end + end + + # Init_var Called By Initialize + def init_var() + @proprieties = Hash.new() + @mutex = Mutex.new + @cv = ConditionVariable.new + # Process Synchronization Tools + @schedBegin = Semaphore.new(0) + @schedEnd = Semaphore.new(0) + end + + #main + def msg_main(args) + # To Be Implemented within The Process... + # The Main Code of The Process to be Executed ... + end + + # Start : To keep the Process Alive and waitin' via semaphore + def start() + @schedBegin.acquire(@mutex,@cv) + #execute The Main Code of The Process ( Example Master ; Slave ...) + msg_main(@pargs) + processExit(self) #Exite the Native Process + @schedEnd.release(@mutex,@cv) + end + +# NetxId + def nextId () + @@nextProcessId +=1 + return @@nextProcessId + end + + if $DEBUG + #Process List + def processList() + Thread.list.each {|t| p t} + end + end + + #Get Own ID + def getID() + return @id + end + + # set Id + def setID(id) + @id = id + end + + #Get a Process ID + def processID(process) + return process.id + end + + #Get Own Name + def getName() + return @name + end + + #Get a Process Name + def processName(process) + return process.name + end + + #Get Bind + def getBind() + return @bind + end + + #Get Binds + def setBind(bind) + @bind = bind + end + + def unschedule() + + @schedEnd.release(@mutex,@cv) +# info("@schedEnd.release(@mutex,@cv)") + @schedBegin.acquire(@mutex,@cv) +# info("@schedBegin.acquire(@mutex,@cv)") + + end + + def schedule() + @schedBegin.release(@mutex,@cv) + @schedEnd.acquire(@mutex,@cv) + end + + #C Simualateur Process Equivalent Management + # After Binding Ruby Process to C Process + +# pause + def pause() + processSuspend(self) + end + +# restart + def restart() + processResume(self) + end + +# isSuspended + def isSuspended() + processIsSuspended(self) + end + +# getHost + def getHost() + processGetHost(self) + end + +# The Rest of Methods !!! To be Continued ... +end + +######################################################################## +# Class ProcessFactory +######################################################################## + +class ProcessFactory + +# Attributes + attr_accessor :args, :proprieties, :hostName, :function +# Initlialize + def initialize() + + @args = Array.new + @proprieties = Hash.new + @hostName = nil + @function = nil + + end + +# setProcessIdentity + def setProcessIdentity(hostName,function) + @hostName = hostName + @function = function + + if !args.empty? + args.clear + end + + if !proprieties.empty? + proprieties.clear + end + + end + +# RegisterProcess + def registerProcessArg(arg) + + @args.push(arg) + + end + +# CreateProcess + def createProcess() + + process = rubyNewInstance(@function) # process = rubyNewInstanceArgs(@function,@args) # + size = @args.size + for i in 0..size-1 + process.pargs.push(@args[i]) + end + process.name = @function + process.id = process.nextId() # This increment Automaticaly The Static ProcessNextId for The Class RbProces + host = RbHost.getByName(@hostName) + processCreate(process,host) + process.properties = @properties + @proprieties = Hash.new + + end + +# SetProperty + def setProperty(id,value) + @proprieties[id] = value + end +end + +######################################################################### +# Class ApplicationHandler +######################################################################### +class ApplicationHandler + @processFactory +# Initialize + def initialize() + #Nothing todo + end + + # onStartDocument + def onStartDocument() + + @processFactory = ProcessFactory.new + if ($DEBUG) + puts "onStartDocument" + end + + end + +# onBeginProcess + def onBeginProcess(hostName,function) + + @processFactory.setProcessIdentity(hostName,function) + + if ($DEBUG) + puts "onBeginProcess" + end + + end + +# onProperty + def onProperty(id,value) + + @processFactory.setProperty(id,value) + + if ($DEBUG) + puts "onProperty" + end + + end + +# RegisterProcessArg + def onProcessArg(arg) + + @processFactory.registerProcessArg(arg) + + if ($DEBUG) + puts "onProcessArg" + end + + end + +# OnEndProcess + def onEndProcess() + + @processFactory.createProcess() + + if ($DEBUG) + puts "onEndProcess" + end + + end + + # onEndDocument + def onEndDocument() +# Erm... Actually nothing to do !! + + if($DEBUG) + puts "onEndDocument" + end + end + + # End Class + end + +######################### +# Class RbHost +######################### + +class RbHost < Host +# Attributes + attr_accessor :bind, :data + +# Initialize + def initialize() + super() + @bind = 0 + @data = nil + end + +end + +######################### +# Class RbTask +######################### +class RbTask < Task + attr_accessor :bind + + def initialize(name,comp_size,comm_size) + super(name,comp_size,comm_size) + end + +end + +######################### +# Main chunck +######################### +MSG.init(ARGV) diff --git a/src/bindings/ruby/simgrid_ruby.c b/src/bindings/ruby/simgrid_ruby.c new file mode 100644 index 0000000000..ac65b0d781 --- /dev/null +++ b/src/bindings/ruby/simgrid_ruby.c @@ -0,0 +1,212 @@ +/* SimGrid Ruby bindings */ + +/* Copyright (c) 2010, the 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 "xbt.h" +#include "bindings/ruby_bindings.h" + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby); + +// MSG Module +VALUE rb_msg; +// MSG Classes +VALUE rb_task; +VALUE rb_host; + + +//Init Msg From Ruby +static void msg_init(VALUE Class,VALUE args) +{ + char **argv=NULL; + const char *tmp; + int argc,type,i; + VALUE *ptr ; + // Testing The Args Type + type = TYPE(args); + if (type != T_ARRAY ) + { + rb_raise(rb_eRuntimeError,"Argh!!! Bad Arguments to msg_init"); + return; + } + ptr= RARRAY(args)->ptr; + argc= RARRAY(args)->len; + // Create C Array to Hold Data_Get_Struct + argc++; + argv = xbt_new0(char *, argc); + argv[0] = strdup("ruby"); + for (i=0;iptr; + argv[i+1] = strdup(tmp); + } + // Calling C Msg_Init Method + MSG_global_init(&argc,argv); + MSG_set_channel_number(10); // Okey !! Okey !! This Must Be Fixed Dynamiclly , But Later ;) + SIMIX_context_select_factory("ruby"); + + // Free Stuffs + for (i=0;idata);// ??!! + } + + if (MSG_OK != MSG_clean()){ + rb_raise(rb_eRuntimeError,"MSG_clean() failed"); + } + return; +} + +static void msg_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; + MSG_create_environment(platform); + DEBUG1("Create Environment (%s)...Done",platform); +} + +//deploy Application +static void msg_deployApplication(VALUE class,VALUE deploymentFile ) { + + int type = TYPE(deploymentFile); + if ( type != T_STRING ) + rb_raise(rb_eRuntimeError,"Bad Argument's Type for deployApplication "); + const char *dep_file = RSTRING(deploymentFile)->ptr; + surf_parse_reset_parser(); + surfxml_add_callback(STag_surfxml_process_cb_list, + rb_application_handler_on_begin_process); + + surfxml_add_callback(ETag_surfxml_argument_cb_list, + rb_application_handler_on_process_arg); + + surfxml_add_callback(STag_surfxml_prop_cb_list, + rb_application_handler_on_property); + + surfxml_add_callback(ETag_surfxml_process_cb_list, + rb_application_handler_on_end_process); + + surf_parse_open(dep_file); + rb_application_handler_on_start_document(); + if(surf_parse()) + rb_raise(rb_eRuntimeError,"surf_parse() failed"); + surf_parse_close(); + rb_application_handler_on_end_document(); + + DEBUG1("Deploy Application(%s)...Done",dep_file); +} + +// INFO +static void msg_info(VALUE class,VALUE msg) { + const char *s = RSTRING(msg)->ptr; + INFO1("%s",s); +} + +// Get Clock +static void msg_get_clock(VALUE class) { + + printf("Simulation time %f\n",MSG_get_clock()); + +} + +// Ruby intropspection : Instanciate a ruby Class From its Name +// Used by ProcessFactory::createProcess + +static VALUE msg_new_ruby_instance(VALUE class,VALUE className) { + ruby_init(); + ruby_init_loadpath(); + char * p_className = RSTRING(className)->ptr; + + return rb_funcall3(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"),0, 0); +} + +//This Time With Args +static VALUE msg_new_ruby_instance_with_args(VALUE class,VALUE className,VALUE args) { + ruby_init(); + ruby_init_loadpath(); + char * p_className = RSTRING(className)->ptr; + return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"), 1, args); +} + + + +typedef VALUE(*rb_meth)(ANYARGS); +void Init_simgrid_ruby() { + // Modules + rb_msg = rb_define_module("MSG"); + //Associated Environment Methods! + rb_define_method(rb_msg,"init",(rb_meth)msg_init,1); + rb_define_method(rb_msg,"run",(rb_meth)msg_run,0); + rb_define_method(rb_msg,"createEnvironment",(rb_meth)msg_createEnvironment,1); + rb_define_method(rb_msg,"deployApplication",(rb_meth)msg_deployApplication,1); + rb_define_method(rb_msg,"info",(rb_meth)msg_info,1); + rb_define_method(rb_msg,"getClock",(rb_meth)msg_get_clock,0); + rb_define_method(rb_msg,"rubyNewInstance",(rb_meth)msg_new_ruby_instance,1); + rb_define_method(rb_msg,"rubyNewInstanceArgs",(rb_meth)msg_new_ruby_instance_with_args,2); + + // Associated Process Methods + rb_define_method(rb_msg,"processCreate",(rb_meth)rb_process_create,2); + rb_define_method(rb_msg,"processSuspend",(rb_meth)rb_process_suspend,1); + rb_define_method(rb_msg,"processResume",(rb_meth)rb_process_resume,1); + rb_define_method(rb_msg,"processIsSuspend",(rb_meth)rb_process_isSuspended,1); + rb_define_method(rb_msg,"processKill",(rb_meth)rb_process_kill_up,1); + rb_define_method(rb_msg,"processGetHost",(rb_meth)rb_process_getHost,1); + rb_define_method(rb_msg,"processExit",(rb_meth)rb_process_exit,1); + + //Classes + rb_task = rb_define_class_under(rb_msg,"Task",rb_cObject); + rb_host = rb_define_class_under(rb_msg,"Host",rb_cObject); + + //Task Methods + rb_define_module_function(rb_task,"new",(rb_meth)rb_task_new,3); + rb_define_module_function(rb_task,"compSize",(rb_meth)rb_task_comp,1); + rb_define_module_function(rb_task,"name",(rb_meth)rb_task_name,1); + rb_define_module_function(rb_task,"execute",(rb_meth)rb_task_execute,1); + rb_define_module_function(rb_task,"send",(rb_meth)rb_task_send,2); + rb_define_module_function(rb_task,"receive",(rb_meth)rb_task_receive,1); + rb_define_module_function(rb_task,"sender",(rb_meth)rb_task_sender,1); + rb_define_module_function(rb_task,"source",(rb_meth)rb_task_source,1); + rb_define_module_function(rb_task,"listen",(rb_meth)rb_task_listen,2); + rb_define_module_function(rb_task,"listenFromHost",(rb_meth)rb_task_listen_host,3); + + //Host Methods + rb_define_module_function(rb_host,"getByName",(rb_meth)rb_host_get_by_name,1); + rb_define_module_function(rb_host,"name",(rb_meth)rb_host_name,1); + rb_define_module_function(rb_host,"speed",(rb_meth)rb_host_speed,1); + rb_define_module_function(rb_host,"number",(rb_meth)rb_host_number,0); + rb_define_module_function(rb_host,"setData",(rb_meth)rb_host_set_data,2); + rb_define_module_function(rb_host,"getData",(rb_meth)rb_host_get_data,1); + //rb_define_module_function(rb_host,"hasData",host_has_data,1); + rb_define_module_function(rb_host,"isAvail",(rb_meth)rb_host_is_avail,1); + +} diff --git a/src/bindings/ruby/simgrid_ruby.so b/src/bindings/ruby/simgrid_ruby.so new file mode 120000 index 0000000000..bcaf952a30 --- /dev/null +++ b/src/bindings/ruby/simgrid_ruby.so @@ -0,0 +1 @@ +../../.libs/simgrid_ruby.so \ No newline at end of file diff --git a/src/bindings/ruby/smx_context_ruby.c b/src/bindings/ruby/smx_context_ruby.c deleted file mode 100644 index 602217bab0..0000000000 --- a/src/bindings/ruby/smx_context_ruby.c +++ /dev/null @@ -1,193 +0,0 @@ -/* $Id$ */ - -/* context_Ruby - implementation of context switching with lua coroutines */ - -/* Copyright (c) 2004-2008 the 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 -#include "private.h" -#include "xbt/function_types.h" -#include "context_sysv_config.h" - -typedef struct s_smx_ctx_ruby -{ - SMX_CTX_BASE_T; - VALUE process; // The Process Ruby Instance - //... -}s_smx_ctx_ruby_t,*smx_ctx_ruby_t; - -static smx_context_t -smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv, - void_f_pvoid_t cleanup_func,void *cleanup_arg); - - -static int smx_ctx_ruby_factory_finalize(smx_context_factory_t *factory); - -static void smx_ctx_ruby_free(smx_context_t context); - -static void smx_ctx_ruby_start(smx_context_t context); - -static void smx_ctx_ruby_stop(smx_context_t context); - -static void smx_ctx_ruby_suspend(smx_context_t context); - -static void - smx_ctx_ruby_resume(smx_context_t old_context,smx_context_t new_context); - -static void smx_ctx_ruby_wrapper(void); //??!! - - - -void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory) -{ - - *factory = xbt_new0(s_smx_context_factory_t,1); - - (*factory)->create_context = smx_ctx_ruby_create_context; - (*factory)->finalize = smx_ctx_ruby_factory_finalize; - (*factory)->free = smx_ctx_ruby_free; - (*factory)->start = smx_ctx_ruby_start; - (*factory)->stop = smx_ctx_ruby_stop; - (*factory)->suspend = smx_ctx_ruby_suspend; - (*factory)->resume = smx_ctx_ruby_resume; - (*factory)->name = "smx_ruby_context_factory"; - - ruby_init(); - ruby_init_loadpath(); - -} - -static int smx_ctx_ruby_factory_finalize(smx_context_factory_t *factory) -{ - - free(*factory); - *factory = NULL; - return 0; - -} - -static smx_context_t - smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv, - void_f_pvoid_t cleanup_func,void* cleanup_arg) -{ - - smx_ctx_ruby_t context = xbt_new0(s_smx_ruby_t,1); - - /*if the user provided a function for the process , then use it - Otherwise it's the context for maestro */ - if( code ) - { - context->cleanup_func = cleanup_func; - context->cleanup_arg = cleanup_arg; - context->process = rb_funcall3(rb_const_get(rb_cObject, rb_intern("RbProcess")), rb_intern("new"), 0, 0); - //context->process = rb_thread_main(); // Or VALUE rb_thread_create ( VALUE(*(ANYARGS), void *) ) - - - } - return (smx_context_t) context; - -} - -static void smx_ctx_ruby_free(smx_context_t context) -{ - - if (context) - { - smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) context; - - if (ctx_ruby->process) - VALUE process = ctx_ruby->process; - - ctx_ruby->process = Qnil; - - // if the Ruby Process is Alive , Join it - - if ( process_isAlive(process) ) - process_join(process); - - } - - free(context); - context = NULL; - - - } - - -} - - -static void smx_ctx_ruby_start(smx_context_t context) -{ - - - -} - - -static void smx_ctx_ruby_stop(smx_context_t context) -{ - - VALUE process = Qnil; - smx_ctx_ruby_t ctx_ruby; - - if ( context->cleanup_func) - (*(context->cleanup_func)) (context->cleanup_arg); - - ctx_ruby = (smx_ctx_ruby_t) context; - - // Well , Let's Do The Same as JNI Stoppin' Process - if ( simix_gloabl->current_process->iwannadie ) - { - if( ctx_ruby->process ) - { - //if the Ruby Process still Alive ,let's Schedule it - if ( process_isAlive( ctx_ruby->process ) ) - { - - current = (smx_ctx_ruby_t)simix_gloabl->current_process->context: - process_schedule(current->process); - - process = ctx_ruby->process; - - // interupt/kill The Ruby Process - process_kill(process); - - } - } - }else { - - process = ctx_ruby->process; - ctx_ruby->process = Qnil; - - } - - -} - -static void smx_ctx_ruby_suspend(smx_context_t context) -{ - -if ( context ) -{ -smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) context; - if (ctx_ruby->process) - process_unschedule( ctx_ruby->process ) ; -} - - else - rb_raise(rb_eRuntimeError,"smx_ctx_ruby_suspend failed"); - -} - -static void smx_ctx_ruby_resume(smx_context_t old_context,smx_context_t new_context) -{ - - smx_ctx_ruby_t ctx_ruby = (smx_ctx_ruby_t) new_context; - - process_schedule( ctx_ruby->process ); - - -} diff --git a/src/bindings/ruby_bindings.h b/src/bindings/ruby_bindings.h index fca1ace4b9..fa201097c7 100644 --- a/src/bindings/ruby_bindings.h +++ b/src/bindings/ruby_bindings.h @@ -51,7 +51,7 @@ typedef struct s_smx_ctx_ruby { }s_smx_ctx_ruby_t,*smx_ctx_ruby_t; void SIMIX_ctx_ruby_factory_init(smx_context_factory_t *factory); - +void Init_simgrid_ruby(void); /* Load the bindings */ void initRuby(void); // Mandatory to call Ruby methods from C /* *********************************************** * @@ -117,4 +117,12 @@ VALUE rb_task_source(VALUE Class,VALUE task); VALUE rb_task_listen(VALUE Class,VALUE task,VALUE alias); //Listen From Alias (=mailbox) VALUE rb_task_listen_host(VALUE Class,VALUE task,VALUE alias,VALUE host); //Listen from Host +/* Upcalls for the application handler */ +void rb_application_handler_on_start_document(void); +void rb_application_handler_on_end_document(void); +void rb_application_handler_on_begin_process(void); +void rb_application_handler_on_process_arg(void); +void rb_application_handler_on_property(void); +void rb_application_handler_on_end_process(void); + #endif /* RB_SG_BINDINGS */ diff --git a/src/msg/msg_mailbox.c b/src/msg/msg_mailbox.c index 3425a413dc..9b5cec87ea 100644 --- a/src/msg/msg_mailbox.c +++ b/src/msg/msg_mailbox.c @@ -109,11 +109,11 @@ MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t *task, m_host_t host, double timeout) { xbt_ex_t e; - size_t task_size = sizeof(void*); MSG_error_t ret = MSG_OK; smx_comm_t comm; CHECK_HOST(); + memset(&comm,0,sizeof(comm)); /* Kept for compatibility with older implementation */ xbt_assert1(!MSG_mailbox_get_cond(mailbox), "A process is already blocked on this channel %s", @@ -132,7 +132,7 @@ MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t *task, m_host_t host, /* Try to receive it by calling SIMIX network layer */ TRY{ - SIMIX_network_recv(mailbox->rdv, timeout, task, &task_size, &comm); + SIMIX_network_recv(mailbox->rdv, timeout, task, NULL, &comm); (*task)->simdata->refcount--; } CATCH(e){ diff --git a/src/simix/private.h b/src/simix/private.h index ca905b9a3e..b130cf9337 100644 --- a/src/simix/private.h +++ b/src/simix/private.h @@ -156,8 +156,8 @@ typedef struct s_smx_comm { /* Data to be transfered */ void *src_buff; - size_t src_buff_size; void *dst_buff; + size_t src_buff_size; size_t *dst_buff_size; void *data; /* User data associated to communication */ } s_smx_comm_t; diff --git a/src/simix/smx_context_ruby.c b/src/simix/smx_context_ruby.c index 0dc94daaa2..4ff0c80025 100644 --- a/src/simix/smx_context_ruby.c +++ b/src/simix/smx_context_ruby.c @@ -13,6 +13,8 @@ #include "bindings/ruby_bindings.h" +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby); + static smx_context_t smx_ctx_ruby_create_context(xbt_main_func_t code,int argc,char** argv, void_f_pvoid_t cleanup_func,void *cleanup_arg); diff --git a/src/simix/smx_network.c b/src/simix/smx_network.c index 12f2be8a82..79c604ba10 100644 --- a/src/simix/smx_network.c +++ b/src/simix/smx_network.c @@ -327,24 +327,23 @@ void SIMIX_network_copy_data(smx_comm_t comm) if(!comm->src_buff || !comm->dst_buff) return; - size_t src_buff_size = comm->src_buff_size; - size_t dst_buff_size = *comm->dst_buff_size; - /* Copy at most dst_buff_size bytes of the message to receiver's buffer */ - dst_buff_size = MIN(dst_buff_size, src_buff_size); + size_t buff_size = comm->src_buff_size; + if (comm->dst_buff_size) + buff_size = MIN(buff_size,*(comm->dst_buff_size)); /* Update the receiver's buffer size to the copied amount */ if (comm->dst_buff_size) - *comm->dst_buff_size = dst_buff_size; + *comm->dst_buff_size = buff_size; - if(dst_buff_size == 0) + if(buff_size == 0) return; - memcpy(comm->dst_buff, comm->src_buff, dst_buff_size); - - DEBUG4("Copying comm %p data from %s -> %s (%zu bytes)", + DEBUG4("Copying comm %p data from %s -> %s (%zu bytes)", comm, comm->src_proc->smx_host->name, comm->dst_proc->smx_host->name, - dst_buff_size); + buff_size); + + memcpy(comm->dst_buff, comm->src_buff, buff_size); } /**