collision detection examples
authorEhsan Totoni <etotoni@disneyanimation.com>
Tue, 24 Jul 2012 22:48:54 +0000 (15:48 -0700)
committerEhsan Totoni <etotoni@disneyanimation.com>
Tue, 24 Jul 2012 22:48:54 +0000 (15:48 -0700)
examples/collide/collidecharm/Makefile [new file with mode: 0644]
examples/collide/collidecharm/hello.C [new file with mode: 0644]
examples/collide/collidecharm/hello.ci [new file with mode: 0644]
examples/collide/collidethread/Makefile [new file with mode: 0644]
examples/collide/collidethread/hello.C [new file with mode: 0644]
examples/collide/collidethread/hello.ci [new file with mode: 0644]

diff --git a/examples/collide/collidecharm/Makefile b/examples/collide/collidecharm/Makefile
new file mode 100644 (file)
index 0000000..052fccb
--- /dev/null
@@ -0,0 +1,20 @@
+CHARMC=../../../bin/charmc $(OPTS)
+
+OBJS = hello.o
+
+all:   cifiles hello
+
+hello: $(OBJS)
+       $(CHARMC) -language charm++ -module collidecharm -o hello $(OBJS)
+
+cifiles: hello.ci
+       $(CHARMC)  hello.ci
+
+clean:
+       rm -f *.decl.h *.def.h conv-host *.o hello charmrun
+
+hello.o: hello.C 
+       $(CHARMC) -c hello.C
+
+test: all
+       ./charmrun hello +p4 10
diff --git a/examples/collide/collidecharm/hello.C b/examples/collide/collidecharm/hello.C
new file mode 100644 (file)
index 0000000..6625e05
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+Simple Charm++ collision detection test program--
+Orion Sky Lawlor, olawlor@acm.org, 2003/3/18
+ */
+#include <stdio.h>
+#include <string.h>
+#include "collidecharm.h"
+#include "hello.decl.h"
+
+CProxy_main mid;
+CProxy_Hello arr;
+int nElements;
+
+void printCollisionHandler(void *param,int nColl,Collision *colls)
+{
+       CkPrintf("**********************************************\n");
+       CkPrintf("*** Final collision handler called-- %d records:\n",nColl);
+       int nPrint=nColl;
+       const int maxPrint=30;
+       if (nPrint>maxPrint) nPrint=maxPrint;
+       for (int c=0;c<nPrint;c++) {
+               CkPrintf("%d:%d hits %d:%d\n",
+                       colls[c].A.chunk,colls[c].A.number,
+                       colls[c].B.chunk,colls[c].B.number);
+       }
+       CkPrintf("**********************************************\n");
+       mid.maindone();
+}
+
+class main : public Chare
+{
+public:
+  main(CkMigrateMessage *m) {}
+  main(CkArgMsg* m)
+  {
+    nElements=5;
+    if(m->argc > 1) nElements = atoi(m->argv[1]);
+    delete m;
+    CkPrintf("Running Hello on %d processors for %d elements\n",
+            CkNumPes(),nElements);
+    mid = thishandle;
+    
+    CollideGrid3d gridMap(CkVector3d(0,0,0),CkVector3d(2,100,2));
+    CollideHandle collide=CollideCreate(gridMap,
+       CollideSerialClient(printCollisionHandler,0));
+    
+    arr = CProxy_Hello::ckNew(collide,nElements);
+    
+    arr.DoIt();
+  };
+
+  void maindone(void)
+  {
+    CkPrintf("All done\n");
+    CkExit();
+  };
+};
+
+class Hello : public ArrayElement1D
+{
+       CollideHandle collide;
+       int nTimes;
+public:
+  Hello(const CollideHandle &collide_) :collide(collide_)
+  {
+         CkPrintf("Creating element %d on PE %d\n",thisIndex,CkMyPe());
+         nTimes=0;
+         CollideRegister(collide,thisIndex);
+  }
+
+  Hello(CkMigrateMessage *m) :ArrayElement1D(m) {}
+  void pup(PUP::er &p) {
+     ArrayElement1D::pup(p);
+     p|collide;
+     if (p.isUnpacking())
+       CollideRegister(collide,thisIndex);
+  }
+  ~Hello() {
+     CollideUnregister(collide,thisIndex);
+  }
+
+  void DoIt(void)
+  {
+       CkPrintf("Contributing to reduction %d, element %04d\n",nTimes,thisIndex);
+       CkVector3d o(-6.8,7.9,8.0), x(4.0,0,0), y(0,0.3,0);
+       CkVector3d boxSize(0.2,0.2,0.2);
+       int nBoxes=1000;
+       bbox3d *box=new bbox3d[nBoxes];
+       for (int i=0;i<nBoxes;i++) {
+               CkVector3d c(o+x*thisIndex+y*i);
+               CkVector3d c2(c+boxSize);
+               box[i].empty();
+               box[i].add(c); box[i].add(c2);
+       } 
+       // first box stretches over into next object:
+       box[0].add(o+x*(thisIndex+1.5)+y*2);
+       
+       CollideBoxesPrio(collide,thisIndex,nBoxes,box,NULL);
+       
+       delete[] box;
+       nTimes++;
+  }
+};
+
+#include "hello.def.h"
+
diff --git a/examples/collide/collidecharm/hello.ci b/examples/collide/collidecharm/hello.ci
new file mode 100644 (file)
index 0000000..5ec517d
--- /dev/null
@@ -0,0 +1,14 @@
+mainmodule hello {
+  readonly CProxy_main mid;
+  readonly CProxy_Hello arr;
+
+  mainchare main {
+    entry main();
+    entry void maindone(void);
+  };
+
+  array [1D] Hello {
+    entry Hello(CollideHandle collide);
+    entry void DoIt(void);
+  };
+};
diff --git a/examples/collide/collidethread/Makefile b/examples/collide/collidethread/Makefile
new file mode 100644 (file)
index 0000000..2e7abc6
--- /dev/null
@@ -0,0 +1,20 @@
+CHARMC=../../../bin/charmc $(OPTS)
+
+OBJS = hello.o
+
+all:   cifiles hello
+
+hello: $(OBJS)
+       $(CHARMC) -language ampi -module collide -o hello $(OBJS)
+
+cifiles: hello.ci
+       $(CHARMC)  hello.ci
+
+clean:
+       rm -f *.decl.h *.def.h conv-host *.o hello charmrun
+
+hello.o: hello.C 
+       $(CHARMC) -c hello.C
+
+test: all
+       ./charmrun hello +p4 +vp10
diff --git a/examples/collide/collidethread/hello.C b/examples/collide/collidethread/hello.C
new file mode 100644 (file)
index 0000000..da1b983
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+Simple TCharm collision detection test program--
+       Shows no collisions on a single rank,
+       collisions reported for +vp2 and above.
+
+Orion Sky Lawlor, olawlor@acm.org, 2003/3/18.  Updated 2012-07-22 (Public Domain)
+ */
+#include <stdio.h>
+#include <string.h>
+#include "mpi.h"
+#include "collidec.h"
+#include "bbox.h"
+
+void addObjects(collide_t c,int thisIndex)
+{
+       CkVector3d o(-6.8,7.9,8.0), x(4.0,0,0), y(0,0.3,0);
+       CkVector3d boxSize(0.2,0.2,0.2);
+       int nBoxes=2;
+       bbox3d *box=new bbox3d[nBoxes];
+       for (int i=0;i<nBoxes;i++) {
+               CkVector3d c(o+x*thisIndex+y*i);
+               CkVector3d c2(c+boxSize);
+               box[i].empty();
+               box[i].add(c); box[i].add(c2);
+       } 
+       // first box stretches over into next object:
+       box[0].add(o+x*(thisIndex+1.5)+y*2);
+       
+       COLLIDE_Boxes(c,nBoxes,(const double *)box);
+       
+       delete[] box;
+}
+
+void printCollisions(int myRank,int nColl,int *colls)
+{
+       printf("**********************************************\n");
+       printf("*** %d final collision-- %d records:\n",myRank,nColl);
+       int nPrint=nColl;
+       const int maxPrint=30;
+       if (nPrint>maxPrint) nPrint=maxPrint;
+       for (int c=0;c<nPrint;c++) {
+               printf("%d:%d hits %d:%d\n",
+                       myRank,colls[3*c+0],
+                       colls[3*c+1],colls[3*c+2]);
+       }
+       if (nPrint!=nColl) printf("<more collisions omitted>");
+       printf("**********************************************\n");
+}
+
+int main(int argc,char *argv[]) {
+       MPI_Init(&argc,&argv);
+
+       int myRank, size;
+       MPI_Comm comm=MPI_COMM_WORLD;
+       MPI_Comm_rank(comm,&myRank);
+       MPI_Comm_size(comm,&size);
+       
+       const static double gridStart[3]={0,0,0};
+       const static double gridSize[3]={2,100,2};
+       collide_t c=COLLIDE_Init(comm,gridStart,gridSize);
+       
+       /** Begin a collision step **/
+       addObjects(c,myRank);
+       
+       int nColl=COLLIDE_Count(c);
+       int *colls=new int[3*nColl];
+       COLLIDE_List(c,colls);
+       printCollisions(myRank,nColl,colls);
+       delete[] colls;
+       
+       return 0;
+}
+
diff --git a/examples/collide/collidethread/hello.ci b/examples/collide/collidethread/hello.ci
new file mode 100644 (file)
index 0000000..22ea35a
--- /dev/null
@@ -0,0 +1,14 @@
+mainmodule hello {
+  readonly CProxy_main mid;
+  readonly CProxy_Hello arr;
+
+  mainchare main {
+    entry main();
+    entry void maindone(void);
+  };
+
+  array [1D] Hello {
+    entry Hello(COLLIDE_Handle collide);
+    entry void DoIt(void);
+  };
+};