allreduce example added to charm
authorEhsan Totoni <totoni2@illinois.edu>
Fri, 24 Jun 2011 16:25:18 +0000 (11:25 -0500)
committerEhsan Totoni <totoni2@illinois.edu>
Fri, 24 Jun 2011 16:25:18 +0000 (11:25 -0500)
examples/charm++/AllReduce/AllReduce [new file with mode: 0755]
examples/charm++/AllReduce/AllReduce.C [new file with mode: 0644]
examples/charm++/AllReduce/AllReduce.ci [new file with mode: 0644]
examples/charm++/AllReduce/AllReduce.h [new file with mode: 0644]
examples/charm++/AllReduce/Makefile [new file with mode: 0644]

diff --git a/examples/charm++/AllReduce/AllReduce b/examples/charm++/AllReduce/AllReduce
new file mode 100755 (executable)
index 0000000..1b1dbf1
Binary files /dev/null and b/examples/charm++/AllReduce/AllReduce differ
diff --git a/examples/charm++/AllReduce/AllReduce.C b/examples/charm++/AllReduce/AllReduce.C
new file mode 100644 (file)
index 0000000..68935e3
--- /dev/null
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include "AllReduce.decl.h"
+#include <math.h>
+
+
+/*readonly*/ CProxy_main mainProxy;
+/*readonly*/ int units;
+/*readonly*/ int allredSize;
+#include "AllReduce.h"
+
+/*mainchare*/
+
+main::main(CkArgMsg* m)
+  {
+    //Process command-line arguments
+    //Start the computation
+
+    mainProxy = thishandle;
+    if(m->argc<2)
+      {
+             CkPrintf("Needs number of array elements and allreduce data size\n");
+       CkExit();
+      }
+    units=atoi(m->argv[1]);
+    allredSize=atoi(m->argv[2]);
+    
+    arr = CProxy_AllReduce::ckNew(thisProxy, units);
+
+    CkPrintf("AllReduce for %d pes on %d units for %d size\n",
+            CkNumPes(),units,allredSize);
+
+    arr.init();
+    startTime = CmiWallTimer();
+    arr.dowork();
+  }
+
+#include "AllReduce.def.h"
diff --git a/examples/charm++/AllReduce/AllReduce.ci b/examples/charm++/AllReduce/AllReduce.ci
new file mode 100644 (file)
index 0000000..fa5b8a3
--- /dev/null
@@ -0,0 +1,16 @@
+mainmodule AllReduce {
+  readonly int units;
+  readonly int allredSize;
+
+  mainchare main {
+    entry main(CkArgMsg *m);
+    entry void done();
+  };
+
+  array [1D] AllReduce {
+    entry AllReduce(CProxy_main);
+    entry void init();
+    entry void dowork(void);   
+    entry void report(CkReductionMsg *msg);
+  };           
+}
diff --git a/examples/charm++/AllReduce/AllReduce.h b/examples/charm++/AllReduce/AllReduce.h
new file mode 100644 (file)
index 0000000..8996d63
--- /dev/null
@@ -0,0 +1,70 @@
+//! main char object.  Creates array, handles command line, catches reduction.
+#include <ctime>
+
+class main : public CBase_main
+{
+private:
+  CProxy_AllReduce arr;
+       double startTime;
+public:
+
+  main(CkArgMsg* m);
+
+  void done()
+  {
+         static int checkIn=0;
+         checkIn++;
+         if (checkIn==units) {
+                 CkPrintf("All done in %lf seconds\n",(CmiWallTimer()-startTime));
+                 CkExit();  
+         }
+  }
+
+};
+
+
+//! AllReduce. A small example which does nothing useful, but provides an extremely simple, yet complete, use of a normal reduction case.
+/** The reduction performs a summation i+k1 i+k2 for i=0 to units
+ */
+class AllReduce : public CBase_AllReduce
+{
+ private:
+       CProxy_main mainProxy;
+       double* myData;
+ public:
+
+       AllReduce(CProxy_main ma)   { mainProxy=ma;  }
+
+  AllReduce(CkMigrateMessage *m) {};
+
+       void init()
+       {
+                               myData = new double[allredSize/(sizeof(double))];
+                                               for (int i=0; i<allredSize/sizeof(double); i++) {
+                                                                               myData[i] = (double)i;
+                                                                                               }
+       }
+
+  //! add our index to the global floats and contribute the result.
+  void dowork ()
+    {
+       CkCallback cb(CkIndex_AllReduce::report(NULL),  thisProxy);
+      contribute(allredSize,myData,CkReduction::sum_double,cb); 
+    }
+  //! catches the completed reduction results.
+  void report(CkReductionMsg *msg)
+  {
+         int reducedArrSize=msg->getSize()/sizeof(double);
+         double *output=(double *)msg->getData();
+         CkPrintf("done %d\n", thisIndex);
+         for(int i=0;i<reducedArrSize;i++) {
+                 if (output[i]!=i*units) {
+                         CkPrintf("result error!\n");
+                 }
+         }
+         delete msg;
+         mainProxy.done();
+  }
+
+};
+
diff --git a/examples/charm++/AllReduce/Makefile b/examples/charm++/AllReduce/Makefile
new file mode 100644 (file)
index 0000000..3573d1f
--- /dev/null
@@ -0,0 +1,23 @@
+CHARMC=../../../bin/charmc $(OPTS) $(MOPTS) 
+
+OBJS = AllReduce.o
+
+all: AllReduce
+
+AllReduce: $(OBJS)
+       $(CHARMC) -language charm++ -o AllReduce $(OBJS) 
+
+AllReduce.decl.h: AllReduce.ci
+       $(CHARMC)  AllReduce.ci
+
+clean:
+       rm -f *.decl.h *.def.h conv-host *.o AllReduce charmrun *.log *.sum *.sts charmrun.exe AllReduce.exe AllReduce.pdb AllReduce.ilk
+
+AllReduce.o: AllReduce.C AllReduce.decl.h AllReduce.h
+       $(CHARMC) -c AllReduce.C
+
+test: all
+       ./charmrun ./AllReduce 20 100000 +p4 $(TESTOPTS)
+
+bgtest: all
+       ./charmrun ./AllReduce 20 100000 +p4 +x2 +y2 +z2