00001 #include "PythonCCS.h"
00002
00003
00004
00005
00006 CtvDeclare(PyObject *, pythonReturnValue);
00007
00008 CProxy_PythonCCS pythonCcsProxy;
00009
00010 #if 0
00011 #define CkPythonDebugf CkPrintf
00012 #else
00013 void CkPythonDebugf(const char *fmt, ...) {
00014
00015 }
00016 #endif
00017
00018
00019
00020 static PyObject *CkPy_printstr(PyObject *self, PyObject *args) {
00021 char *stringToPrint;
00022 if (!PyArg_ParseTuple(args, "s:printstr", &stringToPrint)) return NULL;
00023 CkPrintf("%s\n",stringToPrint);
00024 Py_INCREF(Py_None);return Py_None;
00025 }
00026
00027
00028
00029 static inline void Ck_printclient(PythonObject *object, CmiUInt4 ref, const char* str) {
00030
00031 PythonStruct &worker = object->pyWorkers[ref];
00032
00033
00034 if (worker.clientReady > 0) {
00035
00036
00037
00038
00039 CcsSendDelayedReply(worker.client, strlen(str), str);
00040 worker.printed.erase();
00041 worker.clientReady = 0;
00042 } else {
00043
00044 if (worker.isKeepPrint) {
00045 worker.printed += std::string(str);
00046 }
00047
00048 }
00049
00050 }
00051
00052 static PyObject *CkPy_print(PyObject *self, PyObject *args) {
00053 char *stringToPrint;
00054 CkPythonDebugf("-1\n");
00055 if (!PyArg_ParseTuple(args, "s:printclient", &stringToPrint)) {
00056 CkPrintf("CkPy_print returning abnormally!\n");
00057 return NULL;
00058 }
00059 CkPythonDebugf("zero\n");
00060 PyObject *dict = PyModule_GetDict(PyImport_AddModule("__main__"));
00061 CkPythonDebugf("uno\n");
00062 CmiUInt4 pyReference = PyInt_AsLong(PyDict_GetItemString(dict,"__charmNumber__"));
00063 CkPythonDebugf("due\n");
00064 PythonObject *object = (PythonObject*)PyLong_AsVoidPtr(PyDict_GetItemString(dict,"__charmObject__"));
00065 CkPythonDebugf("calling Ck_printclient\n");
00066 Ck_printclient(object, pyReference, stringToPrint);
00067 CkPythonDebugf("CkPy_print returning normally\n");
00068 Py_INCREF(Py_None);return Py_None;
00069 }
00070
00071 static PyObject *CkPy_mype(PyObject *self, PyObject *args) {
00072 if (!PyArg_ParseTuple(args, ":mype")) return NULL;
00073 return Py_BuildValue("i", CkMyPe());
00074 }
00075
00076 static PyObject *CkPy_numpes(PyObject *self, PyObject *args) {
00077 if (!PyArg_ParseTuple(args, ":numpes")) return NULL;
00078 return Py_BuildValue("i", CkNumPes());
00079 }
00080
00081 static PyObject *CkPy_myindex(PyObject *self, PyObject *args) {
00082 if (!PyArg_ParseTuple(args, ":myindex")) return NULL;
00083 PyObject *dict = PyModule_GetDict(PyImport_AddModule("__main__"));
00084 PythonObject *object = (PythonObject*)PyLong_AsVoidPtr(PyDict_GetItemString(dict,"__charmObject__"));
00085
00086 ArrayElement1D *pyArray1 = dynamic_cast<ArrayElement1D*>(object);
00087 ArrayElement2D *pyArray2 = dynamic_cast<ArrayElement2D*>(object);
00088 ArrayElement3D *pyArray3 = dynamic_cast<ArrayElement3D*>(object);
00089 ArrayElement4D *pyArray4 = dynamic_cast<ArrayElement4D*>(object);
00090 ArrayElement5D *pyArray5 = dynamic_cast<ArrayElement5D*>(object);
00091 ArrayElement6D *pyArray6 = dynamic_cast<ArrayElement6D*>(object);
00092
00093 if (pyArray1) return Py_BuildValue("(i)", pyArray1->thisIndex);
00094 else if (pyArray2) return Py_BuildValue("(ii)", pyArray2->thisIndex.x, pyArray2->thisIndex.y);
00095 else if (pyArray3) return Py_BuildValue("(iii)", pyArray3->thisIndex.x, pyArray3->thisIndex.y, pyArray3->thisIndex.z);
00096 else if (pyArray4) return Py_BuildValue("(iiii)", pyArray4->thisIndex.w, pyArray4->thisIndex.x, pyArray4->thisIndex.y, pyArray4->thisIndex.z);
00097 else if (pyArray5) return Py_BuildValue("(iiiii)", pyArray5->thisIndex.v, pyArray5->thisIndex.w, pyArray5->thisIndex.x, pyArray5->thisIndex.y, pyArray5->thisIndex.z);
00098 else if (pyArray6) return Py_BuildValue("(iiiiii)", pyArray6->thisIndex.x1, pyArray6->thisIndex.y1, pyArray6->thisIndex.z1, pyArray6->thisIndex.x2, pyArray6->thisIndex.y2, pyArray6->thisIndex.z2);
00099 else { Py_INCREF(Py_None);return Py_None;}
00100
00101 }
00102
00103
00104 static PyObject *CkPy_read(PyObject *self, PyObject *args) {
00105 if (!PyArg_ParseTuple(args, "O:read")) return NULL;
00106 PyObject *dict = PyModule_GetDict(PyImport_AddModule("__main__"));
00107 PythonObject *object = (PythonObject*)PyLong_AsVoidPtr(PyDict_GetItemString(dict,"__charmObject__"));
00108
00109
00110 return object->read(args);
00111 }
00112
00113
00114 static PyObject *CkPy_write(PyObject *self, PyObject *args) {
00115 PyObject *where, *what;
00116 if (!PyArg_ParseTuple(args, "OO:write",&where,&what)) return NULL;
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 PyObject *whereT, *whatT;
00127 if (PyTuple_Check(where)) whereT = where;
00128 else {
00129 whereT = PyTuple_New(1);
00130 PyTuple_SET_ITEM(whereT, 0, where);
00131 }
00132 if (PyTuple_Check(what)) whatT = what;
00133 else {
00134 whatT = PyTuple_New(1);
00135 PyTuple_SET_ITEM(whatT, 0, what);
00136 }
00137 PyObject *dict = PyModule_GetDict(PyImport_AddModule("__main__"));
00138 PythonObject *object = (PythonObject*)PyLong_AsVoidPtr(PyDict_GetItemString(dict,"__charmObject__"));
00139 object->write(whereT, whatT);
00140 Py_DECREF(whereT);
00141 Py_DECREF(whatT);
00142 Py_INCREF(Py_None);return Py_None;
00143 }
00144
00145 PyMethodDef PythonObject::CkPy_MethodsCustom[] = {
00146 {NULL, NULL}
00147 };
00148
00149 const char* PythonObject::CkPy_MethodsCustomDoc = "";
00150
00151 PyMethodDef CkPy_MethodsDefault[] = {
00152 {"printstr", CkPy_printstr, METH_VARARGS},
00153 {"printclient", CkPy_print, METH_VARARGS},
00154 {"mype", CkPy_mype, METH_VARARGS},
00155 {"numpes", CkPy_numpes, METH_VARARGS},
00156 {"myindex", CkPy_myindex, METH_VARARGS},
00157 {"read", CkPy_read, METH_VARARGS},
00158 {"write", CkPy_write, METH_VARARGS},
00159 {NULL, NULL}
00160 };
00161
00162 void PythonObject::replyIntValue(PythonObject *obj, CcsDelayedReply *reply, CmiUInt4 *value) {
00163 CkPythonDebugf("[%d] PythonObject::replyIntValue\n",CkMyPe());
00164 PythonReplyInt forward;
00165 forward.reply = *reply;
00166 forward.value = *value;
00167 CkCallback cb(CkIndex_PythonCCS::forwardInt(0), pythonCcsProxy);
00168 ArrayElement *array = dynamic_cast<ArrayElement *>(obj);
00169 Group *group = dynamic_cast<Group *>(obj);
00170 if (array != NULL) array->contribute(sizeof(PythonReplyInt), &forward, CkReduction::bitvec_and, cb);
00171 else if (group != NULL) group->contribute(sizeof(PythonReplyInt), &forward, CkReduction::bitvec_and, cb);
00172 else CcsSendDelayedReply(*reply, sizeof(CmiUInt4), (void *)value);
00173 }
00174
00175 void PythonObject::pyRequest (CkCcsRequestMsg *msg) {
00176 PythonAbstract *pyAbstract = (PythonAbstract *)msg->data;
00177 pyAbstract->unpack();
00178
00179 if (pyAbstract->isExecute()) {
00180 execute(msg, &msg->reply);
00181
00182
00183
00184 } else if (pyAbstract->isPrint()) {
00185 print((PythonPrint *)msg->data, &msg->reply);
00186 delete msg;
00187 } else if (pyAbstract->isFinished()) {
00188 finished((PythonFinished *)msg->data, &msg->reply);
00189 delete msg;
00190 } else {
00191 CkPrintf("Wrong request arrived!\n");
00192 delete msg;
00193 }
00194 }
00195
00196
00197 void PythonObject::print (PythonPrint *pyMsg, CcsDelayedReply *reply) {
00198
00199
00200 CmiUInt4 returnValue;
00201 pyMsg->unpack();
00202
00203 PythonTable::iterator iter = pyWorkers.find(pyMsg->interpreter);
00204 if (iter == pyWorkers.end()) {
00205
00206
00207
00208 returnValue = htonl(0);
00209 replyIntFn(this, reply, &returnValue);
00210 } else {
00211
00212
00213 if (pyMsg->isKill()) {
00214
00215 if (iter->second.clientReady == 1) {
00216 returnValue = htonl(0);
00217 replyIntFn(this, &iter->second.client, &returnValue);
00218 }
00219
00220
00221 CcsNoDelayedReply(*reply);
00222 return;
00223 }
00224
00225
00226 if (iter->second.printed.length() > 0) {
00227
00228 const char *str = iter->second.printed.c_str();
00229 int length = strlen(str);
00230
00231 PythonReplyString *forward = new (length) PythonReplyString();
00232 forward->reply = *reply;
00233 memcpy(forward->data, str, length);
00234 CkCallback cb(CkIndex_PythonCCS::forwardString(0), pythonCcsProxy);
00235 ArrayElement *array = dynamic_cast<ArrayElement *>(this);
00236 Group *group = dynamic_cast<Group *>(this);
00237 if (array != NULL) array->contribute(sizeof(CcsDelayedReply)+length, forward, PythonCCS::reduceString, cb);
00238 else if (group != NULL) group->contribute(sizeof(CcsDelayedReply)+length, forward, PythonCCS::reduceString, cb);
00239 else CcsSendDelayedReply(*reply, length, str);
00240 iter->second.printed.erase();
00241 if (iter->second.clientReady == -1) {
00242
00243 pyWorkers.erase(pyMsg->interpreter);
00244 }
00245 } else {
00246
00247 if (pyMsg->isWait()) {
00248
00249 if (iter->second.clientReady == 1) {
00250 returnValue = htonl(0);
00251 replyIntFn(this, &iter->second.client, &returnValue);
00252 }
00253 iter->second.client = *reply;
00254 iter->second.clientReady = 1;
00255 } else {
00256
00257 returnValue = htonl(0);
00258 replyIntFn(this, reply, &returnValue);
00259 }
00260 }
00261
00262 }
00263 }
00264
00265 void PythonObject::finished (PythonFinished *pyMsg, CcsDelayedReply *reply) {
00266
00267
00268 CmiUInt4 pyReference = pyMsg->interpreter;
00269 CmiUInt4 returnValue;
00270 pyMsg->unpack();
00271
00272 PythonTable::iterator iter = pyWorkers.find(pyMsg->interpreter);
00273 if (iter == pyWorkers.end() || !iter->second.inUse) {
00274
00275 returnValue = htonl(pyReference);
00276 replyIntFn(this, reply, &returnValue);
00277
00278 return;
00279 }
00280
00281
00282 if (pyMsg->isWait()) {
00283
00284 if (iter->second.finishReady) {
00285
00286 returnValue = htonl(0);
00287 replyIntFn(this, &iter->second.finish, &returnValue);
00288 }
00289
00290 iter->second.finish = *reply;
00291 iter->second.finishReady = 1;
00292 } else {
00293
00294 returnValue = htonl(0);
00295 replyIntFn(this, reply, &returnValue);
00296 }
00297
00298 }
00299
00300 void PythonObject::execute (CkCcsRequestMsg *msg, CcsDelayedReply *reply) {
00301
00302 PythonExecute *pyMsg = (PythonExecute *)msg->data;
00303
00304
00305
00306
00307 pyMsg->unpack();
00308
00309 double _startTime = CkWallTimer();
00310 CmiUInt4 pyReference = prepareInterpreter(pyMsg);
00311 ckout<<"Python creation time "<<CmiWallTimer()-_startTime<<endl;
00312
00313 CmiUInt4 returnValue;
00314
00315 if (pyReference == 0) {
00316 returnValue = htonl(0);
00317 replyIntFn(this, reply, &returnValue);
00318 return;
00319 }
00320
00321 pyWorkers[pyReference].inUse = true;
00322
00323
00324
00325
00326
00327
00328 if (pyMsg->isWait()) {
00329 pyWorkers[pyReference].finish = *reply;
00330 pyWorkers[pyReference].finishReady = 1;
00331 } else {
00332 pyWorkers[pyReference].finishReady = 0;
00333
00334 ckout<<"new interpreter created "<<pyReference<<endl;
00335 returnValue = htonl(pyReference);
00336 replyIntFn(this, reply, &returnValue);
00337 }
00338
00339
00340 if (pyMsg->isHighLevel()) {
00341 CthResume(CthCreate((CthVoidFn)_callthr_executeThread, new CkThrCallArg(msg,this), 0));
00342
00343 } else {
00344 executeThread(pyMsg);
00345
00346 delete msg;
00347 }
00348 }
00349
00358 int PythonObject::prepareInterpreter(PythonExecute *pyMsg) {
00359 CmiUInt4 pyReference;
00360
00361 if (pyMsg->interpreter > 0) {
00362
00363 PythonTable::iterator iter;
00364 if ((iter=pyWorkers.find(pyMsg->interpreter))!=pyWorkers.end() && !iter->second.inUse && iter->second.clientReady!=-1) {
00365
00366
00367 pyReference = pyMsg->interpreter;
00368 PyEval_AcquireLock();
00369 PyThreadState_Swap(iter->second.pythread);
00370 CkPythonDebugf("Status 0 %p\n",PyErr_Occurred());
00371 } else {
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 return 0;
00382 }
00383 } else {
00384
00385
00386
00387
00388 pyReference = ++pyNumber;
00389 pyNumber &= ~(1<<31);
00390
00391 pyWorkers[pyReference].clientReady = 0;
00392
00393
00394 PyEval_AcquireLock();
00395 PyThreadState *pts = Py_NewInterpreter();
00396
00397 CkAssert(pts != NULL);
00398 pyWorkers[pyReference].pythread = pts;
00399 pyWorkers[pyReference].inUse = false;
00400
00401 Py_InitModule("ck", CkPy_MethodsDefault);
00402 if (pyMsg->isHighLevel()) Py_InitModule("charm", getMethods());
00403
00404
00405 PyObject *mod = PyImport_AddModule("__main__");
00406 PyObject *dict = PyModule_GetDict(mod);
00407
00408 PyDict_SetItemString(dict,"__charmNumber__",PyInt_FromLong(pyReference));
00409 PyDict_SetItemString(dict,"__charmObject__",PyLong_FromVoidPtr(this));
00410 PyRun_String("import ck\nimport sys\n"
00411 "ck.__doc__ = \"Ck module: basic charm routines\\n"
00412 "printstr(str) -- print a string on the server\\n"
00413 "printclient(str) -- print a string on the client\\n"
00414 "mype() -- return an integer for MyPe()\\n"
00415 "numpes() -- return an integer for NumPes()\\n"
00416 "myindex() -- return a tuple containing the array index (valid only for arrays)\\n"
00417 "read(where) -- read a value on the chare (uses the \\\"read\\\" method of the chare)\\n"
00418 "write(where, what) -- write a value back on the chare (uses the \\\"write\\\" method of the chare)\\n\"",
00419 Py_file_input,dict,dict);
00420 if (pyMsg->isHighLevel()) {
00421 PyRun_String("import charm",Py_file_input,dict,dict);
00422 PyRun_String(getMethodsDoc(),Py_file_input,dict,dict);
00423 }
00424
00425 PyRun_String("class __charmOutput__:\n"
00426 " def __init__(self, stdout):\n"
00427 " self.stdout = stdout\n"
00428 " def write(self, s):\n"
00429 " ck.printclient(s)\n"
00430 "sys.stdout = __charmOutput__(sys.stdout)"
00431 ,Py_file_input,dict,dict);
00432
00433 }
00434
00435 if (pyMsg->isKeepPrint()) {
00436 pyWorkers[pyReference].isKeepPrint = true;
00437 } else {
00438 pyWorkers[pyReference].isKeepPrint = false;
00439 }
00440
00441 return pyReference;
00442 }
00443
00444 std::string getTraceback() {
00445 std::string result;
00446 PyObject *exception, *v, *traceback;
00447 PyErr_Fetch(&exception, &v, &traceback);
00448 PyErr_NormalizeException(&exception, &v, &traceback);
00449
00450
00451
00452
00453
00454 PyObject* tbstr = PyString_FromString("traceback");
00455 PyObject* tbmod = PyImport_Import(tbstr);
00456 if (!tbmod)
00457 return NULL;
00458 PyObject* tbdict = PyModule_GetDict(tbmod);
00459 PyObject* formatFunc = PyDict_GetItemString(tbdict,
00460 "format_exception");
00461 if (!formatFunc)
00462 return NULL;
00463 if (!traceback) {
00464 traceback = Py_None;
00465 Py_INCREF(Py_None);
00466 }
00467 PyObject* args = Py_BuildValue("(OOO)", exception, v, traceback);
00468 PyObject* lst = PyObject_CallObject(formatFunc, args);
00469
00470 for (int i=0; i<PyList_GET_SIZE(lst); i++) {
00471 result += PyString_AsString(PyList_GetItem(lst, i));
00472 }
00473
00474 Py_DECREF(args);
00475 Py_DECREF(lst);
00476 return result;
00477 }
00478
00479
00480
00481
00482 void PythonObject::_callthr_executeThread(CkThrCallArg *impl_arg) {
00483 CkCcsRequestMsg *impl_msg = (CkCcsRequestMsg*)impl_arg->msg;
00484 PythonObject *impl_obj = (PythonObject *) impl_arg->obj;
00485 delete impl_arg;
00486
00487 impl_obj->executeThread((PythonExecute*)impl_msg->data);
00488 delete impl_msg;
00489 }
00490
00491 void PythonObject::executeThread(PythonExecute *pyMsg) {
00492
00493
00494 PyThreadState *mine = PyThreadState_Get();
00495 PyObject *dict = PyModule_GetDict(PyImport_AddModule("__main__"));
00496 CmiUInt4 pyReference = PyInt_AsLong(PyDict_GetItemString(dict,"__charmNumber__"));
00497
00498 CkPythonDebugf("Status 1 %p\n",PyErr_Occurred());
00499
00500 if (pyMsg->isHighLevel()) {
00501
00502 pyWorkers[pyReference].thread=CthSelf();
00503
00504 }
00505
00506
00507 if (!pyMsg->isIterate()) {
00508 double _startTime = CmiWallTimer();
00509 PyObject* python_output = PyRun_String(pyMsg->code.code, Py_file_input, dict, dict);
00510 ckout<<"Python execution time "<<CmiWallTimer()-_startTime<<endl;
00511
00512
00513 if (python_output == NULL) {
00514
00515 PyObject *ptype, *pvalue, *ptraceback;
00516 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
00517 PyObject *strP = PyObject_Str(ptype);
00518 char *str = PyString_AsString(strP);
00519 Ck_printclient(this, pyReference, str);
00520
00521 Py_DECREF(strP);
00522 Ck_printclient(this, pyReference, ": ");
00523 strP = PyObject_Str(pvalue);
00524 str = PyString_AsString(strP);
00525 Ck_printclient(this, pyReference, str);
00526
00527 Py_DECREF(strP);
00528
00529
00530
00531
00532
00533
00534 }
00535 } else {
00536
00537 CkPythonDebugf("userCode: |%s|",pyMsg->code);
00538 CkPythonDebugf("method: |%s|",pyMsg->methodName.methodName);
00539
00540 char *userMethod = pyMsg->methodName.methodName;
00541 PyObject *mod = PyImport_AddModule("__main__");
00542 PyObject *dict = PyModule_GetDict(mod);
00543 PyObject *item = PyDict_GetItemString(dict, userMethod);
00544 struct _node *programNode = NULL;
00545 PyCodeObject *program = NULL;
00546 PyObject *code = NULL;
00547 if (item == NULL) {
00548 CkPythonDebugf("Could not find the requested method\n");
00549
00550 char *userCode = pyMsg->code.code;
00551 programNode = PyParser_SimpleParseString(userCode, Py_file_input);
00552 if (programNode==NULL) {
00553 CkPrintf("Program Parse Error\n");
00554
00555 cleanup(pyMsg, mine, pyReference);
00556 return;
00557 }
00558 program = PyNode_Compile(programNode, "");
00559 if (program==NULL) {
00560 CkPrintf("Program Compile Error\n");
00561 PyNode_Free(programNode);
00562
00563 cleanup(pyMsg, mine, pyReference);
00564 return;
00565 }
00566 code = PyEval_EvalCode(program, dict, dict);
00567 if (code==NULL) {
00568 CkPrintf("Program Eval Error\n");
00569 PyNode_Free(programNode);
00570 Py_DECREF(program);
00571
00572 cleanup(pyMsg, mine, pyReference);
00573 return;
00574 }
00575
00576
00577 char *userMethod = pyMsg->methodName.methodName;
00578 item = PyDict_GetItemString(dict, userMethod);
00579 if (item==NULL) {
00580 CkPrintf("Method not found\n");
00581 PyNode_Free(programNode);
00582 Py_DECREF(program);
00583 Py_DECREF(code);
00584
00585 cleanup(pyMsg, mine, pyReference);
00586 return;
00587 }
00588
00589
00590
00591 PyRun_String("class CharmContainer:\n\tpass\n\n", Py_file_input, dict, dict);
00592 }
00593 CkPythonDebugf("Item: %d %d %d\n",PyFunction_Check(item),PyMethod_Check(item),PyCallable_Check(item));
00594
00595
00596 CkPythonDebugf("Status 2 %p\n",PyErr_Occurred());
00597 PyObject *part = PyRun_String("CharmContainer()", Py_eval_input, dict, dict);
00598 if (part == NULL) {
00599 CkPythonDebugf("Allocate oldArg null: error=%d\n",PyErr_Occurred());
00600 PyErr_Clear();
00601 PyRun_String("class CharmContainer:\n\tpass\n\n", Py_file_input, dict, dict);
00602 }
00603 part = PyRun_String("CharmContainer()", Py_eval_input, dict, dict);
00604 CkPythonDebugf("Allocate oldArg=%p\n",part);
00605 PyObject *arg = PyTuple_New(1);
00606 PyObject *oldArg = part;
00607
00608 double _startTime = CmiWallTimer();
00609
00610 PythonIterator *userIterator = pyMsg->info.info;
00611 int more = buildIterator(part, userIterator);
00612 CkPythonDebugf("Executing iterative: %p %p %p %d\n",item,part,oldArg,more);
00613 if (oldArg != part) { Py_DECREF(oldArg); }
00614 PyTuple_SetItem(arg, 0, part);
00615
00616
00617 PyObject *result;
00618
00619 CkPythonDebugf("Errors: %p %p %p %p %p\n%p %p %p %p %p\n%p %p %p %p %p\n%p %p %p %p %p\n%p %p %p %p %p\n",
00620 PyExc_Exception, PyExc_StandardError, PyExc_ArithmeticError, PyExc_LookupError, PyExc_AssertionError,
00621 PyExc_AttributeError, PyExc_EOFError, PyExc_EnvironmentError, PyExc_FloatingPointError, PyExc_IOError,
00622 PyExc_ImportError, PyExc_IndexError, PyExc_KeyError, PyExc_NameError, PyExc_NotImplementedError,
00623 PyExc_OSError, PyExc_OverflowError, PyExc_ReferenceError, PyExc_RuntimeError, PyExc_SyntaxError,
00624 PyExc_SystemError, PyExc_SystemExit, PyExc_TypeError, PyExc_ValueError, PyExc_ZeroDivisionError);
00625
00626 while (more) {
00627 CkPythonDebugf("Status in 0 %p\n",PyErr_Occurred());
00628 result = PyObject_CallObject(item, arg);
00629 CkPythonDebugf("Status in 1 %p\n",PyErr_Occurred());
00630 if (PyErr_Occurred() != 0) {
00631
00632
00633 PyObject *exception, *v, *traceback, *tmp;
00634 PyErr_Fetch(&exception, &v, &traceback);
00635 PyErr_NormalizeException(&exception, &v, &traceback);
00636 CkPrintf("%s\n",PyString_AsString(tmp=PyObject_Repr(exception)));
00637 Py_DECREF(tmp);
00638 CkPrintf("%s\n",PyString_AsString(tmp=PyObject_Repr(v)));
00639 Py_DECREF(tmp);
00640 CkPrintf("%s\n",PyString_AsString(tmp=PyObject_Repr(traceback)));
00641 Py_DECREF(tmp);
00642 CkPrintf("%s\n",PyString_AsString(tmp=PyObject_Str(exception)));
00643 Py_DECREF(tmp);
00644 CkPrintf("%s\n",PyString_AsString(tmp=PyObject_Str(v)));
00645 Py_DECREF(tmp);
00646 CkPrintf("%s\n",PyString_AsString(tmp=PyObject_Str(traceback)));
00647 Py_DECREF(tmp);
00648 if (exception!=NULL) { Py_DECREF(exception); }
00649 if (v!=NULL) { Py_DECREF(v); }
00650 if (traceback!=NULL) { Py_DECREF(traceback); }
00651 }
00652 if (!result) {
00653 CkPrintf("Python Call error\n");
00654
00655 break;
00656 }
00657 oldArg = part;
00658 more = nextIteratorUpdate(part, result, userIterator);
00659 CkPythonDebugf("Status in 2 %p\n",PyErr_Occurred());
00660 if (oldArg != part) {
00661 Py_DECREF(oldArg);
00662 PyTuple_SetItem(arg, 0, part);
00663 }
00664 Py_DECREF(result);
00665 CkPythonDebugf("Status in 3 %p\n",PyErr_Occurred());
00666 }
00667 CkPythonDebugf("Python iterative execution time %lf\n",CmiWallTimer()-_startTime);
00668
00669 Py_DECREF(part);
00670 Py_DECREF(arg);
00671
00672
00673
00674
00675 }
00676 CkPythonDebugf("Status 3 %p\n",PyErr_Occurred());
00677
00678 cleanup(pyMsg, mine, pyReference);
00679
00680 }
00681
00682
00683
00684 void PythonObject::cleanup (PythonExecute *pyMsg, PyThreadState *pts, CmiUInt4 pyReference) {
00685 CmiUInt4 returnValue;
00686
00687
00688
00689
00690 if (pyWorkers[pyReference].finishReady) {
00691
00692 returnValue = htonl(pyReference);
00693 replyIntFn(this, &pyWorkers[pyReference].finish, &returnValue);
00694 pyWorkers[pyReference].finishReady = 0;
00695 }
00696
00697
00698 pyWorkers[pyReference].inUse = false;
00699 if (!pyMsg->isPersistent()) {
00700 PyErr_Clear();
00701 Py_EndInterpreter(pts);
00702 pyWorkers[pyReference].clientReady = -1;
00703 } else {
00704 PyErr_Clear();
00705 }
00706 PyEval_ReleaseLock();
00707
00708 if (!pyMsg->isPersistent() && pyWorkers[pyReference].printed.length()==0) {
00709
00710
00711 pyWorkers.erase(pyReference);
00712 }
00713
00714 }
00715
00716 void PythonObject::getPrint(CkCcsRequestMsg *msg) {
00717
00718 }
00719
00720
00721 void PythonObject::pythonSetString(PyObject *arg, char *descr, char *value) {
00722 PyObject *tmp = PyString_FromString(value);
00723 PyObject_SetAttrString(arg, descr, tmp);
00724 Py_DECREF(tmp);
00725 }
00726
00727 void PythonObject::pythonSetString(PyObject *arg, char *descr, char *value, int len) {
00728 PyObject *tmp = PyString_FromStringAndSize(value, len);
00729 PyObject_SetAttrString(arg, descr, tmp);
00730 Py_DECREF(tmp);
00731 }
00732
00733 void PythonObject::pythonGetString(PyObject *arg, char *descr, char **result) {
00734 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00735 *result = PyString_AsString(tmp);
00736 Py_DECREF(tmp);
00737 }
00738
00739 void PythonObject::pythonSetInt(PyObject *arg, char *descr, long value) {
00740 PyObject *tmp = PyInt_FromLong(value);
00741 PyObject_SetAttrString(arg, descr, tmp);
00742 Py_DECREF(tmp);
00743 }
00744
00745 void PythonObject::pythonGetInt(PyObject *arg, char *descr, long *result) {
00746 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00747 *result = PyInt_AsLong(tmp);
00748 Py_DECREF(tmp);
00749 }
00750
00751 void PythonObject::pythonSetLong(PyObject *arg, char *descr, long value) {
00752 PyObject *tmp = PyLong_FromLong(value);
00753 PyObject_SetAttrString(arg, descr, tmp);
00754 Py_DECREF(tmp);
00755 }
00756
00757 void PythonObject::pythonSetLong(PyObject *arg, char *descr, unsigned long value) {
00758 PyObject *tmp = PyLong_FromUnsignedLong(value);
00759 PyObject_SetAttrString(arg, descr, tmp);
00760 Py_DECREF(tmp);
00761 }
00762
00763 void PythonObject::pythonSetLong(PyObject *arg, char *descr, double value) {
00764 PyObject *tmp = PyLong_FromDouble(value);
00765 PyObject_SetAttrString(arg, descr, tmp);
00766 Py_DECREF(tmp);
00767 }
00768
00769 void PythonObject::pythonGetLong(PyObject *arg, char *descr, long *result) {
00770 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00771 *result = PyLong_AsLong(tmp);
00772 Py_DECREF(tmp);
00773 }
00774
00775 void PythonObject::pythonGetLong(PyObject *arg, char *descr, unsigned long *result) {
00776 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00777 *result = PyLong_AsUnsignedLong(tmp);
00778 Py_DECREF(tmp);
00779 }
00780
00781 void PythonObject::pythonGetLong(PyObject *arg, char *descr, double *result) {
00782 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00783 *result = PyLong_AsDouble(tmp);
00784 Py_DECREF(tmp);
00785 }
00786
00787 void PythonObject::pythonSetFloat(PyObject *arg, char *descr, double value) {
00788 PyObject *tmp = PyFloat_FromDouble(value);
00789 PyObject_SetAttrString(arg, descr, tmp);
00790 Py_DECREF(tmp);
00791 }
00792
00793 void PythonObject::pythonGetFloat(PyObject *arg, char *descr, double *result) {
00794 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00795 *result = PyFloat_AsDouble(tmp);
00796 Py_DECREF(tmp);
00797 }
00798
00799 void PythonObject::pythonSetComplex(PyObject *arg, char *descr, double real, double imag) {
00800 PyObject *tmp = PyComplex_FromDoubles(real, imag);
00801 PyObject_SetAttrString(arg, descr, tmp);
00802 Py_DECREF(tmp);
00803 }
00804
00805 void PythonObject::pythonGetComplex(PyObject *arg, char *descr, double *real, double *imag) {
00806 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00807 *real = PyComplex_RealAsDouble(tmp);
00808 *imag = PyComplex_ImagAsDouble(tmp);
00809 Py_DECREF(tmp);
00810 }
00811
00812 void PythonObject::pythonSetPointer(PyObject *arg, char *descr, void *ptr) {
00813 PyObject *tmp = PyLong_FromVoidPtr(ptr);
00814 PyObject_SetAttrString(arg, descr, tmp);
00815 Py_DECREF(tmp);
00816 }
00817
00818 void PythonObject::pythonGetPointer(PyObject *arg, char *descr, void **ptr) {
00819 PyObject *tmp = PyObject_GetAttrString(arg, descr);
00820 *ptr = PyLong_AsVoidPtr(tmp);
00821 Py_DECREF(tmp);
00822 }
00823
00824 PyObject *PythonObject::pythonGetArg(int handle) {
00825
00826 PyObject *result = pyWorkers[handle].arg;
00827
00828 return result;
00829 }
00830
00831 void PythonObject::pythonReturn(int handle) {
00832
00833
00834
00835
00836
00837
00838
00839 }
00840
00841 void PythonObject::pythonReturn(int handle, PyObject* data) {
00842
00843
00844 * pyWorkers[handle].result = data;
00845
00846
00847 }
00848
00849 void PythonObject::pythonAwake(int handle) {
00850
00851
00852
00853
00854
00855 }
00856
00857 void PythonObject::pythonSleep(int handle) {
00858
00859 }
00860
00861 PythonCCS::PythonCCS(CkArgMsg *arg) {
00862 pythonCcsProxy = thishandle;
00863 delete arg;
00864 }
00865
00866 CkReductionMsg *pythonCombinePrint(int nMsg, CkReductionMsg **msgs) {
00867
00868
00869
00870 int length = - (nMsg-1) * sizeof(CcsDelayedReply);
00871 for (int i=0; i<nMsg; ++i) {
00872 length += msgs[i]->getSize();
00873 }
00874
00875 CkReductionMsg *result = CkReductionMsg::buildNew(length,NULL);
00876
00877 PythonReplyString *data = (PythonReplyString*)(result->getData());
00878 data->reply = ((PythonReplyString*)msgs[0]->getData())->reply;
00879 char *cur=data->data;
00880 for (int i=0; i<nMsg; ++i) {
00881 int messageBytes=msgs[i]->getSize() - sizeof(CcsDelayedReply);
00882 memcpy((void *)cur,(void *)((PythonReplyString*)msgs[i]->getData())->data,messageBytes);
00883 cur+=messageBytes;
00884 }
00885 return result;
00886 }
00887
00888 void PythonCCS::forwardString(CkReductionMsg *msg) {
00889 PythonReplyString *forward = (PythonReplyString *)msg->getData();
00890 CcsSendDelayedReply(forward->reply, msg->getSize()-sizeof(CcsDelayedReply), (void *)forward->data);
00891 }
00892
00893 void PythonCCS::forwardInt(CkReductionMsg *msg) {
00894 CkPythonDebugf("PythonCCS::forwardInt\n");
00895 PythonReplyInt *forward = (PythonReplyInt *)msg->getData();
00896 CcsSendDelayedReply(forward->reply, sizeof(CmiUInt4), (void *)&forward->value);
00897 }
00898
00899 CkReduction::reducerType PythonCCS::reduceString;
00900
00901 static void initializePythonDefault(void) {
00902
00903
00904
00905
00906
00907
00908 CtvInitialize(PyObject *,pythonReturnValue);
00909
00910 PythonCCS::reduceString = CkReduction::addReducer(pythonCombinePrint, false, "pythonCombinePrint");
00911
00912 Py_Initialize();
00913 PyEval_InitThreads();
00914
00915 PyEval_ReleaseLock();
00916 }
00917
00918 #include "PythonCCS.def.h"