examples: added example program demonstrating threaded entry methods
authorAaron Becker <akbecker@gmail.com>
Thu, 2 Aug 2012 23:43:49 +0000 (18:43 -0500)
committerAaron Becker <akbecker@gmail.com>
Thu, 2 Aug 2012 23:44:15 +0000 (18:44 -0500)
examples/charm++/threaded_ring/Makefile [new file with mode: 0644]
examples/charm++/threaded_ring/threaded_ring.cc [new file with mode: 0644]
examples/charm++/threaded_ring/threaded_ring.ci [new file with mode: 0644]
examples/charm++/threaded_ring/threaded_ring.h [new file with mode: 0644]

diff --git a/examples/charm++/threaded_ring/Makefile b/examples/charm++/threaded_ring/Makefile
new file mode 100644 (file)
index 0000000..1934cae
--- /dev/null
@@ -0,0 +1,27 @@
+CHARMC = ../../../bin/charmc
+BINARY = threaded_ring
+
+CHARMCFLAGS =
+CHARMCLINKFLAGS = -language charm++
+TESTFLAGS =
+
+%.o: %.cc
+
+all: $(BINARY)
+$(BINARY): $(patsubst %.cc,%.o,$(wildcard *.cc))
+       $(CHARMC) $(CHARMCLINKFLAGS) -o $@ $+
+
+.SECONDARY: $(patsubst %.cc,%.decl.h,$(wildcard *.cc))
+.SECONDARY: $(patsubst %.cc,%.def.h,$(wildcard *.cc))
+
+%.o: %.cc %.decl.h %.def.h
+       $(CHARMC) $(CHARMCFLAGS) $<
+
+%.decl.h %.def.h: %.ci
+       $(CHARMC) $(CHARMCFLAGS) $<
+
+test: $(BINARY)
+       ./charmrun ./$(BINARY) $(TESTFLAGS)
+
+clean:
+       rm -f *.o *.decl.h *.def.h charmrun $(BINARY)
diff --git a/examples/charm++/threaded_ring/threaded_ring.cc b/examples/charm++/threaded_ring/threaded_ring.cc
new file mode 100644 (file)
index 0000000..c9c3981
--- /dev/null
@@ -0,0 +1,49 @@
+#include "threaded_ring.h"
+
+Main::Main(CkArgMsg*)
+{
+    nElems = 10;
+    CProxy_Ring A = CProxy_Ring::ckNew(nElems);
+    CkCallback* cb = new CkCallback(CkCallback::ckExit);
+    A.ckSetReductionClient(cb);
+    A.run();
+}
+
+void Ring::run()
+{
+    CkPrintf("[%d]: nElems = %d\n", thisIndex, nElems);
+    nElems = 10;
+    for (int i=0; i<nElems; ++i) {
+        CkPrintf("[%d] iteration %d\n", thisIndex, i);
+        thisProxy((thisIndex+1) % nElems).getData();
+        waitFor();
+    }
+    CkPrintf("[%d] done\n", thisIndex);
+    contribute(0, 0, CkReduction::concat);
+}
+
+void Ring::getData()
+{
+    CkPrintf("[%d]::getData()\n", thisIndex);
+    if (threadWaiting) {
+        CthAwaken(t);
+        threadWaiting = false;
+    } else {
+        dataHere++;
+    }
+}
+
+void Ring::waitFor()
+{
+    CkPrintf("[%d]::waitFor()\n", thisIndex);
+    if (dataHere) {
+        dataHere--;
+        return;
+    }
+
+    t = CthSelf();
+    threadWaiting = true;
+    CthSuspend();
+}
+
+#include "threaded_ring.def.h"
diff --git a/examples/charm++/threaded_ring/threaded_ring.ci b/examples/charm++/threaded_ring/threaded_ring.ci
new file mode 100644 (file)
index 0000000..4162c4d
--- /dev/null
@@ -0,0 +1,12 @@
+mainmodule threaded_ring {
+    readonly int nElems;
+    mainchare Main {
+        entry Main(CkArgMsg*);
+    }
+
+    array [1D] Ring {
+        entry Ring();
+        entry [threaded] void run();
+        entry void getData();
+    }
+}
diff --git a/examples/charm++/threaded_ring/threaded_ring.h b/examples/charm++/threaded_ring/threaded_ring.h
new file mode 100644 (file)
index 0000000..454d5a2
--- /dev/null
@@ -0,0 +1,27 @@
+#include "threaded_ring.decl.h"
+
+int nElems;
+
+class Main : public CBase_Main
+{
+public:
+    Main(CkArgMsg*);
+    Main(CkMigrateMessage*) {}
+};
+
+class Ring : public CBase_Ring
+{
+public:
+    Ring() {
+        threadWaiting = false;
+        dataHere =  0;
+    }
+    Ring(CkMigrateMessage*) {}
+    void run();
+    void getData();
+    void waitFor();
+private:
+    CthThread t;
+    int dataHere;
+    bool threadWaiting;
+};