example code for one sided communication
authorNilesh Choudhury <nchoudh2@uiuc.edu>
Fri, 2 Jun 2006 22:13:44 +0000 (22:13 +0000)
committerNilesh Choudhury <nchoudh2@uiuc.edu>
Fri, 2 Jun 2006 22:13:44 +0000 (22:13 +0000)
examples/charm++/onesided/Makefile [new file with mode: 0644]
examples/charm++/onesided/onesided.C [new file with mode: 0644]
examples/charm++/onesided/onesided.ci [new file with mode: 0644]
examples/charm++/onesided/onesided.h [new file with mode: 0644]

diff --git a/examples/charm++/onesided/Makefile b/examples/charm++/onesided/Makefile
new file mode 100644 (file)
index 0000000..5a5009e
--- /dev/null
@@ -0,0 +1,15 @@
+CHARMC=../../../bin/charmc $(OPTS)
+
+pgm:  onesided.o
+       $(CHARMC) onesided.o  -o onesidedtest -language charm++
+
+onesided.o : onesided.C onesided.h onesided.ci
+       $(CHARMC) -c onesided.ci 
+       $(CHARMC) -c onesided.C
+
+test: pgm
+       ./charmrun onesidedtest 0 +p2
+       ./charmrun onesidedtest 1 +p2
+
+clean:
+       rm -f conv-host onesidedtest *.def.h *.decl.h *.o *~ charmrun
diff --git a/examples/charm++/onesided/onesided.C b/examples/charm++/onesided/onesided.C
new file mode 100644 (file)
index 0000000..622e0ff
--- /dev/null
@@ -0,0 +1,151 @@
+#include <unistd.h>
+#include "onesided.h"
+
+CProxy_Main mainProxy;
+
+Main::Main(CkArgMsg* m)
+{
+  int numProcs = 2;
+  CProxy_commtest arr = CProxy_commtest::ckNew(2);
+  mainProxy = thisProxy;
+
+  arr[0].startRMA(atoi(m->argv[1]));
+  delete m;
+}
+
+void Main::done(void)
+{
+  CkExit();
+}
+
+
+commtest::commtest()
+{
+  idx = CkMyPe();
+  dest = (idx==0)?1:0;
+  srcAddr = NULL;
+  destAddr = NULL;
+  size = 50;
+  srcChar = (idx==0)?'1':'2';
+  destChar = (idx==0)?'2':'1';
+  operation = 0;
+  CkPrintf("[%d]Object created on %d\n",idx,idx);
+}
+
+void commtest::startRMA(int op) {
+  operation = op;
+  //srcAddr = (char*)malloc(size*sizeof(char));
+  srcAddr = (char*)CmiDMAAlloc(size);
+  initializeMem(srcAddr,size,srcChar);
+  //CkPrintf("[%d]Trying to register memory %p\n",idx,srcAddr);
+  //CmiRegisterMemory((void*)srcAddr,size);
+  thisProxy[dest].remoteRMA(size,operation);
+}
+
+void commtest::remoteRMA(int len,int op) {
+  size = len;
+  operation = 1 - op;
+  //destAddr = (char*)malloc(size*sizeof(char));
+  destAddr = (char*)CmiDMAAlloc(size);
+  initializeMem(destAddr,size,srcChar);
+  //CkPrintf("[%d]Trying to register memory %p\n",idx,destAddr);
+  //CmiRegisterMemory((void*)destAddr,size);
+  charMsg *cm = new charMsg();
+  cm->addr = destAddr;
+  thisProxy[dest].recvAddr(cm);
+}
+
+void commtest::recvAddr(charMsg *cm)
+{
+  destAddr = cm->addr;
+  //now that we have all info, could do either a get or put
+  if(operation==0) {
+    CkPrintf("[%d]Trying to do a remote put %p to %p\n",idx,srcAddr,destAddr);
+    pend = CmiPut(idx, dest, (void*)srcAddr, (void*)destAddr, size);
+  }
+  else {
+    CkPrintf("[%d]Trying to do a remote get %p to %p\n",idx,srcAddr,destAddr);
+    pend = CmiGet(idx, dest, (void*)srcAddr, (void*)destAddr, size);
+  }
+  testDone();
+  delete cm;
+}
+
+void commtest::verifyCorrectRMA(char c) {
+  char *tmp;
+  if(idx==0) {
+    tmp = srcAddr;
+  }
+  else {
+    tmp = destAddr;
+  }
+  CkPrintf("[%d]Verifying correct RMA\n",idx);
+  bool flag = true;
+  for(int i=0; i<size; i++) {
+    if(*tmp != c) {
+      CkPrintf("[%d]Error: address: %p, expectedValue: %c actualValue: %c\n",idx,tmp,c,*tmp);
+      flag = false;
+      break;
+    }
+    tmp++;
+  }
+  if(!flag) {
+    //thisProxy[dest].testDone();
+  }
+  else {
+    CkPrintf("[%d]DMA operation %d correctly finished!!\n",idx,operation);
+  }
+  mainProxy.done();
+}
+
+void commtest::doJnkWork(void) {
+  const double left=0.1, right=0.2;
+  register double v1=0.1,v2=0.3,v3=1.0;
+  int i;
+  int iter = 30000000;
+  for (i=0;i<iter;i++) {
+    v1=(v2+v3)*0.5;
+    v2=(v3+v1)*0.5;
+    v3=(v1+v2)*0.5;
+  }
+  CkPrintf("[%d]Did jnk work\n",idx);
+  thisProxy[dest].testDone();
+}
+
+void commtest::testDone(void) {
+  testForCompletion();
+  int done = CmiWaitTest(pend);
+  CkPrintf("[%d]Test if done %p, %d!!\n",idx,pend,*((int*)pend));
+  if(done==1) {
+    testForCorrectness();
+  }
+}
+
+void commtest::initializeMem(char *addr, int len, char c) {
+  for(int i=0; i<len; i++) {
+    *addr = c;
+    addr++;
+  }
+}
+
+void commtest::testForCompletion(void) {
+  int done = CmiWaitTest(pend);
+  if(done!=1) {
+    CkPrintf("[%d]Do jnk work\n",idx);
+    thisProxy[dest].doJnkWork();
+    done = CmiWaitTest(pend);
+  }
+}
+
+void commtest::testForCorrectness(void) {
+  CkPrintf("[%d]Just finished RMA operation %d\n",idx,operation);
+  if(operation==0) { //put operation
+    thisProxy[dest].verifyCorrectRMA(srcChar);
+  }
+  else {
+    verifyCorrectRMA(destChar);
+  }
+}
+
+#include "onesided.def.h"
+
diff --git a/examples/charm++/onesided/onesided.ci b/examples/charm++/onesided/onesided.ci
new file mode 100644 (file)
index 0000000..2c4497b
--- /dev/null
@@ -0,0 +1,22 @@
+mainmodule onesided {
+
+  readonly CProxy_Main mainProxy;
+
+  message charMsg;
+
+  mainchare Main {
+    entry Main(CkArgMsg *m);
+    entry void done(void);
+  };
+  
+  array  [1D] commtest {
+    entry commtest(void);
+    entry void startRMA(int op);
+    entry void remoteRMA(int len, int op);
+    entry void recvAddr(charMsg *cm);
+    entry void verifyCorrectRMA(char c);
+    entry void doJnkWork(void);
+    entry void testDone(void);
+  };
+}
+
diff --git a/examples/charm++/onesided/onesided.h b/examples/charm++/onesided/onesided.h
new file mode 100644 (file)
index 0000000..69af464
--- /dev/null
@@ -0,0 +1,36 @@
+#include "onesided.decl.h"
+
+class charMsg : public CMessage_charMsg {
+ public:
+  char *addr;
+};
+
+class Main : public CBase_Main {
+ public:
+  Main(CkMigrateMessage *m) {}
+  Main(CkArgMsg *);
+  void done(void);
+};
+
+class commtest : public CBase_commtest {
+ private:
+  int idx;
+  char *srcAddr, *destAddr;
+  unsigned int size, dest;
+  char srcChar, destChar;
+  int operation; //get or put (current operation)
+  void *pend;
+ public:
+  commtest(CkMigrateMessage *m) {}
+  commtest(void);
+  void startRMA(int op);
+  void remoteRMA(int len, int op);
+  void recvAddr(charMsg *cm);
+  void verifyCorrectRMA(char c);
+  void doJnkWork(void);
+  void testDone(void);
+  void initializeMem(char *addr, int len, char c);
+  void testForCompletion(void);
+  void testForCorrectness(void);
+};
+