moved from pgms
authorEric Bohm <ebohm@illinois.edu>
Wed, 29 Sep 2004 19:25:25 +0000 (19:25 +0000)
committerEric Bohm <ebohm@illinois.edu>
Wed, 29 Sep 2004 19:25:25 +0000 (19:25 +0000)
examples/charm++/queens/Makefile [new file with mode: 0644]
examples/charm++/queens/Makefile.win32 [new file with mode: 0644]
examples/charm++/queens/counter.C [new file with mode: 0644]
examples/charm++/queens/counter.ci [new file with mode: 0644]
examples/charm++/queens/counter.h [new file with mode: 0644]
examples/charm++/queens/pgm.C [new file with mode: 0644]
examples/charm++/queens/pgm.ci [new file with mode: 0644]
examples/charm++/queens/pgm.h [new file with mode: 0644]

diff --git a/examples/charm++/queens/Makefile b/examples/charm++/queens/Makefile
new file mode 100644 (file)
index 0000000..0d889e5
--- /dev/null
@@ -0,0 +1,28 @@
+CHARMC=../../../bin/charmc $(OPTS)
+
+pgm:   counter.o pgm.o
+       $(CHARMC) counter.o pgm.o -o pgm -language charm++ 
+
+pgm.o : pgm.C pgm.h Pgm.def.h Pgm.decl.h
+       $(CHARMC) -c pgm.C
+
+counter.o : counter.C counter.h Counter.def.h Counter.decl.h
+       $(CHARMC) -c counter.C
+
+Pgm.def.h Pgm.decl.h : pgm.ci
+       $(CHARMC) pgm.ci
+
+Counter.def.h Counter.decl.h : counter.ci
+       $(CHARMC) counter.ci
+
+clean:
+       rm -f conv-host *.o charmrun
+       rm -f *.def.h *.decl.h
+       rm -f pgm pgm.*.log pgm.sts
+       rm -f gmon.out #*#
+       rm -f core *~
+       rm -f TAGS *.headers
+
+
+test: pgm
+       ./charmrun ./pgm +p4 10 7
diff --git a/examples/charm++/queens/Makefile.win32 b/examples/charm++/queens/Makefile.win32
new file mode 100644 (file)
index 0000000..358f3fa
--- /dev/null
@@ -0,0 +1,35 @@
+CHARMBIN=..\..\..\bin
+CPP = cl.exe
+
+INC = /I"..\..\..\include"
+
+CPP_PROJ=/nologo /MT /w /GX /Z7 /Od $(INC)\
+ /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /YX\
+ /Fo /TP /c
+
+LINK=link.exe
+LINK_FLAGS = /nologo /LIBPATH:"..\..\..\lib"
+LINK_OTHER = libldb-rand.o libck.a libconv-core.a libconv-util.a libtrace-none.a libconv-cplus-y.a ws2_32.lib kernel32.lib
+
+OBJS = counter.obj pgm.obj
+
+#.SILENT:
+
+all:   counter.obj pgm.obj
+       $(LINK) $(LINK_FLAGS) $(LINK_OTHER) /out:"pgm.exe" $(OBJS)
+       copy $(CHARMBIN)\charmrun.exe .
+
+pgm.obj : pgm.C pgm.h pgm.def.h pgm.decl.h
+       $(CPP) $(CPP_PROJ) pgm.C
+       
+counter.obj : counter.C counter.h counter.def.h counter.decl.h
+       $(CPP) $(CPP_PROJ) counter.C
+
+pgm.def.h pgm.decl.h : pgm.ci
+       $(CHARMBIN)\charmxi  pgm.ci
+
+counter.def.h counter.decl.h : counter.ci
+       $(CHARMBIN)\charmxi  counter.ci
+
+clean:
+       del /q *.decl.h *.def.h conv-host.exe *.obj pgm.exe vc60.*
diff --git a/examples/charm++/queens/counter.C b/examples/charm++/queens/counter.C
new file mode 100644 (file)
index 0000000..d65862e
--- /dev/null
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "charm++.h"
+
+#include "counter.h"
+#include "Counter.def.h"
+
+counter::counter(DUMMY *m)
+{ 
+  // initialize the local count;
+  mygrp = thisgroup;
+  myCount = totalCount = 0;
+  waitFor = CkNumPes(); // wait for all processors to report
+  threadId = NULL;
+}
+
+void counter::increment()
+{
+  myCount++;
+}
+
+void counter::sendCounts(DUMMY *m)
+  // This method is invoked via a broadcast. Each branch then reports 
+  // its count to the branch on 0 (or via a spanning tree.)
+{
+  CProxy_counter grp(mygrp);
+  grp[0].childCount(new countMsg(myCount));
+  delete m;
+}
+
+void counter::childCount(countMsg *m)
+{
+  totalCount += m->count;
+  waitFor--;
+  if (waitFor == 0) 
+    if (threadId) { CthAwaken(threadId);}
+}
+
+int counter::getTotalCount()
+{
+  CProxy_counter grp(mygrp);
+  grp.sendCounts(new DUMMY);//this is a broadcast, as no processor is mentioned
+  threadId = CthSelf();
+  while (waitFor != 0)  CthSuspend(); 
+  return totalCount;
+}
+CkGroupID  counterInit()
+{
+  DUMMY *m = new  DUMMY;
+  CkGroupID g =CProxy_counter::ckNew(m);  // create a new group of class "counter"
+  return g;
+}
diff --git a/examples/charm++/queens/counter.ci b/examples/charm++/queens/counter.ci
new file mode 100644 (file)
index 0000000..d545458
--- /dev/null
@@ -0,0 +1,10 @@
+module Counter {
+  message countMsg;
+  message DUMMY;
+  
+  group counter {
+    entry counter(DUMMY *);
+    entry void sendCounts(DUMMY *);
+    entry void childCount(countMsg *);
+  };
+};
diff --git a/examples/charm++/queens/counter.h b/examples/charm++/queens/counter.h
new file mode 100644 (file)
index 0000000..81717ae
--- /dev/null
@@ -0,0 +1,30 @@
+#include "Counter.decl.h"
+
+extern CkGroupID counterInit();
+extern int counterReport();
+extern void counterIncrement();
+
+class countMsg : public CMessage_countMsg {
+public:
+  int count;
+  countMsg(int c) : count(c) {};
+};
+
+class DUMMY : public CMessage_DUMMY {
+};
+
+class counter: public Group {
+private:
+  CkGroupID mygrp;
+  int myCount;
+  int totalCount;
+  int waitFor;
+  CthThread threadId;
+public:
+  counter(CkMigrateMessage *m) {}
+  counter(DUMMY *);
+  void childCount(countMsg *);
+  void increment();
+  void sendCounts(DUMMY *);
+  int  getTotalCount();
+};
diff --git a/examples/charm++/queens/pgm.C b/examples/charm++/queens/pgm.C
new file mode 100644 (file)
index 0000000..33d8c64
--- /dev/null
@@ -0,0 +1,123 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "charm++.h"
+
+#include "pgm.h"
+#include "Pgm.def.h"
+
+#include "counter.h"
+
+extern "C" void CApplicationInit(void);
+extern "C" void CApplicationDepositData(char *data);
+
+#define TIME_INTERVAL 500
+#ifndef WIN32
+#include <sys/time.h>
+#include <unistd.h>
+#endif
+
+main::main(CkArgMsg *m)
+{ 
+  int arg_index = 1;
+  mainhandle=thishandle;
+  PartialBoard *pMsg = new  PartialBoard ;
+
+  if(m->argc<3)
+    CmiAbort("Usage: pgm N grain\n");
+  while ( m->argv[arg_index][0] == '+' )
+    arg_index++;
+  N = atoi(m->argv[arg_index]);
+  arg_index++;
+  grain = atoi(m->argv[arg_index]);
+  pMsg->nextRow = 0;
+  CProxy_queens::ckNew(pMsg);
+  t0 = CkTimer();
+  counterGroup = counterInit();
+  CkStartQD(CkIndex_main::Quiescence1((DUMMYMSG *)0), &mainhandle);
+}
+
+void main::Quiescence1(DUMMYMSG *msg) 
+{ 
+  int numSolutions = CProxy_counter(counterGroup).ckLocalBranch()->getTotalCount();
+  CkPrintf("There are %d Solutions to %d queens. Finish time=%f\n",
+         numSolutions, N, CkTimer()-t0);
+  CkExit();
+}
+
+queens::queens(PartialBoard *m)
+{ 
+  int col,i, row;
+  PartialBoard *newMsg;
+
+  row = m->nextRow;  
+  if (row == N) 
+    solutionFound(m->Queens);
+  else if ( (N-row) < grain) 
+    seqQueens(m->Queens, row); 
+  else
+  {
+    for (col = 0; col<N; col++) 
+      if (consistent(m->Queens, row, col))
+      {
+       newMsg = new  PartialBoard;
+       newMsg->nextRow = row + 1;
+       for (i=0; i<N; i++)
+         newMsg->Queens[i] = m->Queens[i];
+       newMsg->Queens[row] = col;
+       CProxy_queens::ckNew(newMsg);  
+      }
+  }
+  delete m;
+  delete this;
+}
+
+
+void queens::solutionFound(int kqueens[])
+{ 
+  int row;
+  char buf[800], tmp[800];
+  CProxy_counter(counterGroup).ckLocalBranch()->increment();
+  buf[0] = '\0';
+  sprintf(tmp,"%d:", CkMyPe());
+  strcat(buf,tmp);
+  for (row=0; row<N; row++)
+  { 
+    sprintf(tmp, "(%d,%d) ", row, kqueens[row]);
+    strcat(buf, tmp);
+  }
+  //  CkPrintf("%s\n", buf);
+}
+
+void queens::seqQueens(int kqueens[], int nextRow)
+{ 
+  int col;
+
+  if (nextRow == N) 
+  { solutionFound(kqueens);
+    return; 
+  }
+  for (col = 0; col<N; col++) 
+    if (consistent(kqueens, nextRow, col))
+    {
+      kqueens[nextRow] = col;
+      seqQueens(kqueens, nextRow+1);
+    }
+}
+
+int queens::consistent(int kqueens[], int lastRow, int col)
+{ 
+  /* check if the new queen is safe from each of the */
+  /* previously placed queens */
+  int x,y;
+
+  for (x=0; x<lastRow; x++)
+  { 
+    y = kqueens[x];
+    if ((y==col)  || ((lastRow-x) == (col-y)) || ((lastRow-x) == (y-col)) ) {
+      return(0);
+    }
+  }
+  return(1);
+}
diff --git a/examples/charm++/queens/pgm.ci b/examples/charm++/queens/pgm.ci
new file mode 100644 (file)
index 0000000..91c4063
--- /dev/null
@@ -0,0 +1,20 @@
+mainmodule Pgm {
+  extern module Counter;
+  readonly CkChareID mainhandle;
+  readonly int grain;
+  readonly int N;
+  readonly CkGroupID counterGroup;
+  
+  message PartialBoard;
+  message DUMMYMSG;
+  
+  mainchare main {
+    entry main();
+    entry [threaded] void Quiescence1(DUMMYMSG *);
+  };
+  
+  chare queens {
+    entry queens(PartialBoard *);
+  };
+
+};
diff --git a/examples/charm++/queens/pgm.h b/examples/charm++/queens/pgm.h
new file mode 100644 (file)
index 0000000..c53b807
--- /dev/null
@@ -0,0 +1,41 @@
+#include "Pgm.decl.h"
+// must include this to allow our message types (e.g. PartialBoard) to 
+// inherit from system-generated ones.
+
+
+#define Nmax 20
+
+int grain, N;
+CkGroupID counterGroup;
+CkChareID mainhandle;
+
+class PartialBoard : public CMessage_PartialBoard { 
+public:
+  int nextRow;
+  int Queens[Nmax];  
+};
+
+class DUMMYMSG : public CMessage_DUMMYMSG {
+public:
+};
+
+class main : public Chare {
+private:
+  double t0; /* starting time */
+  
+public:
+  main(CkMigrateMessage *m) {}
+  main(CkArgMsg *m);
+  void Quiescence1(DUMMYMSG *msg);
+};
+
+class queens : public Chare {
+private:
+  void seqQueens(int queens[], int nextRow);
+  int consistent(int queens[], int lastRow, int col);
+  void solutionFound(int queens[]);
+
+public:
+  queens(CkMigrateMessage *m) {}
+  queens(PartialBoard *m);
+};