From: Konstantinos Margaritis Date: Tue, 11 Oct 2022 09:49:24 +0000 (+0000) Subject: move pypowersim_wrapper on its own X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3664670c89d7a7571208e857b50aeaa9c3629ea9;p=openpower-isa.git move pypowersim_wrapper on its own --- diff --git a/media/pypowersim_wrapper/Makefile b/media/pypowersim_wrapper/Makefile new file mode 100644 index 00000000..7b604827 --- /dev/null +++ b/media/pypowersim_wrapper/Makefile @@ -0,0 +1,21 @@ +EXAMPLE=pypowersim_wrapper_example + +CC=gcc +CXX=g++ +AS=powerpc64le-linux-gnu-as +OBJCOPY=powerpc64le-linux-gnu-objcopy +CFLAGS= -Iinclude -O -g3 -I/usr/include/python3.7m +CXXFLAGS= -Iinclude -O -g3 +ASFLAGS= -mlibresoc -mregnames +LDFLAGS=-lgtest -pthread -lpython3.7m + +EXAMPLEC = pypowersim_wrapper_example.c +EXAMPLEOBJ = ${EXAMPLEC:.c=.o} + +${EXAMPLE}: ${EXAMPLEOBJ} + +all: ${EXAMPLE} + +.PHONY: clean +clean: + rm -f ${EXAMPLE} ${EXAMPLEOBJ} diff --git a/media/pypowersim_wrapper/pypowersim_wrapper_common.h b/media/pypowersim_wrapper/pypowersim_wrapper_common.h new file mode 100644 index 00000000..9a50d35a --- /dev/null +++ b/media/pypowersim_wrapper/pypowersim_wrapper_common.h @@ -0,0 +1,133 @@ +#include +#include +#include + +static const char* PLUGIN_NAME = "pypowersim"; +static int python_initialized = 0; +static PyObject *plugin_name = NULL; +static PyObject *plugin_module = NULL; + +typedef struct pypowersim_state { + PyObject *binary; + PyObject *bigendian; + PyObject *prog; + PyObject *qemu_cosim; + PyObject *initial_regs; + PyObject *initial_sprs; + PyObject *svstate; + PyObject *mmu; + PyObject *initial_cr; + PyObject *initial_mem; + PyObject *initial_fprs; + PyObject *initial_pc; + PyObject *args; + PyObject *simulator; + PyObject *result_obj; +} pypowersim_state_t; + +static pypowersim_state_t *pypowersim_prepare(void) { + // Allocate memory for state + pypowersim_state_t *state = malloc(sizeof(pypowersim_state_t)); + if (!state) { + printf("Error creating pypowersim_state object\n"); + exit(1); + } + memset(state, 0, sizeof(pypowersim_state_t)); + + // Add pypowersim directory to Python path + if (!python_initialized) { + // Initialize Python C API + Py_Initialize(); + + PyObject* sysPath = PySys_GetObject((char*)"path"); + PyObject* curDir = PyUnicode_FromString("../../../src/openpower/decoder/isa/"); + PyList_Append(sysPath, curDir); + Py_DECREF(curDir); + + // Set plugin name and module + plugin_name = PyUnicode_FromString(PLUGIN_NAME); + plugin_module = PyImport_Import(plugin_name); + Py_DECREF(plugin_name); + if (!plugin_module) { + PyErr_Print(); + printf("Error importing module\n"); + exit(1); + } + python_initialized = 1; + } + + // Set simulator object + state->simulator = PyObject_GetAttrString(plugin_module, "run_a_simulation"); + if (!state->simulator) { + PyErr_Print(); + printf("Error retrieving 'run_a_simulation'\n"); + exit(1); + } + + // Little Endian for now + state->bigendian = Py_False; + state->prog = Py_None; + state->qemu_cosim = Py_False; + // Set and clear 128 GPRs + state->initial_regs = PyList_New(128); + for (int i=0; i < 128; i++) { + PyList_SetItem(state->initial_regs, i, PyLong_FromLong(0)); + } + // Create SPRs to all bits set + state->initial_sprs= PyDict_New(); + PyDict_SetItemString(state->initial_sprs, "LR", PyLong_FromLong(0xffffff)); + // Set empty SVSTATE + state->svstate = PyLong_FromLong(0); + // Set no MMU + state->mmu = Py_None; + // Set no initial CR + state->initial_cr = PyLong_FromLong(0); + // Set empty initial Memory + state->initial_mem = PyDict_New(); + // Set and Clear 128 FPR + state->initial_fprs = PyList_New(128); + for (int i=0; i < 128; i++) { + PyList_SetItem(state->initial_fprs, i, PyLong_FromLong(0)); + } + // Set initial Program Counter + state->initial_pc= PyLong_FromLong(0x0); + + return state; +} + +static void pypowersim_prepareargs(pypowersim_state_t *state) { + // Set the tuple with the state objects + state->args = PyTuple_Pack(12, state->binary, state->bigendian, state->prog, state->qemu_cosim, + state->initial_regs, state->initial_sprs, state->svstate, state->mmu, + state->initial_cr, state->initial_mem, state->initial_fprs, state->initial_pc ); + if (!state->args) { + PyErr_Print(); + Py_DECREF(state->simulator); + printf("Error building args tuple\n"); + exit(1); + } +} + +static void pypowersim_finalize(pypowersim_state_t *state) { + if (state->simulator) Py_DECREF(state->simulator); + if (state->binary) Py_DECREF(state->binary); + if (state->bigendian) Py_DECREF(state->bigendian); + if (state->prog) Py_DECREF(state->prog); + if (state->qemu_cosim) Py_DECREF(state->qemu_cosim); + if (state->initial_regs) Py_DECREF(state->initial_regs); + if (state->initial_sprs) Py_DECREF(state->initial_sprs); + if (state->svstate) Py_DECREF(state->svstate); + if (state->mmu) Py_DECREF(state->mmu); + if (state->initial_cr) Py_DECREF(state->initial_cr); + if (state->initial_mem) Py_DECREF(state->initial_mem); + if (state->initial_fprs) Py_DECREF(state->initial_fprs); + if (state->initial_pc) Py_DECREF(state->initial_pc); + if (state->args) Py_DECREF(state->args); + if (state->result_obj) Py_DECREF(state->result_obj); + memset(state, 0, sizeof(pypowersim_state_t)); + if (state) free(state); + + // Finalize Python C API + // Py_Finalize(); +} + diff --git a/media/pypowersim_wrapper/pypowersim_wrapper_example.c b/media/pypowersim_wrapper/pypowersim_wrapper_example.c new file mode 100644 index 00000000..08e88f18 --- /dev/null +++ b/media/pypowersim_wrapper/pypowersim_wrapper_example.c @@ -0,0 +1,66 @@ +#include +#include +#include + +#include "pypowersim_wrapper_common.h" + +int test_function(int x) { + int result = 0; + for (int i=0; i < x; i++) + result += 2*i; + return result; +} + +int test_function_wrapper(int x) { + // Create the pypowersim_state + pypowersim_state_t *state = pypowersim_prepare(); + + // Change the relevant elements, mandatory: body + // + state->binary = PyBytes_FromStringAndSize((const char *)&test_function, 1000); + // Set GPR #3 to the argument x + PyList_SetItem(state->initial_regs, 3, PyLong_FromLong(x)); + + // Prepare the args object + pypowersim_prepareargs(state); + + // Call the function and get the resulting object + state->result_obj = PyObject_CallObject(state->simulator, state->args); + Py_DECREF(state->simulator); + Py_DECREF(state->args); + if (!state->result_obj) { + PyErr_Print(); + printf("Error invoking 'run_a_simulation'\n"); + } + + // Get the GPRs from the result_obj + PyObject *final_regs = PyObject_GetAttrString(state->result_obj, "gpr"); + if (!final_regs) { + PyErr_Print(); + Py_DECREF(state->result_obj); + printf("Error getting final GPRs\n"); + } + + // GPR #3 holds the return value as an integer + PyObject *key = PyLong_FromLong(3); + PyObject *itm = PyDict_GetItem(final_regs, key); + PyObject *value = PyObject_GetAttrString(itm, "value"); + uint64_t val = PyLong_AsLongLong(value); + + Py_DECREF(state->result_obj); + + pypowersim_finalize(state); + + // Return value + return val; +} + +int main(int argc, char* argv[]) { + for (int i=0; i < 20; i++) { + int result = test_function_wrapper(i); + printf("i = %d, result = %d\n", i, result); + } + return 0; +} + + diff --git a/media/video/libvpx/Makefile b/media/video/libvpx/Makefile index 9d16c33a..d035095d 100644 --- a/media/video/libvpx/Makefile +++ b/media/video/libvpx/Makefile @@ -1,12 +1,11 @@ VPXTARGET=libvpx_variance_test VP8TARGET=vp8_dct_test -EXAMPLE=pypowersim_wrapper_example CC=gcc CXX=g++ AS=powerpc64le-linux-gnu-as OBJCOPY=powerpc64le-linux-gnu-objcopy -CFLAGS= -Iinclude -O -g3 -I/usr/include/python3.7m +CFLAGS= -Iinclude -I../../pypowersim_wrapper -O -g3 -I/usr/include/python3.7m CXXFLAGS= -Iinclude -O -g3 ASFLAGS= -mlibresoc -mregnames LDFLAGS=-lgtest -pthread -lpython3.7m @@ -18,8 +17,6 @@ VP8_CFILES = vp8_dct4x4_ref.c vp8_dct4x4_wrappers.c VPX_CFILES = variance_ref.c variancefuncs_svp64.c variance_svp64_wrappers.c vpx_mem.c VP8_CPPFILES = test_libvpx.cc vp8_fdct4x4_test.cc VPX_CPPFILES = test_libvpx.cc variance_test.cc -EXAMPLEC = pypowersim_wrapper_example.c -EXAMPLEOBJ = ${EXAMPLEC:.c=.o} VP8_OBJFILES = $(VP8_ASFILES:.s=.o) $(VP8_CFILES:.c=.o) $(VP8_CPPFILES:.cc=.o) VPX_OBJFILES = $(VPX_ASFILES:.s=.o) $(VPX_CFILES:.c=.o) $(VPX_CPPFILES:.cc=.o) @@ -32,10 +29,8 @@ ${VP8TARGET}: ${VP8_OBJFILES} ${VPXTARGET}: ${VPX_OBJFILES} ${CXX} -o ${VPXTARGET} ${VPX_OBJFILES} ${LDFLAGS} -${EXAMPLE}: ${EXAMPLEOBJ} - -all: ${VP8TARGET} ${VPXTARGET} ${EXAMPLE} ${BINFILES} +all: ${VP8TARGET} ${VPXTARGET} ${BINFILES} .PHONY: clean clean: - rm -f ${VP8TARGET} ${VPXTARGET} ${VP8_OBJFILES} ${VPX_OBJFILES} ${BINFILES} ${EXAMPLE} ${EXAMPLEOBJ} + rm -f ${VP8TARGET} ${VPXTARGET} ${VP8_OBJFILES} ${VPX_OBJFILES} ${BINFILES} diff --git a/media/video/libvpx/pypowersim_wrapper_common.h b/media/video/libvpx/pypowersim_wrapper_common.h deleted file mode 100644 index de358109..00000000 --- a/media/video/libvpx/pypowersim_wrapper_common.h +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include -#include - -static const char* PLUGIN_NAME = "pypowersim"; -static int python_initialized = 0; -static PyObject *plugin_name = NULL; -static PyObject *plugin_module = NULL; - -typedef struct pypowersim_state { - PyObject *binary; - PyObject *bigendian; - PyObject *prog; - PyObject *qemu_cosim; - PyObject *initial_regs; - PyObject *initial_sprs; - PyObject *svstate; - PyObject *mmu; - PyObject *initial_cr; - PyObject *initial_mem; - PyObject *initial_fprs; - PyObject *initial_pc; - PyObject *args; - PyObject *simulator; - PyObject *result_obj; -} pypowersim_state_t; - -pypowersim_state_t *pypowersim_prepare(void) { - // Allocate memory for state - pypowersim_state_t *state = malloc(sizeof(pypowersim_state_t)); - if (!state) { - printf("Error creating pypowersim_state object\n"); - exit(1); - } - memset(state, 0, sizeof(pypowersim_state_t)); - - // Add pypowersim directory to Python path - if (!python_initialized) { - // Initialize Python C API - Py_Initialize(); - - PyObject* sysPath = PySys_GetObject((char*)"path"); - PyObject* curDir = PyUnicode_FromString("../../../src/openpower/decoder/isa/"); - PyList_Append(sysPath, curDir); - Py_DECREF(curDir); - - // Set plugin name and module - plugin_name = PyUnicode_FromString(PLUGIN_NAME); - plugin_module = PyImport_Import(plugin_name); - Py_DECREF(plugin_name); - if (!plugin_module) { - PyErr_Print(); - printf("Error importing module\n"); - exit(1); - } - python_initialized = 1; - } - - // Set simulator object - state->simulator = PyObject_GetAttrString(plugin_module, "run_a_simulation"); - if (!state->simulator) { - PyErr_Print(); - printf("Error retrieving 'run_a_simulation'\n"); - exit(1); - } - - // Little Endian for now - state->bigendian = Py_False; - state->prog = Py_None; - state->qemu_cosim = Py_False; - // Set and clear 128 GPRs - state->initial_regs = PyList_New(128); - for (int i=0; i < 128; i++) { - PyList_SetItem(state->initial_regs, i, PyLong_FromLong(0)); - } - // Create SPRs to all bits set - state->initial_sprs= PyDict_New(); - PyDict_SetItemString(state->initial_sprs, "LR", PyLong_FromLong(0xffffff)); - // Set empty SVSTATE - state->svstate = PyLong_FromLong(0); - // Set no MMU - state->mmu = Py_None; - // Set no initial CR - state->initial_cr = PyLong_FromLong(0); - // Set empty initial Memory - state->initial_mem = PyDict_New(); - // Set and Clear 128 FPR - state->initial_fprs = PyList_New(128); - for (int i=0; i < 128; i++) { - PyList_SetItem(state->initial_fprs, i, PyLong_FromLong(0)); - } - // Set initial Program Counter - state->initial_pc= PyLong_FromLong(0x0); - - return state; -} - -void pypowersim_prepareargs(pypowersim_state_t *state) { - // Set the tuple with the state objects - state->args = PyTuple_Pack(12, state->binary, state->bigendian, state->prog, state->qemu_cosim, - state->initial_regs, state->initial_sprs, state->svstate, state->mmu, - state->initial_cr, state->initial_mem, state->initial_fprs, state->initial_pc ); - if (!state->args) { - PyErr_Print(); - Py_DECREF(state->simulator); - printf("Error building args tuple\n"); - exit(1); - } -} - -void pypowersim_finalize(pypowersim_state_t *state) { - if (state->simulator) Py_DECREF(state->simulator); - if (state->binary) Py_DECREF(state->binary); - if (state->bigendian) Py_DECREF(state->bigendian); - if (state->prog) Py_DECREF(state->prog); - if (state->qemu_cosim) Py_DECREF(state->qemu_cosim); - if (state->initial_regs) Py_DECREF(state->initial_regs); - if (state->initial_sprs) Py_DECREF(state->initial_sprs); - if (state->svstate) Py_DECREF(state->svstate); - if (state->mmu) Py_DECREF(state->mmu); - if (state->initial_cr) Py_DECREF(state->initial_cr); - if (state->initial_mem) Py_DECREF(state->initial_mem); - if (state->initial_fprs) Py_DECREF(state->initial_fprs); - if (state->initial_pc) Py_DECREF(state->initial_pc); - if (state->args) Py_DECREF(state->args); - if (state->result_obj) Py_DECREF(state->result_obj); - memset(state, 0, sizeof(pypowersim_state_t)); - if (state) free(state); - - // Finalize Python C API - // Py_Finalize(); -} - diff --git a/media/video/libvpx/pypowersim_wrapper_example.c b/media/video/libvpx/pypowersim_wrapper_example.c deleted file mode 100644 index 08e88f18..00000000 --- a/media/video/libvpx/pypowersim_wrapper_example.c +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include - -#include "pypowersim_wrapper_common.h" - -int test_function(int x) { - int result = 0; - for (int i=0; i < x; i++) - result += 2*i; - return result; -} - -int test_function_wrapper(int x) { - // Create the pypowersim_state - pypowersim_state_t *state = pypowersim_prepare(); - - // Change the relevant elements, mandatory: body - // - state->binary = PyBytes_FromStringAndSize((const char *)&test_function, 1000); - // Set GPR #3 to the argument x - PyList_SetItem(state->initial_regs, 3, PyLong_FromLong(x)); - - // Prepare the args object - pypowersim_prepareargs(state); - - // Call the function and get the resulting object - state->result_obj = PyObject_CallObject(state->simulator, state->args); - Py_DECREF(state->simulator); - Py_DECREF(state->args); - if (!state->result_obj) { - PyErr_Print(); - printf("Error invoking 'run_a_simulation'\n"); - } - - // Get the GPRs from the result_obj - PyObject *final_regs = PyObject_GetAttrString(state->result_obj, "gpr"); - if (!final_regs) { - PyErr_Print(); - Py_DECREF(state->result_obj); - printf("Error getting final GPRs\n"); - } - - // GPR #3 holds the return value as an integer - PyObject *key = PyLong_FromLong(3); - PyObject *itm = PyDict_GetItem(final_regs, key); - PyObject *value = PyObject_GetAttrString(itm, "value"); - uint64_t val = PyLong_AsLongLong(value); - - Py_DECREF(state->result_obj); - - pypowersim_finalize(state); - - // Return value - return val; -} - -int main(int argc, char* argv[]) { - for (int i=0; i < 20; i++) { - int result = test_function_wrapper(i); - printf("i = %d, result = %d\n", i, result); - } - return 0; -} - -