Skip to content

Commit

Permalink
WIP (ref #153)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-woz committed Sep 17, 2018
1 parent 732ca64 commit 611ea8e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
5 changes: 5 additions & 0 deletions turbine/code/export/python.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@
boolean exceptions_are_errors=true)
"turbine" "0.1.0"
[ "set <<output>> [ turbine::python 1 <<exceptions_are_errors>> <<code>> <<expr>> ]" ];


@par @dispatch=WORKER (string s)
python_parallel_persist(string code, string expr)
"turbine" "1.0" "python_parallel_tcl" ;
1 change: 1 addition & 0 deletions turbine/code/lib/make-package.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set items [ eval list -load $libtclturbine \
-source checkpoint.tcl \
-source langs.tcl \
-source launch.tcl \
-source python.tcl \
-source gemtc_worker.tcl \
-source helpers.tcl ]

Expand Down
1 change: 1 addition & 0 deletions turbine/code/lib/module.mk.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ TCL_SRC := $(DIR)/helpers.tcl \
$(DIR)/app.tcl \
$(DIR)/langs.tcl \
$(DIR)/launch.tcl \
$(DIR)/python.tcl \
$(DIR)/gemtc_worker.tcl

# Builds the Turbine Tcl package
Expand Down
15 changes: 14 additions & 1 deletion turbine/code/src/tcl/python/module.mk.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@

DIR := src/tcl/python

TCL_PYTHON_SRC := $(DIR)/tcl-python.c
TCL_PYTHON_SRC :=
TCL_PYTHON_SRC += $(DIR)/tcl-python.c
TCL_PYTHON_SRC += $(DIR)/python-swig_wrap.c

# The file generated by SWIG:
SWIG_C_FILE_PYTHON = $(DIR)/python-swig_wrap.c

$(SWIG_C_FILE_PYTHON): $(DIR)/python-swig.i $(DIR)/python-swig.h
$(Q) " SWIG $(<)"
$(E) swig -outdir $(DIR) $(<)

clean::
$(Q) " CLEAN $(DIR)"
$(E) rm -fv $(SWIG_C_FILE_PYTHON)
62 changes: 47 additions & 15 deletions turbine/code/src/tcl/python/tcl-python.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static bool initialized = false;

static int python_init(void)
{
/* Loading python library symbols so that dynamic extensions don't throw symbol not found error.
/* Loading python library symbols so that dynamic extensions don't throw symbol not found error.
Ref Link: http://stackoverflow.com/questions/29880931/importerror-and-pyexc-systemerror-while-embedding-python-script-within-c-for-pam
*/
char str_python_lib[17];
Expand Down Expand Up @@ -121,10 +121,10 @@ static void python_finalize(void);
static char* python_result_default = "__NOTHING__";
static char* python_result_exception = "__EXCEPTION__";

#define EXCEPTION(ee) \
{ \
*output = Tcl_NewStringObj(python_result_exception, -1); \
return handle_python_exception(ee); \
#define EXCEPTION(ee) \
{ \
*result = python_result_exception; \
return handle_python_exception(ee); \
}

/**
Expand All @@ -133,15 +133,15 @@ static char* python_result_exception = "__EXCEPTION__";
@param exceptions_are_errors: If true, abort on Python exception
@param code: The multiline string of Python code.
@param expr: A Python expression to be evaluated to the returned result
@param output: Store result pointer here
@param result: Store result pointer here
@return Tcl return code
*/
static int
python_eval(bool persist, bool exceptions_are_errors,
const char* code, const char* expr, Tcl_Obj** output)
const char* code, const char* expr, char** result)
{
int rc;
char* result = python_result_default;
char* s = python_result_default;

// Initialize:
rc = python_init();
Expand All @@ -158,11 +158,12 @@ python_eval(bool persist, bool exceptions_are_errors,
main_dict, local_dict);
if (o == NULL) EXCEPTION(exceptions_are_errors);

// Convert Python result to C string, then to Tcl string:
rc = PyArg_Parse(o, "s", &result);
// Convert Python result to C string
rc = PyArg_Parse(o, "s", &s);
if (rc != 1) return handle_python_non_string(o);
DEBUG_TCL_TURBINE("python: result: %s\n", result);
*output = Tcl_NewStringObj(result, -1);
// DEBUG_TCL_TURBINE
printf("python: result: %s\n", s);
*result = strdup(s);

// Clean up and return:
Py_DECREF(o);
Expand Down Expand Up @@ -192,18 +193,49 @@ Python_Eval_Cmd(ClientData cdata, Tcl_Interp *interp,
"python: argument exceptions_are_errors should be integer!");
char* code = Tcl_GetString(objv[3]);
char* expr = Tcl_GetString(objv[4]);
Tcl_Obj* result = NULL;
char* output = NULL;
rc = python_eval(persist, exceptions_are_errors,
code, expr, &result);
code, expr, &output);
TCL_CHECK(rc);
printf("python: output: %s\n", output);
Tcl_Obj* result = Tcl_NewStringObj(output, -1);
Tcl_SetObjResult(interp, result);
free(output);
return TCL_OK;
}

char*
python_parallel_persist(MPI_Comm comm, char* code, char* expr)
{
int task_rank, task_size;
MPI_Comm_rank(comm, &task_rank);
MPI_Comm_size(comm, &task_size);
printf("In ppp(): rank: %i/%i\n", task_rank, task_size);
printf("code: %s\n", code);
printf("expr: %s\n", expr);

char* output;
int rc = python_eval(true, true, code, expr, &output);
if (rc != TCL_OK)
{
printf("python parallel task failed!\n");
exit(EXIT_FAILURE);
}

MPI_Comm_free(&comm);
if (task_rank == 0)
// Return a real value
return output;
// Return a placeholder
free(output);
return NULL;
}


#else // Python disabled

static int
Python_Eval_Cmd(ClientData cdata, Tcl_Interp *interp,
Python_Eval_Cmd(ClientData cdata, Tcl_Interp *interp
int objc, Tcl_Obj *const objv[])
{
return turbine_user_errorv(interp,
Expand Down
5 changes: 1 addition & 4 deletions turbine/code/src/tcl/python/tcl-python.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
* Author: wozniak
*/

#ifndef TCL_PYTHON_H
#define TCL_PYTHON_H
#pragma once

void tcl_python_init(Tcl_Interp* interp);

#endif
3 changes: 3 additions & 0 deletions turbine/code/src/tcl/turbine/tcl-turbine.c
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,8 @@ turbine_extract_ids(Tcl_Interp* interp, Tcl_Obj *const objv[],
int Blob_Init(Tcl_Interp* interp);
// See the tcl/launch module
int Launch_Init(Tcl_Interp* interp);
// See the tcl/python module
int Python_Init(Tcl_Interp* interp);

/*
turbine::noop_exec_register
Expand Down Expand Up @@ -1786,6 +1788,7 @@ Tclturbine_Init(Tcl_Interp* interp)
tcl_r_init(interp);
Blob_Init(interp);
Launch_Init(interp);
Python_Init(interp);

COMMAND("init", Turbine_Init_Cmd);
COMMAND("init_debug", Turbine_Init_Debug_Cmd);
Expand Down

0 comments on commit 611ea8e

Please sign in to comment.