Merge branch 'charm' of charmgit:charm into charm
[charm.git] / examples / charm++ / manyToMany / manyToMany.C
1 #include "manyToMany.decl.h"
2 #include <math.h>
3
4 /*readonly*/ CProxy_Main mainProxy;
5 /*readonly*/ int numChares;
6 /*readonly*/ int msgSize;
7 /*readonly*/ int max_iter;
8
9 struct manyToManyMsg : public CMessage_manyToManyMsg {
10   char *data;
11 };
12
13 struct Main : public CBase_Main {
14         double start;
15         CProxy_manyToMany manyToManyProxy;
16     int         iter;
17
18         Main(CkArgMsg* m) {
19
20         max_iter = 400;
21         numChares = sqrt(CkNumPes());
22         msgSize = 1024;
23                 // 3D manyToMany on a NxNxN array
24         if(m->argc >= 2)
25         {
26             msgSize = atoi(m->argv[1]);
27         }
28         if(m->argc >= 3)
29         {
30             numChares = atoi(m->argv[2]);
31         }
32         
33         delete m;
34
35         iter = 0;
36                 mainProxy = thisProxy;
37                 // Construct an array of manyToMany chares to do the calculation
38                 manyToManyProxy = CProxy_manyToMany::ckNew(numChares, numChares);
39         }
40
41         void manyToManyReady() {
42                 start = CkWallTimer();
43                 // Broadcast the 'go' signal to the manyToMany chare array
44                 manyToManyProxy.domanyToMany();
45         
46         }
47
48         void nextmanyToMany() {
49                 
50         iter++;
51         if(iter < max_iter)
52         {
53             manyToManyProxy.domanyToMany();
54         }else
55         {
56             double time = CkWallTimer() - start;
57             CkPrintf("manyToMany on %d cores for msg size: %d per iteration:%f ms\n",
58                 CkNumPes(), msgSize,  time/max_iter*1000);
59             CkExit();
60         }
61     }
62
63 };
64
65 struct manyToMany : public CBase_manyToMany {
66         manyToMany_SDAG_CODE
67
68         int count;
69     int iter;
70     manyToManyMsg  **msgs;
71     int recvCnt;
72
73         manyToMany() {
74                  __sdag_init();
75         iter = 0;
76         recvCnt = 0;
77                 msgs = new manyToManyMsg*[numChares];
78                 for(int i = 0; i < numChares; i++) {
79                         msgs[i] = new (msgSize) manyToManyMsg;
80                 }
81
82                 // reduction to the mainchare to signal that initialization is complete
83                 contribute(CkCallback(CkReductionTarget(Main,manyToManyReady), mainProxy));
84         }
85
86         // Sends transpose messages to every other chare
87         void sendmanyToMany() {
88                 
89         for(int i = thisIndex.y; i < thisIndex.y+numChares; i++) {
90                         int t = i % numChares;
91                         CkSetRefNum(msgs[t],iter);
92                         thisProxy(thisIndex.x, t).getmanyToMany(msgs[t]);
93                 }
94         }
95
96         // Sends transpose messages to every other chare
97         void getmanyToMany() {
98         }
99
100     void processmanyToMany(manyToManyMsg *msg)
101     {
102         msgs[recvCnt] = msg;
103         recvCnt++;
104     }
105
106
107         void finish(){
108             recvCnt = 0;        
109         contribute(CkCallback(CkReductionTarget(Main,nextmanyToMany), mainProxy));
110         }
111
112         manyToMany(CkMigrateMessage* m) {}
113         ~manyToMany() {}
114
115 };
116
117 #include "manyToMany.def.h"