-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
embed python into Application module
- Loading branch information
Showing
19 changed files
with
175 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
compile_commands.json | ||
*.plan | ||
results | ||
.oppfeaturestate | ||
|
||
### coverage files | ||
quisp/coverage* | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<features cppSourceRoots = "src" definesFile = "quisp/features.h"> | ||
<feature | ||
id = "EmbeddedPython" | ||
name = "Embedded Python" | ||
description = "this allows user to run python code in a simulation" | ||
initiallyEnabled = "false" | ||
extraSourceFolders = "" | ||
compileFlags = "-DENABLE_PYTHON" | ||
linkerFlags = "" | ||
/> | ||
|
||
</features> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ out/ | |
.oppfeaturestate | ||
quisp.dSYM/ | ||
Test* | ||
feature_defines.h | ||
|
||
PhotonicQubit_m.cc | ||
PhotonicQubit_m.h | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
. | ||
./channels | ||
./modules | ||
./networks | ||
channels | ||
modules | ||
networks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import opp | ||
import application | ||
|
||
def init_app_module(app): | ||
num_resources = app.par("distant_measure_count").int() | ||
pattern = app.par("TrafficPattern").int() | ||
initiator_addr = app.par("LoneInitiatorAddress").int() | ||
|
||
if pattern == 1 and app.my_address == initiator_addr: | ||
dest_addr = app.get_one_random_endnode_addr() | ||
pk = app.create_conn_setup_req(dest_addr,num_resources) | ||
app.schedule_at(opp.sim_time(), pk) | ||
if pattern == 2: | ||
dest_addr = app.get_one_random_endnode_addr() | ||
pk = app.create_conn_setup_req(dest_addr,num_resources) | ||
app.schedule_at(opp.sim_time() + 0.00001 * app.my_address, pk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "PythonEmbeddedable.h" | ||
#include <omnetpp.h> | ||
#include <pybind11/detail/common.h> | ||
#include <pybind11/embed.h> | ||
#include <pybind11/operators.h> | ||
#include <pybind11/pybind11.h> | ||
#include <filesystem> | ||
#include <memory> | ||
#include "feature_defines.h" | ||
|
||
namespace py = pybind11; | ||
namespace opp = omnetpp; | ||
namespace fs = std::filesystem; | ||
namespace quisp::modules::python_embeddable { | ||
|
||
#ifdef ENABLE_PYTHON | ||
PYBIND11_EMBEDDED_MODULE(opp, m) { | ||
py::class_<opp::cPar, std::unique_ptr<opp::cPar, py::nodelete>> OppPar(m, "Par"); | ||
OppPar.def("str", &omnetpp::cPar::str); | ||
OppPar.def("int", &omnetpp::cPar::intValue); | ||
|
||
py::class_<opp::cSimpleModule, std::unique_ptr<opp::cSimpleModule, py::nodelete>> OppSimpleModule(m, "SimpleModule"); | ||
OppSimpleModule.def("par", py::overload_cast<const char*>(&opp::cSimpleModule::par), py::return_value_policy::reference); | ||
OppSimpleModule.def("schedule_at", &opp::cSimpleModule::scheduleAt); | ||
m.def("sim_time", &opp::simTime, py::return_value_policy::reference); | ||
|
||
py::class_<opp::SimTime> OppSimTime(m, "SimTime"); | ||
OppSimTime.def(py::self + float()); | ||
OppSimTime.def(py::self += float()); | ||
OppSimTime.def(float() + py::self); | ||
|
||
py::class_<opp::cMessage, std::unique_ptr<opp::cMessage, py::nodelete>> OppCMessage(m, "Message"); | ||
} | ||
#endif | ||
|
||
void PythonEmbeddable::compile_code(const char* file_name) { | ||
auto* env = omnetpp::getEnvir(); | ||
auto* config = env->getConfigEx(); | ||
fs::path exe_path(env->getArgVector()[0]); | ||
fs::path conf_path(env->resolveResourcePath(config->getFileName())); | ||
py::module sys = py::module::import("sys"); | ||
py::cast<py::list>(sys.attr("path")).append(fs::absolute(exe_path.parent_path()).string()); | ||
py::cast<py::list>(sys.attr("path")).append(fs::absolute(conf_path.parent_path()).string()); | ||
py::print(sys.attr("path")); | ||
module = pybind11::module_::import(file_name); | ||
} | ||
|
||
void PythonEmbeddable::execute_python(const char* func_name, py::object& arg1) { py::object ret = module.attr(func_name)(arg1); } | ||
|
||
} // namespace quisp::modules::python_embeddable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,16 @@ | ||
#pragma once | ||
#include <Python.h> | ||
#include <cstdio> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
#include "pybind11/pybind11.h" | ||
#include <pybind11/pybind11.h> | ||
|
||
namespace quisp::modules::python_embeddable { | ||
namespace py = pybind11; | ||
|
||
class PythonEmbeddable { | ||
public: | ||
PythonEmbeddable() {} | ||
~PythonEmbeddable() {} | ||
void compile_code(const char* file_name) { | ||
FILE* file = std::fopen(file_name, "rt"); | ||
if (file == NULL) { | ||
std::cout << file_name << " not found" << std::endl; | ||
return; | ||
} | ||
std::fseek(file, 0L, SEEK_END); | ||
auto length = std::ftell(file); | ||
std::rewind(file); | ||
char* source = (char*)malloc(length + 1); | ||
std::fread(source, sizeof(char), length, file); | ||
fclose(file); | ||
source[length] = '\0'; | ||
std::cout << source << std::endl; | ||
code_object = Py_CompileString(source, file_name, Py_file_input); | ||
globals = pybind11::globals().ptr(); | ||
} | ||
|
||
void execute_python() { PyEval_EvalCode(code_object, globals, pybind11::dict().ptr()); } | ||
void compile_code(const char* file_name); | ||
void execute_python(const char* func_name, py::object& arg1); | ||
|
||
PyObject* globals; | ||
PyObject* code_object = nullptr; | ||
pybind11::module_ module; | ||
}; | ||
} // namespace quisp::modules::python_embeddable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.