Skip to content

Commit

Permalink
upd jit, WIP py parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
littlemine committed Apr 2, 2024
1 parent 37d17db commit 322dadd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions projects/CUDA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if (ZS_PYTHON_FOUND AND ZENO_WITH_PyZpc)
get_target_property(ZENO_BIN_DIR zeno RUNTIME_OUTPUT_DIRECTORY)
add_custom_target(copy_py)
add_dependencies(zeno copy_py)
add_dependencies(copy_py zpc_jit_py)
# message(STATUS "retrieved python impl: ${Python3_INTERPRETER_ID}")
message(STATUS "python3 libraries: ${ZS_OVERWRITE_PYTHON_LIBRARIES}")
message(STATUS "python3 include directories: ${ZS_OVERWRITE_PYTHON_INCLUDE_DIR}")
Expand Down
53 changes: 52 additions & 1 deletion projects/CUDA/graph/Task.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include "zensim/ZpcTuple.hpp"
#include "zensim/zpc_tpls/fmt/format.h"
#include <cctype>
Expand Down Expand Up @@ -30,16 +33,64 @@ struct WorkNode : IObject {
std::map<std::string, std::shared_ptr<WorkNode>> deps;
};

namespace detail {
void python_evaluate(const std::string& fmtStr, const std::vector<const char*> args) {
// #if defined(ZENO_WITH_PYTHON3) or ZS_PYTHON_FOUND
std::string evalStmt = "print(\'" + fmtStr + "\'";
if (args.size()) {
evalStmt += ").format("+std::string(args[0]);
for (int i = 1; i != args.size(); ++i)
evalStmt += ", " + std::string(args[i]);
evalStmt += ")";
}
evalStmt += ")";

PyRun_SimpleString(
"import sys\n"
"from io import StringIO\n"
"old_stdout = sys.stdout\n" // Backup the original stdout
"sys.stdout = StringIO()\n" // Replace stdout with a StringIO object to capture outputs
);
PyRun_SimpleString(evalStmt.data());
fmt::print("assembled cmd str: {}\n", evalStmt);

PyObject *sys = PyImport_ImportModule("sys");
PyObject *stdout = PyObject_GetAttrString(sys, "stdout");
PyObject *output = PyObject_GetAttrString(stdout, "getvalue");
PyObject *result = PyObject_CallObject(output, NULL);

// Convert the captured output to a C++ string
const char* result_cstr = PyUnicode_AsUTF8(result);
std::string result_str(result_cstr);

// Print the captured output in C++
std::cout << "Captured Python output: " << result_str << std::endl;

// Restore the original stdout
PyRun_SimpleString("sys.stdout = old_stdout");

// Finalize the Python interpreter
Py_Finalize();
}
}

struct CommandGenerator : INode {
void apply() override {
auto tag = get_input2<std::string>("name_tag");
if (tag.empty())
throw std::runtime_error("work name must not be empty!");

auto cmdFmtStr = get_input2<std::string>("cmd_fmt_string");
auto cmd = fmt::format(cmdFmtStr, 0);

auto args = get_input<ListObject>("arguments");
std::vector<const char*> as;
for (auto &&arg : args->get())
if (auto ptr = dynamic_cast<StringObject *>(arg.get()); ptr != nullptr)
as.push_back(ptr->get().c_str());
// auto cmd = fmt::format(cmdFmtStr, as);
// fmt::print("parsed: [[{}]]\n", cmd);
detail::python_evaluate(cmdFmtStr, as);

auto ret = std::make_shared<WorkNode>();

set_output("job", ret);
Expand Down
2 changes: 1 addition & 1 deletion projects/CUDA/zpc_jit
Submodule zpc_jit updated 2 files
+57 −0 example/nbody_rk2.py
+3 −3 setup.py

0 comments on commit 322dadd

Please sign in to comment.