x-array sections: Hello world example code to demo cross-array sections
authorRamprasad Venkataraman <ramv@illinois.edu>
Fri, 27 Aug 2010 16:45:46 +0000 (11:45 -0500)
committerRamprasad Venkataraman <ramv@illinois.edu>
Tue, 31 Aug 2010 19:30:37 +0000 (14:30 -0500)
examples/charm++/hello/xarraySection/Makefile [new file with mode: 0644]
examples/charm++/hello/xarraySection/README [new file with mode: 0644]
examples/charm++/hello/xarraySection/hello.C [new file with mode: 0644]
examples/charm++/hello/xarraySection/hello.ci [new file with mode: 0644]

diff --git a/examples/charm++/hello/xarraySection/Makefile b/examples/charm++/hello/xarraySection/Makefile
new file mode 100644 (file)
index 0000000..ed3831e
--- /dev/null
@@ -0,0 +1,75 @@
+# Point this to your charm installation
+CHARM ?= ../../../..
+
+# Charm directory structure
+CHARMBIN := $(CHARM)/bin
+CHARMINC := $(CHARM)/include
+
+# The relevant source files for this project
+HDR       =
+SRC       = hello.C
+OBJ       = $(SRC:.C=.o)
+INTF      = hello.ci
+
+# Specify the exe name and the arguments to run it with
+NP        = 4
+TARGET    = xarraySection
+ARGS      = 10 3 3
+
+# Specify the compilers, run script, flags etc.
+CXX       = $(CHARMBIN)/charmc
+CHARMC    = $(CHARMBIN)/charmc
+OPT       = -O0 -g
+CPPFLAGS +=
+CXXFLAGS += -language charm++ $(OPT)
+LDFLAGS  += -module CkMulticast
+LDLIBS   +=
+EXEC      = ./charmrun
+EXECFLAGS = +p$(NP)
+ifdef $(NODELIST)
+  EXECFLAGS += ++nodelist $(NODELIST)
+endif
+
+
+########### This stuff should be able take care of itself ############
+
+.PHONY: all clean again test bgtest translateInterface
+
+all: $(TARGET)
+
+$(TARGET): $(OBJ)
+       $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+clean:
+       $(RM) $(wildcard *.decl.h *.def.h *.d *.di *.o *.log *.projrc *.sts *.stamp) $(TARGET) charmrun
+
+again:
+       $(MAKE) clean; $(MAKE)
+
+test: all
+       @echo "########################################################################################"
+       $(EXEC) $(EXECFLAGS) $(TARGET) $(ARGS)
+
+####### Pattern rules
+# Rule to generate dependency information for C++ source files
+%.d: %.C
+       $(info Generating dependencies for $<)
+       @g++ -MM -MG $(CPPFLAGS) $(INCDIRS:%=-I%) -I$(CHARMINC) $< | perl $(CHARMBIN)/dep.pl $(CHARMINC) > $@
+#      @$(SHELL) -ec 'g++ -MM -MG $(CPPFLAGS) $(INCDIRS:%=-I%) $< \
+#      | sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
+#      [ -s $@ ] || rm -f $@'
+
+# Rule to generate dependency info for charm++ interface (ci) definition files
+%.di: %.ci
+       $(info Generating dependencies for $<)
+       @$(CHARMC) -M $< > $@
+
+# Compilation rule for ci files
+%.ci.stamp: %.ci
+       $(info-ci)
+       $q$(CHARMC) $< && touch $@
+
+# Include the generated files containing dependency info
+-include $(SRC:.C=.d)
+-include $(INTF:.ci=.di)
+
diff --git a/examples/charm++/hello/xarraySection/README b/examples/charm++/hello/xarraySection/README
new file mode 100644 (file)
index 0000000..0f9830f
--- /dev/null
@@ -0,0 +1,7 @@
+Demo for delegated cross-array sections.
+
+Test creates one or more chare arrays and creates a cross-array section that
+includes chares from each of these chare arrays. Section members are pinged
+via a multicast and then participate in a hello world.
+
+Multicasts to the section are delegated to the CkMulticast library.
diff --git a/examples/charm++/hello/xarraySection/hello.C b/examples/charm++/hello/xarraySection/hello.C
new file mode 100644 (file)
index 0000000..75db728
--- /dev/null
@@ -0,0 +1,121 @@
+#include <stdio.h>
+#include "hello.decl.h"
+#include "ckmulticast.h"
+
+/*readonly*/ CProxy_Main mainProxy;
+
+
+class pingMsg: public CkMcastBaseMsg, public CMessage_pingMsg
+{
+    public:
+        pingMsg(int num=0): hiNo(num) {}
+        int hiNo;
+};
+
+/*mainchare*/
+class Main : public CBase_Main
+{
+public:
+  Main(CkArgMsg* m): numArrays(2), numElements(5), sectionSize(numElements), maxIter(3), numReceived(0), numIter(0)
+  {
+    //Process command-line arguments
+    if (m->argc > 1) numElements = atoi(m->argv[1]);
+    if (m->argc > 2) numArrays   = atoi(m->argv[2]);
+    if (m->argc > 3) sectionSize = atoi(m->argv[3]); else sectionSize = numElements;
+    if (m->argc > 4) maxIter     = atoi(m->argv[4]);
+    if (m->argc > 5)
+    { CkPrintf("Syntax: %s [numElements numArrays sectionSize maxIter]",m->argv[0]); CkAbort("Wrong Usage\n"); }
+    delete m;
+
+    CkPrintf("Running a cross-array section demo.\n");
+    CkPrintf("\tnum PEs = %d\n\tnum arrays = %d\n\tnum elements = %d\n\tsection size = %d\n\tnum iterations = %d\n",
+             CkNumPes(), numArrays, numElements, sectionSize, maxIter);
+
+    mainProxy = thisProxy;
+    // Setup section member index bounds
+    int afloor = 0, aceiling = sectionSize-1;
+
+    // Allocate space
+    CProxy_Hello *arrayOfArrays = new CProxy_Hello[numArrays];
+    CkArrayID *arrID            = new CkArrayID[numArrays];
+    int *nelems                 = new int[numArrays];
+    CkArrayIndexMax **elems     = new CkArrayIndexMax*[numArrays];
+
+    // Create a list of array section members
+    for(int k=0; k < numArrays; k++)
+    {
+        // Create the array
+        arrayOfArrays[k] = CProxy_Hello::ckNew(k, numElements);
+        // Store the AID
+        arrID[k]  = arrayOfArrays[k].ckGetArrayID();
+        // Create a list of section member indices in this array
+        nelems[k] = sectionSize;
+        elems[k]  = new CkArrayIndexMax[sectionSize];
+        for(int i=afloor,j=0; i <= aceiling; i++,j++)
+            elems[k][j] = CkArrayIndex1D(i);
+    }
+    // Create the x-array-section
+    sectionProxy = CProxySection_Hello(numArrays, arrID, elems, nelems);
+
+    // Create a multicast manager group
+    CkGroupID mcastMgrGID = CProxy_CkMulticastMgr::ckNew();
+    CkMulticastMgr *mcastMgr = CProxy_CkMulticastMgr(mcastMgrGID).ckLocalBranch();
+    // Delegate the section comm to the CkMulticast library
+    sectionProxy.ckSectionDelegate(mcastMgr);
+
+    // Start the test by pinging the section
+    pingMsg *msg = new pingMsg(numIter);
+    sectionProxy.SayHi(msg);
+  };
+
+  /// Test controller method
+  void done(void)
+  {
+      if (++numReceived >= numArrays * sectionSize)
+      {
+          numReceived = 0;
+          if (++numIter == maxIter) {
+              CkPrintf("----------------- testController: All %d iterations done\n", numIter);
+              CkExit();
+          }
+          else {
+              // Ping the section
+              CkPrintf("----------------- testController: Iteration %d done\n", numIter);
+              pingMsg *msg = new pingMsg(numIter);
+              sectionProxy.SayHi(msg);
+          }
+      }
+  };
+
+private:
+  /// Input parameters
+  int numArrays, numElements, sectionSize, maxIter;
+  /// Counters
+  int numReceived, numIter;
+  /// The cross-array section proxy
+  CProxySection_Hello sectionProxy;
+};
+
+/*array [1D]*/
+class Hello : public CBase_Hello
+{
+public:
+  Hello(int _aNum): aNum(_aNum)
+  {
+    CkPrintf("AID[%d] Hello %d created on pe %d\n", aNum, thisIndex, CkMyPe());
+  }
+
+  Hello(CkMigrateMessage *m) {}
+
+  void SayHi(pingMsg *msg)
+  {
+    CkPrintf("AID[%d] Hi[%d] from element %d\n", aNum, msg->hiNo, thisIndex);
+    mainProxy.done();
+    delete msg;
+  }
+private:
+  int aNum;
+};
+
+#include "hello.def.h"
+
diff --git a/examples/charm++/hello/xarraySection/hello.ci b/examples/charm++/hello/xarraySection/hello.ci
new file mode 100644 (file)
index 0000000..9be7f76
--- /dev/null
@@ -0,0 +1,15 @@
+mainmodule hello {
+  readonly CProxy_Main mainProxy;
+
+  message pingMsg;
+
+  mainchare Main {
+    entry Main(CkArgMsg *m);
+    entry void done(void);
+  };
+
+  array [1D] Hello {
+    entry Hello(int aNum);
+    entry void SayHi(pingMsg *msg);
+  };
+};