void td_py_invoke0(td_val_t *out, char *fname) { PyObject *pFunc, *pArgs, *pValue; pFunc = td_py_get_callable(fname); if (pFunc == NULL) return; pArgs = PyTuple_New(0); pValue = PyObject_CallObject(pFunc, pArgs); if (pValue == NULL) { fprintf(stderr, "Error in Python call %s\n", fname); return; } to_td_val(out, pValue); }
void td_py_call(td_val_t *out, PyObject *pArgs, char *fname) { PyObject *pFunc, *pValue; pFunc = td_py_get_callable(fname); if (pFunc == NULL) return; pValue = PyObject_CallObject(pFunc, pArgs); if (pValue == NULL) { fprintf(stderr, "Error in Python call %s\n", fname); return; } to_td_val(out, pValue); Py_DECREF(pFunc); Py_DECREF(pValue); }
void td_py_invoke1(td_val_t *out, char *fname, td_val_t *arg) { PyObject *pFunc, *pArgs, *pValue; pFunc = td_py_get_callable(fname); if (pFunc == NULL) return; pArgs = PyTuple_New(1); pValue = from_td_val(arg); if (pValue == NULL) return; PyTuple_SetItem(pArgs, 0, pValue); Py_DECREF(pValue); pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pFunc); Py_DECREF(pArgs); if (pValue == NULL) { fprintf(stderr, "Error in Python call %s\n", fname); return; } to_td_val(out, pValue); Py_DECREF(pValue); }
void td_py_invoke_graph_and_csc(td_val_t *out, char *fname, graph_t *in_graph, int *in_csc_offsets, int *in_csc_indices) { PyObject *pArgs; td_array_t node_array = { .data=in_graph->nodeNames, .length=in_graph->numNodes, .eltype=TD_INT64, .ndims=1 }; td_val_t nodes = { .tag = TD_ARRAY, .object = &node_array }; td_array_t csr_offset_array = { .data=in_graph->rowOffsets, .length=in_graph->numNodes+1, .eltype=TD_INT32, .ndims=1 }; td_val_t csr_offsets = { .tag = TD_ARRAY, .object = &csr_offset_array }; td_array_t csr_indices_array = { .data=in_graph->colIndices, .length=in_graph->numEdges, .eltype=TD_INT32, .ndims=1 }; td_val_t csr_indices = { .tag = TD_ARRAY, .object = &csr_indices_array }; td_array_t csc_offset_array = { .data=in_csc_offsets, .length=in_graph->numNodes+1, .eltype=TD_INT32, .ndims=1 }; td_val_t csc_offsets = { .tag = TD_ARRAY, .object = &csc_offset_array }; td_array_t csc_indices_array = { .data=in_csc_indices, .length=in_graph->numEdges, .eltype=TD_INT32, .ndims=1 }; td_val_t csc_indices = { .tag = TD_ARRAY, .object = &csc_indices_array }; pArgs = PyTuple_New(5); td_py_add_arg(pArgs, 0, &nodes); td_py_add_arg(pArgs, 1, &csr_offsets); td_py_add_arg(pArgs, 2, &csr_indices); td_py_add_arg(pArgs, 3, &csc_offsets); td_py_add_arg(pArgs, 4, &csc_indices); td_py_call(out, pArgs, fname); Py_DECREF(pArgs); } void td_py_eval(td_val_t *out, char *str) { PyObject *v = PyRun_String(str, 0, NULL, NULL); to_td_val(out, v); } td_tag_t td_py_get_type(void *v) { return py_type_to_td((PyObject*) v); } td_tag_t td_py_get_eltype(void *v) { if (PyArray_Check( (PyObject *) v)){ PyArrayObject *arr = (PyArrayObject *) v; return numpy_type_to_td(PyArray_TYPE(arr)); } return TD_UNKNOWN; } void *td_py_get_dataptr(void *v) { if (PyArray_Check( (PyObject *) v)) return PyArray_DATA( (PyArrayObject *) v); return NULL; } size_t td_py_get_length(void *v) { if (PyArray_Check( (PyObject *) v)) return PyArray_SIZE( (PyArrayObject *)v); return 1; } size_t td_py_get_ndims(void *v) { if (PyArray_Check( (PyObject *) v)) return PyArray_NDIM( (PyArrayObject *)v); return 0; } // initialization void td_py_init(char *homedir) { Py_Initialize(); import_array(); td_env_t *env = (td_env_t*)malloc(sizeof(td_env_t)); env->name = "python"; env->eval = &td_py_eval; env->invoke0 = &td_py_invoke0; env->invoke1 = &td_py_invoke1; env->invoke2 = &td_py_invoke2; env->invoke3 = &td_py_invoke3; env->invoke4 = &td_py_invoke4; env->invoke5 = &td_py_invoke5; env->invokeGraph1 = &td_py_invoke_graph1; env->invokeGraphAndCSC = &td_py_invoke_graph_and_csc; //env->retain //env->release env->get_type = &td_py_get_type; env->get_eltype = &td_py_get_eltype; env->get_dataptr = &td_py_get_dataptr; env->get_length = &td_py_get_length; env->get_ndims = &td_py_get_ndims; //env->get_dims //env->get_strides td_provide_python(env); }
void td_jl_eval(td_val_t *out, char *str) { jl_value_t *v = jl_eval_string(str); to_td_val(out, v); }
void td_jl_invoke1(td_val_t *out, char *fname, td_val_t *arg) { jl_function_t *f = jl_get_function(jl_base_module, fname); jl_value_t *v = jl_call1(f, from_td_val(arg)); to_td_val(out, v); }
void td_jl_invoke0(td_val_t *out, char *fname) { jl_function_t *f = jl_get_function(jl_base_module, fname); jl_value_t *v = jl_call0(f); to_td_val(out, v); }
void td_py_eval(td_val_t *out, char *str) { PyObject *v = PyRun_String(str, 0, NULL, NULL); to_td_val(out, v); }