Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'with_java'
[simgrid.git] / doc / doxygen / bindings.doc
1 /*! \page bindings Bindings
2
3 \section bindings_binding_Ruby Ruby Binding
4 Check online for our specific [Simgrid-Ruby documentation](http://simgrid.gforge.inria.fr/documentation.html).
5
6 \section bindings_binding_Java Java Binding
7 Simgrid-java is a java API that let you use [Simgrid](http://simgrid.gforge.inria.fr/)
8 MSG API in your favorite language (java). Without it, you would be forced to
9 use C or one of the other bindings provided.
10
11 MSG was the first distributed programming environment provided within SimGrid.
12 While almost realistic, it remains quite simple. This describes the Java
13 bindings to this interface.
14
15 The javadoc is accessible [here](javadoc/index.html)
16
17 \subsection bindings_binding_Java_jMSG_who Who should use this (and who shouldn't)
18 You should use MSG if you want to study some heuristics for a given problem you
19 don't really want to implement. SimGrid-java let you use MSG while coding in
20 Java. So if your need is MSG + Java, you're in the right section!
21
22 \subsection SimGrid-java Usage overview 
23
24 To make a long story short, it's a JNI binding, so it implies that:
25 - Most of the MSG and SimGrid documentation about behavioral aspects applies
26   directly to what you are programming. 
27 - MSG data structures are mapped to Java objects. So it means that from the
28   syntax point of view, you have to know how those objects are. Fortunately,
29   we have generated the Javadoc for those objects. So take a look at it
30
31 Finally, it implies also that your program can crash for 3 main reasons: 
32 - Your Java part is not good: you'll have a good old java exception thrown,
33   and hence you should be able to correct it by yourself.
34 - Our java part is not good: you'll also have a java exception thrown, but
35   we have real doubts this can happen, since the java part is only a JNI
36   binding. The other option is that it crashed because you used incorrectly
37   the MSG API, so this means also you should have an MSGException. It means
38   you should read carefully MSG samples and/or documentation.
39 - Something has crashed in the C part. Okay, here comes the tricky thing.
40
41 C crashes mainly for 2 reasons: 
42 - When something goes wrong in your simulation, sometimes the C part stops
43   because you used SimGrid incorrectly, and JNI bindings are not fond of that.
44   It means that you'll have something that looks ugly, but you should be able
45   to identify what's going wrong in your code by carefully reading the whole
46   error message
47 - It may happen that the problem comes directly from SimGrid: in this case,
48   the error should be uglier. In that case, you may submit a bug directly to
49   SimGrid.
50
51 \subsection bindings_binding_java_install How to install Simgrid-java
52
53 To use java with Simgrid you have to install some dependencies:
54 - Simgrid (see [install Simgrid](http://simgrid.gforge.inria.fr/simgrid/latest/doc/install.html).
55   You should set the `SIMGRID_ROOT` environment variable to the path where you installed SimGrid.
56 - Java JDK packages, such as `openjdk7` or `sun-java6-jdk` (with `libgcj10-dev` or another
57   version of gcj). For maximal performance and scalability, use a coroutine-enabled JVM (see
58   \ref bindings_binding_java_coroutines).
59  
60 Then Download and install package Simgrid-java:
61 ~~~~{.sh}
62 git clone git://scm.gforge.inria.fr/simgrid/simgrid-java.git
63 cd simgrid-java
64 cmake -DCMAKE_INSTALL_PREFIX=$HOME/Install/simgrid-java/ .
65 make install
66 ~~~~
67  
68 Cmake output
69 ~~~~{.sh}
70 -- SITE        : Linux_2.6.38-8-generic_x86_64
71 -- BUILDNAME   : Simgrid-Java
72 -- Looking for lib SimGrid
73 -- Looking for lib SimGrid - found
74 -- Simgrid version : 3.8.1
75 -- Looking for gras.h
76 -- Looking for gras.h - found
77 -- Found Tesh: /home/user/Bureau/simgrid/git/bin/tesh
78 -- Found gras_stub_generator: /home/user/Bureau/simgrid/git/bin/gras_stub_generator
79 -- Java version 1.6.0.22 configured successfully!
80 -- Looking for jni.h
81 -- Looking for jni.h - found
82 -- Add flags -I/usr/lib/jvm/java-6-openjdk/include
83 -- Looking for jni_md.h
84 -- Looking for jni_md.h - found
85 -- Found javac: /usr/bin/javac
86 -- Found jar: /usr/bin/jar
87 -- Configuring done
88 -- Generating done
89 -- Build files have been written to: /home/user/workspace/simgrid-java/build
90 ~~~~
91
92 If cmake complains that **jni could not be found**, you need to tell it where
93 JNI header files are located. the following command should tell you:
94
95 ~~~~{.sh}
96 $ locate jni.h
97 /usr/lib/jvm/java-6-openjdk-amd64/include/jni.h
98 /usr/lib/jvm/java-7-openjdk-amd64/include/jni.h
99 ~~~~
100
101 If you have several version of jni installed (as in the example
102 above), you need to check the version of java that is used by default
103 on your machine (using javac -version), and pick the right one. Then
104 set the `JAVA_INCLUDE_PATH` environment variable to the right path (note
105 that we remove the filename `jni.h` from that path), and relaunch cmake.
106
107 ~~~~{.sh}
108 $ export JAVA_INCLUDE_PATH=/usr/lib/jvm/java-6-openjdk-amd64/include/
109 $ cmake .
110 ~~~~
111
112 \subsubsection bindings_binding_java_use How to use Simgrid-java
113
114 To execute the examples you need to add the path where you installed
115 the generated `libSG_java` library (`<build_dir>/lib`) and `libsimgrid`
116 (`$SIMGRID_ROOT/lib`) into the `LD_LIBRARY_PATH`.
117
118 Be careful on Mac, this variable is called `DYLD_LIBRARY_PATH` and not
119 `LD_LIBRARY_PATH`.
120
121 ~~~~{.sh}
122 $ export SIMGRID_ROOT="$HOME/Install/simgrid/" # change it to the path where you installed the SimGrid library
123 $ export SIMGRID_JAVA_ROOT="$HOME/Install/simgrid-java" # change it to the path where you installed the java bindings of SimGrid 
124 $ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$SIMGRID_ROOT/lib:$SIMGRID_JAVA_ROOT/java
125 $ cd examples
126 $ java -cp .:../simgrid.jar basic/BasicTest platform.xml basic/basicDeployment.xml
127 ~~~~
128
129 If you want to make these settings permanent even after a reboot, you
130 need to add the export lines into your `~/.bashrc` file, or equivalent.
131
132 \subsubsection bindings_binding_java_coroutines How to use the coroutines context factory
133
134 There is two main motivations to use the coroutine variant of SimGrid
135 Java bindings: it's about 5 times faster than the default thread-based
136 context factory, and the amount of runnable processes is then only
137 limited by the amount of RAM that you have. The drawbacks are that it
138 requires a specific and rather experimental JVM to run, and that this
139 context factory itself remains a bit experimental so far.
140
141 \subsubsection  bindings_java_coro_install Getting a mlvm JVM
142
143 You need to get a patched JVM from [here](http://ssw.jku.at/General/Staff/LS/coro/)
144 (many thanks to Lukas Stadler for this work!). 
145
146 You can either get a prebuilt binary, or recompile your own JVM. Make
147 sure to get a coro-simple version, as we don't need to serialize nor
148 migrate stacks in SimGrid. You should be able to follow the `README.txt`
149 that you'll get in the repository, but here is how we did it, just in
150 case. The instructions are given for a debian or Ubuntu box, but I
151 think you should manage to convert it to your system quite easily.
152 Finally, if you're really stuck, you can get the version compiled by
153 Jonathan Rouzaud-Cornabas from his web page. This version is known to
154 work with SimGrid for sure!
155 http://graal.ens-lyon.fr/~jrouzaud/files/corosimple-linux-amd64-20120914.tgz
156  
157  -# Install mercurial and some dependencies
158 ~~~~{.sh}
159 sudo apt-get install mercurial ksh libfreetype6-dev libcups2-dev libasound2-dev gawk openjdk-7-jdk libxext-dev libxrender-dev libxtst-dev
160 # Grab the forest extension: we need to source-install it
161 hg clone https://bitbucket.org/gxti/hgforest hgforest \endverbatim
162 ~~~~
163  -# Configure the mercurial extensions: Edit ~/.hgrc and paste the
164     following lines. Don't forget to change the /path/to/forest.py to
165     point to where you just downloaded the source. 
166     
167     Forest extension is needed to download the openjdk source code and
168     patches while the mq line is needed to apply the patches. The
169     username is needed at the step "preparing the sources", not sure why.
170 ~~~~
171 [ui]
172 username = YouUserameWithoutSpaces
173 [extensions]
174 forest=/path/to/forest.py
175 mq=
176 ~~~~
177  -# Prepare the source code
178 ~~~~{.sh}
179 # create a working directory, and enter it
180 mkdir davinci; cd davinci
181
182 # Grab the sources
183 hg fclone http://hg.openjdk.java.net/hsx/hotspot-comp sources
184 # Grab the patches
185 hg fclone http://hg.openjdk.java.net/mlvm/mlvm patches
186
187 # Link the patch directories into the sources
188 bash patches/make/link-patch-dirs.sh sources patches
189 # Test wether the previous command worked with
190 ls -i patches/hotspot/series sources/hotspot/.hg/patches/series
191 # It should display something like the following.
192 # (note that both file share the same inode number)
193 #  9707849 patches/hotspot/series  
194 #  9707849 sources/hotspot/.hg/patches/series
195
196
197 # Specify what to compile. 
198 export davinci=$(pwd) guards="buildable testable coro-simple"
199 # Apply the patches
200 sh patches/make/each-patch-repo.sh hg qselect --reapply $guards '$(sh $davinci/patches/make/current-release.sh)'
201 # Check that it understood that you want the patch applied:
202 grep GLOBAL_GUARDS patches/make/* -r
203 # this should display something like the following (maybe amonst other unrelated lines)
204 # GLOBAL_GUARDS=buildable testable coro-simple
205 # If this does not work, edit patches/make/Makefile,
206 #   manually coro-simple to GLOBAL_GUARDS and then 
207 #   rerun the patches/make/each-patch-repo.sh script as earlier
208
209
210 # Finish the setup
211 (cd patches/make; make setup && make force && make && make FORCE_VERSIONS=1 && echo "Sources are properly setup")
212 # If this last command failed, check your mercurial config within ~/.hgrc (see above)
213 ~~~~       
214  -# Compile it all
215 ~~~~{.sh}
216 unset LD_LIBRARY_PATH
217 export ALT_BOOTDIR=/usr/lib/jvm/java-7-openjdk-amd64/
218 cd sources
219 # Check that everything is fine
220 make sanity
221 # Go for it (it takes about half an hour on my machine)
222 make all
223
224 # Check that the coroutine library got compiled in
225 ls sources/build/linux-amd64/classes/java/dyn/
226 # This should display a bunch of class files. If not, something went wrong, you need to investigate further
227 ~~~~
228
229 \subsubsection  bindings_java_coro_use Using coroutine contexts
230
231 SimGrid Java will automatically switch to the coroutine context
232 factory if your JVM support it, so you will just need to execute your
233 simulation with the correct JVM. The selected context factory gets
234 displayed automatically.
235 ~~~~{.sh}
236 export LD_LIBRARY_PATH=/path/to/simgrid.so:/path/to/libSG_java.so
237 cd examples
238 $PATH_TO_COROUTINE_JVM/java -cp .:../simgrid.jar masterslave.Masterslave masterslave/ masterslaveDeployment.xml platform.xml
239 ~~~~
240
241 Note that you may have to adjust the "coro.stacksPerThread"
242 configuration option to run large simulations. The default is 100 and
243 you want to increase it to run more processes.
244 ~~~~{.sh}
245 $ $PATH_TO_COROUTINE_JVM/java -Dcoro.stacksPerThread=$STACKS_NUMBER -cp .:../simgrid.jar basic/BasicTest platform.xml basic/basicDeployment.xml
246 ~~~~
247
248 If you reach the point where the creation of new simulated processes
249 fail with the message "Can't create coroutine object", you may need to
250 increase the relevant system limit with the following command.
251 ~~~~{.sh}
252 sysctl -w vm.max_map_count = 131072
253 ~~~~
254
255 The full story is that each coroutine requires two memory maps, and
256 that Linux puts a limit on the total amount of memory maps that each
257 process can manage (by default, this limit is often at 65535). Since
258 the JVM needs a few dozen of such maps on its own (three maps per
259 dynamic library -- check `/proc/the_pid/maps` if you don't believe it),
260 this is enough to create over 30,000 simulated processes. But to go
261 futher, that limit must be modified. 
262
263 If you want to make this change permanent on your machine, edit your
264 `/etc/sysctl.conf` file. Otherwise, you have to redo it by calling
265 sysctl after each reboot.
266
267 \section bindings_binding_lua Lua Binding
268
269 Most of Simgrid modules require a  good level in C programming, since simgrid is used to be as standard C library.
270  Sometime users prefer using some kind of “easy scripts” or a language easier to code with, for their works,
271  which avoid dealing with C errors, and sometime an important  gain of time.
272 Besides Java Binding, Lua  and Ruby bindings are available since version 3.4 of Simgrid
273 for MSG Module, and we are currenlty working on bindings for other modules.
274
275 \subsection bindings_binding_lua_about What is lua ?
276 Lua is a lightweight, reflective, imperative and functional programming language,
277  designed as a scripting language with extensible semantics as a primary goal (see official web site <a href="http://www.lua.org">here</a>).
278 \subsubsection bindings_binding_lua_why Why lua ?
279 Lua is a fast, portable and powerful script language, quite simple to use for developpers.
280 it combines procedural features with powerful data description facilities,
281  by using a simple, yet powerful, mechanism of tables.
282 Lua has a relatively simple C API compared to other scripting languages,
283 and accordingly it provides a robust, easy to use it.
284 \subsubsection bindings_binding_lua_simgrid How to use lua in Simgrid ?
285 Actually, the use of lua in Simgrid is quite simple, you have just to follow the same steps as coding with C in Simgird :
286   - Coding functions coresponding to each process
287   - loading the platforme/deployment XML file that describe the environment of simulation
288   - and … Running the Simulation.
289
290 \dontinclude lua/masterslave/master.lua
291 \subsection bindings_binding_lua_example_master_slave Master/Slave Example
292
293  \li Master Code
294  \until end_of_master
295 we mainly  use   simgrid.Task.new(task_name,computation_size,communication_size) to create our MSG Task,
296          then simgrid.Task.send(task,alias) to send it.
297 we use also simgrid.Task.name(task), to get the task's name.
298
299 \dontinclude lua/masterslave/slave.lua
300 \li Slave Code
301 \until end_of_slave
302 Here, we see the use of simgrid.Task.recv(alias) to receive a task with a specific alias,
303 this function return directly the task recevied.
304
305 \dontinclude lua/masterslave/master_slave.lua
306 \li Set Environmenet and run application
307 \until end-of-master-slave
308
309 \subsection bindings_binding_lua_example_data Exchanging Data
310 You can also exchange data between Process using lua. for that, you have to deal with lua task as a table,
311 since lua is based itself on a mechanism of tables,
312 so you can exchange any kind of data (tables, matrix, strings,…) between process via tasks.
313
314 \li Sender process
315 \verbatim
316   task = simgrid.Task.new("data_task",task_comp,task_comm);
317   task['matrix'] = my_matrix;
318   task['table'] = my_table;
319   task['message'] = "Hello from (Lua || Simgrid ) !! "
320   …
321   simgrid.Task.send(task,alias)
322 \endverbatim
323         After creating task, we associate to it various kind of data with a specific key (string in this case)
324         to distinguish between data variables. The receiver will use this key to access easily to datas.
325
326
327 \li Receiver processe
328 \verbatim
329   task = simgrid.Task.recv(alias);
330   sender_matrix = task['matrix'];
331   sender_table = task['table'];
332   sender_message = task['message']
333   ...
334 \endverbatim
335         Note that in lua, both sender and receiver share the same lua task.
336         So that the receiver could joint data directly on the received task without sending it back.
337         You can find  a complet example (matrix multiplication case) in the file example/lua/mult_matrix.lua.
338
339
340 \subsection bindings_binding_lua_example_bypass Bypass XML
341         maybe you wonder if there is a way to bypass the XML files,
342          and describe your platform directly from the code, with lua bindings it's Possible !! how ?
343         We provide some additional (tricky?) functions in lua that allows you to set up your own platform without using the XML files
344      ( this can be useful for large platforms, so a simple for loop will avoid you to deal with an annoying XML File ;) )
345
346
347 \li set Routing mode
348 \verbatim
349    simgrid.AS.new{id="AS0",mode="Full"};
350 \endverbatim
351
352 \li set Hosts
353 \verbatim
354   simgrid.Host.new{id="Tremblay",power=98095000};
355   simgrid.Host.new{id="Jupiter",power=76296000};
356   simgrid.Host.new{id="Fafard",power=76296000};
357   simgrid.Host.new{id="Ginette",power=48492000};
358   simgrid.Host.new{id="Bourassa",power=48492000};
359 \endverbatim
360   we use simgrid.Host.new{id=id_host,power=power_host} to instanciate our hosts.
361
362 \li set Links
363 \verbatim
364   for i=0,11 do
365     simgrid.Link.new{id=i,bandwidth=252750+ i*768,latency=0.000270544+i*0.087};    --  some crazy values ;)
366   end
367 \endverbatim
368   we used simgrid.Link.new{id=link_id,bandwidth=bw,latency=lat} with a simple for loop to create all links we need (much easier than XML hein ?)
369
370 \li set Routes
371 \verbatim
372 -- simgrid.Route.new(src_id,des_id,links_nb,links_list)
373    simgrid.Route.new("Tremblay","Jupiter",1,{"1"});
374    simgrid.Route.new("Tremblay","Fafard",6,{"0","1","2","3","4","8"});
375    simgrid.Route.new("Tremblay","Ginette",3,{"3","4","5"});
376    simgrid.Route.new("Tremblay","Bourassa",7,{"0","1","3","2","4","6","7"});
377
378    simgrid.Route.new("Jupiter","Tremblay",1,{"1"});
379    simgrid.Route.new("Jupiter","Fafard",7,{"0","1","2","3","4","8","9"});
380    simgrid.Route.new("Jupiter","Ginette",4,{"3","4","5","9"});
381    simgrid.Route.new("Jupiter","Bourassa",8,{"0","1","2","3","4","6","7","9"});
382    ...
383 \endverbatim
384   for each host you have to specify which route to choose to access to the rest of hosts connected in the grid.
385
386 \li Save platform
387 \verbatim
388   simgrid.register_platform();
389 \endverbatim
390 Don't forget to register your platform, that SURF callbacks starts their work ;)
391
392 \li set application
393 \verbatim
394    simgrid.Host.setFunction("Tremblay","Master",4,{"20","550000000","1000000","4"});
395    simgrid.Host.setFunction("Bourassa","Slave",1,{"0"});
396    simgrid.Host.setFunction("Jupiter","Slave",1,{"1"});
397    simgrid.Host.setFunction("Fafard","Slave",1,{"2"});
398    simgrid.Host.setFunction("Ginette","Slave",1,{"3"});
399 \endverbatim
400   you don't  need to use a deployment XML file, thanks to  simgrid.Host.setFunction(host_id,function,args_number,args_list)
401   you can associate functions for each host with arguments if needed .
402
403 \li
404 \verbatim
405    simgrid.register_application();
406 \endverbatim
407 Yes, Here too you have to register your application before running the simulation.
408
409 the full example is distributed in the file examples/lua/master_slave_bypass.lua
410
411 \subsection MSG_ex_master_slave_lua Master/slave Lua application
412
413 Simulation of a master-slave application using lua bindings    
414 - \ref MSG_ext_ms_master_lua
415 - \ref MSG_ext_ms_slave_lua
416 - \ref MSG_ext_ms_core_lua
417 - \ref MSG_ext_ms_helping
418 - \ref MSG_ext_ms_application
419 - \ref MSG_ext_ms_platform
420
421
422      
423 \subsubsection MSG_ext_ms_master_lua Master code
424
425 as described in the C native master/Slave example, this function has to be assigned to a msg_process_t that will behave as the master.
426
427 Lua style arguments (...) in for the master are interpreted as:
428 - the number of tasks to distribute
429 - the computation size of each task
430 - the size of the files associated to each task
431 - a list of host that will accept those tasks.
432
433 Tasks are dumbly sent in a round-robin style.
434 \dontinclude lua/masterslave/master.lua
435 \skip Dispatch the tasks
436 \until Done sending
437 \until end
438
439
440 \subsubsection MSG_ext_ms_slave_lua Slave code
441
442 This function has to be assigned to a #msg_process_t that has to behave as a slave.
443 This function keeps waiting for tasks and executes them as it receives them.
444 \dontinclude lua/masterslave/slave.lua
445 \until end_of_slave
446 \subsubsection MSG_ext_ms_core_lua Simulation core
447
448 in this section the core of the simulation which start by including the simgrid lib for bindings
449 : <i>require "simgrid" </i>
450
451 -# Simulation settings : <i>simgrid.platform</i> creates a realistic
452    environment
453 -# Application deployment : create the processes on the right locations with
454     <i>simgrid.application</i>
455 -# The simulation is run with <i>simgrid.run</i>
456
457 Its arguments are:
458 - <i>platform_file</i>: the name of a file containing an valid surfxml platform description.( first command line argument)
459 - <i>application_file</i>: the name of a file containing a valid surfxml application description ( second commande line argument )
460 \dontinclude lua/masterslave/master_slave.lua
461 \skip platform
462 \until run
463
464
465 \subsection MSG_ex_master_slave_lua_bypass Master/slave Bypass Lua application
466
467 Simulation of a master-slave application using lua bindings, Bypassing the XML parser
468 - \ref MSG_ext_ms_bp_master_lua
469 - \ref MSG_ext_ms_bp_slave_lua
470 - \ref MSG_ext_ms_bp_core_lua
471
472       
473
474 \subsubsection MSG_ext_ms_bp_master_lua Master code
475
476 as described in the C native master/Slave example, this function has to be assigned to a msg_process_t that will behave as the master.
477
478 Lua style arguments (...) in for the master are interpreted as:
479 - the number of tasks to distribute
480 - the computation size of each task
481 - the size of the files associated to each task
482 - a list of host that will accept those tasks.
483
484 Tasks are dumbly sent in a round-robin style.
485
486 \dontinclude lua/console/master.lua
487 \until end_of_master
488
489 \subsubsection MSG_ext_ms_bp_slave_lua Slave code
490
491 This function has to be assigned to a #msg_process_t that has to behave as a slave.
492 This function keeps waiting for tasks and executes them as it receives them.
493
494 \dontinclude lua/console/slave.lua
495 \until end_of_slave
496
497 \subsubsection MSG_ext_ms_bp_core_lua Simulation core
498
499 in this section the core of the simulation which start by including the simgrid lib for bindings, then create the resources we need to set up our environment bypassing the XML parser.
500 : <i>require "simgrid" </i>
501
502 -# Hosts : <i>simgrid.Host.new</i> instanciate a new host with an id, and power.
503 -# Links : <i>simgrid.Link.new</i> instanictae a new link that will require an id, bandwith and latency values.
504 -# Route : <i>simgrid.Route.new</i> define a route between two hosts specifying the links to use.
505 -# Simulation settings : <i>simgrid.register_platform();</i> register own platform without using the XML SURF parser.
506
507 we can also bypass the XML deployment file, and associate functions for each of defined hosts.
508 - <i>simgrid.Host.setFunction</i>: associate a function to a host, specifying arguments if needed.
509 - <i>simgrid.register_application()</i>: saving the deployment settings before running the simualtion.
510
511 \include lua/console/master_slave_bypass.lua
512
513
514  */