From: mquinson Date: Fri, 15 Jan 2010 15:31:12 +0000 (+0000) Subject: first working prototype of the lua bindings. Veeeeeery crude at this point X-Git-Tag: SVN~720 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/d2b7bc8b012af5858b6dba6ba5285f5e29d12e70?hp=ce7fc8b7a8a23fabcb30bb3846043442927eb013 first working prototype of the lua bindings. Veeeeeery crude at this point git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7010 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/bindings/lua/Msglua.h b/src/bindings/lua/Msglua.c similarity index 83% rename from src/bindings/lua/Msglua.h rename to src/bindings/lua/Msglua.c index 1c484dd909..3b4dd08bef 100644 --- a/src/bindings/lua/Msglua.h +++ b/src/bindings/lua/Msglua.c @@ -4,7 +4,8 @@ // Msg Includes #include -#include "msg/msg.h" +#include "msg/msg.h" +#include "msg/datatypes.h" #include "xbt/sysdep.h" #include "xbt/log.h" #include "xbt/asserts.h" @@ -20,17 +21,57 @@ ============================================= */ +XBT_LOG_NEW_DEFAULT_CATEGORY(lua,"Lua bindings"); - -#define TASK "Task" -#define HOST "Host" -#define PROCESS "Process" +#define TASK "Msg.Task" +#define HOST "Msg.Host" +#define PROCESS "Msg.Process" typedef m_task_t Task; typedef m_host_t Host; typedef m_process_t Process; + +static void stackDump (lua_State *L) { + int i; + int top = lua_gettop(L); + void *p; + fflush(stdout); + return; + INFO0("That's me"); + printf("STACK(top=%d): ",top); + for (i = 1; i <= top; i++) { /* repeat for each level */ + int t = lua_type(L, i); + switch (t) { + + case LUA_TSTRING: /* strings */ + printf("`%s'", lua_tostring(L, i)); + break; + + case LUA_TBOOLEAN: /* booleans */ + printf(lua_toboolean(L, i) ? "true" : "false"); + break; + + case LUA_TNUMBER: /* numbers */ + printf("%g", lua_tonumber(L, i)); + break; + + default: /* other values */ + if ((p = luaL_checkudata(L,i,TASK))) { + printf("task"); + } else { + printf("%s", lua_typename(L, t)); + } + break; + + } + printf(" "); /* put a separator */ + } + printf("\n"); /* end the listing */ + fflush(stdout); +} + //**********************************************************TASK SECTION*************************************************** @@ -77,7 +118,7 @@ static Task *pushTask (lua_State *L,Task tk) static int Task_new(lua_State* L) { - char *name=luaL_checkstring(L,1); + const char *name=luaL_checkstring(L,1); int comp_size = luaL_checkint(L,2); int msg_size = luaL_checkint(L,3); // We Will Set The Data as a Null for The Moment @@ -179,7 +220,7 @@ static Host *pushHost (lua_State *L,Host ht) static int Host_new(lua_State* L) { - char *host_name=luaL_checkstring(L,1); + const char *host_name=luaL_checkstring(L,1); pushHost(L,MSG_get_host_by_name(host_name)); return 1; } @@ -240,7 +281,7 @@ m_process_t MSG_process_create ( const char * name, static int Process_new(lua_State* L) { - char *name=luaL_checkstring(L,1); + const char *name=luaL_checkstring(L,1); //int code = luaL_checkint(L,2); //xbt_main_func_t << code // We Will Set The Data as a Null for The Moment @@ -264,25 +305,39 @@ static int Process_kill(lua_State *L) /** * Function to Send a Task */ -static void Task_send(lua_State *L) { +static int Task_send(lua_State *L) { Task tk = checkTask(L,1); - char *mailbox = luaL_checkstring(L,2); - + const char *mailbox = luaL_checkstring(L,2); + stackDump(L); int res = MSG_task_send(tk,mailbox); + stackDump(L); + return 0; } /** -* Function to Get ( Recieve !! ) a Task +* Function Recieve a Task */ -static int Task_recv(lua_State *L) { - m_task_t tk = NULL; +static int Task_recv2(lua_State *L) { - char *mailbox = luaL_checkstring(L,1); + Task *tk = (Task *)checkTask(L,1); + const char *mailbox = luaL_checkstring(L,2); + MSG_task_receive(tk,mailbox); + printf("Task Name Within Receive Fct: >>>%s\n",MSG_task_get_name(*tk)); + + return 0; +} + + +static int Task_recv(lua_State *L) { + Task tk = NULL; + //stackDump(L); + const char *mailbox = luaL_checkstring(L,1); int res = MSG_task_receive(&tk,mailbox); + INFO1("Task Name : >>>%s",MSG_task_get_name(tk)); pushTask(L,tk); +// stackDump(L); return 1; } - //******************************************************* PLATFORM & APPLICATION SECTION **************************************************************** @@ -338,6 +393,7 @@ static const luaL_reg Task_methods[] = { {"destroy", Task_destroy}, {"send", Task_send}, {"recv", Task_recv}, +{"recv2", Task_recv2}, {0,0} }; @@ -364,8 +420,8 @@ static const luaL_reg Process_methods[] = { static int Task_gc(lua_State *L) { Task tk=toTask(L,1); - if (tk) MSG_task_destroy(tk); - printf("GoodBye Task(%p)\n",lua_touserdata(L,1)); + //if (tk) MSG_task_destroy(tk); + //printf("GoodBye Task(%p)\n",lua_touserdata(L,1)); return 0; } diff --git a/src/bindings/lua/doit b/src/bindings/lua/doit new file mode 100755 index 0000000000..a504a989cf --- /dev/null +++ b/src/bindings/lua/doit @@ -0,0 +1,4 @@ +set -e +make -C ../../ +gcc -g3 -o runner tasktest.c Msglua.c -I ../../../include/ -I /usr/include/lua5.1/ -I . -L../../.libs -ldl -llua5.1 -lsimgrid +valgrind -q ./runner ../../../examples/msg/small_platform.xml deploy.xml tasklua.lua diff --git a/src/bindings/lua/tasklua.lua b/src/bindings/lua/tasklua.lua index ece8bb31f6..511f78979f 100644 --- a/src/bindings/lua/tasklua.lua +++ b/src/bindings/lua/tasklua.lua @@ -1,12 +1,3 @@ -function getn(t) - if type(t.n) == "number" then return t.n end - local max=0 - for i,_ in to do - if type(i) == "number" and i>max then max=i end - end - t.n = max - return max -end --Master Function function master(...) @@ -15,54 +6,38 @@ print("Hello from lua, I'm the master") for i,v in ipairs(arg) do print("Got "..v) end -t_tasks={} --tasks table -t_slaves={} --slaves table nb_task = arg[1]; comp_size = arg[2]; comm_size = arg[3]; - --- Let's Create the tasks to dispatch -for i=1,nb_task do - t_tasks[i] = Task.new("Task "..i,comp_size,comm_size); --data set to NULL -end +slave_count = arg[4] --- Process Organisation +argc=#arg +print("Argc="..argc.." (should be 4)") -argc=getn(arg) -print("Argc="..argc.." (should be 8)") +-- Dispatch the tasks -slave_count = getn(arg) -3; - -for i=4,argc do -slv_name = arg[i]; -t_slaves[i - 3] = Host.new(slv_name); --- do not do directly that : t_slaves[i - 4] = Host.new(argv[i]); +for i=1,nb_task do + tk = Msg.Task.new("Task "..i,comp_size,comm_size); + alias = "slave "..(i%slave_count); + print("Master sending '" .. Msg.Task.name(tk) .."' To '" .. alias .."'"); + Msg.Task.send(tk,alias); -- C user data set to NULL + print("Master done sending '".. Msg.Task.name(tk) .."' To '" .. alias .."'"); end - - - --- Print List Of Tasks / Slaves --[[ -for i=1,nb_task do -todo = Task.name(t_tasks[i]); -print(i % slave_count+1); -slv = Host.name(t_slaves[i % slave_count+1]); -print ( "Sending : " .. todo .. " To : " .. slv); -end -]]-- - - +Sending Finalize Message To Others +--]] -for i=1,nb_task do -tk_name = Task.name(t_tasks[i]); -ht_name = Host.name(t_slaves[i]); -print("Sending " .. tk_name .." To " .. ht_name); -Task.send(t_task[i],"slave "..(i%slave_count)); -print("Sent"); +print("Master: All tasks have been dispatched. Let's tell everybody the computation is over."); +for i=0,slave_count-1 do + alias = "slave "..i; + print("Master: sending finalize to "..alias); + Msg.Task.send(Msg.Task.new("finalize",0,0),alias); end +print("Master: Everything's done."); + end @@ -72,24 +47,26 @@ function slave(...) my_mailbox="slave "..arg[1] print("Hello from lua, I'm a poor slave with mbox: "..my_mailbox) + while true do - tk = Task.recv(my_mailbox); - print("Got something"); - --testing res !!!! - tk_name = Task.name(tk) +-- tk = Msg.Task.new("",0,0); --?? +-- Msg.Task.recv2(tk,my_mailbox); + tk = Msg.Task.recv(my_mailbox); + + tk_name = Msg.Task.name(tk) if (tk_name == "finalize") then - Task.destroy(tk); - end + print("Slave '" ..my_mailbox.."' got finalize msg"); + break + end - print("Processing "..Task.name(tk)) - Task.execute(tk); + print("Slave '" ..my_mailbox.."' processing "..Msg.Task.name(tk)) + Msg.Task.execute(tk); - print(Task.name(tk) .. "Done") - Task.destroy(tk); + print("Slave '" ..my_mailbox.."': task "..Msg.Task.name(tk) .. " done") end -- while -print("I'm Done . See You !!"); +print("Slave '" ..my_mailbox.."': I'm Done . See You !!"); end -- function ---------------------------------------------------------- --]] diff --git a/src/bindings/lua/tasktest.c b/src/bindings/lua/tasktest.c index 2d90301af8..c774842245 100644 --- a/src/bindings/lua/tasktest.c +++ b/src/bindings/lua/tasktest.c @@ -1,4 +1,14 @@ -#include "Msglua.h" +#include +#include "lauxlib.h" +#include "lualib.h" + +// Msg Includes +#include +#include "msg/msg.h" +#include "msg/datatypes.h" +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "xbt/asserts.h" @@ -8,7 +18,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, int master_lua(int argc, char *argv[]); int slave_lua(int argc, char *argv[]); -int load_lua(char * file); +//int load_lua(char * file); //int forwarder(int argc, char *argv[]); LUA MSG_error_t test_all(const char *platform_file, const char *application_file); @@ -19,12 +29,9 @@ typedef enum { } channel_t; -lua_State *L; - +char *lua_file; //***************************** LOAD LUA ************************************************* -int load_lua(char * luaFile) { - L = lua_open(); - +static int load_lua(char * luaFile, lua_State *L) { luaL_openlibs(L); // Lua Stuff @@ -42,8 +49,8 @@ int load_lua(char * luaFile) { } int lua_wrapper(int argc, char *argv[]) { - // Table that Lua will read to read Arguments - lua_newtable(L); + lua_State *L = lua_open(); + load_lua(lua_file, L); // Seek the right lua function lua_getglobal(L,argv[0]); @@ -63,6 +70,7 @@ int lua_wrapper(int argc, char *argv[]) { // User process terminated lua_pop(L, 1); + lua_close(L); return 0; } @@ -86,19 +94,19 @@ int main(int argc,char * argv[]) } - load_lua(argv[3]); + lua_file=argv[3]; +// load_lua(argv[3]); /* MSG_config("surf_workstation_model","KCCFLN05"); */ MSG_create_environment(argv[1]); MSG_function_register_default(&lua_wrapper); MSG_launch_application(argv[2]); - MSG_set_channel_number(10); res = MSG_main(); + fflush(stdout); INFO1("Simulation time %g", MSG_get_clock()); MSG_clean(); - lua_close(L); if (res == MSG_OK) return 0;