-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_functions.c
106 lines (89 loc) · 3.25 KB
/
utility_functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "utility_functions.h"
#include <Python.h>
double call_python_function(double numbers[], int size) {
// Initialize the Python Interpreter
// Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./venv/Lib/site-packages')");
// Import the Python module
PyObject *pName = PyUnicode_DecodeFSDefault("utility");
PyObject *pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
PyObject *pFunc = PyObject_GetAttrString(pModule, "calculate_mean");
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pArgs = PyTuple_New(1);
PyObject *pValue = PyList_New(size);
// Add numbers to the Python list
for (int i = 0; i < size; i++) {
PyList_SetItem(pValue, i, PyFloat_FromDouble(numbers[i]));
}
PyTuple_SetItem(pArgs, 0, pValue);
PyObject *pResult = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pResult != NULL && PyFloat_Check(pResult)) {
double result = PyFloat_AsDouble(pResult);
Py_DECREF(pResult);
Py_DECREF(pFunc);
Py_DECREF(pModule);
Py_Finalize();
return result;
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Call failed or returned non-float\n");
Py_XDECREF(pResult);
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", "calculate_mean");
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", "utility");
}
// Py_Finalize();
return 0; // Return 0 or an appropriate error value
}
const char* get_pytorch_version() {
// Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./venv/Lib/site-packages')");
PyObject *pName = PyUnicode_DecodeFSDefault("utility");
PyObject *pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
PyObject *pFunc = PyObject_GetAttrString(pModule, "get_torch_version");
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pResult = PyObject_CallObject(pFunc, NULL);
if (pResult != NULL) {
const char* version_temp = PyUnicode_AsUTF8(pResult);
char* version = strdup(version_temp); // Duplicate the string
Py_DECREF(pResult);
Py_DECREF(pFunc);
Py_DECREF(pModule);
// Py_Finalize();
return version;
} else {
PyErr_Print();
fprintf(stderr, "Call failed\n");
}
} else {
PyErr_Print();
fprintf(stderr, "Cannot find function 'get_torch_version'\n");
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
} else {
PyErr_Print();
fprintf(stderr, "Failed to load 'pytorch_helpers'\n");
}
// Py_Finalize();
return NULL; // Return NULL in case of error
}