From: Martin Quinson Date: Wed, 26 Dec 2018 23:46:44 +0000 (+0100) Subject: python: First working example X-Git-Tag: v3_22~756 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/2fb1fa1697309cad26ab7d472db4f8bfa617e0b4?hp=b926615d2e02b1077d531e37297a18a67009ed5a;ds=sidebyside python: First working example --- diff --git a/docs/source/img/lang_cpp.png b/docs/source/img/lang_cpp.png new file mode 100644 index 0000000000..ad7a89a015 Binary files /dev/null and b/docs/source/img/lang_cpp.png differ diff --git a/docs/source/img/lang_python.png b/docs/source/img/lang_python.png new file mode 100644 index 0000000000..585bc0d340 Binary files /dev/null and b/docs/source/img/lang_python.png differ diff --git a/examples/python/CMakeLists.txt b/examples/python/CMakeLists.txt new file mode 100644 index 0000000000..e38a65f249 --- /dev/null +++ b/examples/python/CMakeLists.txt @@ -0,0 +1,10 @@ +foreach(example exec-basic) + set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.tesh) + set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.py) + + ADD_TESH(python-${example} --setenv srcdir=${example} + --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms + --setenv LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib + --setenv PYTHONPATH=${CMAKE_BINARY_DIR}/lib + ${CMAKE_HOME_DIRECTORY}/examples/python/${example}/${example}.tesh) +endforeach() \ No newline at end of file diff --git a/examples/python/exec-basic/exec-basic.py b/examples/python/exec-basic/exec-basic.py new file mode 100644 index 0000000000..0d609653d3 --- /dev/null +++ b/examples/python/exec-basic/exec-basic.py @@ -0,0 +1,39 @@ +# Copyright (c) 2018. The SimGrid Team. All rights 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. + +import sys +import simgrid as sg + +def executor(): + # execute() tells SimGrid to pause the calling actor until + # its host has computed the amount of flops passed as a parameter + sg.execute(98095) + sg.info("Done.") + # This simple example does not do anything beyond that + +def privileged(): + # This version of execute() with two parameters specifies that this execution + # gets a larger share of the resource. + # + # Since the priority is 2, it computes twice as fast as a regular one. + # + # So instead of a half/half sharing between the two executions, + # we get a 1/3 vs 2/3 sharing. + sg.execute(98095, 2); + sg.info("Done."); + + # Note that the timings printed when executing this example are a bit misleading, + # because the uneven sharing only last until the privileged actor ends. + # After this point, the unprivileged one gets 100% of the CPU and finishes + # quite quickly. + +i = sys.argv.index("--") +e = sg.Engine(sys.argv[0:i]) +e.load_platform(sys.argv[i+1]) + +sg.create_actor("executor", sg.Host.by_name("Tremblay"), executor) +sg.create_actor("privileged", sg.Host.by_name("Tremblay"), privileged) + +e.run() \ No newline at end of file diff --git a/examples/python/exec-basic/exec-basic.tesh b/examples/python/exec-basic/exec-basic.tesh new file mode 100644 index 0000000000..07e77b9633 --- /dev/null +++ b/examples/python/exec-basic/exec-basic.tesh @@ -0,0 +1,6 @@ +#!/usr/bin/env tesh + +p Start remote processes +$ python3 ${srcdir}/exec-basic.py -- ${platfdir}/small_platform.xml +> [Tremblay:privileged:(2) 0.001500] [python/INFO] Done. +> [Tremblay:executor:(1) 0.002000] [python/INFO] Done. diff --git a/examples/s4u/README.rst b/examples/s4u/README.rst index b1b996da8b..e730f217b9 100644 --- a/examples/s4u/README.rst +++ b/examples/s4u/README.rst @@ -16,8 +16,8 @@ SimGrid comes with an extensive set of examples, documented on this page. Most of them only demonstrate one single feature, with some larger examplars listed below. -Each of these examples can be found in a subdirectory under -examples/s4u in the archive. It contains the source code (also listed +The C++ examples can be found under examples/s4u while python examples +are in examples/python. Each such directory contains the source code (also listed from this page), and the so-called tesh file containing how to call the binary obtained by compiling this example and also the expected output. Tesh files are used to turn each of our examples into an @@ -151,7 +151,8 @@ Executions on the CPU the actor until a given amount of flops gets computed on its simulated host. Some executions can be given an higher priority so that they get more resources. - |br| `examples/s4u/exec-basic/s4u-exec-basic.cpp `_ + |br| |cpp| `examples/s4u/exec-basic/s4u-exec-basic.cpp `_ + |br| |py| `examples/python/exec-basic/exec-basic.py `_ - **Asynchronous execution:** You can start asynchronous executions, just like you would fire @@ -340,3 +341,11 @@ Distributed Hash Tables (DHT) .. |br| raw:: html
+ +.. |cpp| image:: /img/lang_cpp.png + :align: middle + :width: 12 + +.. |py| image:: /img/lang_python.png + :align: middle + :width: 12 diff --git a/src/bindings/python/simgrid_python.cpp b/src/bindings/python/simgrid_python.cpp index aace9c9162..4a9fd8759e 100644 --- a/src/bindings/python/simgrid_python.cpp +++ b/src/bindings/python/simgrid_python.cpp @@ -23,7 +23,7 @@ using simgrid::s4u::ActorPtr; using simgrid::s4u::Engine; using simgrid::s4u::Host; -XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_python, "S4U python"); +XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python"); PYBIND11_DECLARE_HOLDER_TYPE(T, boost::intrusive_ptr); @@ -52,6 +52,8 @@ PYBIND11_MODULE(simgrid, m) /* this_actor namespace */ m.def("execute", py::overload_cast(&simgrid::s4u::this_actor::execute), "Block the actor, computing the given amount of flops"); + m.def("execute", py::overload_cast(&simgrid::s4u::this_actor::execute), + "Block the actor, computing the given amount of flops at the given priority"); m.def("yield_", &simgrid::s4u::this_actor::yield, "Yield the actor"); /* Class Engine */ diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index e05bf0f41f..39c8d01b25 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -905,6 +905,8 @@ set(DOC_SOURCES docs/source/img/extlink.png docs/source/img/extlink.svg docs/source/img/graphical-toc.svg + source/img/lang_cpp.png + source/img/lang_python.png docs/source/img/smpi_simgrid_alltoall_pair_16.png docs/source/img/smpi_simgrid_alltoall_ring_16.png docs/source/img/zone_hierarchy.png @@ -1008,6 +1010,7 @@ set(CMAKEFILES_TXT examples/java/CMakeLists.txt examples/msg/CMakeLists.txt examples/msg/mc/CMakeLists.txt + examples/python/CMakeLists.txt examples/s4u/CMakeLists.txt examples/simdag/CMakeLists.txt examples/smpi/CMakeLists.txt