# make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
require 'simgrid'
+
include MSG
#################################################
# Class Master
#################################################
-class Master < MsgProcess
+class Master < MSG::Process
# main : that function that will be executed when Running Simulation
def main(args) # args is an array containing arguments for function master
size = args.size
for i in 0..size-1
- info("args["+String(i)+"]="+args[i])
+ MSG::info("args["+String(i)+"]="+args[i])
end
raise "Master needs 3 arguments" if size < 3
# Creates and sends the tasks
for i in 0..numberOfTask-1
- task = Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
+ task = MSG::Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
mailbox = "slave " + (i%slaveCount).to_s
- info("Master Sending "+ Task.name(task) + " to " + mailbox + " with Comput Size " +
- Task.compSize(task).to_s)
+ MSG::info("Master Sending "+ MSG::Task.name(task) + " to " + mailbox + " with Comput Size " +
+ MSG::Task.compSize(task).to_s)
# task.compSize.to_s) # FIXME: This version creates a deadlock. Interesting
- Task.send(task,mailbox)
- info("Master Done Sending " +Task.name(task) + " to " + mailbox)
+ MSG::Task.send(task,mailbox)
+ MSG::info("Master Done Sending " +MSG::Task.name(task) + " to " + mailbox)
end
- # Sending Finalize Tasks
- info ("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
+ # Sending Finalize MSG::Tasks
+ MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
for i in 0..slaveCount-1
mailbox = "slave " + i.to_s
- info ("Master Sending Finalize to " + mailbox)
- Task.send(Task.new("finalize",0,0),mailbox)
+ MSG::info("Master Sending Finalize to " + mailbox)
+ MSG::Task.send(Task.new("finalize",0,0),mailbox)
end
- info("Master : Everything's Done")
+ MSG::info("Master : Everything's Done")
end
end
#################################################
# Class Slave
#################################################
-class Slave < MsgProcess
+class Slave < MSG::Process
def main(args)
mailbox = "slave " + args[0]
while true
info("Ready to Receive Task")
- task = Task.receive(mailbox)
- task_name = Task.name(task)
- info ("Task Received : " + task.name)
+ task = MSG::Task.receive(mailbox)
+ task_name = MSG::Task.name(task)
+ MSG::info("Task Received : " + task.name)
if (task_name == "finalize")
- info("Slave" + s_mailbox + "got finalize msg")
+ MSG::info("Slave" + s_mailbox + "got finalize msg")
break
end
- info("Slave " + s_mailbox + " ...Processing" + Task.name(task))
- Task.execute(task)
+ MSG::info("Slave " + s_mailbox + " ...Processing" + MSG::Task.name(task))
+ MSG::Task.execute(task)
end
- info("Slave " + s_mailbox + "I'm Done , See You !!")
+ MSG::info("Slave " + s_mailbox + "I'm Done , See You !!")
end
end
end
# Thread.list.each {|t| p t}
-MSG.run()
-MSG.getClock()
+MSG.run
+MSG.getClock #FIXME: write "puts MSG.getClock" instead
# exit()
require 'simgrid_ruby'
-include MSG
require 'thread'
-$DEBUG = false # This is a Global Variable Useful for Debugging
+$DEBUG = false # This is a Global Variable Useful for MSG::debugging
###########################################################################
# Class Semaphore
end
def acquire
+ MSG::debug("Acquire "+self.to_s)
Thread.critical = true
if (@counter -= 1) < 0
@waiting_list.push(Thread.current)
end
def release
+ MSG::debug("Release "+self.to_s)
Thread.critical = true
begin
if (@counter += 1) <= 0
- t = @waiting_list.shift
- t.wakeup if t
+ t = @waiting_list.shift
+ t.wakeup if t
+ MSG::debug("Wakeup "+t.to_s)
+ else
+ MSG::debug("Nobody to wakeup")
end
rescue ThreadError
retry
########################################################################
# Class Process
########################################################################
-class MsgProcess < Thread
+class MSG::Process < Thread
@@nextProcessId = 0
# Attributes
# Initialize : Used from ApplicationHandler to fill it in
def initialize(*args)
+ # FIXME: use only one variante (the one with 3 args) and kill the others
@schedBegin = Semaphore.new(0)
@schedEnd = Semaphore.new(0)
@properties = Hash.new()
@name = ""
@pargs = Array.new()
start()
- debug "Initializer without any argument"
+ MSG::debug "Initializer without any argument"
}
# 2 arguments: (HostName,Name) Or (Host , Name)
elsif argc == 2
super(){
- debug "Initilize with 2 args"
+ MSG::debug "Initilize with 2 args"
type = args[0].type()
if ( type.to_s == "String")
host = Host.getByName(args[0])
# 3 arguments: (hostName,Name,args[]) or (Host,Name,args[])
elsif argc == 3
super(){
- debug "Initilize with 3 args"
+ MSG::debug "Initilize with 3 args"
type = args[0].type()
if ( type.to_s == "String")
host = Host.getByName(args[0])
def start()
@schedBegin.acquire
# execute the main code of the process
- debug("Begin execution")
+ MSG::debug("Begin execution")
main(@pargs)
processExit(self) # Exit the Native Process
@schedEnd.release
end
- def processList()
+ def processList() (KILLME?)
Thread.list.each {|t| p t}
end
- #Get Own ID
+ #Get Own ID (KILLME?)
def getID()
return @id
end
- #Get a Process ID
+ #Get a Process ID (KILLME?)
def processID(process)
return process.id
end
- #Get Own Name
+ #Get Own Name (KILLME?)
def getName()
return @name
end
- #Get a Process Name
- def processName(process)
- return process.name
- end
-
- #Get Bind
+ #Get Bind (KILLME?)
def getBind()
return @bind
end
- #Get Binds
+ #Get Binds (KILLME?)
def setBind(bind)
@bind = bind
end
@hostName = hostName
@function = function
- debug("onBeginProcess("+hostName+","+function+")")
+ MSG::debug("onBeginProcess("+hostName+","+function+")")
end
def onProperty(id,value)
end
def onEndProcess()
- process = rubyNewInstance(@function)
+ # must be in C, called from a callback to the FlexML parser
+ # newInstance must take args and hostname as argument to initialize everything, *and* bind it to C element
+ # Allows to mark all attributes of process (but properties) to read-only
+ process = MSG::rubyNewInstance(@function)
process.pargs = @args
process.name = @function
- host = Host.getByName(@hostName)
- processCreate(process,host)
+ host = MSG::Host.getByName(@hostName)
+ MSG::processCreate(process,host)
process.properties = @properties
end
end
DEBUG1("%s",s);
}
-// Get Clock
+// Get Clock FIXME: return the double instead of displaying it
static void msg_get_clock(VALUE class) {
printf("Simulation time %f\n",MSG_get_clock());
// 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,"debug",(rb_meth)msg_debug,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);
+ rb_define_module_function(rb_msg,"init",(rb_meth)msg_init,1);
+ rb_define_module_function(rb_msg,"run",(rb_meth)msg_run,0);
+ rb_define_module_function(rb_msg,"createEnvironment",(rb_meth)msg_createEnvironment,1);
+ rb_define_module_function(rb_msg,"deployApplication",(rb_meth)msg_deployApplication,1);
+ rb_define_module_function(rb_msg,"info",(rb_meth)msg_info,1);
+ rb_define_module_function(rb_msg,"debug",(rb_meth)msg_debug,1);
+ rb_define_module_function(rb_msg,"getClock",(rb_meth)msg_get_clock,0);
+ rb_define_module_function(rb_msg,"rubyNewInstance",(rb_meth)msg_new_ruby_instance,1);
+ rb_define_module_function(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_module_function(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_task = rb_define_class_under(rb_msg,"Task",rb_cObject);
rb_host = rb_define_class_under(rb_msg,"Host",rb_cObject);
- //Task Methods
+ //Task Methods FIXME: Convert to 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);
(*env)->ReleaseStringUTFChars(env, jalias, alias);
+ /* FIXME throw the right exception corresponding to HostFailureException, TransferFailureException, TimeoutFailureException
+ * Note: these exceptions must be created beforehand
+ * then, you want to create some functions like jxbt_throw_notbound()
+ * then, you must declare in the MsgNative stuff that these native functions can throw these exceptions
+ */
if (MSG_OK != rv)
jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
package simgrid.msg;
+/* FIXME: Kill the process factory: that's just a storage of informations, AppHandler can do the work itself */
+
import java.util.Vector;
import java.util.Hashtable;
}
}
+ /* FIXME: kill these C crufts */
/** Returns the last error code of the simulation */
public final static native int getErrCode();
package simgrid.msg;
+/* FIXME: split into internal classes of Msg, Task, Host etc. */
+
/**
* Contains all the native methods related to Process, Host and Task.
*/