updated to latest delegation API
[charm.git] / tests / charm++ / delegation / 1darray / hello.C
1 #include <stdio.h>
2 #include "hello.decl.h"
3 #include "envelope.h"
4
5 /*readonly*/ CProxy_Main mainProxy;
6 /*readonly*/ CProxy_Hello arr;
7 /*readonly*/ int nElements;
8 /*readonly*/ CProxy_DelegateMgr delMgr;
9
10 /*mainchare*/
11 class Main : public Chare
12 {
13 public:
14   Main(CkArgMsg* m)
15   {
16     //Process command-line arguments
17     nElements=5;
18     if(m->argc >1 ) nElements=atoi(m->argv[1]);
19     delete m;
20
21     //Start the computation
22     CkPrintf("Running Hello on %d processors for %d elements\n",
23              CkNumPes(),nElements);
24     mainProxy = thishandle;
25  
26     delMgr = CProxy_DelegateMgr::ckNew();
27
28     arr = CProxy_Hello::ckNew(nElements);
29
30     arr[0].SayHi(17);
31   };
32
33   void done(void)
34   {
35     CkPrintf("All done\n");
36     CkExit();
37   };
38 };
39
40 /*group*/
41 class DelegateMgr : public CkDelegateMgr {
42 public:
43         DelegateMgr(void) {}
44         virtual void ArraySend(CkDelegateData *pd,int ep,void *m,const CkArrayIndex &idx,CkArrayID a)
45         {
46                 CkArray *arrMgr=CProxy_CkArray(a).ckLocalBranch();
47                 int onPE=arrMgr->lastKnown(idx);
48                 if (onPE==CkMyPe()) 
49                 { //Send to local element
50                         arrMgr->deliver((CkMessage *)m, CkDeliver_queue);
51                 } else 
52                 { //Forward to remote element
53                         ckout<<"DelegateMgr> Sending message for "<<idx.data()[0]<<" to "<<onPE<<endl;
54                         envelope *env=UsrToEnv(m);
55                         CkPackMessage(&env);
56                         forwardMsg(ep,idx,a,env->getTotalsize(),(char *)env);
57                         CkFreeMsg(m);
58                 }
59         }
60         void forwardMsg(int ep,const CkArrayIndex &idx,
61                         const CkArrayID &a,
62                         int nBytes,char *env)
63         {
64                 ckout<<"DelegateMgr> Recv message for "<<idx.data()[0]<<endl;
65                 //Have to allocate a new message because of Charm++'s 
66                 // weird allocate rules:
67                 envelope *msg=(envelope *)CmiAlloc(nBytes);
68                 memcpy(msg,env,nBytes);
69                 CkUnpackMessage(&msg);
70                 CProxy_CkArray ap(msg->getsetArrayMgr());
71                 ap.ckLocalBranch()->deliver((CkMessage *)EnvToUsr(msg),CkDeliver_inline);
72         }
73 };
74
75 /*array [1D]*/
76 class Hello : public ArrayElement1D
77 {
78 public:
79   Hello()
80   {
81     CkPrintf("Hello %d created\n",thisIndex);
82   }
83
84   Hello(CkMigrateMessage *m) {}
85   
86   void SayHi(int hiNo)
87   {
88     CkPrintf("Hi[%d] from element %d\n",hiNo,thisIndex);
89     CProxy_Hello delArr=arr;
90     delArr.ckDelegate(delMgr.ckLocalBranch());
91     if (thisIndex < nElements-1)
92       //Pass the hello on:
93       delArr[thisIndex+1].SayHi(hiNo+1);
94     else 
95       //We've been around once-- we're done.
96       mainProxy.done();
97   }
98 };
99
100 #include "hello.def.h"