examples: add example program demonstrating sync entry methods
[charm.git] / examples / charm++ / X10 / X10_lib.C
1 //-------------------------------------------------------------
2 // file   : X10_test2.C
3 // author : Isaac Dooley
4 // date   : April 2006
5 //
6
7 #include <pup.h>
8 #include <converse.h>
9 #include "X10_lib.decl.h"
10 #include "X10_lib.h"
11
12
13 /*readonly*/ CProxy_Main mainProxy;
14 /*readonly*/ CProxy_Places placesProxy;
15 /*readonly*/ int nPlaces;
16
17 class asyncMsg : public CMessage_asyncMsg{
18  public:
19   int temp;
20   asyncMsg& operator=(const asyncMsg& obj) {
21         temp = obj.temp;
22     return *this;
23   }
24
25   asyncMsg(){temp=1;}
26
27 };
28
29
30 typedef CkVec<CkFutureID> *finishHandle;
31
32 void *beginFinish(){
33   CkVec<CkFutureID> *FinishFutureList = new CkVec<CkFutureID>;
34   CkAssert(FinishFutureList->size()==0);
35   return (void*)FinishFutureList;
36
37
38 void endFinish(void* ffl){
39   CkVec<CkFutureID> *FinishFutureList = (CkVec<CkFutureID> *)ffl;
40   int last = FinishFutureList->length();
41   //  CkPrintf("MainThread: Future waiting   last=%d\n", last);
42   int len = FinishFutureList->length();
43   while(len > 0){
44         asyncMsg *msg = (asyncMsg *)CkWaitFuture((*FinishFutureList)[len-1]);
45         FinishFutureList->remove(len-1);
46         len = FinishFutureList->length();       
47   }
48
49   //  CkPrintf("MainThread: Future awaken\n");
50   delete FinishFutureList;
51 }
52
53 void asyncCall(void *ffl, int place, int whichFunction, void *packedParams){
54   CkVec<CkFutureID> * FinishFutureList = (CkVec<CkFutureID> *)ffl;
55   asyncMsg *msg = new asyncMsg;
56   CkFutureID ftHandle = CkCreateAttachedFuture((void*)msg);
57   FinishFutureList->push_back(ftHandle);
58   //  CkPrintf("MainThread: Created Future with handle %d\n", ftHandle);
59   //(*FinishFutureList)[FinishFutureList->length()]=ftHandle;
60   FinishFutureList->push_back(ftHandle);
61   placesProxy[place].startAsync(whichFunction,ftHandle,CkMyPe());
62   // CkPrintf("MainThread: Created Async call with handle %d\n", ftHandle);
63 }
64
65
66 FutureHandle futureCall(int place, int whichFunction, void *packedParams){
67   CkFutureID *fh = new CkFutureID;
68   asyncMsg *msg = new asyncMsg;
69   CkFutureID ftHandle = CkCreateAttachedFuture((void*)msg);
70   placesProxy[place].startFuture(whichFunction,ftHandle,CkMyPe());
71   return fh;
72 }
73
74 void * futureForce(FutureHandle fh){
75   asyncMsg *msg = (asyncMsg *)CkWaitFuture(*fh);
76   delete fh;
77   return NULL;  
78 }
79
80
81
82 /*mainchare*/
83 class Main : public CBase_Main
84 {
85  public:
86
87   Main(CkArgMsg* m)
88   {
89         nPlaces=CkNumPes();
90         CkPrintf("Starting Up: %d Places(Processors)\n", nPlaces);  
91         mainProxy = thishandle;
92
93         CkAssert(nPlaces >= 2);
94
95     // Create X10 Places        
96         placesProxy = CProxy_Places::ckNew(nPlaces);
97
98         mainProxy.libThread();
99
100   }
101   
102   void libThread(){
103         //      CkPrintf("MainThread: executing in Main Chare\n");
104         mainThread();   
105         CkExit();
106   }
107   
108 };
109
110 /*mainchare*/
111 class Places : public CBase_Places
112 {
113 public:
114   
115   Places(CkMigrateMessage *m){}
116   Places(){}
117   
118   void startAsync(int whichStatement, CkFutureID ftHandle, int pe_src){
119         asyncMsg *msg = new asyncMsg;
120         asnycHandler(whichStatement);
121         //      CkPrintf("Place %d: Finished async function, setting completion of Future [%d] \n", thisIndex, ftHandle);
122         CkSendToFuture(ftHandle, (void *)msg, pe_src);
123   }
124   
125   void startFuture(int whichStatement, CkFutureID ftHandle, int pe_src){
126         asyncMsg *msg = new asyncMsg;
127         futureHandler(whichStatement);
128         CkSendToFuture(ftHandle, (void *)msg, pe_src);
129   }
130   
131 };
132
133
134
135   
136 #include "X10_lib.def.h"