00001
00002
00003
00004 #ifndef __CKPYTHON_H
00005 #define __CKPYTHON_H
00006
00007 #include "ckcallback-ccs.h"
00008 #include "./pythonIncludes.h"
00009
00010
00011
00012
00013 #include "PythonCCS.decl.h"
00014 #include "PythonCCS-client.h"
00015 #include "string"
00016 #include "map"
00017
00018 #define PYTHON_ENABLE_HIGH private: \
00019 static PyMethodDef CkPy_MethodsCustom[]; \
00020 public: \
00021 PyMethodDef *getMethods() {return CkPy_MethodsCustom;}
00022
00023 extern PyMethodDef CkPy_MethodsDefault[];
00024
00025 typedef struct {
00026
00027 bool inUse;
00028 bool isKeepPrint;
00029 PyObject *arg;
00030 PyObject **result;
00031 CthThread thread;
00032 PyThreadState *pythread;
00033
00034 int clientReady;
00035
00036
00037
00038
00039
00040
00041 CcsDelayedReply client;
00042
00043 int finishReady;
00044
00045
00046
00047
00048
00049 CcsDelayedReply finish;
00050
00051 std::string printed;
00052 } PythonStruct;
00053 typedef std::map<CmiUInt4,PythonStruct> PythonTable;
00054
00055 class PythonObject {
00056 static PyMethodDef CkPy_MethodsCustom[];
00057 static const char* CkPy_MethodsCustomDoc;
00058 public:
00059 CmiUInt4 pyNumber;
00060 PythonTable pyWorkers;
00061
00062 void pyRequest(CkCcsRequestMsg *msg);
00063 void execute(CkCcsRequestMsg *msg, CcsDelayedReply *reply);
00064 void print(PythonPrint *pyMsg, CcsDelayedReply *reply);
00065 void finished(PythonFinished *pyMsg, CcsDelayedReply *reply);
00066 protected:
00067 int prepareInterpreter(PythonExecute *pyMsg);
00068
00069 public:
00070 static void replyIntValue(PythonObject* obj, CcsDelayedReply *reply, CmiUInt4 *value);
00071 void (*replyIntFn)(PythonObject*,CcsDelayedReply*,CmiUInt4*);
00072 void cleanup(PythonExecute *pyMsg, PyThreadState *pts, CmiUInt4 pyVal);
00073 void getPrint(CkCcsRequestMsg *msg);
00074 static void _callthr_executeThread(CkThrCallArg *impl_arg);
00075 void executeThread(PythonExecute *pyMsg);
00076 virtual PyMethodDef *getMethods() {return CkPy_MethodsCustom;}
00077 virtual const char *getMethodsDoc() {return CkPy_MethodsCustomDoc;}
00078
00079 PythonObject() : pyNumber(0) { replyIntFn=&PythonObject::replyIntValue; }
00080
00081
00082
00083
00084 void pythonSetString(PyObject*, char*, char*);
00085 void pythonSetString(PyObject*, char*, char*, int);
00086 void pythonSetInt(PyObject*, char*, long);
00087 void pythonSetLong(PyObject*, char*, long);
00088 void pythonSetLong(PyObject*, char*, unsigned long);
00089 void pythonSetLong(PyObject*, char*, double);
00090 void pythonSetFloat(PyObject*, char*, double);
00091 void pythonSetComplex(PyObject*, char*, double, double);
00092 void pythonSetPointer(PyObject*, char*, void*);
00093
00094 void pythonGetString(PyObject*, char*, char**);
00095 void pythonGetInt(PyObject*, char*, long*);
00096 void pythonGetLong(PyObject*, char*, long*);
00097 void pythonGetLong(PyObject*, char*, unsigned long*);
00098 void pythonGetLong(PyObject*, char*, double*);
00099 void pythonGetFloat(PyObject*, char*, double*);
00100 void pythonGetComplex(PyObject*, char*, double*, double*);
00101 void pythonGetPointer(PyObject*, char*, void**);
00102
00103
00104 PyObject *pythonGetArg(int);
00105 void pythonReturn(int);
00106 void pythonReturn(int, PyObject*);
00107 void pythonAwake(int);
00108 void pythonSleep(int);
00109
00110
00111
00112
00113
00114
00115 virtual PyObject* read(PyObject* where) {CkAbort("PythonCCS: Method read should be reimplemented"); return NULL; };
00116 virtual void write(PyObject* where, PyObject* what) {CkAbort("PythonCCS: Method write should be reimplemented");};
00117
00118
00119
00120
00121 virtual int buildIterator(PyObject*&, void*) {CkAbort("PythonCCS: Method buildIterator should be reimplemented"); return 0; };
00122 virtual int nextIteratorUpdate(PyObject*&, PyObject*, void*) {CkAbort("PythonCCS: Method nextIteratorUpdate should be reimplemented"); return 0; };
00123
00124 };
00125
00126
00127
00128
00129 CtvExtern(PyObject *, pythonReturnValue);
00130
00131 typedef struct PythonReplyInt {
00132 CcsDelayedReply reply;
00133 CmiUInt4 value;
00134 } PythonReplyInt;
00135
00136 typedef struct PythonReplyString {
00137 CcsDelayedReply reply;
00138 char data[1];
00139
00140 void *operator new(size_t size, const int length) {
00141 return malloc(size+length);
00142 }
00143 } PythonReplyString;
00144
00145 class PythonCCS : public CBase_PythonCCS {
00146 public:
00147 PythonCCS(CkArgMsg *arg);
00148 void forwardString(CkReductionMsg *msg);
00149 void forwardInt(CkReductionMsg *msg);
00150
00151 static CkReduction::reducerType reduceString;
00152 };
00153
00154 class CkCallbackPython : public CkCallbackResumeThread {
00155 public:
00156 CkCallbackPython(void) { }
00157 CkCallbackPython(void* &ptr) : CkCallbackResumeThread(ptr) { }
00158 ~CkCallbackPython(void) {
00159 void *res = thread_delay();
00160 if (result != NULL) *result = res;
00161 }
00162 void *thread_delay(void) const {
00163 PyThreadState *myState = PyThreadState_Get();
00164 PyEval_ReleaseLock();
00165 void *res = impl_thread_delay();
00166 PyEval_AcquireLock();
00167 PyThreadState_Swap(myState);
00168 return res;
00169 }
00170 };
00171
00172 #endif //__CKPYTHON_H