new example for python interface with a high level functionatily
authorFilippo Gioachin <gioachin@illinois.edu>
Fri, 24 Sep 2004 21:57:48 +0000 (21:57 +0000)
committerFilippo Gioachin <gioachin@illinois.edu>
Fri, 24 Sep 2004 21:57:48 +0000 (21:57 +0000)
tests/charm++/python/Makefile [new file with mode: 0644]
tests/charm++/python/client.C [new file with mode: 0644]
tests/charm++/python/server.C [new file with mode: 0644]
tests/charm++/python/server.ci [new file with mode: 0644]
tests/charm++/python/server.h [new file with mode: 0644]

diff --git a/tests/charm++/python/Makefile b/tests/charm++/python/Makefile
new file mode 100644 (file)
index 0000000..e54d722
--- /dev/null
@@ -0,0 +1,35 @@
+CHARMC = ../../../../bin/charmc -g
+INCL = -I/usr/include/python/
+LIBS = /usr/lib/python/config/libpython2.3.a -lpthread -lutil -ldl -lm
+INCLC = -I../../../../include/
+MODULES = -module PythonCCS
+PYMODULE = ../../../../lib/libmodulePythonCCS.a
+
+OBJS = server.o
+
+
+all: server client
+
+server: $(OBJS)
+       $(CHARMC) $(INCL) -language charm++ -o server $(OBJS) $(LIBS) $(MODULES)
+
+server.decl.h: server.ci $(PYMODULE) ../../../../bin/charmxi
+       $(CHARMC) $<
+
+client: client.C
+       g++ $(INCLC) -o $@ $<
+
+clean:
+       rm -f *.decl.h *.def.h *.o server client charmrun *~
+
+
+server.o: server.C server.decl.h server.h
+       $(CHARMC) -c $<
+
+test: all
+       ./charmrun ./server ++remote-shell ssh ++server ++server-port 1234 +p2 $(OPTS) &
+       ./client localhost 1234
+       sleep 2
+       ./client localhost 1234
+       sleep 2
+       ./client localhost 1234 kill
diff --git a/tests/charm++/python/client.C b/tests/charm++/python/client.C
new file mode 100644 (file)
index 0000000..73924c2
--- /dev/null
@@ -0,0 +1,23 @@
+#include <stdlib.h>
+#include "ccs-client.h"
+#include "ccs-client.c"
+#include <string.h>
+
+int main (int argc, char** argv) {
+
+  if (argc<3) return 1;
+
+  char code[1000] = "ck.printstr('python')\n"
+    "value=charm.runhigh()\n"
+    "ck.printstr('python value: '+repr(value))\n";
+
+  CcsServer server;
+  char *host=argv[1];
+  int port=atoi(argv[2]);
+
+  CcsConnect (&server, host, port, NULL);
+
+  // if there is a third argument means kill the server
+  if (argc>3) CcsSendRequest (&server, "kill", 0, 1, code);
+  else CcsSendRequest (&server, "pyCode", 0, strlen(code)+1, code);
+}
diff --git a/tests/charm++/python/server.C b/tests/charm++/python/server.C
new file mode 100644 (file)
index 0000000..70ab4f6
--- /dev/null
@@ -0,0 +1,66 @@
+#include "server.h"
+
+/* global readonly variable */
+CProxy_Main mainProxy;
+CProxy_MyArray mypython;
+
+Main::Main (CkArgMsg *msg) {
+
+  /* Load all data needed by the program and then create the interactor for python,
+     it can also be created as Array or Nodegroup with the consequent differences
+     for the python code been inserted */
+  elem = 10;
+
+  mypython = CProxy_MyArray::ckNew(elem);
+
+  count=0;
+  total=0;
+  mainProxy = thishandle;
+  pythonHandle1=0;
+  pythonHandle2=0;
+
+  // register handler for callback
+  CcsRegisterHandler("pyCode", CkCallback(CkIndex_Main::execute(0),thishandle));
+  CcsRegisterHandler("kill", CkCallback(CkIndex_Main::exit(),thishandle));
+
+}
+
+void Main::exit () {
+  CkExit();
+}
+
+void Main::ccs_kill (CkCcsRequestMsg *msg) {
+  CkExit();
+}
+
+void Main::runhigh(int i) {
+  if (pythonHandle1) pythonHandle2=i;
+  else pythonHandle1=i;
+  mypython.run();
+}
+
+void Main::arrayResult (int value) {
+  total += value;
+  if (++count == elem) {
+    count = 0;
+    int *pythonHandle;
+    if (pythonHandle1) pythonHandle=&pythonHandle1;
+    else pythonHandle=&pythonHandle2;
+    pythonPrepareReturn(*pythonHandle);
+    pythonReturn(*pythonHandle,Py_BuildValue("i",total));
+    *pythonHandle = 0;
+    total=0;
+  }
+}
+
+MyArray::MyArray () {mynumber = thisIndex+1000;}
+
+MyArray::MyArray (CkMigrateMessage *msg) {}
+
+void MyArray::run() {
+  CkPrintf("[%d] in run %d\n",thisIndex,mynumber);
+  sleep(1);
+  mainProxy.arrayResult(mynumber);
+}
+
+#include "server.def.h"
diff --git a/tests/charm++/python/server.ci b/tests/charm++/python/server.ci
new file mode 100644 (file)
index 0000000..6819772
--- /dev/null
@@ -0,0 +1,19 @@
+mainmodule server {
+
+       readonly CProxy_MyArray mypython;
+       readonly CProxy_Main mainProxy;
+
+       mainchare [python] Main {
+               entry Main(CkArgMsg *msg);
+               entry void exit(void);
+               entry void arrayResult(int value);
+               entry void ccs_kill (CkCcsRequestMsg *msg);
+               entry [python] void runhigh(int);
+       }
+
+       array [1D] MyArray {
+               entry MyArray(void);
+               entry void run(void);
+       }
+
+}
diff --git a/tests/charm++/python/server.h b/tests/charm++/python/server.h
new file mode 100644 (file)
index 0000000..15001d7
--- /dev/null
@@ -0,0 +1,41 @@
+//#include "PythonCCS.h"
+#include "server.decl.h"
+
+/*
+class Python_Main : public PythonChare {
+ protected:
+  static PyMethodDef CkPy_MethodsCustom[];
+  PyObject *localArgs;
+  PyObject **localResult;
+  CthThread localThread;
+  PyThreadState *localPyThread;
+ public:
+  PyMethodDef *getMethods() {return CkPy_MethodsCustom;}
+  void _Py_runhigh(PyObject*, PyObject**, CthThread, PyThreadState*);
+};
+*/
+
+class Main : public CBase_Main {
+ public:
+  Main(CkArgMsg *msg);
+  void exit();
+  void arrayResult(int mycontrib);
+  void ccs_kill (CkCcsRequestMsg *msg);
+  void runhigh(int);
+  PyObject *returnValue;
+ private:
+  int count;
+  int total;
+  int elem;
+  int pythonHandle1;
+  int pythonHandle2;
+};
+
+class MyArray : public CBase_MyArray {
+ public:
+  MyArray();
+  MyArray(CkMigrateMessage *msg);
+  void run();
+ private:
+  int mynumber;
+};