examples/charm++: Always use CkWallTimer, not CmiWallTimer (tested)
[charm.git] / examples / charm++ / AllReduce / bench / AllReduce.h
1 //! main char object.  Creates array, handles command line, catches reduction.
2 #include <ctime>
3
4 class main : public CBase_main
5 {
6 private:
7   CProxy_AllReduce arr;
8         double startTime;
9         int sizeInd;
10         double *timeForEach;
11         int iterNo;
12         int baseSize;
13         int sizesNo;
14         int numItr;
15         int currentSize;
16 public:
17
18   main(CkArgMsg* m);
19
20   void done()
21   {
22           static int checkIn=0;
23           checkIn++;
24           if (checkIn==units) {
25                   checkIn = 0;
26                   // CkPrintf("All done in %lf seconds\n",(CkWallTimer()-startTime));
27                   double newTime= CkWallTimer();
28                   iterNo++;
29                   timeForEach[sizeInd] += newTime-startTime;
30                   if(iterNo==numItr)
31                   {
32                           timeForEach[sizeInd]/=numItr;
33                           sizeInd++;
34                           if(sizeInd==sizesNo){
35                                   print();
36                                 CkExit();  
37                           }
38                           iterNo=0;
39                           currentSize *= 2;
40
41                   }
42                         startTime = CkWallTimer();
43                         arr.dowork(currentSize);
44           }
45   }
46   void print(){
47           int sizeA = baseSize;
48           for(int i=0; i<sizesNo; i++)
49           {
50                   CkPrintf("size:%d time:%lf\n",sizeA, timeForEach[i]);
51                   sizeA *= 2;
52           }
53   }
54
55 };
56
57
58 //! AllReduce. A small example which does nothing useful, but provides an extremely simple, yet complete, use of a normal reduction case.
59 /** The reduction performs a summation i+k1 i+k2 for i=0 to units
60  */
61 class AllReduce : public CBase_AllReduce
62 {
63  private:
64         CProxy_main mainProxy;
65         double* myData;
66  public:
67
68         AllReduce(CProxy_main ma)   { mainProxy=ma;  }
69
70   AllReduce(CkMigrateMessage *m) {};
71
72         void init()
73         {
74                                 myData = new double[allredSize/(sizeof(double))];
75                                                 for (int i=0; i<allredSize/sizeof(double); i++) {
76                                                                                 myData[i] = (double)i;
77                                                                                                 }
78         }
79
80   //! add our index to the global floats and contribute the result.
81   void dowork (int redSize)
82     {
83         CkCallback cb(CkIndex_AllReduce::report(NULL),  thisProxy);
84       contribute(redSize,myData,CkReduction::sum_double,cb); 
85     }
86   //! catches the completed reduction results.
87   void report(CkReductionMsg *msg)
88   {
89 //        int reducedArrSize=msg->getSize()/sizeof(double);
90 //        double *output=(double *)msg->getData();
91 //        CkPrintf("done %d\n", thisIndex);
92 //        for(int i=0;i<reducedArrSize;i++) {
93 //                if (output[i]!=i*units) {
94 //                        CkPrintf("result error!\n");
95 //                }
96 //        }
97           mainProxy.done();
98           delete msg;
99   }
100
101 };
102